1 | package org.greenstone.gatherer.gui;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.awt.event.*;
|
---|
5 | import java.util.*;
|
---|
6 | import javax.swing.*;
|
---|
7 | import org.greenstone.gatherer.Gatherer;
|
---|
8 | import org.greenstone.gatherer.checklist.CheckList;
|
---|
9 | import org.greenstone.gatherer.checklist.Entry;
|
---|
10 | import org.greenstone.gatherer.gui.GUIManager;
|
---|
11 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
12 | import org.greenstone.gatherer.gui.NumberField;
|
---|
13 | import org.greenstone.gatherer.gui.SimpleMenuBar;
|
---|
14 | import org.greenstone.gatherer.util.Utility;
|
---|
15 | import org.greenstone.gatherer.util.XORToggleButtonGroup;
|
---|
16 | import org.w3c.dom.*;
|
---|
17 |
|
---|
18 | public class Preferences
|
---|
19 | extends ModalDialog {
|
---|
20 |
|
---|
21 | private CheckList warning_preferences_check_list;
|
---|
22 | private JButton apply_button;
|
---|
23 | private JButton cancel_button;
|
---|
24 | private JButton ok_button;
|
---|
25 | private JCheckBox use_proxy_checkbox;
|
---|
26 | private JCheckBox workflow_browse;
|
---|
27 | private JCheckBox workflow_create;
|
---|
28 | private JCheckBox workflow_mirror;
|
---|
29 | private JCheckBox workflow_gather;
|
---|
30 | private JCheckBox workflow_enrich;
|
---|
31 | private JCheckBox workflow_design;
|
---|
32 | private JCheckBox workflow_export;
|
---|
33 | private JCheckBox workflow_preview;
|
---|
34 | private JTabbedPane tab_pane;
|
---|
35 | private JTextField proxy_host_field;
|
---|
36 | private JTextField proxy_port_field;
|
---|
37 | private Preferences self;
|
---|
38 |
|
---|
39 | static final Dimension LABEL_SIZE = new Dimension(100, 25);
|
---|
40 | static final Dimension ROW_SIZE = new Dimension(380, 25);
|
---|
41 | static final Dimension SIZE = new Dimension(400, 345);
|
---|
42 | static final String TRUE = "true";
|
---|
43 |
|
---|
44 | public Preferences() {
|
---|
45 | // Initialize
|
---|
46 | super(Gatherer.g_man, true);
|
---|
47 | this.self = this;
|
---|
48 | setSize(SIZE);
|
---|
49 | setTitle("Preferences");
|
---|
50 | setJMenuBar(new SimpleMenuBar("10.2"));
|
---|
51 | // Creation
|
---|
52 | JPanel content_pane = (JPanel) getContentPane();
|
---|
53 | tab_pane = new JTabbedPane();
|
---|
54 | tab_pane.add("Preferences.Workflow", createWorkflowPreferences());
|
---|
55 | tab_pane.add("Preferences.Connection", createConnectionPreferences());
|
---|
56 | tab_pane.add("Preferences.Warnings", createWarningPreferences());
|
---|
57 |
|
---|
58 | JPanel button_pane = new JPanel();
|
---|
59 | ok_button = new JButton("General.OK");
|
---|
60 | apply_button = new JButton("General.Apply");
|
---|
61 | cancel_button = new JButton("General.Cancel");
|
---|
62 | // Connection
|
---|
63 | Gatherer.dictionary.register(this, null, false);
|
---|
64 | Gatherer.dictionary.register(tab_pane, null, false);
|
---|
65 | Gatherer.dictionary.register(ok_button, null, false);
|
---|
66 | Gatherer.dictionary.register(apply_button, null, false);
|
---|
67 | Gatherer.dictionary.register(cancel_button, null, false);
|
---|
68 | ok_button.addActionListener(new OKButtonListener(true));
|
---|
69 | apply_button.addActionListener(new OKButtonListener(false));
|
---|
70 | cancel_button.addActionListener(new CancelButtonListener());
|
---|
71 | // Layout
|
---|
72 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,2,2,2));
|
---|
73 | button_pane.setLayout(new GridLayout(1,3,0,5));
|
---|
74 | button_pane.add(ok_button);
|
---|
75 | button_pane.add(apply_button);
|
---|
76 | button_pane.add(cancel_button);
|
---|
77 |
|
---|
78 | content_pane.setLayout(new BorderLayout());
|
---|
79 | content_pane.add(tab_pane, BorderLayout.CENTER);
|
---|
80 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
81 |
|
---|
82 | Dimension frame_size = Gatherer.g_man.getSize();
|
---|
83 | Point frame_location = Gatherer.g_man.getLocation();
|
---|
84 | //setLocation(frame_location.x + ((frame_size.width - SIZE.width) / 2), frame_location.y + ((frame_size.height - SIZE.height)));
|
---|
85 | setLocation(((frame_size.width - SIZE.width) / 2), ((frame_size.height - SIZE.height)));
|
---|
86 |
|
---|
87 | // Clean up
|
---|
88 | frame_location = null;
|
---|
89 | frame_size = null;
|
---|
90 | cancel_button = null;
|
---|
91 | ok_button = null;
|
---|
92 | button_pane = null;
|
---|
93 | tab_pane = null;
|
---|
94 | content_pane = null;
|
---|
95 | setVisible(true);
|
---|
96 | }
|
---|
97 |
|
---|
98 | private JPanel createConnectionPreferences() {
|
---|
99 | boolean currently_enabled = Gatherer.config.get("general.use_proxy", true);
|
---|
100 | // Creation
|
---|
101 | JPanel connection_pane = new JPanel();
|
---|
102 | use_proxy_checkbox = new JCheckBox("Preferences.Connection.Use_Proxy", currently_enabled);
|
---|
103 | use_proxy_checkbox.setPreferredSize(ROW_SIZE);
|
---|
104 | JPanel proxy_host_pane = new JPanel();
|
---|
105 | proxy_host_pane.setPreferredSize(ROW_SIZE);
|
---|
106 | JLabel proxy_host_label = new JLabel("Preferences.Connection.Proxy_Host");
|
---|
107 | proxy_host_label.setPreferredSize(LABEL_SIZE);
|
---|
108 | proxy_host_field = new JTextField(Gatherer.config.getString("general.proxy_host", true));
|
---|
109 | proxy_host_field.setEnabled(currently_enabled);
|
---|
110 | JPanel proxy_port_pane = new JPanel();
|
---|
111 | proxy_port_pane.setPreferredSize(ROW_SIZE);
|
---|
112 | JLabel proxy_port_label = new JLabel("Preferences.Connection.Proxy_Port");
|
---|
113 | proxy_port_label.setPreferredSize(LABEL_SIZE);
|
---|
114 | proxy_port_field = new NumberField(Gatherer.config.getString("general.proxy_port", true));
|
---|
115 | proxy_port_field.setEnabled(currently_enabled);
|
---|
116 |
|
---|
117 | // Connection
|
---|
118 | Gatherer.dictionary.register(use_proxy_checkbox, null, false);
|
---|
119 | Gatherer.dictionary.register(proxy_host_label, null, false);
|
---|
120 | Gatherer.dictionary.register(proxy_port_label, null, false);
|
---|
121 | use_proxy_checkbox.addActionListener(new UseProxyListener());
|
---|
122 | // Layout
|
---|
123 | proxy_host_pane.setLayout(new BorderLayout());
|
---|
124 | proxy_host_pane.add(proxy_host_label, BorderLayout.WEST);
|
---|
125 | proxy_host_pane.add(proxy_host_field, BorderLayout.CENTER);
|
---|
126 |
|
---|
127 | proxy_port_pane.setLayout(new BorderLayout());
|
---|
128 | proxy_port_pane.add(proxy_port_label, BorderLayout.WEST);
|
---|
129 | proxy_port_pane.add(proxy_port_field, BorderLayout.CENTER);
|
---|
130 |
|
---|
131 | connection_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
132 | connection_pane.add(use_proxy_checkbox);
|
---|
133 | connection_pane.add(proxy_host_pane);
|
---|
134 | connection_pane.add(proxy_port_pane);
|
---|
135 |
|
---|
136 | return connection_pane;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** The warning preferences are controlled through a checklist. */
|
---|
140 | private JPanel createWarningPreferences() {
|
---|
141 | // Retrieve all of the warning preferences settings.
|
---|
142 | HashMap warning_preferences = Gatherer.config.getAll("warning\\..*", true);
|
---|
143 | warning_preferences_check_list = new CheckList();
|
---|
144 | for(Iterator keys = warning_preferences.keySet().iterator(); keys.hasNext(); ) {
|
---|
145 | String property = (String) keys.next();
|
---|
146 | String value = (String) warning_preferences.get(property);
|
---|
147 | // Remove 'warning.'
|
---|
148 | String title = Gatherer.dictionary.get(property.substring(8) + ".Title");
|
---|
149 | Entry entry = new Entry(title, value.equalsIgnoreCase(TRUE));
|
---|
150 | entry.setProperty(property);
|
---|
151 | warning_preferences_check_list.addEntry(entry);
|
---|
152 | }
|
---|
153 | // Creation
|
---|
154 | JPanel warning_preferences_pane = new JPanel();
|
---|
155 | // Connection
|
---|
156 | // Layout
|
---|
157 | warning_preferences_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
158 | warning_preferences_pane.setLayout(new BorderLayout());
|
---|
159 | warning_preferences_pane.add(new JScrollPane(warning_preferences_check_list), BorderLayout.CENTER);
|
---|
160 |
|
---|
161 | return warning_preferences_pane;
|
---|
162 | }
|
---|
163 |
|
---|
164 | private JPanel createWorkflowPreferences() {
|
---|
165 | // Read in the predefined configurations file.
|
---|
166 | Vector predefined = new Vector();
|
---|
167 | Document predefined_document = Utility.parse("xml/workflows.xml", true);
|
---|
168 | Element predefined_element = predefined_document.getDocumentElement();
|
---|
169 | NodeList workflow_elements = predefined_element.getElementsByTagName("Workflow");
|
---|
170 | for(int i = 0; i < workflow_elements.getLength(); i++) {
|
---|
171 | predefined.add(new WorkflowElementWrapper((Element)workflow_elements.item(i)));
|
---|
172 | }
|
---|
173 | // Creation
|
---|
174 | JPanel workflow_preferences_pane = new JPanel();
|
---|
175 | JPanel checklist_pane = new JPanel();
|
---|
176 | JLabel title_label = new JLabel("Preferences.Workflow.Title");
|
---|
177 | title_label.setPreferredSize(ROW_SIZE);
|
---|
178 | XORToggleButtonGroup checkbox_group = new XORToggleButtonGroup();
|
---|
179 | workflow_browse = new JCheckBox("Preferences.Workflow.Browse", Gatherer.config.get("workflow.browse", false));
|
---|
180 | workflow_browse.setPreferredSize(ROW_SIZE);
|
---|
181 | workflow_mirror = new JCheckBox("Preferences.Workflow.Mirror", Gatherer.config.get("workflow.mirror", false));
|
---|
182 | workflow_mirror.setPreferredSize(ROW_SIZE);
|
---|
183 | workflow_gather = new JCheckBox("Preferences.Workflow.Gather", Gatherer.config.get("workflow.gather", false));
|
---|
184 | workflow_gather.setPreferredSize(ROW_SIZE);
|
---|
185 | workflow_enrich = new JCheckBox("Preferences.Workflow.Enrich", Gatherer.config.get("workflow.enrich", false));
|
---|
186 | workflow_enrich.setPreferredSize(ROW_SIZE);
|
---|
187 | workflow_design = new JCheckBox("Preferences.Workflow.Design", Gatherer.config.get("workflow.design", false));
|
---|
188 | workflow_design.setPreferredSize(ROW_SIZE);
|
---|
189 | workflow_export = new JCheckBox("Preferences.Workflow.Export", Gatherer.config.get("workflow.export", false));
|
---|
190 | workflow_export.setPreferredSize(ROW_SIZE);
|
---|
191 | workflow_create = new JCheckBox("Preferences.Workflow.Create", Gatherer.config.get("workflow.create", false));
|
---|
192 | workflow_create.setPreferredSize(ROW_SIZE);
|
---|
193 | workflow_preview = new JCheckBox("Preferences.Workflow.Preview", Gatherer.config.get("workflow.preview", false));
|
---|
194 | workflow_preview.setPreferredSize(ROW_SIZE);
|
---|
195 | JPanel predefined_pane = new JPanel();
|
---|
196 | JLabel predefined_label = new JLabel("Preferences.Workflow.Predefined.Label");
|
---|
197 | predefined_label.setPreferredSize(new Dimension(150,25));
|
---|
198 | JComboBox predefined_combobox = new JComboBox(predefined);
|
---|
199 | // Connection
|
---|
200 | checkbox_group.add(workflow_mirror);
|
---|
201 | checkbox_group.add(workflow_gather);
|
---|
202 | checkbox_group.add(workflow_enrich);
|
---|
203 | checkbox_group.add(workflow_design);
|
---|
204 | checkbox_group.add(workflow_export);
|
---|
205 | checkbox_group.add(workflow_create);
|
---|
206 | checkbox_group.add(workflow_preview);
|
---|
207 | Gatherer.dictionary.register(title_label, null, false);
|
---|
208 | Gatherer.dictionary.register(workflow_browse, null, false);
|
---|
209 | Gatherer.dictionary.register(workflow_mirror, null, false);
|
---|
210 | Gatherer.dictionary.register(workflow_gather, null, false);
|
---|
211 | Gatherer.dictionary.register(workflow_enrich, null, false);
|
---|
212 | Gatherer.dictionary.register(workflow_design, null, false);
|
---|
213 | Gatherer.dictionary.register(workflow_export, null, false);
|
---|
214 | Gatherer.dictionary.register(workflow_create, null, false);
|
---|
215 | Gatherer.dictionary.register(workflow_preview, null, false);
|
---|
216 | Gatherer.dictionary.register(predefined_label, null, false);
|
---|
217 | predefined_combobox.addActionListener(new PredefinedActionListener());
|
---|
218 | // Layout
|
---|
219 | checklist_pane.setLayout(new BoxLayout(checklist_pane, BoxLayout.Y_AXIS));
|
---|
220 | checklist_pane.add(title_label);
|
---|
221 | if(Gatherer.config.get("workflow.browse", true)) {
|
---|
222 | checklist_pane.add(workflow_browse);
|
---|
223 | }
|
---|
224 | if(Gatherer.config.get("workflow.mirror", true)) {
|
---|
225 | checklist_pane.add(workflow_mirror);
|
---|
226 | }
|
---|
227 | if(Gatherer.config.get("workflow.gather", true)) {
|
---|
228 | checklist_pane.add(workflow_gather);
|
---|
229 | }
|
---|
230 | if(Gatherer.config.get("workflow.enrich", true)) {
|
---|
231 | checklist_pane.add(workflow_enrich);
|
---|
232 | }
|
---|
233 | if(Gatherer.config.get("workflow.design", true)) {
|
---|
234 | checklist_pane.add(workflow_design);
|
---|
235 | }
|
---|
236 | if(Gatherer.config.get("workflow.export", true)) {
|
---|
237 | checklist_pane.add(workflow_export);
|
---|
238 | }
|
---|
239 | if(Gatherer.config.get("workflow.create", true)) {
|
---|
240 | checklist_pane.add(workflow_create);
|
---|
241 | }
|
---|
242 | if(Gatherer.config.get("workflow.preview", true)) {
|
---|
243 | checklist_pane.add(workflow_preview);
|
---|
244 | }
|
---|
245 | predefined_pane.setLayout(new BorderLayout());
|
---|
246 | predefined_pane.add(predefined_label, BorderLayout.WEST);
|
---|
247 | predefined_pane.add(predefined_combobox, BorderLayout.CENTER);
|
---|
248 |
|
---|
249 | workflow_preferences_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
250 | workflow_preferences_pane.setLayout(new BorderLayout());
|
---|
251 | workflow_preferences_pane.add(checklist_pane, BorderLayout.CENTER);
|
---|
252 | workflow_preferences_pane.add(predefined_pane, BorderLayout.SOUTH);
|
---|
253 |
|
---|
254 | return workflow_preferences_pane;
|
---|
255 | }
|
---|
256 |
|
---|
257 | public void dispose() {
|
---|
258 | // Deregister dictionary components
|
---|
259 | Gatherer.dictionary.deregister(this);
|
---|
260 | Gatherer.dictionary.deregister(tab_pane);
|
---|
261 | Gatherer.dictionary.deregister(ok_button);
|
---|
262 | Gatherer.dictionary.deregister(apply_button);
|
---|
263 | Gatherer.dictionary.deregister(cancel_button);
|
---|
264 | // Dispose
|
---|
265 | super.dispose();
|
---|
266 | }
|
---|
267 |
|
---|
268 | private class OKButtonListener
|
---|
269 | implements ActionListener {
|
---|
270 | private boolean close;
|
---|
271 | public OKButtonListener(boolean close) {
|
---|
272 | this.close = close;
|
---|
273 | }
|
---|
274 | public void actionPerformed(ActionEvent event) {
|
---|
275 | // Submit the various changes.
|
---|
276 | // Connection preferences
|
---|
277 | Gatherer.config.set("general.use_proxy", true, use_proxy_checkbox.isSelected());
|
---|
278 | Gatherer.config.setString("general.proxy_host", true, proxy_host_field.getText());
|
---|
279 | Gatherer.config.setString("general.proxy_port", true, proxy_port_field.getText());
|
---|
280 | Gatherer.setProxy();
|
---|
281 | // Warning preferences
|
---|
282 | for(int i = 0; i < warning_preferences_check_list.getEntryCount(); i++) {
|
---|
283 | Entry entry = warning_preferences_check_list.get(i);
|
---|
284 | Gatherer.config.set(entry.getProperty(), true, entry.isSelected());
|
---|
285 | }
|
---|
286 | // Workflow preferences
|
---|
287 | Gatherer.config.set("workflow.browse", false, workflow_browse.isSelected());
|
---|
288 | Gatherer.config.set("workflow.mirror", false, workflow_mirror.isSelected());
|
---|
289 | Gatherer.config.set("workflow.gather", false, workflow_gather.isSelected());
|
---|
290 | Gatherer.config.set("workflow.enrich", false, workflow_enrich.isSelected());
|
---|
291 | Gatherer.config.set("workflow.design", false, workflow_design.isSelected());
|
---|
292 | Gatherer.config.set("workflow.export", false, workflow_export.isSelected());
|
---|
293 | Gatherer.config.set("workflow.create", false, workflow_create.isSelected());
|
---|
294 | Gatherer.config.set("workflow.preview", false, workflow_preview.isSelected());
|
---|
295 | Gatherer.g_man.workflowUpdate("Browser", workflow_browse.isSelected());
|
---|
296 | Gatherer.g_man.workflowUpdate("Mirroring", workflow_mirror.isSelected());
|
---|
297 | Gatherer.g_man.workflowUpdate("Collection", workflow_gather.isSelected());
|
---|
298 | Gatherer.g_man.workflowUpdate("MetaEdit", workflow_enrich.isSelected());
|
---|
299 | Gatherer.g_man.workflowUpdate("Build", workflow_design.isSelected());
|
---|
300 | Gatherer.g_man.workflowUpdate("Export", workflow_export.isSelected());
|
---|
301 | Gatherer.g_man.workflowUpdate("Create", workflow_create.isSelected());
|
---|
302 | Gatherer.g_man.workflowUpdate("Preview", workflow_preview.isSelected());
|
---|
303 | // Hide dialog
|
---|
304 | if(close) {
|
---|
305 | self.dispose();
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | private class CancelButtonListener
|
---|
311 | implements ActionListener {
|
---|
312 | public void actionPerformed(ActionEvent event) {
|
---|
313 | self.dispose();
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | private class PredefinedActionListener
|
---|
318 | implements ActionListener {
|
---|
319 | public void actionPerformed(ActionEvent event) {
|
---|
320 | JComboBox cb = (JComboBox) event.getSource();
|
---|
321 | WorkflowElementWrapper element = (WorkflowElementWrapper) cb.getSelectedItem();
|
---|
322 | CheckboxUpdater task = new CheckboxUpdater(element);
|
---|
323 | SwingUtilities.invokeLater(task);
|
---|
324 | }
|
---|
325 |
|
---|
326 | private class CheckboxUpdater
|
---|
327 | implements Runnable {
|
---|
328 | private WorkflowElementWrapper element;
|
---|
329 | public CheckboxUpdater(WorkflowElementWrapper element) {
|
---|
330 | this.element = element;
|
---|
331 | }
|
---|
332 | public void run() {
|
---|
333 | workflow_browse.setSelected(element.getEnabled("browse"));
|
---|
334 | workflow_create.setSelected(element.getEnabled("create"));
|
---|
335 | workflow_mirror.setSelected(element.getEnabled("mirror"));
|
---|
336 | workflow_gather.setSelected(element.getEnabled("gather"));
|
---|
337 | workflow_enrich.setSelected(element.getEnabled("enrich"));
|
---|
338 | workflow_design.setSelected(element.getEnabled("design"));
|
---|
339 | workflow_export.setSelected(element.getEnabled("export"));
|
---|
340 | workflow_preview.setSelected(element.getEnabled("preview"));
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | private class UseProxyListener
|
---|
346 | implements ActionListener {
|
---|
347 | public void actionPerformed(ActionEvent event) {
|
---|
348 | boolean enabled = use_proxy_checkbox.isSelected();
|
---|
349 | Gatherer.config.set("general.use_proxy", true, enabled);
|
---|
350 | // Fortunately this is already driven by the event thread.
|
---|
351 | proxy_host_field.setEnabled(enabled);
|
---|
352 | proxy_port_field.setEnabled(enabled);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | private class WorkflowElementWrapper {
|
---|
357 | private Element element;
|
---|
358 | private String text;
|
---|
359 | public WorkflowElementWrapper(Element element) {
|
---|
360 | this.element = element;
|
---|
361 | }
|
---|
362 | public boolean getEnabled(String name) {
|
---|
363 | boolean result = true;
|
---|
364 | if(element.getAttribute(name).equalsIgnoreCase("false")) {
|
---|
365 | result = false;
|
---|
366 | }
|
---|
367 | return result;
|
---|
368 | }
|
---|
369 | public String toString() {
|
---|
370 | if(text == null) {
|
---|
371 | text = element.getFirstChild().getNodeValue();
|
---|
372 | }
|
---|
373 | return text;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|