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

Last change on this file since 16335 was 16335, checked in by ak19, 16 years ago

Value_field is now a JComponent rather than a JTextField, since URLField.java has changed to encompass JCombobox as well (it was previously a JTextField only)

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