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