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

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

Ok button is made the default button so that it is activated on pressing Enter

  • 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 getRootPane().setDefaultButton(ok_button);
152 }
153
154 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
155 content_pane.setLayout(new BorderLayout());
156 content_pane.add(text_pane, BorderLayout.CENTER);
157 content_pane.add(bottom_pane, BorderLayout.SOUTH);
158
159 // Position
160 Dimension size = getSize();
161 if (Gatherer.g_man != null) {
162 Rectangle frame_bounds = Gatherer.g_man.getBounds();
163 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
164 }
165 else {
166 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
167 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
168 }
169 }
170
171 public void actionPerformed(ActionEvent event) {
172 boolean valid_value = true;
173 if(event.getSource() == ok_button) {
174 if(affected_property != null && Configuration.self != null) {
175 valid_value = URLField.validateURL(value_field);
176 if(valid_value) {
177 // Store the value of the property
178 URLField.store(value_field, affected_property); //Configuration.setString(affected_property, true, value);
179 }
180 }
181 if(valid_value) {
182 result = JOptionPane.OK_OPTION;
183 }
184 }
185
186 if(valid_value) {
187 if (Configuration.self != null && showcheckbox) {
188 // Store the state of the show message checkbox.
189 Configuration.set(full_property, true, !show_check.isSelected());
190 }
191 // Done.
192 setVisible(false);
193 }
194 else {
195 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
196 }
197 }
198
199 /** Gives notification of key events on the buttons */
200 public void keyPressed(KeyEvent e) {}
201
202 public void keyReleased(KeyEvent e) {
203 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
204 // Enter: Click the button in focus
205 Object source = e.getSource();
206 if (source instanceof AbstractButton) {
207 ((AbstractButton) source).doClick();
208 }
209 }
210 }
211
212 public void keyTyped(KeyEvent e) {}
213
214
215 public int display() {
216 if (Configuration.self == null || Configuration.get(full_property, false)) {
217 setVisible(true);
218 }
219 // We are no longer showing this dialog, so result must always be true.
220 else {
221 result = JOptionPane.OK_OPTION;
222 }
223 // Ask the parent to repaint, just to be sure
224 if(Gatherer.g_man != null) {
225 Gatherer.g_man.repaint();
226 }
227 return result;
228 }
229
230 /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
231 * @param message_only true if this dialog shows a message, false for a warning
232 */
233 public void setMessageOnly(boolean message_only) {
234 if(!showcheckbox) {
235 return;
236 }
237
238 // If this is a message then change the checkbox
239 if(message_only) {
240 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again_Message"));
241 }
242 // And if its a warning, change them back
243 else {
244 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again"));
245 }
246 }
247
248 /** 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.
249 * @param control the JTextField subclass you want to use for the control
250 */
251 public void setValueField(JComponent control) {
252 // Remove the current control
253 value_panel.remove(value_field);
254 // Replace with the new one
255 value_field = control;
256 // Re-add
257 value_panel.add(value_field, BorderLayout.CENTER);
258 }
259
260 protected void processWindowEvent(WindowEvent event) {
261 if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
262 if(affected_property != null) {
263 value_field.requestFocus();
264 }
265 else {
266 ok_button.requestFocus();
267 }
268 }
269 else {
270 super.processWindowEvent(event);
271 }
272 }
273
274 public void setValueField(String field_value){
275 URLField.setText(this.value_field, field_value);
276 }
277
278}
Note: See TracBrowser for help on using the repository browser.