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

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

Removed all occurrences of classes explicitly importing other classes in the same package.

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