source: trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionIndexManager.java@ 5590

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

Could it be I've finished adding tooltips?? Why yes, very nearly... and a big "hallelulah" for that.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.cdm.CollectionConfiguration;
37import org.greenstone.gatherer.cdm.CollectionDesignManager;
38import org.greenstone.gatherer.cdm.Control;
39import org.greenstone.gatherer.cdm.DOMProxyListModel;
40import org.greenstone.gatherer.cdm.Subcollection;
41import org.greenstone.gatherer.cdm.SubcollectionIndex;
42import org.greenstone.gatherer.msm.ElementWrapper;
43import org.greenstone.gatherer.util.ExclusiveListSelectionListener;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.Utility;
46import org.w3c.dom.*;
47/** This class maintains a list of indexes partitions for the purpose of defining subcollections.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.4
50 */
51public class SubcollectionIndexManager
52 extends DOMProxyListModel {
53
54 private Control controls;
55 private DOMProxyListModel model;
56 private SubcollectionIndex default_index;
57
58 /** Constructor. */
59 public SubcollectionIndexManager(Element subindexes) {
60 super(subindexes, CollectionConfiguration.INDEX_ELEMENT, new SubcollectionIndex());
61 Gatherer.println("SubcollectionIndexManager: " + getSize() + " subcollection indexes parsed.");
62 model = this;
63 // Parse and retrieve the default index
64 NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT);
65 if(default_index_elements.getLength() > 0) {
66 default_index = new SubcollectionIndex((Element)default_index_elements.item(0));
67 }
68 }
69
70 /** Method to add a subindex.
71 * @param subindex a SubcollectionIndex
72 * @see org.greenstone.gatherer.Gatherer
73 * @see org.greenstone.gatherer.collection.CollectionManager
74 */
75 public void addSubcollectionIndex(SubcollectionIndex subindex) {
76 if(!contains(subindex)) {
77 add(getSize(), subindex);
78 Gatherer.c_man.configurationChanged();
79 }
80 }
81
82 public void destroy() {
83 if(controls != null) {
84 controls.destroy();
85 controls = null;
86 }
87 default_index = null;
88 model = null;
89 }
90
91 public Control getControls() {
92 if(controls == null) {
93 controls = new SubcollectionIndexControl();
94 }
95 return controls;
96 }
97
98 /** Method to retrieve the default index.
99 * @return the default Index, or null if no such index assigned
100 */
101 public SubcollectionIndex getDefaultSubcollectionIndex() {
102 if(default_index != null && default_index.isAssigned()) {
103 return default_index;
104 }
105 else {
106 return null;
107 }
108 }
109
110 /** Retrieve a certain subindex given its name.
111 * @param id the String identifier of a subcollectionindex
112 * @return the SubcollectionIndex requested or null if no such subindex
113 */
114 public SubcollectionIndex getSubcollectionIndex(String id) {
115 int size = getSize();
116 for(int i = 0; i < size; i++) {
117 SubcollectionIndex subindex = (SubcollectionIndex) getElementAt(i);
118 if(subindex.getID().equals(id)) {
119 return subindex;
120 }
121 }
122 return null;
123 }
124
125 /** Method to get all of the subindexes set.
126 * @return an ArrayList containing all the defined indexes
127 */
128 public ArrayList getSubcollectionIndexes() {
129 return children();
130 }
131
132 /** Method to remove a certain subindex.
133 * @param subindex the Index you wish to remove
134 * @see org.greenstone.gatherer.Gatherer
135 * @see org.greenstone.gatherer.collection.CollectionManager
136 */
137 public void removeSubcollectionIndex(SubcollectionIndex subindex) {
138 if(subindex != null) {
139 // Remove any current metadata from this index
140 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + subindex.getID());
141 // Check if the index removed happens to be the default index
142 if(default_index != null && default_index.equals(subindex)) {
143 default_index.setAssigned(false);
144 }
145 // Remove the index
146 remove(subindex);
147 Gatherer.c_man.configurationChanged();
148 }
149 }
150
151 /** Method to remove all of the subindexes that contain a certain subcollection.
152 * @param sub the Subcollection that has been removed
153 * @see org.greenstone.gatherer.cdm.Subcollection
154 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
155 */
156 public void removeSubcollectionIndexes(Subcollection subcollection) {
157 String subcollection_name = subcollection.getName();
158 int size = getSize();
159 for(int i = size - 1; i >= 0; i--) {
160 SubcollectionIndex subindex = (SubcollectionIndex)getElementAt(i);
161 if(subindex.getSources().contains(subcollection_name)) {
162 removeSubcollectionIndex(subindex);
163 }
164 subindex = null;
165 }
166 subcollection_name = null;
167 }
168
169 /** Method to set the default subcollection index.
170 * @param subcollection The <strong>Subcollection</strong> to use as the default index.
171 * @see org.greenstone.gatherer.Gatherer
172 * @see org.greenstone.gatherer.collection.CollectionManager
173 */
174 public void setDefaultSubcollectionIndex(SubcollectionIndex index) {
175 if(index != null) {
176 if(default_index == null) {
177 // Create the default index element, and place immediately after indexes element.
178 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT);
179 default_index = new SubcollectionIndex(default_index_element);
180 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
181 if(target_node != null) {
182 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
183 }
184 else {
185 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
186 }
187 }
188 default_index.setAssigned(true);
189 default_index.setSources(index.getSources());
190 }
191 else {
192 if(default_index != null) {
193 default_index.setAssigned(false);
194 }
195 }
196 Gatherer.c_man.configurationChanged();
197 }
198
199 private class SubcollectionIndexControl
200 extends JPanel
201 implements Control {
202
203 private JButton add_index_button;
204 private JButton clear_default_button;
205 private JButton remove_index_button;
206 private JButton set_default_button;
207 private JList subcollection_list;
208 private JList subcollectionindexes_list;
209 private JTextField default_value_field;
210 private JTextField subcollectionindex_name_field;
211
212 public SubcollectionIndexControl() {
213 super();
214 // Creation
215 JPanel subcollection_panel = new JPanel();
216 JPanel subindex_name_panel = new JPanel();
217 JLabel subindex_name_label = new JLabel();
218 Dictionary.registerText(subindex_name_label, "CDM.SubcollectionIndexManager.PartitionName");
219 subcollectionindex_name_field = new JTextField();
220 Dictionary.registerTooltip(subcollectionindex_name_field, "CDM.SubcollectionIndexManager.PartitionName_Tooltip");
221
222 JPanel button_pane = new JPanel();
223
224 add_index_button = new JButton();
225 add_index_button.setMnemonic(KeyEvent.VK_A);
226 add_index_button.setEnabled(false);
227 Dictionary.registerBoth(add_index_button, "CDM.SubcollectionIndexManager.Add_Subindex", "CDM.SubcollectionIndexManager.Add_Subindex_Tooltip");
228
229 clear_default_button = new JButton();
230 clear_default_button.setMnemonic(KeyEvent.VK_C);
231 clear_default_button.setEnabled(default_index != null);
232 Dictionary.registerBoth(clear_default_button, "CDM.SubcollectionIndexManager.Clear_Default_Subindex", "CDM.SubcollectionIndexManager.Clear_Default_Subindex_Tooltip");
233
234 JLabel default_label = new JLabel();
235 default_label.setPreferredSize(Utility.LABEL_SIZE);
236 Dictionary.registerText(default_label, "CDM.SubcollectionIndexManager.Default_Subindex");
237
238 JPanel default_pane = new JPanel();
239 if(default_index == null) {
240 default_value_field = new JTextField();
241 }
242 else {
243 default_value_field = new JTextField(default_index.toString());
244 }
245 default_value_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
246 default_value_field.setEditable(false);
247
248 remove_index_button = new JButton();
249 remove_index_button.setMnemonic(KeyEvent.VK_R);
250 remove_index_button.setEnabled(false);
251 Dictionary.registerBoth(remove_index_button, "CDM.SubcollectionIndexManager.Remove_Subindex", "CDM.SubcollectionIndexManager.Remove_Subindex_Tooltip");
252
253 set_default_button = new JButton();
254 set_default_button.setMnemonic(KeyEvent.VK_S);
255 set_default_button.setEnabled(false);
256 Dictionary.registerBoth(set_default_button, "CDM.SubcollectionIndexManager.Set_Default_Subindex", "CDM.SubcollectionIndexManager.Set_Default_Subindex_Tooltip");
257
258 JLabel subcollection_label = new JLabel();
259 Dictionary.registerText(subcollection_label, "CDM.SubcollectionIndexManager.Subcollection");
260 subcollection_list = new JList(CollectionDesignManager.subcollection_manager);
261 JPanel list_pane = new JPanel();
262 JLabel subindexes_label = new JLabel();
263 Dictionary.registerText(subcollection_label, "CDM.SubcollectionIndexManager.Subindexes");
264 subcollectionindexes_list = new JList(model);
265 subcollectionindexes_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
266 JPanel subindexes_pane = new JPanel();
267
268 // Add listeners
269 ExclusiveListSelectionListener ell = new ExclusiveListSelectionListener();
270 ell.add(subcollection_list);
271 ell.add(subcollectionindexes_list);
272 add_index_button.addActionListener(new AddSubIndexListener());
273 clear_default_button.addActionListener(new ClearDefaultSubIndexListener());
274 remove_index_button.addActionListener(new RemoveSubIndexListener());
275 set_default_button.addActionListener(new SetDefaultSubIndexListener());
276
277 subcollectionindexes_list.addListSelectionListener(new SubcollectionListListener());
278
279 subcollectionindex_name_field.getDocument().addDocumentListener(new SubcollectionIndexListener());
280 subcollection_list.addListSelectionListener(new SubcollectionIndexListener());
281
282 // Layout
283 default_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
284
285 default_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2,0,0,0), BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createEmptyBorder(2,2,2,2))));
286 default_pane.setLayout(new BorderLayout());
287 default_pane.add(default_label, BorderLayout.WEST);
288 default_pane.add(default_value_field, BorderLayout.CENTER);
289
290 subindexes_pane.setLayout(new BorderLayout());
291 subindexes_pane.add(subindexes_label, BorderLayout.NORTH);
292 subindexes_pane.add(new JScrollPane(subcollectionindexes_list), BorderLayout.CENTER);
293 subindexes_pane.add(default_pane, BorderLayout.SOUTH);
294
295 subindex_name_panel.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
296 subindex_name_panel.setLayout(new BorderLayout());
297 subindex_name_panel.add(subindex_name_label, BorderLayout.WEST);
298 subindex_name_panel.add(subcollectionindex_name_field, BorderLayout.CENTER);
299
300 list_pane.setBorder(BorderFactory.createEmptyBorder(5,0,2,0));
301 list_pane.setLayout(new BorderLayout());
302 list_pane.add(subcollection_label, BorderLayout.NORTH);
303 list_pane.add(new JScrollPane(subcollection_list), BorderLayout.CENTER);
304 list_pane.add(subindex_name_panel, BorderLayout.SOUTH);
305
306 button_pane.setLayout(new GridLayout(2,2));
307 button_pane.add(add_index_button);
308 button_pane.add(remove_index_button);
309 button_pane.add(set_default_button);
310 button_pane.add(clear_default_button);
311
312 subcollection_panel.setLayout(new BorderLayout());
313 //subcollection_panel.add(subindex_name_panel, BorderLayout.NORTH);
314 subcollection_panel.add(list_pane, BorderLayout.CENTER);
315 subcollection_panel.add(button_pane, BorderLayout.SOUTH);
316
317 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
318 setLayout(new BorderLayout());
319 add(subindexes_pane, BorderLayout.CENTER);
320 add(subcollection_panel, BorderLayout.SOUTH);
321 }
322
323 public void destroy() {
324 }
325
326 public void gainFocus() {
327 }
328
329 public void loseFocus() {
330 }
331
332 /** Listens for actions apon the 'add subindex' button in the SubcollectionManager controls, and if detected calls the addSubindex method of the manager with a newly created subindex. */
333 private class AddSubIndexListener
334 implements ActionListener {
335 /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a series of subcollections selected, and if so build an new subindex based apon them.
336 * @param event An <strong>ActionEvent</strong> containing information about the event.
337 * @see org.greenstone.gatherer.cdm.SubIndex
338 */
339 public void actionPerformed(ActionEvent event) {
340 if(!subcollection_list.isSelectionEmpty() && subcollectionindex_name_field.getText().length() > 0) {
341 SubcollectionIndex subindex = new SubcollectionIndex(subcollection_list.getSelectedValues());
342 addSubcollectionIndex(subindex);
343 // Add the subindexes name.
344 CollectionMeta metadatum = new CollectionMeta("." + subindex.getID());
345 metadatum.setValue(subcollectionindex_name_field.getText());
346 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
347 }
348 }
349 }
350
351 /** Listens for actions apon the 'clear default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubcollectionIndex() method of the manager with <i>null</i>. */
352 private class ClearDefaultSubIndexListener
353 implements ActionListener {
354 /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to clear the default subindex.
355 * @param event An <strong>ActionEvent</strong> containing information about the event.
356 */
357 public void actionPerformed(ActionEvent event) {
358 setDefaultSubcollectionIndex(null);
359 default_value_field.setText("");
360 clear_default_button.setEnabled(false);
361 set_default_button.setEnabled(!subcollectionindexes_list.isSelectionEmpty());
362 }
363 }
364
365 /** Listens for actions apon the 'remove subindex' button in the SubcollectionManager controls, and if detected calls the removeSubcollectionIndex method of the manager with the SubcollectionIndex selected for removal. */
366 private class RemoveSubIndexListener
367 implements ActionListener {
368 /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so remove it.
369 * @param event An <strong>ActionEvent</strong> containing information about the event.
370 * @see org.greenstone.gatherer.cdm.SubIndex
371 */
372 public void actionPerformed(ActionEvent event) {
373 if(!subcollectionindexes_list.isSelectionEmpty()) {
374 removeSubcollectionIndex((SubcollectionIndex)subcollectionindexes_list.getSelectedValue());
375 }
376 }
377 }
378 /** Listens for actions apon the 'set default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubcollectionIndex method of the manager with the SubIndex selected for default. */
379 private class SetDefaultSubIndexListener
380 implements ActionListener {
381 /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so set it as default.
382 * @param event An <strong>ActionEvent</strong> containing information about the event.
383 * @see org.greenstone.gatherer.cdm.DefaultSubIndex
384 * @see org.greenstone.gatherer.cdm.SubIndex
385 */
386 public void actionPerformed(ActionEvent event) {
387 if(!subcollectionindexes_list.isSelectionEmpty()) {
388 setDefaultSubcollectionIndex((SubcollectionIndex)subcollectionindexes_list.getSelectedValue());
389 default_value_field.setText(default_index.toString());
390 clear_default_button.setEnabled(true);
391 set_default_button.setEnabled(false);
392 }
393 }
394 }
395
396 private class SubcollectionIndexListener
397 implements DocumentListener, ListSelectionListener {
398
399 /** Gives notification that an attribute or set of attributes changed. */
400 public void changedUpdate(DocumentEvent e) {
401 update();
402 }
403 /** Gives notification that there was an insert into the document. */
404 public void insertUpdate(DocumentEvent e) {
405 update();
406 }
407 /** Gives notification that a portion of the document has been removed. */
408 public void removeUpdate(DocumentEvent e) {
409 update();
410 }
411
412 public void valueChanged(ListSelectionEvent event) {
413 if(!event.getValueIsAdjusting()) {
414 update();
415 }
416 }
417
418 /** The text area has changed in some way. Given that this can only happed when we are editing or adding a text fragment we better respond appropriately. */
419 private void update() {
420 if(!subcollection_list.isSelectionEmpty() && subcollectionindex_name_field.getText().length() > 0) {
421 if (getSubcollectionIndex(subcollectionindex_name_field.getText()) == null) {
422 SubcollectionIndex subindex = new SubcollectionIndex(subcollection_list.getSelectedValues());
423 add_index_button.setEnabled(!model.contains(subindex));
424 } else {
425 add_index_button.setEnabled(false);
426 }
427 }
428 else {
429 add_index_button.setEnabled(false);
430 }
431 }
432 }
433
434 private class SubcollectionListListener
435 implements ListSelectionListener {
436
437 public void valueChanged(ListSelectionEvent event) {
438 if(!event.getValueIsAdjusting()) {
439 boolean enable = !subcollectionindexes_list.isSelectionEmpty();
440 remove_index_button.setEnabled(enable);
441 set_default_button.setEnabled(enable);
442 }
443 }
444 }
445 }
446}
Note: See TracBrowser for help on using the repository browser.