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

Last change on this file since 8056 was 8002, checked in by mdewsnip, 20 years ago

Tightened up many public functions to private.

  • Property svn:keywords set to Author Date Id Revision
File size: 19.2 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.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 private 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 /* private 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 private 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 private 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 ((SubcollectionIndexControl)controls).clearDefaultIndex();
145 }
146 // Remove the index
147 remove(subindex);
148 Gatherer.c_man.configurationChanged();
149 }
150 }
151
152 /** Method to remove all of the subindexes that contain a certain subcollection.
153 * @param subcollection the Subcollection that has been removed
154 * @see org.greenstone.gatherer.cdm.Subcollection
155 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
156 */
157 public void removeSubcollectionIndexes(Subcollection subcollection) {
158 String subcollection_name = subcollection.getName();
159 int size = getSize();
160 for(int i = size - 1; i >= 0; i--) {
161 SubcollectionIndex subindex = (SubcollectionIndex)getElementAt(i);
162 if(subindex.getSources().contains(subcollection_name)) {
163 removeSubcollectionIndex(subindex);
164 }
165 subindex = null;
166 }
167 subcollection_name = null;
168 }
169
170 /** Method to set the default subcollection index.
171 * @param index The <strong>SubcollectionIndex</strong> to use as the default index.
172 * @see org.greenstone.gatherer.Gatherer
173 * @see org.greenstone.gatherer.collection.CollectionManager
174 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
175 */
176 private 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 subcollectionindex_name_field.setPreferredSize(Utility.LABEL_SIZE);
223 Dictionary.registerTooltip(subcollectionindex_name_field, "CDM.SubcollectionIndexManager.PartitionName_Tooltip");
224
225 JPanel button_pane = new JPanel();
226
227 add_index_button = new GLIButton();
228 add_index_button.setMnemonic(KeyEvent.VK_A);
229 add_index_button.setEnabled(false);
230 Dictionary.registerBoth(add_index_button, "CDM.SubcollectionIndexManager.Add_Subindex", "CDM.SubcollectionIndexManager.Add_Subindex_Tooltip");
231
232 clear_default_button = new GLIButton();
233 clear_default_button.setMnemonic(KeyEvent.VK_C);
234 clear_default_button.setEnabled(default_index != null);
235 Dictionary.registerBoth(clear_default_button, "CDM.SubcollectionIndexManager.Clear_Default_Subindex", "CDM.SubcollectionIndexManager.Clear_Default_Subindex_Tooltip");
236
237 JLabel default_label = new JLabel();
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.setPreferredSize(Utility.LABEL_SIZE);
248 default_value_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
249 default_value_field.setEditable(false);
250
251 remove_index_button = new GLIButton();
252 remove_index_button.setMnemonic(KeyEvent.VK_R);
253 remove_index_button.setEnabled(false);
254 Dictionary.registerBoth(remove_index_button, "CDM.SubcollectionIndexManager.Remove_Subindex", "CDM.SubcollectionIndexManager.Remove_Subindex_Tooltip");
255
256 set_default_button = new GLIButton();
257 set_default_button.setMnemonic(KeyEvent.VK_S);
258 set_default_button.setEnabled(false);
259 Dictionary.registerBoth(set_default_button, "CDM.SubcollectionIndexManager.Set_Default_Subindex", "CDM.SubcollectionIndexManager.Set_Default_Subindex_Tooltip");
260
261 JLabel subcollection_label = new JLabel();
262 Dictionary.registerText(subcollection_label, "CDM.SubcollectionIndexManager.Subcollection");
263 subcollection_list = new JList(CollectionDesignManager.subcollection_manager);
264 JPanel list_pane = new JPanel();
265 JLabel subindexes_label = new JLabel();
266 Dictionary.registerText(subcollection_label, "CDM.SubcollectionIndexManager.Subindexes");
267 subcollectionindexes_list = new JList(model);
268 subcollectionindexes_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
269 JPanel subindexes_pane = new JPanel();
270
271 // Add listeners
272 ExclusiveListSelectionListener ell = new ExclusiveListSelectionListener();
273 ell.add(subcollection_list);
274 ell.add(subcollectionindexes_list);
275 add_index_button.addActionListener(new AddSubIndexListener());
276 clear_default_button.addActionListener(new ClearDefaultSubIndexListener());
277 remove_index_button.addActionListener(new RemoveSubIndexListener());
278 set_default_button.addActionListener(new SetDefaultSubIndexListener());
279
280 subcollectionindexes_list.addListSelectionListener(new SubcollectionListListener());
281
282 subcollectionindex_name_field.getDocument().addDocumentListener(new SubcollectionIndexListener());
283 subcollection_list.addListSelectionListener(new SubcollectionIndexListener());
284
285 // Layout
286 default_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
287
288 default_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2,0,0,0), BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createEmptyBorder(2,2,2,2))));
289 default_pane.setLayout(new BorderLayout(5,0));
290 default_pane.add(default_label, BorderLayout.WEST);
291 default_pane.add(default_value_field, BorderLayout.CENTER);
292
293 subindexes_pane.setLayout(new BorderLayout());
294 subindexes_pane.add(subindexes_label, BorderLayout.NORTH);
295 subindexes_pane.add(new JScrollPane(subcollectionindexes_list), BorderLayout.CENTER);
296 subindexes_pane.add(default_pane, BorderLayout.SOUTH);
297
298 subindex_name_panel.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
299 subindex_name_panel.setLayout(new BorderLayout(5,0));
300 subindex_name_panel.add(subindex_name_label, BorderLayout.WEST);
301 subindex_name_panel.add(subcollectionindex_name_field, BorderLayout.CENTER);
302
303 list_pane.setBorder(BorderFactory.createEmptyBorder(5,0,2,0));
304 list_pane.setLayout(new BorderLayout());
305 list_pane.add(subcollection_label, BorderLayout.NORTH);
306 list_pane.add(new JScrollPane(subcollection_list), BorderLayout.CENTER);
307 list_pane.add(subindex_name_panel, BorderLayout.SOUTH);
308
309 button_pane.setLayout(new GridLayout(2,2));
310 button_pane.add(add_index_button);
311 button_pane.add(remove_index_button);
312 button_pane.add(set_default_button);
313 button_pane.add(clear_default_button);
314
315 subcollection_panel.setLayout(new BorderLayout());
316 //subcollection_panel.add(subindex_name_panel, BorderLayout.NORTH);
317 subcollection_panel.add(list_pane, BorderLayout.CENTER);
318 subcollection_panel.add(button_pane, BorderLayout.SOUTH);
319
320 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
321 setLayout(new BorderLayout());
322 add(subindexes_pane, BorderLayout.CENTER);
323 add(subcollection_panel, BorderLayout.SOUTH);
324 }
325
326 public void clearDefaultIndex() {
327 clear_default_button.doClick();
328 }
329
330 public void destroy() {
331 }
332
333 public void gainFocus() {
334 }
335
336 public void loseFocus() {
337 }
338
339 /** 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. */
340 private class AddSubIndexListener
341 implements ActionListener {
342 /** 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.
343 * @param event An <strong>ActionEvent</strong> containing information about the event.
344 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
345 */
346 public void actionPerformed(ActionEvent event) {
347 if(!subcollection_list.isSelectionEmpty() && subcollectionindex_name_field.getText().length() > 0) {
348 SubcollectionIndex subindex = new SubcollectionIndex(subcollection_list.getSelectedValues());
349 addSubcollectionIndex(subindex);
350 // Add the subindexes name.
351 CollectionMeta metadatum = new CollectionMeta("." + subindex.getID());
352 metadatum.setValue(subcollectionindex_name_field.getText());
353 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
354 }
355 }
356 }
357
358 /** 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>. */
359 private class ClearDefaultSubIndexListener
360 implements ActionListener {
361 /** 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.
362 * @param event An <strong>ActionEvent</strong> containing information about the event.
363 */
364 public void actionPerformed(ActionEvent event) {
365 setDefaultSubcollectionIndex(null);
366 default_value_field.setText("");
367 clear_default_button.setEnabled(false);
368 set_default_button.setEnabled(!subcollectionindexes_list.isSelectionEmpty());
369 }
370 }
371
372 /** 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. */
373 private class RemoveSubIndexListener
374 implements ActionListener {
375 /** 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.
376 * @param event An <strong>ActionEvent</strong> containing information about the event.
377 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
378 */
379 public void actionPerformed(ActionEvent event) {
380 if(!subcollectionindexes_list.isSelectionEmpty()) {
381 removeSubcollectionIndex((SubcollectionIndex)subcollectionindexes_list.getSelectedValue());
382 }
383 }
384 }
385 /** 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. */
386 private class SetDefaultSubIndexListener
387 implements ActionListener {
388 /** 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.
389 * @param event An <strong>ActionEvent</strong> containing information about the event.
390 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
391 */
392 public void actionPerformed(ActionEvent event) {
393 if(!subcollectionindexes_list.isSelectionEmpty()) {
394 setDefaultSubcollectionIndex((SubcollectionIndex)subcollectionindexes_list.getSelectedValue());
395 default_value_field.setText(default_index.toString());
396 clear_default_button.setEnabled(true);
397 set_default_button.setEnabled(false);
398 }
399 }
400 }
401
402 private class SubcollectionIndexListener
403 implements DocumentListener, ListSelectionListener {
404
405 /** Gives notification that an attribute or set of attributes changed. */
406 public void changedUpdate(DocumentEvent e) {
407 update();
408 }
409 /** Gives notification that there was an insert into the document. */
410 public void insertUpdate(DocumentEvent e) {
411 update();
412 }
413 /** Gives notification that a portion of the document has been removed. */
414 public void removeUpdate(DocumentEvent e) {
415 update();
416 }
417
418 public void valueChanged(ListSelectionEvent event) {
419 if(!event.getValueIsAdjusting()) {
420 update();
421 }
422 }
423
424 /** 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. */
425 private void update() {
426 if(!subcollection_list.isSelectionEmpty() && subcollectionindex_name_field.getText().length() > 0) {
427 if (getSubcollectionIndex(subcollectionindex_name_field.getText()) == null) {
428 SubcollectionIndex subindex = new SubcollectionIndex(subcollection_list.getSelectedValues());
429 add_index_button.setEnabled(!model.contains(subindex));
430 } else {
431 add_index_button.setEnabled(false);
432 }
433 }
434 else {
435 add_index_button.setEnabled(false);
436 }
437 }
438 }
439
440 private class SubcollectionListListener
441 implements ListSelectionListener {
442
443 public void valueChanged(ListSelectionEvent event) {
444 if(!event.getValueIsAdjusting()) {
445 boolean enable = !subcollectionindexes_list.isSelectionEmpty();
446 remove_index_button.setEnabled(enable);
447 set_default_button.setEnabled(enable);
448 }
449 }
450 }
451 }
452}
Note: See TracBrowser for help on using the repository browser.