source: trunk/gli/src/org/greenstone/gatherer/gui/WarningDialog.java@ 8257

Last change on this file since 8257 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 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.Utility;
36
37/** Warn a user that they are about to add folder level metadata. */
38public class WarningDialog
39 extends ModalDialog
40 implements ActionListener, KeyListener {
41
42 static final private Dimension NORMAL_SIZE = new Dimension(450, 160);
43 static final private Dimension SETTING_SIZE = new Dimension(450, 200);
44
45 private Dictionary dictionary;
46 private int result = JOptionPane.CANCEL_OPTION;
47 private JButton cancel_button;
48 private JButton ok_button;
49 private JCheckBox show_check;
50 private JTextField value_field;
51 private JPanel value_panel;
52 private String affected_property;
53 private String full_property;
54 private String warning_name;
55
56 public WarningDialog(String full_property) {
57 this(full_property, false, null, Gatherer.dictionary);
58 }
59
60 public WarningDialog(String full_property, boolean can_cancel) {
61 this(full_property, can_cancel, null, Gatherer.dictionary);
62 }
63
64 public WarningDialog(String full_property, String affected_property) {
65 this(full_property, false, affected_property, Gatherer.dictionary);
66 }
67
68 public WarningDialog(String full_property, boolean can_cancel, Dictionary dictionary) {
69 this(full_property, can_cancel, null, dictionary);
70 }
71
72 public WarningDialog(String full_property, String affected_feature, Dictionary dictionary) {
73 this(full_property, false, affected_feature, dictionary);
74 }
75
76 public WarningDialog(String full_property, boolean can_cancel, String affected_property) {
77 this(full_property, can_cancel, affected_property, Gatherer.dictionary);
78 }
79
80 public WarningDialog(String full_property, boolean can_cancel, String affected_property, Dictionary dictionary) {
81 super(Gatherer.g_man, "Warning", true);
82
83 // Determine the name of this prompt.
84 this.affected_property = affected_property;
85 this.dictionary = dictionary;
86 this.full_property = full_property;
87 warning_name = full_property.substring(full_property.indexOf(".") + 1);
88
89 // Now build dialog.
90 if (affected_property != null) {
91 setSize(SETTING_SIZE);
92 }
93 else {
94 setSize(NORMAL_SIZE);
95 }
96 Dictionary.setText(this, warning_name + ".Title");
97
98 // Creation
99 JPanel content_pane = (JPanel) getContentPane();
100 JPanel text_pane = new JPanel();
101 JLabel icon_label = new JLabel(Utility.getImage("gatherer_medium.gif"));
102
103 JTextArea text_area = new JTextArea();
104 text_area.setCaretPosition(0);
105 text_area.setEditable(false);
106 text_area.setLineWrap(true);
107 text_area.setWrapStyleWord(true);
108 Dictionary.setText(text_area, warning_name + ".Message");
109
110 value_panel = new JPanel();
111 JLabel value_label = new JLabel();
112 Dictionary.setText(value_label, "WarningDialog.Value");
113
114 value_field = new JTextField();
115 JPanel bottom_pane = new JPanel();
116 show_check = new JCheckBox();
117 Dictionary.setText(show_check, "WarningDialog.Dont_Show_Again");
118 JPanel control_pane = new JPanel();
119 ok_button = new GLIButton();
120 ok_button.setMnemonic(KeyEvent.VK_O);
121 Dictionary.setBoth(ok_button, "General.OK", "General.OK_Tooltip");
122 cancel_button = new GLIButton();
123 cancel_button.setMnemonic(KeyEvent.VK_C);
124 Dictionary.setBoth(cancel_button, "General.Cancel", "General.Pure_Cancel_Tooltip");
125
126 // Connection
127 ok_button.addActionListener(this);
128 cancel_button.addActionListener(this);
129 ok_button.addKeyListener(this);
130 cancel_button.addKeyListener(this);
131 getRootPane().setDefaultButton(ok_button);
132
133 // Layout
134 icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
135
136 value_label.setBorder(BorderFactory.createEmptyBorder(0, icon_label.getPreferredSize().width, 0, 0));
137
138 value_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
139 value_panel.setLayout(new BorderLayout(5,0));
140 value_panel.add(value_label, BorderLayout.WEST);
141 value_panel.add(value_field, BorderLayout.CENTER);
142
143 text_pane.setLayout(new BorderLayout());
144 text_pane.add(icon_label, BorderLayout.WEST);
145 text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
146 if(affected_property != null) {
147 text_pane.add(value_panel, BorderLayout.SOUTH);
148 }
149
150 if(can_cancel) {
151 control_pane.setLayout(new GridLayout(1,2,5,0));
152 control_pane.add(ok_button);
153 control_pane.add(cancel_button);
154 }
155 else {
156 control_pane.setLayout(new BorderLayout());
157 control_pane.add(ok_button, BorderLayout.EAST);
158 }
159
160 bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
161 bottom_pane.setLayout(new BorderLayout());
162 bottom_pane.add(show_check, BorderLayout.CENTER);
163 bottom_pane.add(control_pane, BorderLayout.EAST);
164
165 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
166 content_pane.setLayout(new BorderLayout());
167 content_pane.add(text_pane, BorderLayout.CENTER);
168 content_pane.add(bottom_pane, BorderLayout.SOUTH);
169
170 // Position
171 Dimension size = getSize();
172 if (Gatherer.g_man != null) {
173 Rectangle frame_bounds = Gatherer.g_man.getBounds();
174 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
175 }
176 else {
177 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
178 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
179 }
180 }
181
182 public void actionPerformed(ActionEvent event) {
183 boolean bad_value = false;
184 if(event.getSource() == ok_button) {
185 if(affected_property != null && Configuration.self != null) {
186 String value = value_field.getText();
187 if(value.length() > 0) {
188 if(value_field instanceof URLField) {
189 bad_value = !((URLField)value_field).validateURL();
190 }
191 if(!bad_value) {
192 // Store the value of the property
193 Configuration.setString(affected_property, true, value_field.getText());
194 }
195 }
196 }
197 if(!bad_value) {
198 result = JOptionPane.OK_OPTION;
199 }
200 }
201 if(!bad_value) {
202 if (Configuration.self != null) {
203 // Store the state of the show message checkbox.
204 Configuration.set(full_property, true, !show_check.isSelected());
205 }
206 // Done.
207 setVisible(false);
208 }
209 else {
210 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
211 }
212 }
213
214 /** Gives notification of key events on the buttons */
215 public void keyPressed(KeyEvent e) {
216 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
217 // Enter: Click the button in focus
218 Object source = e.getSource();
219 if (source instanceof AbstractButton) {
220 ((AbstractButton) source).doClick();
221 }
222 }
223 }
224
225 public void keyReleased(KeyEvent e) { }
226
227 public void keyTyped(KeyEvent e) { }
228
229
230 public int display() {
231 if (Configuration.self == null || Configuration.get(full_property, false)) {
232 setVisible(true);
233 }
234 // We are no longer showing this dialog, so result must always be true.
235 else {
236 result = JOptionPane.OK_OPTION;
237 }
238 // Ask the parent to repaint, just to be sure
239 if(Gatherer.g_man != null) {
240 Gatherer.g_man.repaint();
241 }
242 return result;
243 }
244
245 /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
246 * @param message_only true if this dialog shows a message, false for a warning
247 */
248 public void setMessageOnly(boolean message_only) {
249 // If this is a message then change the checkbox
250 if(message_only) {
251 Dictionary.setText(show_check, "WarningDialog.Dont_Show_Again_Message");
252 }
253 // And if its a warning, change them back
254 else {
255 Dictionary.setText(show_check, "WarningDialog.Dont_Show_Again");
256 }
257 }
258
259 /** 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.
260 * @param control the JTextField subclass you want to use for the control
261 */
262 public void setValueField(JTextField control) {
263 // Remove the current control
264 value_panel.remove(value_field);
265 // Replace with the new one
266 value_field = control;
267 // Re-add
268 value_panel.add(value_field, BorderLayout.CENTER);
269 }
270
271 protected void processWindowEvent(WindowEvent event) {
272 if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
273 if(affected_property != null) {
274 value_field.requestFocus();
275 }
276 else {
277 ok_button.requestFocus();
278 }
279 }
280 else {
281 super.processWindowEvent(event);
282 }
283 }
284}
285
286
287
288
289
290
291
292
Note: See TracBrowser for help on using the repository browser.