source: trunk/gli/src/org/greenstone/gatherer/cdm/SearchTypeManager.java@ 10049

Last change on this file since 10049 was 10011, checked in by mdewsnip, 19 years ago

Moved Utility.getImage into JarTools, as part of tidying up the Utility class.

  • Property svn:keywords set to Author Date Id Revision
File size: 20.9 KB
Line 
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 Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 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 */
27package org.greenstone.gatherer.cdm;
28
29/**************************************************************************************
30 * Written: 16/07/03
31 * Revised:
32 **************************************************************************************/
33import java.awt.*;
34import java.awt.event.*;
35import java.util.*;
36import javax.swing.*;
37import javax.swing.event.*;
38import org.greenstone.gatherer.Configuration;
39import org.greenstone.gatherer.DebugStream;
40import org.greenstone.gatherer.Dictionary;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.gui.GComboBox;
43import org.greenstone.gatherer.gui.GLIButton;
44import org.greenstone.gatherer.util.JarTools;
45import org.w3c.dom.*;
46
47/** This class maintains an ordered list of the search types available in the collection (MGPP/lucene command available in G2.39 or later). Currently only 'form' and 'plain' are valid.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.4
50 */
51public class SearchTypeManager
52 extends DOMProxyListModel
53{
54 static final private Dimension LABEL_SIZE = new Dimension(150, 25);
55
56 static final public String[] SEARCH_TYPES = { "plain" , "form"};
57 static final public String[] BUILD_TYPES = { "lucene", "mgpp" };
58
59 /** the buildtype element in the config file - searchtypemanager looks
60 * after this now */
61 public CollectionMeta build_type = null;
62 /** The controls used to edit the search types. */
63 private Control controls = null;
64 /** A reference to ourselves so our inner classes have access. */
65 private DOMProxyListModel model;
66
67 public SearchTypeManager(Element searchtypes_element) {
68 super(searchtypes_element, CollectionConfiguration.CONTENT_ELEMENT, new SearchType());
69 this.model = this;
70 DebugStream.println("SearchTypeManager: parsed " + getSize() + " search types.");
71 build_type = new CollectionMeta(CollectionDesignManager.collect_config.getBuildType());
72
73 }
74
75 private void addSearchType(SearchType searchtype) {
76 if(!contains(searchtype)) {
77 add(getSize(), searchtype);
78 Gatherer.c_man.configurationChanged();
79 }
80 }
81
82 public Control getControls() {
83 if(controls == null) {
84 controls = new SearchTypeControl();
85 }
86 return controls;
87 }
88
89 /** Return a list of the currently assigned search types as a comma separated string.
90 * @return a String
91 */
92 public String getSearchTypes() {
93 StringBuffer search_types = new StringBuffer();
94 ArrayList types = children();
95 for (int i = 0; i < types.size(); i++) {
96 if (i>0) {
97 search_types.append(",");
98 }
99 search_types.append(((SearchType)types.get(i)).getName());
100 }
101 return search_types.toString();
102 }
103
104 /** By examining the SearchType 'root' we were created with, determine if mgpp is enabled.
105 * @return true if MGPP is enabled, false otherwise
106 */
107 public boolean isMGPPEnabled() {
108 return root.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR);
109 }
110
111 private void moveSearchType(SearchType search_type, boolean direction) {
112 // Try to move the classifier one step in the desired direction.
113 int index = indexOf(search_type);
114 if(direction) {
115 index--;
116 }
117 else {
118 index++;
119 }
120 // Check we aren't at an edge
121 if((direction && index < 0) || (!direction && index >= getSize())) {
122 String args[] = new String[2];
123 args[0] = Dictionary.get("CDM.SearchTypeManager.SearchType");
124 args[1] = search_type.toString();
125 String message = null;
126 if (direction) {
127 message = Dictionary.get("CDM.Move.At_Top", args);
128 }
129 else {
130 message = Dictionary.get("CDM.Move.At_Bottom", args);
131 }
132 JOptionPane.showMessageDialog(Gatherer.g_man, message, Dictionary.get("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE);
133 return;
134 }
135 remove(search_type);
136 add(index, search_type);
137 Gatherer.c_man.configurationChanged();
138 }
139
140 private void removeSearchType(SearchType searchtype) {
141 if(contains(searchtype)) {
142 remove(searchtype);
143 Gatherer.c_man.configurationChanged();
144 }
145 }
146
147 private class SearchTypeControl
148 extends JPanel
149 implements Control {
150
151 private GComboBox search_type_combobox;
152 private GComboBox build_type_combobox;
153
154 private JButton add_button;
155 private JButton move_down_button;
156 private JButton move_up_button;
157 private JButton remove_button;
158
159 private JCheckBox enable_advanced_searches_checkbox;
160
161 private JLabel current_search_types_label;
162 private JLabel search_type_label;
163 private JLabel title_label;
164
165 private JList current_search_types_list;
166
167 private JTextArea instructions_textarea;
168
169 public SearchTypeControl() {
170
171 // Creation
172 title_label = new JLabel();
173 title_label.setHorizontalAlignment(JLabel.CENTER);
174 Dictionary.registerText(title_label, "CDM.SearchTypeManager.Title");
175
176 JPanel instructions_panel = new JPanel();
177 instructions_textarea = new JTextArea();
178 instructions_textarea.setCaretPosition(0);
179 instructions_textarea.setEditable(false);
180 instructions_textarea.setLineWrap(true);
181 instructions_textarea.setRows(6);
182 instructions_textarea.setWrapStyleWord(true);
183 Dictionary.registerText(instructions_textarea, "CDM.SearchTypeManager.Instructions");
184
185 JPanel spacer_panel = new JPanel();
186
187 JPanel empty_panel = new JPanel();
188
189 JPanel inner_panel = new JPanel();
190
191 JPanel indexer_type_panel = new JPanel();
192 JPanel build_type_panel = new JPanel();
193
194 enable_advanced_searches_checkbox = new JCheckBox();
195 Dictionary.registerText(enable_advanced_searches_checkbox, "CDM.SearchTypeManager.Enable");
196
197 build_type_combobox = new GComboBox(BUILD_TYPES);
198 build_type_combobox.setPreferredSize(LABEL_SIZE);
199 build_type_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
200 build_type_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
201 build_type_combobox.setEditable(true);
202 build_type_combobox.setSelectedIndex(1);// mgpp is at 1
203 String selected_value = build_type.getValue(CollectionMeta.TEXT);
204 if (!selected_value.equals("")) {
205 for (int i=0; i<build_type_combobox.getItemCount();i++) {
206 String item = (String)build_type_combobox.getItemAt(i);
207 if (item.equals(selected_value)) {
208 build_type_combobox.setSelectedIndex(i);
209 break;
210 }
211 }
212 } else {
213 build_type.setValue((String)build_type_combobox.getSelectedItem());
214 build_type.setAssigned(isMGPPEnabled());
215
216 }
217 build_type_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
218 build_type_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
219 Dictionary.registerTooltip(build_type_combobox, "CDM.SearchTypeManager.BuildType_Selection_Tooltip");
220
221 JLabel build_type_label = new JLabel();
222 Dictionary.registerText(build_type_label, "CDM.SearchTypeManager.BuildType_Selection");
223 JPanel current_search_types_panel = new JPanel();
224 current_search_types_label = new JLabel();
225 Dictionary.registerText(current_search_types_label, "CDM.SearchTypeManager.Assigned");
226 current_search_types_list = new JList(model);
227 current_search_types_list.setVisibleRowCount(3);
228
229 JPanel movement_panel = new JPanel();
230
231 move_up_button = new JButton("", JarTools.getImage("arrow-up.gif"));
232 move_up_button.setEnabled(false);
233 move_up_button.setMnemonic(KeyEvent.VK_U);
234
235 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
236
237 move_down_button = new JButton("", JarTools.getImage("arrow-down.gif"));
238 move_down_button.setEnabled(false);
239 move_down_button.setMnemonic(KeyEvent.VK_D);
240
241 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
242
243 JPanel search_type_panel = new JPanel();
244 search_type_label = new JLabel();
245 Dictionary.registerText(search_type_label, "CDM.SearchTypeManager.SearchType_Selection");
246 search_type_combobox = new GComboBox(SEARCH_TYPES);
247 search_type_combobox.setPreferredSize(LABEL_SIZE);
248 search_type_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
249 search_type_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
250 search_type_combobox.setEditable(true);
251 search_type_combobox.setSelectedIndex(0);
252 search_type_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
253 search_type_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
254 Dictionary.registerTooltip(search_type_combobox, "CDM.SearchTypeManager.SearchType_Selection_Tooltip");
255
256 JPanel button_panel = new JPanel();
257 add_button = new GLIButton();
258 add_button.setEnabled(false);
259 add_button.setMnemonic(KeyEvent.VK_A);
260 Dictionary.registerBoth(add_button, "CDM.SearchTypeManager.Add", "CDM.SearchTypeManager.Add_Tooltip");
261
262 remove_button = new GLIButton();
263 remove_button.setEnabled(false);
264 remove_button.setMnemonic(KeyEvent.VK_R);
265 Dictionary.registerBoth(remove_button, "CDM.SearchTypeManager.Remove", "CDM.SearchTypeManager.Remove_Tooltip");
266
267
268 // Connection
269 add_button.addActionListener(new AddActionListener());
270 build_type_combobox.addActionListener(new BuildTypeActionListener());
271 current_search_types_list.addListSelectionListener(new CurrentSearchTypesListSelectionListener());
272 enable_advanced_searches_checkbox.addActionListener(new EnableAdvancedSearchesActionListener());
273 move_up_button.addActionListener(new MoveListener(true));
274 move_down_button.addActionListener(new MoveListener(false));
275 remove_button.addActionListener(new RemoveActionListener());
276 SearchTypesActionDocumentListener stadl = new SearchTypesActionDocumentListener();
277 search_type_combobox.addActionListener(stadl);
278 ((JTextField)search_type_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(stadl);
279
280 // Layout
281 instructions_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
282 instructions_panel.setLayout(new BorderLayout());
283 instructions_panel.add(title_label, BorderLayout.NORTH);
284 instructions_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
285
286 movement_panel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
287 movement_panel.setLayout(new GridLayout(2,1,5,0));
288 movement_panel.add(move_up_button);
289 movement_panel.add(move_down_button);
290
291 current_search_types_panel.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
292 current_search_types_panel.setLayout(new BorderLayout());
293 current_search_types_panel.add(current_search_types_label, BorderLayout.NORTH);
294 current_search_types_panel.add(new JScrollPane(current_search_types_list), BorderLayout.CENTER);
295 current_search_types_panel.add(movement_panel, BorderLayout.EAST);
296
297 build_type_panel.setLayout(new BorderLayout());
298 build_type_panel.add(build_type_label, BorderLayout.WEST);
299 build_type_panel.add(build_type_combobox, BorderLayout.CENTER);
300
301 indexer_type_panel.setLayout(new GridLayout(1,2,0,5));
302 indexer_type_panel.add(enable_advanced_searches_checkbox);
303 indexer_type_panel.add(build_type_panel);
304
305 button_panel.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
306 button_panel.setLayout(new GridLayout(1,2,0,5));
307 button_panel.add(add_button);
308 button_panel.add(remove_button);
309
310 search_type_panel.setLayout(new BorderLayout(5,3));
311 search_type_panel.add(search_type_label, BorderLayout.WEST);
312 search_type_panel.add(search_type_combobox, BorderLayout.CENTER);
313 search_type_panel.add(button_panel, BorderLayout.SOUTH);
314
315 inner_panel.setLayout(new BorderLayout());
316 inner_panel.add(indexer_type_panel, BorderLayout.NORTH);
317 inner_panel.add(current_search_types_panel, BorderLayout.CENTER);
318 inner_panel.add(search_type_panel, BorderLayout.SOUTH);
319
320 spacer_panel.setLayout(new BorderLayout());
321 spacer_panel.add(inner_panel, BorderLayout.NORTH);
322 spacer_panel.add(empty_panel, BorderLayout.CENTER);
323
324 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
325 setLayout(new BorderLayout());
326 add(instructions_panel, BorderLayout.NORTH);
327 add(spacer_panel, BorderLayout.CENTER);
328 }
329
330 public void destroy() {
331 }
332
333 public void gainFocus() {
334 instructions_textarea.setCaretPosition(0);
335 validateControls(isMGPPEnabled());
336 }
337
338 public void loseFocus() {
339
340 }
341
342 private void validateControls(boolean advanced_search_enabled) {
343 // Enable or disable controls based on whether MGPP is enabled
344 // validate add button, which depends on the current combobox selection and the contents of the assigned search types list
345 Object selected_item = search_type_combobox.getSelectedItem();
346 add_button.setEnabled(advanced_search_enabled && selected_item != null && !model.contains(selected_item));
347 // validate other controls.
348 current_search_types_list.setEnabled(advanced_search_enabled);
349 build_type_combobox.setEnabled(advanced_search_enabled);
350 enable_advanced_searches_checkbox.setSelected(advanced_search_enabled);
351 search_type_combobox.setEnabled(advanced_search_enabled);
352 remove_button.setEnabled(current_search_types_list.getModel().getSize() > 1 && !current_search_types_list.isSelectionEmpty() && advanced_search_enabled);
353 }
354
355 /** Listenes for actions on the Add button, and if detected adds a new search type. */
356 private class AddActionListener
357 implements ActionListener {
358 /** Called when someone actions the Add button.
359 * @param event an ActionEvent containing information about the add button click
360 */
361 public void actionPerformed(ActionEvent event) {
362 Object selected_item = search_type_combobox.getSelectedItem();
363 if(selected_item != null) {
364 if(search_type_combobox.getSelectedIndex() == -1) {
365 search_type_combobox.insertItemAt(selected_item, search_type_combobox.getItemCount());
366 }
367 // Add the search type
368 SearchType new_searchtype = new SearchType((String)selected_item);
369 addSearchType(new_searchtype);
370 }
371 add_button.setEnabled(false);
372 }
373 }
374
375 /** Listens for selections within the search types list and updates the remove button appropriately. */
376 private class CurrentSearchTypesListSelectionListener
377 implements ListSelectionListener {
378 /** Called when the selection in the list changes.
379 * @param event a ListSelectionEvent containing information about the selection change
380 */
381 public void valueChanged(ListSelectionEvent event) {
382 if(!event.getValueIsAdjusting()) {
383 SearchType search_type = null;
384 if ((search_type = (SearchType) current_search_types_list.getSelectedValue()) != null) {
385 int index = model.indexOf(search_type);
386 // Move up is only enabled if the current selection isn't at index 0
387 move_up_button.setEnabled(index != 0);
388 // Move down is only enabled if the current selection isn't at index getSize() - 1
389 move_down_button.setEnabled(index != model.getSize() - 1);
390 // Remove is only enabled if this isn't the last search type
391 remove_button.setEnabled(current_search_types_list.getModel().getSize() > 1);
392 }
393 else {
394 move_up_button.setEnabled(false);
395 move_down_button.setEnabled(false);
396 remove_button.setEnabled(false);
397 }
398 }
399 }
400 }
401
402 /** The most complex listener in this class, this listens for changes to the enable advanced searches checkbox, and when they occur it not only updates the controls on this page, but asks the IndexManager to action the appropriate changes to the underlying DOM so as to support either MG or MGPP styles of indexes. */
403 private class EnableAdvancedSearchesActionListener
404 implements ActionListener {
405 /** Called whenever the checkbox is checked or unchecked.
406 * @param event an ActionEvent containing information about the checking action
407 */
408 public void actionPerformed(ActionEvent event) {
409 Gatherer.g_man.wait(true);
410 boolean advanced_search_enabled = enable_advanced_searches_checkbox.isSelected();
411 model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (advanced_search_enabled ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
412 CollectionDesignManager.index_manager.setMGPPEnabled(advanced_search_enabled);
413 validateControls(advanced_search_enabled);
414 build_type.setAssigned(advanced_search_enabled);
415 Gatherer.g_man.wait(false);
416 }
417 }
418
419 private class BuildTypeActionListener
420 implements ActionListener {
421
422 public void actionPerformed(ActionEvent event) {
423 build_type.setValue((String)build_type_combobox.getSelectedItem());
424 }
425
426 }
427 /** Listenes for actions on the Remove button, and if detected removes the currently selected search types. */
428 private class RemoveActionListener
429 implements ActionListener {
430 /** Called when someone actions the Remove button.
431 * @param event an ActionEvent containing information about the remove button click
432 */
433 public void actionPerformed(ActionEvent event) {
434 if(!current_search_types_list.isSelectionEmpty()) {
435 Object[] selected_items = current_search_types_list.getSelectedValues();
436 for(int i = 0; model.getSize() > 1 && i < selected_items.length; i++) {
437 removeSearchType((SearchType)selected_items[i]);
438 }
439 }
440 Object selected_object = search_type_combobox.getSelectedItem();
441 if(selected_object != null) {
442 add_button.setEnabled(!model.contains(selected_object));
443 }
444 else {
445 add_button.setEnabled(false);
446 }
447 remove_button.setEnabled(false);
448 }
449 }
450
451 private class MoveListener
452 implements ActionListener {
453 private boolean move_up;
454 public MoveListener(boolean move_up) {
455 this.move_up = move_up;
456 }
457 public void actionPerformed(ActionEvent event) {
458 // Retrieve the first selected search type (if any)
459 SearchType search_type = null;
460 if((search_type = (SearchType) current_search_types_list.getSelectedValue()) != null) {
461 // Move search type
462 moveSearchType(search_type, move_up);
463 // Reselect the moved search type
464 current_search_types_list.setSelectedValue(search_type, true);
465 }
466 // No selection - no movement
467 else {
468 move_up_button.setEnabled(false);
469 move_down_button.setEnabled(false);
470 }
471 }
472 }
473
474 /** Listens for changes in the search types combobox, and enabled add button appropriately. */
475 private class SearchTypesActionDocumentListener
476 implements ActionListener, DocumentListener {
477 /** Called whenever a selection action occurs on the combobox.
478 * @param event an ActionEvent containing information about the selection event
479 */
480 public void actionPerformed(ActionEvent event) {
481 validateAddButton();
482 }
483
484 /** Gives notification that an attribute or set of attributes changed.
485 * @param event a DocumentEvent containing information about the text changed
486 */
487 public void changedUpdate(DocumentEvent event) {
488 validateAddButton();
489 }
490 /** Gives notification that there was an insert into the document.
491 * @param event a DocumentEvent containing information about the text added
492 */
493 public void insertUpdate(DocumentEvent event) {
494 validateAddButton();
495 }
496
497 /** Gives notification that a portion of the document has been removed.
498 * @param event a DocumentEvent containing information about the text removed
499 */
500 public void removeUpdate(DocumentEvent event) {
501 validateAddButton();
502 }
503
504 /** Change the enable state of the add button depending on the current value in the search type combobox. */
505 private void validateAddButton() {
506 Object selected_object = search_type_combobox.getSelectedItem();
507 if(selected_object != null) {
508 add_button.setEnabled(!model.contains(selected_object));
509 }
510 else {
511 add_button.setEnabled(false);
512 }
513 }
514 }
515 }
516}
Note: See TracBrowser for help on using the repository browser.