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

Last change on this file since 6540 was 6389, checked in by jmt12, 20 years ago

Introduced the idea of detail modes - these have an effect on several parts of the gui, such as disabling or hiding all regular expression based controls, simplifying the output from perl scripts and (having been given yet another new last minute feature to implement) displays a completely different create pane. Mode is stored in the config.xml and is changable via the Preferences controls

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