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

Last change on this file since 18175 was 18175, checked in by ak19, 15 years ago

Fixed bug I introduced 2 of my commits ago: When in the Enrich Dialog assigning folder-level metadata, if you press enter in the textfield a WarningDialog pops up and the Enter press propagates into the dialog and the OK button is activated which made the WarningDialog disappear almost instantly.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 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
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 JPanel text_pane = new JPanel();
87
88 String gmedium_image = "gatherer_medium.gif";
89 if (Configuration.fedora_info.isActive()) {
90 gmedium_image = "fli-" + gmedium_image;
91 }
92
93 JLabel icon_label = new JLabel(JarTools.getImage(gmedium_image));
94
95 JTextArea text_area = new JTextArea();
96 text_area.setEditable(false);
97 text_area.setLineWrap(true);
98 text_area.setText(warning_message);
99 text_area.setCaretPosition(0);
100 text_area.setWrapStyleWord(true);
101
102 value_panel = new JPanel();
103 JLabel value_label = new JLabel(Dictionary.get("WarningDialog.Value"));
104
105 value_field = new JTextField();
106 JPanel bottom_pane = new JPanel();
107 JPanel control_pane = new JPanel();
108 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
109 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
110
111 // Connection
112 ok_button.addActionListener(this);
113 cancel_button.addActionListener(this);
114 ok_button.addKeyListener(this);
115 cancel_button.addKeyListener(this);
116 getRootPane().setDefaultButton(ok_button);
117
118 // Layout
119 icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
120
121 value_label.setBorder(BorderFactory.createEmptyBorder(0, icon_label.getPreferredSize().width, 0, 0));
122
123 value_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
124 value_panel.setLayout(new BorderLayout(5,0));
125 value_panel.add(value_label, BorderLayout.WEST);
126 value_panel.add(value_field, BorderLayout.CENTER);
127
128 text_pane.setLayout(new BorderLayout());
129 text_pane.add(icon_label, BorderLayout.WEST);
130 text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
131 if(affected_property != null) {
132 text_pane.add(value_panel, BorderLayout.SOUTH);
133 }
134
135 if(can_cancel) {
136 control_pane.setLayout(new GridLayout(1,2,5,0));
137 control_pane.add(ok_button);
138 control_pane.add(cancel_button);
139 }
140 else {
141 control_pane.setLayout(new BorderLayout());
142 control_pane.add(ok_button, BorderLayout.EAST);
143 }
144
145 bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
146 bottom_pane.setLayout(new BorderLayout());
147 bottom_pane.add(control_pane, BorderLayout.EAST);
148 if(showcheckbox) {
149 show_check = new JCheckBox(Dictionary.get("WarningDialog.Dont_Show_Again"));
150 bottom_pane.add(show_check, BorderLayout.CENTER);
151 }
152
153 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
154 content_pane.setLayout(new BorderLayout());
155 content_pane.add(text_pane, BorderLayout.CENTER);
156 content_pane.add(bottom_pane, BorderLayout.SOUTH);
157
158 // Position
159 Dimension size = getSize();
160 if (Gatherer.g_man != null) {
161 Rectangle frame_bounds = Gatherer.g_man.getBounds();
162 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
163 }
164 else {
165 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
166 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
167 }
168 }
169
170 public void actionPerformed(ActionEvent event) {
171 boolean valid_value = true;
172 if(event.getSource() == ok_button) {
173 if(affected_property != null && Configuration.self != null) {
174 valid_value = URLField.validateURL(value_field);
175 if(valid_value) {
176 // Store the value of the property
177 URLField.store(value_field, affected_property); //Configuration.setString(affected_property, true, value);
178 }
179 }
180 if(valid_value) {
181 result = JOptionPane.OK_OPTION;
182 }
183 }
184
185 if(valid_value) {
186 if (Configuration.self != null && showcheckbox) {
187 // Store the state of the show message checkbox.
188 Configuration.set(full_property, true, !show_check.isSelected());
189 }
190 // Done.
191 setVisible(false);
192 }
193 else {
194 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
195 }
196 }
197
198 /** Gives notification of key events on the buttons */
199 public void keyPressed(KeyEvent e) {
200 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
201 // Enter: Click the button in focus
202 Object source = e.getSource();
203 if (source instanceof AbstractButton) {
204 ((AbstractButton) source).doClick();
205 }
206 }
207 }
208
209 public void keyReleased(KeyEvent e) {}
210
211 public void keyTyped(KeyEvent e) {}
212
213
214 public int display() {
215 if (Configuration.self == null || Configuration.get(full_property, false)) {
216 setVisible(true);
217 }
218 // We are no longer showing this dialog, so result must always be true.
219 else {
220 result = JOptionPane.OK_OPTION;
221 }
222 // Ask the parent to repaint, just to be sure
223 if(Gatherer.g_man != null) {
224 Gatherer.g_man.repaint();
225 }
226 return result;
227 }
228
229 /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
230 * @param message_only true if this dialog shows a message, false for a warning
231 */
232 public void setMessageOnly(boolean message_only) {
233 if(!showcheckbox) {
234 return;
235 }
236
237 // If this is a message then change the checkbox
238 if(message_only) {
239 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again_Message"));
240 }
241 // And if its a warning, change them back
242 else {
243 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again"));
244 }
245 }
246
247 /** 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.
248 * @param control the JTextField subclass you want to use for the control
249 */
250 public void setValueField(JComponent control) {
251 // Remove the current control
252 value_panel.remove(value_field);
253 // Replace with the new one
254 value_field = control;
255 // Re-add
256 value_panel.add(value_field, BorderLayout.CENTER);
257 }
258
259 protected void processWindowEvent(WindowEvent event) {
260 if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
261 if(affected_property != null) {
262 value_field.requestFocus();
263 }
264 else {
265 ok_button.requestFocus();
266 }
267 }
268 else {
269 super.processWindowEvent(event);
270 }
271 }
272
273 public void setValueField(String field_value){
274 URLField.setText(this.value_field, field_value);
275 }
276
277}
Note: See TracBrowser for help on using the repository browser.