source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gui/WarningDialog.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 9.9 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Project, NZDL, University of Waikato
9 *
10 * Copyright (C) 2003 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gui;
28
29import java.awt.*;
30import java.awt.event.*;
31import javax.swing.*;
32import org.greenstone.gatherer.Configuration;
33import org.greenstone.gatherer.Dictionary;
34import org.greenstone.gatherer.Gatherer;
35import org.greenstone.gatherer.util.JarTools;
36
37
38/** A dialog that warns about something, and allows the user to turn off the warning in the future. */
39public class WarningDialog
40 extends ModalDialog
41 implements ActionListener, KeyListener
42{
43 static final private Dimension NORMAL_SIZE = new Dimension(450, 160);
44 static final private Dimension SETTING_SIZE = new Dimension(450, 200);
45
46 private int result = JOptionPane.CANCEL_OPTION;
47 private JButton cancel_button;
48 private JButton ok_button;
49 private JCheckBox show_check;
50 private JComponent value_field;
51 private JPanel value_panel;
52 private String affected_property;
53 private String full_property;
54 /** Whether or not to display the checkbox that will turn off this dialog in future */
55 private final boolean showcheckbox;
56
57
58 public WarningDialog(String warning_name, String warning_title, String warning_message, String affected_property, boolean can_cancel)
59 {
60 this(warning_name, warning_title, warning_message, affected_property, can_cancel, true); // true for show checkbox
61 }
62
63
64 public WarningDialog(String warning_name, String warning_title, String warning_message, String affected_property, boolean can_cancel,
65 boolean showcheckbox)
66 {
67 super(Gatherer.g_man, "Warning", true);
68 this.setComponentOrientation(Dictionary.getOrientation());
69 this.showcheckbox = showcheckbox;
70
71 // Determine the name of this prompt.
72 this.affected_property = affected_property;
73 this.full_property = warning_name;
74
75 // Now build dialog.
76 if (affected_property != null) {
77 setSize(SETTING_SIZE);
78 }
79 else {
80 setSize(NORMAL_SIZE);
81 }
82 setTitle(warning_title);
83
84 // Creation
85 JPanel content_pane = (JPanel) getContentPane();
86 content_pane.setComponentOrientation(Dictionary.getOrientation());
87 JPanel text_pane = new JPanel();
88 text_pane.setComponentOrientation(Dictionary.getOrientation());
89
90 String gmedium_image = "gatherer_medium.png";
91 if (Configuration.fedora_info.isActive()) {
92 gmedium_image = "fli-" + gmedium_image;
93 }
94
95 JLabel icon_label = new JLabel(JarTools.getImage(gmedium_image));
96 icon_label.setComponentOrientation(Dictionary.getOrientation());
97
98 JTextArea text_area = new JTextArea();
99 text_area.setComponentOrientation(Dictionary.getOrientation());
100 text_area.setEditable(false);
101 text_area.setLineWrap(true);
102 text_area.setText(warning_message);
103 text_area.setCaretPosition(0);
104 text_area.setWrapStyleWord(true);
105
106 value_panel = new JPanel();
107 value_panel.setComponentOrientation(Dictionary.getOrientation());
108 JLabel value_label = new JLabel(Dictionary.get("WarningDialog.Value"));
109 value_label.setComponentOrientation(Dictionary.getOrientation());
110
111 value_field = new JTextField();
112 value_field.setComponentOrientation(Dictionary.getOrientation());
113 JPanel bottom_pane = new JPanel();
114 bottom_pane.setComponentOrientation(Dictionary.getOrientation());
115
116 JPanel control_pane = new JPanel();
117 control_pane.setComponentOrientation(Dictionary.getOrientation());
118 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
119 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
120
121 // Connection
122 ok_button.addActionListener(this);
123 cancel_button.addActionListener(this);
124 ok_button.addKeyListener(this);
125 cancel_button.addKeyListener(this);
126 getRootPane().setDefaultButton(ok_button);
127
128 // Layout
129 icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
130
131 value_label.setBorder(BorderFactory.createEmptyBorder(0, icon_label.getPreferredSize().width, 0, 0));
132
133 value_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
134 value_panel.setLayout(new BorderLayout(5,0));
135 value_panel.add(value_label, BorderLayout.LINE_START);
136 value_panel.add(value_field, BorderLayout.CENTER);
137
138 text_pane.setLayout(new BorderLayout());
139 text_pane.add(icon_label, BorderLayout.LINE_START);
140 text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
141 if(affected_property != null) {
142 text_pane.add(value_panel, BorderLayout.SOUTH);
143 }
144
145 if(can_cancel) {
146 control_pane.setLayout(new GridLayout(1,2,5,0));
147 control_pane.add(ok_button);
148 control_pane.add(cancel_button);
149 }
150 else {
151 control_pane.setLayout(new BorderLayout());
152 control_pane.add(ok_button, BorderLayout.LINE_END);
153 }
154
155 bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
156 bottom_pane.setLayout(new BorderLayout());
157 bottom_pane.add(control_pane, BorderLayout.LINE_END);
158 if(showcheckbox) {
159 show_check = new JCheckBox(Dictionary.get("WarningDialog.Dont_Show_Again"));
160 show_check.setComponentOrientation(Dictionary.getOrientation());
161 bottom_pane.add(show_check, BorderLayout.CENTER);
162 }
163
164 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
165 content_pane.setLayout(new BorderLayout());
166 content_pane.add(text_pane, BorderLayout.CENTER);
167 content_pane.add(bottom_pane, BorderLayout.SOUTH);
168
169 // Position
170 Dimension size = getSize();
171 if (Gatherer.g_man != null) {
172 Rectangle frame_bounds = Gatherer.g_man.getBounds();
173 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
174 }
175 else {
176 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
177 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
178 }
179 }
180
181 public void actionPerformed(ActionEvent event) {
182 boolean valid_value = true;
183 if(event.getSource() == ok_button) {
184 if(affected_property != null && Configuration.self != null) {
185 valid_value = URLField.validateURL(value_field);
186 if(valid_value) {
187 // Store the value of the property
188 URLField.store(value_field, affected_property); //Configuration.setString(affected_property, true, value);
189 }
190 }
191 if(valid_value) {
192 result = JOptionPane.OK_OPTION;
193 }
194 }
195
196 if(valid_value) {
197 if (Configuration.self != null && showcheckbox) {
198 // Store the state of the show message checkbox.
199 Configuration.set(full_property, true, !show_check.isSelected());
200 }
201 // Done.
202 setVisible(false);
203 }
204 else {
205 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
206 }
207 }
208
209 /** Gives notification of key events on the buttons */
210 public void keyPressed(KeyEvent e) {
211 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
212 // Enter: Click the button in focus
213 Object source = e.getSource();
214 if (source instanceof AbstractButton) {
215 ((AbstractButton) source).doClick();
216 }
217 }
218 }
219
220 public void keyReleased(KeyEvent e) {}
221
222 public void keyTyped(KeyEvent e) {}
223
224
225 public int display() {
226 if (Configuration.self == null || Configuration.get(full_property, false)) {
227 setVisible(true);
228 }
229 // We are no longer showing this dialog, so result must always be true.
230 else {
231 result = JOptionPane.OK_OPTION;
232 }
233 // Ask the parent to repaint, just to be sure
234 if(Gatherer.g_man != null) {
235 Gatherer.g_man.repaint();
236 }
237 return result;
238 }
239
240 /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
241 * @param message_only true if this dialog shows a message, false for a warning
242 */
243 public void setMessageOnly(boolean message_only) {
244 if(!showcheckbox) {
245 return;
246 }
247
248 // If this is a message then change the checkbox
249 if(message_only) {
250 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again_Message"));
251 }
252 // And if its a warning, change them back
253 else {
254 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again"));
255 }
256 }
257
258 /** Allows you to replace the generic text field control with a JTextField subclass with further functionality. For instance you might provide a URLField to allow only valid URLs to be accepted.
259 * @param control the JTextField subclass you want to use for the control
260 */
261 public void setValueField(JComponent control) {
262 // Remove the current control
263 value_panel.remove(value_field);
264 // Replace with the new one
265 value_field = control;
266 // Re-add
267 value_panel.add(value_field, BorderLayout.CENTER);
268 }
269
270 protected void processWindowEvent(WindowEvent event) {
271 if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
272 if(affected_property != null) {
273 value_field.requestFocus();
274 }
275 else {
276 ok_button.requestFocus();
277 }
278 }
279 else {
280 super.processWindowEvent(event);
281 }
282 }
283
284 public void setValueField(String field_value){
285 URLField.setText(this.value_field, field_value);
286 }
287
288}
Note: See TracBrowser for help on using the repository browser.