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

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

Replaced all Gatherer.print* with DebugStream.print*.

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