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.FocusChangerTask;
|
---|
10 | import org.greenstone.gatherer.util.Utility;
|
---|
11 |
|
---|
12 | /** Warn a user that they are about to add folder level metadata. */
|
---|
13 | public class WarningDialog
|
---|
14 | extends JDialog
|
---|
15 | implements ActionListener, KeyListener {
|
---|
16 |
|
---|
17 | static final private Dimension LABEL_SIZE = new Dimension(150, 25);
|
---|
18 | static final private Dimension NORMAL_SIZE = new Dimension(400, 160);
|
---|
19 | static final private Dimension SETTING_SIZE = new Dimension(400, 200);
|
---|
20 |
|
---|
21 | private Dictionary dictionary;
|
---|
22 | private int result = JOptionPane.CANCEL_OPTION;
|
---|
23 | private JButton cancel_button;
|
---|
24 | private JButton ok_button;
|
---|
25 | private JCheckBox show_check;
|
---|
26 | private JTextField value_field;
|
---|
27 | private JPanel value_panel;
|
---|
28 | private String affected_property;
|
---|
29 | private String full_property;
|
---|
30 | private String warning_name;
|
---|
31 |
|
---|
32 | public WarningDialog(String full_property) {
|
---|
33 | this(full_property, false, null, Gatherer.dictionary);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public WarningDialog(String full_property, boolean can_cancel) {
|
---|
37 | this(full_property, can_cancel, null, Gatherer.dictionary);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public WarningDialog(String full_property, String affected_property) {
|
---|
41 | this(full_property, false, affected_property, Gatherer.dictionary);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public WarningDialog(String full_property, boolean can_cancel, Dictionary dictionary) {
|
---|
45 | this(full_property, can_cancel, null, dictionary);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public WarningDialog(String full_property, String affected_feature, Dictionary dictionary) {
|
---|
49 | this(full_property, false, affected_feature, dictionary);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public WarningDialog(String full_property, boolean can_cancel, String affected_property) {
|
---|
53 | this(full_property, can_cancel, affected_property, Gatherer.dictionary);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public WarningDialog(String full_property, boolean can_cancel, String affected_property, Dictionary dictionary) {
|
---|
57 | super(Gatherer.g_man, "Warning", true);
|
---|
58 |
|
---|
59 | // Determine the name of this prompt.
|
---|
60 | this.affected_property = affected_property;
|
---|
61 | this.dictionary = dictionary;
|
---|
62 | this.full_property = full_property;
|
---|
63 | warning_name = full_property.substring(full_property.indexOf(".") + 1);
|
---|
64 |
|
---|
65 | // Now build dialog.
|
---|
66 | if (affected_property != null) {
|
---|
67 | setSize(SETTING_SIZE);
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | setSize(NORMAL_SIZE);
|
---|
71 | }
|
---|
72 | Dictionary.setText(this, warning_name + ".Title");
|
---|
73 |
|
---|
74 | // Creation
|
---|
75 | JPanel content_pane = (JPanel) getContentPane();
|
---|
76 | JPanel text_pane = new JPanel();
|
---|
77 | JLabel icon_label = new JLabel(Utility.getImage("gatherer_medium.gif"));
|
---|
78 |
|
---|
79 | JTextArea text_area = new JTextArea();
|
---|
80 | text_area.setCaretPosition(0);
|
---|
81 | text_area.setEditable(false);
|
---|
82 | text_area.setLineWrap(true);
|
---|
83 | text_area.setWrapStyleWord(true);
|
---|
84 | Dictionary.setText(text_area, warning_name + ".Message");
|
---|
85 |
|
---|
86 | value_panel = new JPanel();
|
---|
87 | JLabel value_label = new JLabel();
|
---|
88 | value_label.setPreferredSize(LABEL_SIZE);
|
---|
89 | Dictionary.setText(value_label, "WarningDialog.Value");
|
---|
90 |
|
---|
91 | value_field = new JTextField();
|
---|
92 | JPanel bottom_pane = new JPanel();
|
---|
93 | show_check = new JCheckBox();
|
---|
94 | Dictionary.setText(show_check, "WarningDialog.Dont_Show_Again");
|
---|
95 | JPanel control_pane = new JPanel();
|
---|
96 | ok_button = new JButton();
|
---|
97 | Dictionary.setBoth(ok_button, "General.OK", "General.OK_Tooltip");
|
---|
98 | cancel_button = new JButton();
|
---|
99 | Dictionary.setBoth(cancel_button, "General.Cancel", "General.Pure_Cancel_Tooltip");
|
---|
100 |
|
---|
101 | // Connection
|
---|
102 | ok_button.addActionListener(this);
|
---|
103 | cancel_button.addActionListener(this);
|
---|
104 | ok_button.addKeyListener(this);
|
---|
105 | cancel_button.addKeyListener(this);
|
---|
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 | bad_value = true;
|
---|
163 | }
|
---|
164 | else if(value_field instanceof URLField) {
|
---|
165 | bad_value = !((URLField)value_field).validateURL();
|
---|
166 | }
|
---|
167 | if(!bad_value) {
|
---|
168 | // Store the value of the property
|
---|
169 | Gatherer.config.setString(affected_property, true, value_field.getText());
|
---|
170 | }
|
---|
171 | }
|
---|
172 | if(!bad_value) {
|
---|
173 | result = JOptionPane.OK_OPTION;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | if(!bad_value) {
|
---|
177 | if(Gatherer.config != null) {
|
---|
178 | // Store the state of the show message checkbox.
|
---|
179 | Gatherer.config.set(full_property, true, !show_check.isSelected());
|
---|
180 | }
|
---|
181 | // Done.
|
---|
182 | dispose();
|
---|
183 | }
|
---|
184 | else {
|
---|
185 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /** Gives notification of key events on the buttons */
|
---|
191 | public void keyPressed(KeyEvent e) {
|
---|
192 | if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
---|
193 | // Enter: Click the button in focus
|
---|
194 | Object source = e.getSource();
|
---|
195 | if (source instanceof AbstractButton) {
|
---|
196 | ((AbstractButton) source).doClick();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void keyReleased(KeyEvent e) { }
|
---|
202 |
|
---|
203 | public void keyTyped(KeyEvent e) { }
|
---|
204 |
|
---|
205 |
|
---|
206 | public int display() {
|
---|
207 | ///ystem.err.println("Show " + full_property + ": " + Gatherer.config.get(full_property, false));
|
---|
208 | if(Gatherer.config == null || Gatherer.config.get(full_property, false)) {
|
---|
209 | // Create and run the shortlived move focus thread
|
---|
210 | FocusChangerTask task = null;
|
---|
211 | if(affected_property != null) {
|
---|
212 | task = new FocusChangerTask(value_field);
|
---|
213 | }
|
---|
214 | else {
|
---|
215 | task = new FocusChangerTask(ok_button);
|
---|
216 | }
|
---|
217 | task.start();
|
---|
218 | show();
|
---|
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 | }
|
---|