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