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

Last change on this file since 6846 was 6846, checked in by kjdon, 20 years ago

unfixed the size of labels so that other langs fit in

  • 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.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 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 public 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 //default_label.setPreferredSize(Utility.LABEL_SIZE);
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(Gatherer.config.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.