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

Last change on this file since 5223 was 5223, checked in by mdewsnip, 21 years ago

Removed unused constant.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 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 * Written: 16/07/03
30 * Revised:
31 **************************************************************************************/
32import java.awt.*;
33import java.awt.event.*;
34import java.util.*;
35import javax.swing.*;
36import javax.swing.event.*;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.cdm.CollectionConfiguration;
39import org.greenstone.gatherer.cdm.CollectionDesignManager;
40import org.greenstone.gatherer.cdm.Control;
41import org.greenstone.gatherer.cdm.SearchType;
42import org.greenstone.gatherer.cdm.DOMProxyListModel;
43import org.greenstone.gatherer.gui.DoubleImageButton;
44import org.greenstone.gatherer.gui.GComboBox;
45import org.greenstone.gatherer.msm.MSMUtils;
46import org.greenstone.gatherer.util.Utility;
47import org.w3c.dom.*;
48/** This class maintains an ordered list of the search types available in the collection (MGPP command available in G2.39 or later). Currently only 'form' and 'plain' are valid.
49 * @author John Thompson, Greenstone Digital Library, University of Waikato
50 * @version 2.4
51 */
52public class SearchTypeManager
53 extends DOMProxyListModel {
54
55 static final public String[] SEARCH_TYPES = {"form", "plain"};
56
57
58 /** The controls used to edit the search types. */
59 private Control controls = null;
60 /** A reference to ourselves so our inner classes have access. */
61 private DOMProxyListModel model;
62
63 public SearchTypeManager(Element searchtypes_element) {
64 super(searchtypes_element, CollectionConfiguration.CONTENT_ELEMENT, new SearchType());
65 this.model = this;
66 Gatherer.println("SearchTypeManager: parsed " + getSize() + " search types.");
67 }
68
69 public void addSearchType(SearchType searchtype) {
70 if(!contains(searchtype)) {
71 add(getSize(), searchtype);
72 Gatherer.c_man.configurationChanged();
73 }
74 }
75
76 public Control getControls() {
77 if(controls == null) {
78 controls = new SearchTypeControl();
79 }
80 return controls;
81 }
82
83 /** Be examining the SearchType 'root' we were created with, determine if mgpp is enabled.
84 * @return true if MGPP is enabled, false otherwise
85 */
86 public boolean isMGPPEnabled() {
87 return root.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR);
88 }
89
90 public void removeSearchType(SearchType searchtype) {
91 if(contains(searchtype)) {
92 remove(searchtype);
93 Gatherer.c_man.configurationChanged();
94 }
95 }
96
97 private class SearchTypeControl
98 extends JPanel
99 implements Control {
100
101 private GComboBox search_type_combobox;
102
103 private JButton add_button;
104 private JButton move_down_button;
105 private JButton move_up_button;
106 private JButton remove_button;
107
108 private JCheckBox enable_advanced_searches_checkbox;
109
110 private JLabel current_search_types_label;
111 private JLabel search_type_label;
112 private JLabel title_label;
113
114 private JList current_search_types_list;
115
116 private JTextArea instructions_textarea;
117
118 public SearchTypeControl() {
119 // Creation
120 JPanel instructions_panel = new JPanel();
121 title_label = new JLabel("CDM.SearchTypeManager.Title");
122 title_label.setHorizontalAlignment(JLabel.CENTER);
123 instructions_textarea = new JTextArea("CDM.SearchTypeManager.Instructions");
124 instructions_textarea.setEditable(false);
125 instructions_textarea.setLineWrap(true);
126 instructions_textarea.setRows(6);
127 instructions_textarea.setWrapStyleWord(true);
128
129 JPanel spacer_panel = new JPanel();
130
131 JPanel empty_panel = new JPanel();
132
133 JPanel inner_panel = new JPanel();
134
135 enable_advanced_searches_checkbox = new JCheckBox("CDM.SearchTypeManager.Enable");
136
137 JPanel current_search_types_panel = new JPanel();
138 current_search_types_label = new JLabel("CDM.SearchTypeManager.Assigned");
139 current_search_types_list = new JList(model);
140 current_search_types_list.setVisibleRowCount(3);
141
142 JPanel movement_panel = new JPanel();
143
144 move_up_button = new DoubleImageButton("CDM.Move.Move_Up", Utility.getImage("arrow-up.gif"), Utility.getImage("arrow-up-disabled.gif"));
145 move_up_button.setEnabled(false);
146 move_up_button.setMnemonic(KeyEvent.VK_U);
147 move_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
148
149 move_down_button = new DoubleImageButton("CDM.Move.Move_Down", Utility.getImage("arrow-down.gif"), Utility.getImage("arrow-down-disabled.gif"));
150 move_down_button.setEnabled(false);
151 move_down_button.setMnemonic(KeyEvent.VK_D);
152 move_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
153
154 JPanel search_type_panel = new JPanel();
155 search_type_label = new JLabel("CDM.SearchTypeManager.SearchType");
156 search_type_combobox = new GComboBox(SEARCH_TYPES);
157 search_type_combobox.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.editable_background", false));
158 search_type_combobox.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
159 search_type_combobox.setEditable(true);
160 search_type_combobox.setSelectedIndex(0);
161 search_type_combobox.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
162 search_type_combobox.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
163
164 JPanel button_panel = new JPanel();
165 add_button = new JButton("CDM.SearchTypeManager.Add");
166 add_button.setEnabled(false);
167 add_button.setMnemonic(KeyEvent.VK_A);
168
169 remove_button = new JButton("CDM.SearchTypeManager.Remove");
170 remove_button.setEnabled(false);
171 remove_button.setMnemonic(KeyEvent.VK_R);
172
173 // Connection
174 add_button.addActionListener(new AddActionListener());
175 current_search_types_list.addListSelectionListener(new CurrentSearchTypesListSelectionListener());
176 enable_advanced_searches_checkbox.addActionListener(new EnableAdvancedSearchesActionListener());
177 Gatherer.dictionary.register(add_button, null, false);
178 Gatherer.dictionary.register(current_search_types_label, null, false);
179 Gatherer.dictionary.register(enable_advanced_searches_checkbox, null, false);
180 Gatherer.dictionary.register(instructions_textarea, null, false);
181 Gatherer.dictionary.register(move_up_button, null, false);
182 Gatherer.dictionary.register(move_down_button, null, false);
183 Gatherer.dictionary.register(remove_button, null, false);
184 Gatherer.dictionary.register(search_type_label, null, false);
185 Gatherer.dictionary.register(title_label, null, false);
186 remove_button.addActionListener(new RemoveActionListener());
187 SearchTypesActionDocumentListener stadl = new SearchTypesActionDocumentListener();
188 search_type_combobox.addActionListener(stadl);
189 ((JTextField)search_type_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(stadl);
190 // Layout
191 instructions_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
192 instructions_panel.setLayout(new BorderLayout());
193 instructions_panel.add(title_label, BorderLayout.NORTH);
194 instructions_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
195
196 movement_panel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
197 movement_panel.setLayout(new GridLayout(2,1,5,0));
198 movement_panel.add(move_up_button);
199 movement_panel.add(move_down_button);
200
201 current_search_types_panel.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
202 current_search_types_panel.setLayout(new BorderLayout());
203 current_search_types_panel.add(current_search_types_label, BorderLayout.NORTH);
204 current_search_types_panel.add(new JScrollPane(current_search_types_list), BorderLayout.CENTER);
205 current_search_types_panel.add(movement_panel, BorderLayout.EAST);
206
207 button_panel.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
208 button_panel.setLayout(new GridLayout(1,2,0,5));
209 button_panel.add(add_button);
210 button_panel.add(remove_button);
211
212 search_type_panel.setLayout(new BorderLayout());
213 search_type_panel.add(search_type_label, BorderLayout.WEST);
214 search_type_panel.add(search_type_combobox, BorderLayout.CENTER);
215 search_type_panel.add(button_panel, BorderLayout.SOUTH);
216
217 inner_panel.setLayout(new BorderLayout());
218 inner_panel.add(enable_advanced_searches_checkbox, BorderLayout.NORTH);
219 inner_panel.add(current_search_types_panel, BorderLayout.CENTER);
220 inner_panel.add(search_type_panel, BorderLayout.SOUTH);
221
222 spacer_panel.setLayout(new BorderLayout());
223 spacer_panel.add(inner_panel, BorderLayout.NORTH);
224 spacer_panel.add(empty_panel, BorderLayout.CENTER);
225
226 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
227 setLayout(new BorderLayout());
228 add(instructions_panel, BorderLayout.NORTH);
229 add(spacer_panel, BorderLayout.CENTER);
230 }
231
232 public void destroy() {
233 Gatherer.dictionary.deregister(add_button);
234 Gatherer.dictionary.deregister(current_search_types_label);
235 Gatherer.dictionary.deregister(enable_advanced_searches_checkbox);
236 Gatherer.dictionary.deregister(instructions_textarea);
237 Gatherer.dictionary.deregister(move_up_button);
238 Gatherer.dictionary.deregister(move_down_button);
239 Gatherer.dictionary.deregister(remove_button);
240 Gatherer.dictionary.deregister(search_type_label);
241 Gatherer.dictionary.deregister(title_label);
242 }
243
244 public void gainFocus() {
245 instructions_textarea.setCaretPosition(0);
246 validateControls(isMGPPEnabled());
247 }
248
249 public void loseFocus() {
250
251 }
252
253 private void validateControls(boolean advanced_search_enabled) {
254 // Enable or disable controls based on whether MGPP is enabled
255 // validate add button, which depends on the current combobox selection and the contents of the assigned search types list
256 Object selected_item = search_type_combobox.getSelectedItem();
257 add_button.setEnabled(advanced_search_enabled && selected_item != null && !model.contains(selected_item));
258 // validate other controls.
259 current_search_types_list.setEnabled(advanced_search_enabled);
260 enable_advanced_searches_checkbox.setSelected(advanced_search_enabled);
261 search_type_combobox.setEnabled(advanced_search_enabled);
262 remove_button.setEnabled(current_search_types_list.getModel().getSize() > 1 && !current_search_types_list.isSelectionEmpty() && advanced_search_enabled);
263 }
264
265 /** Listenes for actions on the Add button, and if detected adds a new search type. */
266 private class AddActionListener
267 implements ActionListener {
268 /** Called when someone actions the Add button.
269 * @param event an ActionEvent containing information about the add button click
270 */
271 public void actionPerformed(ActionEvent event) {
272 Object selected_item = search_type_combobox.getSelectedItem();
273 if(selected_item != null) {
274 if(search_type_combobox.getSelectedIndex() == -1) {
275 search_type_combobox.insertItemAt(selected_item, search_type_combobox.getItemCount());
276 }
277 // Add the search type
278 SearchType new_searchtype = new SearchType((String)selected_item);
279 addSearchType(new_searchtype);
280 }
281 add_button.setEnabled(false);
282 }
283 }
284
285 /** Listens for selections within the search types list and updates the remove button appropriately. */
286 private class CurrentSearchTypesListSelectionListener
287 implements ListSelectionListener {
288 /** Called when the selection in the list changes.
289 * @param event a ListSelectionEvent containing information about the selection change
290 */
291 public void valueChanged(ListSelectionEvent event) {
292 if(!event.getValueIsAdjusting()) {
293 if (current_search_types_list.isSelectionEmpty()) {
294 move_up_button.setEnabled(false);
295 move_down_button.setEnabled(false);
296 }
297 else {
298 move_up_button.setEnabled(true);
299 move_down_button.setEnabled(true);
300 remove_button.setEnabled(current_search_types_list.getModel().getSize() > 1);
301 }
302 }
303 }
304 }
305
306 /** 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. */
307 private class EnableAdvancedSearchesActionListener
308 implements ActionListener {
309 /** Called whenever the checkbox is checked or unchecked.
310 * @param event an ActionEvent containing information about the checking action
311 */
312 public void actionPerformed(ActionEvent event) {
313 Gatherer.g_man.wait(true);
314 boolean advanced_search_enabled = enable_advanced_searches_checkbox.isSelected();
315 model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (advanced_search_enabled ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
316 CollectionDesignManager.index_manager.setMGPPEnabled(advanced_search_enabled);
317 validateControls(advanced_search_enabled);
318 Gatherer.g_man.wait(false);
319 }
320 }
321
322 /** Listenes for actions on the Remove button, and if detected removes the currently selected search types. */
323 private class RemoveActionListener
324 implements ActionListener {
325 /** Called when someone actions the Remove button.
326 * @param event an ActionEvent containing information about the remove button click
327 */
328 public void actionPerformed(ActionEvent event) {
329 if(!current_search_types_list.isSelectionEmpty()) {
330 Object[] selected_items = current_search_types_list.getSelectedValues();
331 for(int i = 0; i < selected_items.length; i++) {
332 removeSearchType((SearchType)selected_items[i]);
333 }
334 }
335 Object selected_object = search_type_combobox.getSelectedItem();
336 if(selected_object != null) {
337 add_button.setEnabled(!model.contains(selected_object));
338 }
339 else {
340 add_button.setEnabled(false);
341 }
342 remove_button.setEnabled(false);
343 }
344 }
345
346 /** Listens for changes in the search types combobox, and enabled add button appropriately. */
347 private class SearchTypesActionDocumentListener
348 implements ActionListener, DocumentListener {
349 /** Called whenever a selection action occurs on the combobox.
350 * @param event an ActionEvent containing information about the selection event
351 */
352 public void actionPerformed(ActionEvent event) {
353 validateAddButton();
354 }
355
356 /** Gives notification that an attribute or set of attributes changed.
357 * @param event a DocumentEvent containing information about the text changed
358 */
359 public void changedUpdate(DocumentEvent event) {
360 validateAddButton();
361 }
362 /** Gives notification that there was an insert into the document.
363 * @param event a DocumentEvent containing information about the text added
364 */
365 public void insertUpdate(DocumentEvent event) {
366 validateAddButton();
367 }
368
369 /** Gives notification that a portion of the document has been removed.
370 * @param event a DocumentEvent containing information about the text removed
371 */
372 public void removeUpdate(DocumentEvent e) {
373 validateAddButton();
374 }
375
376 /** Change the enable state of the add button depending on the current value in the search type combobox. */
377 private void validateAddButton() {
378 Object selected_object = search_type_combobox.getSelectedItem();
379 if(selected_object != null) {
380 add_button.setEnabled(!model.contains(selected_object));
381 }
382 else {
383 add_button.setEnabled(false);
384 }
385 }
386 }
387 }
388}
Note: See TracBrowser for help on using the repository browser.