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