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

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

Replaced all "Gatherer.config" with "Configuration".

  • 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.Configuration;
35import org.greenstone.gatherer.Dictionary;
36import org.greenstone.gatherer.Gatherer;
37import org.greenstone.gatherer.cdm.CollectionConfiguration;
38import org.greenstone.gatherer.cdm.CollectionDesignManager;
39import org.greenstone.gatherer.cdm.Control;
40import org.greenstone.gatherer.cdm.DOMProxyListModel;
41import org.greenstone.gatherer.cdm.Subcollection;
42import org.greenstone.gatherer.cdm.SubcollectionIndex;
43import org.greenstone.gatherer.gui.GLIButton;
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 private 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 private 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 private 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 subcollection 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 index The <strong>SubcollectionIndex</strong> to use as the default index.
173 * @see org.greenstone.gatherer.Gatherer
174 * @see org.greenstone.gatherer.collection.CollectionManager
175 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
176 */
177 private void setDefaultSubcollectionIndex(SubcollectionIndex index) {
178 if(index != null) {
179 if(default_index == null) {
180 // Create the default index element, and place immediately after indexes element.
181 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT);
182 default_index = new SubcollectionIndex(default_index_element);
183 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
184 if(target_node != null) {
185 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
186 }
187 else {
188 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
189 }
190 }
191 default_index.setAssigned(true);
192 default_index.setSources(index.getSources());
193 }
194 else {
195 if(default_index != null) {
196 default_index.setAssigned(false);
197 }
198 }
199 Gatherer.c_man.configurationChanged();
200 }
201
202 private class SubcollectionIndexControl
203 extends JPanel
204 implements Control {
205
206 private JButton add_index_button;
207 private JButton clear_default_button;
208 private JButton remove_index_button;
209 private JButton set_default_button;
210 private JList subcollection_list;
211 private JList subcollectionindexes_list;
212 private JTextField default_value_field;
213 private JTextField subcollectionindex_name_field;
214
215 public SubcollectionIndexControl() {
216 super();
217 // Creation
218 JPanel subcollection_panel = new JPanel();
219 JPanel subindex_name_panel = new JPanel();
220 JLabel subindex_name_label = new JLabel();
221 Dictionary.registerText(subindex_name_label, "CDM.SubcollectionIndexManager.PartitionName");
222 subcollectionindex_name_field = new JTextField();
223 subcollectionindex_name_field.setPreferredSize(Utility.LABEL_SIZE);
224 Dictionary.registerTooltip(subcollectionindex_name_field, "CDM.SubcollectionIndexManager.PartitionName_Tooltip");
225
226 JPanel button_pane = new JPanel();
227
228 add_index_button = new GLIButton();
229 add_index_button.setMnemonic(KeyEvent.VK_A);
230 add_index_button.setEnabled(false);
231 Dictionary.registerBoth(add_index_button, "CDM.SubcollectionIndexManager.Add_Subindex", "CDM.SubcollectionIndexManager.Add_Subindex_Tooltip");
232
233 clear_default_button = new GLIButton();
234 clear_default_button.setMnemonic(KeyEvent.VK_C);
235 clear_default_button.setEnabled(default_index != null);
236 Dictionary.registerBoth(clear_default_button, "CDM.SubcollectionIndexManager.Clear_Default_Subindex", "CDM.SubcollectionIndexManager.Clear_Default_Subindex_Tooltip");
237
238 JLabel default_label = new JLabel();
239 Dictionary.registerText(default_label, "CDM.SubcollectionIndexManager.Default_Subindex");
240
241 JPanel default_pane = new JPanel();
242 if(default_index == null) {
243 default_value_field = new JTextField();
244 }
245 else {
246 default_value_field = new JTextField(default_index.toString());
247 }
248 default_value_field.setPreferredSize(Utility.LABEL_SIZE);
249 default_value_field.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
250 default_value_field.setEditable(false);
251
252 remove_index_button = new GLIButton();
253 remove_index_button.setMnemonic(KeyEvent.VK_R);
254 remove_index_button.setEnabled(false);
255 Dictionary.registerBoth(remove_index_button, "CDM.SubcollectionIndexManager.Remove_Subindex", "CDM.SubcollectionIndexManager.Remove_Subindex_Tooltip");
256
257 set_default_button = new GLIButton();
258 set_default_button.setMnemonic(KeyEvent.VK_S);
259 set_default_button.setEnabled(false);
260 Dictionary.registerBoth(set_default_button, "CDM.SubcollectionIndexManager.Set_Default_Subindex", "CDM.SubcollectionIndexManager.Set_Default_Subindex_Tooltip");
261
262 JLabel subcollection_label = new JLabel();
263 Dictionary.registerText(subcollection_label, "CDM.SubcollectionIndexManager.Subcollection");
264 subcollection_list = new JList(CollectionDesignManager.subcollection_manager);
265 JPanel list_pane = new JPanel();
266 JLabel subindexes_label = new JLabel();
267 Dictionary.registerText(subcollection_label, "CDM.SubcollectionIndexManager.Subindexes");
268 subcollectionindexes_list = new JList(model);
269 subcollectionindexes_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
270 JPanel subindexes_pane = new JPanel();
271
272 // Add listeners
273 ExclusiveListSelectionListener ell = new ExclusiveListSelectionListener();
274 ell.add(subcollection_list);
275 ell.add(subcollectionindexes_list);
276 add_index_button.addActionListener(new AddSubIndexListener());
277 clear_default_button.addActionListener(new ClearDefaultSubIndexListener());
278 remove_index_button.addActionListener(new RemoveSubIndexListener());
279 set_default_button.addActionListener(new SetDefaultSubIndexListener());
280
281 subcollectionindexes_list.addListSelectionListener(new SubcollectionListListener());
282
283 subcollectionindex_name_field.getDocument().addDocumentListener(new SubcollectionIndexListener());
284 subcollection_list.addListSelectionListener(new SubcollectionIndexListener());
285
286 // Layout
287 default_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
288
289 default_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2,0,0,0), BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createEmptyBorder(2,2,2,2))));
290 default_pane.setLayout(new BorderLayout(5,0));
291 default_pane.add(default_label, BorderLayout.WEST);
292 default_pane.add(default_value_field, BorderLayout.CENTER);
293
294 subindexes_pane.setLayout(new BorderLayout());
295 subindexes_pane.add(subindexes_label, BorderLayout.NORTH);
296 subindexes_pane.add(new JScrollPane(subcollectionindexes_list), BorderLayout.CENTER);
297 subindexes_pane.add(default_pane, BorderLayout.SOUTH);
298
299 subindex_name_panel.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
300 subindex_name_panel.setLayout(new BorderLayout(5,0));
301 subindex_name_panel.add(subindex_name_label, BorderLayout.WEST);
302 subindex_name_panel.add(subcollectionindex_name_field, BorderLayout.CENTER);
303
304 list_pane.setBorder(BorderFactory.createEmptyBorder(5,0,2,0));
305 list_pane.setLayout(new BorderLayout());
306 list_pane.add(subcollection_label, BorderLayout.NORTH);
307 list_pane.add(new JScrollPane(subcollection_list), BorderLayout.CENTER);
308 list_pane.add(subindex_name_panel, BorderLayout.SOUTH);
309
310 button_pane.setLayout(new GridLayout(2,2));
311 button_pane.add(add_index_button);
312 button_pane.add(remove_index_button);
313 button_pane.add(set_default_button);
314 button_pane.add(clear_default_button);
315
316 subcollection_panel.setLayout(new BorderLayout());
317 //subcollection_panel.add(subindex_name_panel, BorderLayout.NORTH);
318 subcollection_panel.add(list_pane, BorderLayout.CENTER);
319 subcollection_panel.add(button_pane, BorderLayout.SOUTH);
320
321 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
322 setLayout(new BorderLayout());
323 add(subindexes_pane, BorderLayout.CENTER);
324 add(subcollection_panel, BorderLayout.SOUTH);
325 }
326
327 public void clearDefaultIndex() {
328 clear_default_button.doClick();
329 }
330
331 public void destroy() {
332 }
333
334 public void gainFocus() {
335 }
336
337 public void loseFocus() {
338 }
339
340 /** 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. */
341 private class AddSubIndexListener
342 implements ActionListener {
343 /** 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.
344 * @param event An <strong>ActionEvent</strong> containing information about the event.
345 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
346 */
347 public void actionPerformed(ActionEvent event) {
348 if(!subcollection_list.isSelectionEmpty() && subcollectionindex_name_field.getText().length() > 0) {
349 SubcollectionIndex subindex = new SubcollectionIndex(subcollection_list.getSelectedValues());
350 addSubcollectionIndex(subindex);
351 // Add the subindexes name.
352 CollectionMeta metadatum = new CollectionMeta("." + subindex.getID());
353 metadatum.setValue(subcollectionindex_name_field.getText());
354 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
355 }
356 }
357 }
358
359 /** 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>. */
360 private class ClearDefaultSubIndexListener
361 implements ActionListener {
362 /** 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.
363 * @param event An <strong>ActionEvent</strong> containing information about the event.
364 */
365 public void actionPerformed(ActionEvent event) {
366 setDefaultSubcollectionIndex(null);
367 default_value_field.setText("");
368 clear_default_button.setEnabled(false);
369 set_default_button.setEnabled(!subcollectionindexes_list.isSelectionEmpty());
370 }
371 }
372
373 /** 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. */
374 private class RemoveSubIndexListener
375 implements ActionListener {
376 /** 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.
377 * @param event An <strong>ActionEvent</strong> containing information about the event.
378 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
379 */
380 public void actionPerformed(ActionEvent event) {
381 if(!subcollectionindexes_list.isSelectionEmpty()) {
382 removeSubcollectionIndex((SubcollectionIndex)subcollectionindexes_list.getSelectedValue());
383 }
384 }
385 }
386 /** 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. */
387 private class SetDefaultSubIndexListener
388 implements ActionListener {
389 /** 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.
390 * @param event An <strong>ActionEvent</strong> containing information about the event.
391 * @see org.greenstone.gatherer.cdm.SubcollectionIndex
392 */
393 public void actionPerformed(ActionEvent event) {
394 if(!subcollectionindexes_list.isSelectionEmpty()) {
395 setDefaultSubcollectionIndex((SubcollectionIndex)subcollectionindexes_list.getSelectedValue());
396 default_value_field.setText(default_index.toString());
397 clear_default_button.setEnabled(true);
398 set_default_button.setEnabled(false);
399 }
400 }
401 }
402
403 private class SubcollectionIndexListener
404 implements DocumentListener, ListSelectionListener {
405
406 /** Gives notification that an attribute or set of attributes changed. */
407 public void changedUpdate(DocumentEvent e) {
408 update();
409 }
410 /** Gives notification that there was an insert into the document. */
411 public void insertUpdate(DocumentEvent e) {
412 update();
413 }
414 /** Gives notification that a portion of the document has been removed. */
415 public void removeUpdate(DocumentEvent e) {
416 update();
417 }
418
419 public void valueChanged(ListSelectionEvent event) {
420 if(!event.getValueIsAdjusting()) {
421 update();
422 }
423 }
424
425 /** 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. */
426 private void update() {
427 if(!subcollection_list.isSelectionEmpty() && subcollectionindex_name_field.getText().length() > 0) {
428 if (getSubcollectionIndex(subcollectionindex_name_field.getText()) == null) {
429 SubcollectionIndex subindex = new SubcollectionIndex(subcollection_list.getSelectedValues());
430 add_index_button.setEnabled(!model.contains(subindex));
431 } else {
432 add_index_button.setEnabled(false);
433 }
434 }
435 else {
436 add_index_button.setEnabled(false);
437 }
438 }
439 }
440
441 private class SubcollectionListListener
442 implements ListSelectionListener {
443
444 public void valueChanged(ListSelectionEvent event) {
445 if(!event.getValueIsAdjusting()) {
446 boolean enable = !subcollectionindexes_list.isSelectionEmpty();
447 remove_index_button.setEnabled(enable);
448 set_default_button.setEnabled(enable);
449 }
450 }
451 }
452 }
453}
Note: See TracBrowser for help on using the repository browser.