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

Last change on this file since 4932 was 4932, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

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