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

Last change on this file since 9334 was 8607, checked in by mdewsnip, 20 years ago

First cut at suggesting plugins to use for files dragged into a collection. Seems to work pretty well, although I'm hoping to get all the plugins added to the collection by default which will avoid the need for a non-trivial dialog which hasn't been created yet.

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