source: trunk/gli/src/org/greenstone/gatherer/cdm/LanguageManager.java@ 12300

Last change on this file since 12300 was 12300, checked in by kjdon, 18 years ago

removed some unused variables

  • Property svn:keywords set to Author Date Id Revision
File size: 24.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
29
30import java.awt.*;
31import java.awt.event.*;
32import java.io.*;
33import java.util.*;
34import javax.swing.*;
35import javax.swing.border.*;
36import javax.swing.event.*;
37import org.greenstone.gatherer.Configuration;
38import org.greenstone.gatherer.DebugStream;
39import org.greenstone.gatherer.Dictionary;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.gui.GLIButton;
42import org.greenstone.gatherer.util.CheckList;
43import org.greenstone.gatherer.util.CheckListEntry;
44import org.greenstone.gatherer.util.JarTools;
45import org.greenstone.gatherer.util.XMLTools;
46import org.w3c.dom.*;
47
48/** This class manages the language commands, remembering both a list of languages to build indexes in, plus the default language.
49 * @author John Thompson, Greenstone Digital Library, University of Waikato
50 * @version 2.3
51 */
52public class LanguageManager
53 extends DOMProxyListModel {
54
55 static public Document LANGUAGES_DOCUMENT = XMLTools.parseXMLFile("xml/languages.xml", true);
56
57 static final private Dimension COMPONENT_SIZE = new Dimension(125,25);
58
59 /** The visual controls for this manager. */
60 private Control controls = null;
61 /** A reference to this class as a model, for the inner controls class. */
62 private DOMProxyListModel model = null;
63 /** A hashtable of code->name mappings of known languages. */
64 private LinkedHashMap known_languages = null;
65 /** The default language object. */
66 private Language default_language = null;
67
68 /** Constructor. */
69 public LanguageManager(Element languages_element) {
70 super(languages_element, CollectionConfiguration.LANGUAGE_ELEMENT, new Language());
71
72 DebugStream.println("LanguageManager: " + getSize() + " languages parsed.");
73
74 this.model = this;
75 // Retrieve the default language
76 NodeList default_language_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.LANGUAGE_DEFAULT_ELEMENT);
77 if(default_language_elements.getLength() > 0) {
78 default_language = new Language((Element)default_language_elements.item(0));
79 }
80 // Load a series of code->language mappings into known_languages, by reading from the 'languages.xml' file, which is essentially a subset of the ISO 639 Standard.
81 known_languages = new LinkedHashMap();
82 NodeList language_elements = LANGUAGES_DOCUMENT.getDocumentElement().getElementsByTagName(CollectionConfiguration.LANGUAGE_ELEMENT);
83 for(int i = 0; i < language_elements.getLength(); i++) {
84 Element language_element = (Element) language_elements.item(i);
85 String code = language_element.getAttribute(CollectionConfiguration.CODE_ATTRIBUTE);
86 String name = language_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
87 known_languages.put(code.toLowerCase(), name);
88 name = null;
89 code = null;
90 language_element = null;
91 }
92 }
93
94 /** Method to add a new language.
95 * @param language The <strong>Language</strong> to add.
96 * @see org.greenstone.gatherer.Gatherer
97 * @see org.greenstone.gatherer.collection.CollectionManager
98 */
99 private void addLanguage(Language language) {
100 if(!contains(language)) {
101 // need to add a pseudo metadata
102 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + language.getCode());
103 metadatum.setValue(language.getName());
104 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
105 add(getSize(), language);
106 Gatherer.c_man.configurationChanged();
107 }
108 }
109
110 public void destroy() {
111 if(controls != null) {
112 controls.destroy();
113 controls = null;
114 }
115 known_languages.clear();
116 known_languages = null;
117 default_language = null;
118 }
119
120 /** Method to retrieve the control for this manager.
121 * @return the Control for editing the language partitions
122 */
123 public Control getControls() {
124 if(controls == null) {
125 // Build controls
126 controls = new LanguageControl();
127 }
128 return controls;
129 }
130
131 /** Method to retrieve a certain language object by its code.
132 * @param code The two letter code of a language, as a <strong>String</strong>.
133 * @return The <strong>Language</strong> that matches the given code, or <i>null</i> if no such language exists.
134 */
135 public Language getLanguage(String code) {
136 int size = getSize();
137 for(int i = 0; i < size; i++) {
138 Language language = (Language) getElementAt(i);
139 if(language.getCode().equals(code)) {
140 return language;
141 }
142 }
143 return null;
144 }
145
146 public ArrayList getLanguages() {
147 return children();
148 }
149
150 /** Method to return a list of the known language codes.
151 * @return an ArrayList containing the series of known language codes as per the languages.dat file
152 */
153 public ArrayList getLanguageCodes() {
154 return new ArrayList(known_languages.keySet());
155 }
156
157 public String getLanguageName(String code) {
158 return (String) known_languages.get(code);
159 }
160
161 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
162 * @param mode the new mode as an int
163 */
164 public void modeChanged(int mode) {
165
166 }
167
168 private int moveLanguage(Language lang, boolean move_up)
169 {
170 // Determine the current position of the language
171 int position = indexOf(lang);
172 int new_position;
173
174 // Attempt to move the language up
175 if (move_up) {
176 // Check it's not already at the top
177 if (position == 0) {
178 return position;
179 }
180
181 // This automatically removes the language first, as an Element can only exist once in a particular document
182 new_position = position - 1;
183 addBefore(lang, (Language) getElementAt(new_position));
184 }
185
186 // Attempt to move the language down
187 else {
188 // Check it's not already at the bottom
189 if (position == (getSize()) - 1) {
190 return position;
191 }
192
193 // This automatically removes the language first, as an Element can only exist once in a particular document
194 new_position = position + 1;
195 addAfter(lang, (Language) getElementAt(new_position));
196 }
197
198 // Schedule the collection for saving
199 Gatherer.c_man.configurationChanged();
200 return new_position;
201 }
202
203 /** Method to remove a certain language.
204 * @param language The <strong>Language</strong> to remove.
205 * @see org.greenstone.gatherer.Gatherer
206 * @see org.greenstone.gatherer.collection.CollectionManager
207 */
208 private void removeLanguage(Language language) {
209 remove(language);
210 // Remove any collection metadata for this language
211 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + language.getCode());
212 if(default_language != null && default_language.equals(language)) {
213 setDefault(null);
214 }
215 Gatherer.c_man.configurationChanged();
216 }
217
218 private void replaceLanguage(Language old_language, Language new_language) {
219 // Remove old lang collection meta
220 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + old_language.getCode());
221 // Add new one
222 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + new_language.getCode());
223 metadatum.setValue(new_language.getName());
224 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
225 if(default_language != null && default_language.equals(old_language)) {
226 setDefault(new_language);
227 }
228
229 // get the position of the old one
230 int position = indexOf(old_language);
231 remove(old_language);
232 add(position, new_language);
233
234 // Schedule the collection for saving
235 Gatherer.c_man.configurationChanged();
236
237 }
238 /** Method to set the default language.
239 * @param language The <strong>Language</strong> to use as a default, or <i>null</i> for no default.
240 * @see org.greenstone.gatherer.Gatherer
241 * @see org.greenstone.gatherer.collection.CollectionManager
242 */
243 public void setDefault(Language language) {
244 if(language != null) {
245 if(default_language == null) {
246 // Create the default index element, and place immediately after indexes element.
247 Element default_language_element = root.getOwnerDocument().createElement(CollectionConfiguration.LANGUAGE_DEFAULT_ELEMENT);
248 default_language = new Language(default_language_element);
249 Node target_node = CollectionConfiguration.findInsertionPoint(default_language_element);
250 if(target_node != null) {
251 root.getOwnerDocument().getDocumentElement().insertBefore(default_language_element, target_node);
252 }
253 else {
254 root.getOwnerDocument().getDocumentElement().appendChild(default_language_element);
255 }
256 }
257 default_language.setAssigned(true);
258 default_language.setCode(language.getCode());
259 }
260 else {
261 if(default_language != null) {
262 default_language.setAssigned(false);
263 }
264 }
265 Gatherer.c_man.configurationChanged();
266 }
267
268
269 /** This class represents the visual component of the Language Manager. */
270 private class LanguageControl
271 extends JPanel
272 implements Control {
273 /** The list of available languages */
274 private CheckList language_list = null;
275 /** The button to add a new language support. */
276 private JButton add_button = null;
277 /** The button to replace a language support. */
278 private JButton replace_button = null;
279 /** The button to remove a supported language. */
280 private JButton remove_button = null;
281 /** button to move a language up in the list */
282 private JButton move_down_button;
283 /** button to move a language down in the list */
284 private JButton move_up_button;
285 /** The button to set the current language as the default one. */
286 private JButton set_default_button = null;
287 /** A combobox listing the available supported languages. */
288 //private JComboBox language_combobox = null;
289 /** A list of currently supported languages. */
290 private JList selected_languages_list = null;
291 /** Constructor.
292 * @see org.greenstone.gatherer.cdm.LanguageManager.LanguageControl.AddListener
293 * @see org.greenstone.gatherer.cdm.LanguageManager.LanguageControl.ClearDefaultListener
294 * @see org.greenstone.gatherer.cdm.LanguageManager.LanguageControl.ListListener
295 * @see org.greenstone.gatherer.cdm.LanguageManager.LanguageControl.RemoveListener
296 * @see org.greenstone.gatherer.cdm.LanguageManager.LanguageControl.SelectorListener
297 * @see org.greenstone.gatherer.cdm.LanguageManager.LanguageControl.SetDefaultListener
298 */
299 public LanguageControl() {
300 super();
301 // Creation.
302 JPanel center_panel = new JPanel();
303
304 JLabel selected_languages_list_label = new JLabel(Dictionary.get("CDM.LanguageManager.Assigned_Languages"));
305 selected_languages_list = new JList(model);
306 selected_languages_list.setCellRenderer(new MyLanguageListCellRenderer());
307 selected_languages_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
308 selected_languages_list.setVisibleRowCount(5);
309
310 JPanel control_panel = new JPanel();
311 JLabel selector_label = new JLabel(Dictionary.get("CDM.LanguageManager.Selector"));
312
313 language_list = new CheckList(false);
314 language_list.setListData(getLanguageCodes());
315 language_list.setToolTipText(Dictionary.get("CDM.LanguageManager.Selector_Tooltip"));
316 language_list.setCellRenderer(new LanguageCheckListCellRenderer());
317
318 JPanel movement_pane = new JPanel();
319 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
320 move_up_button.setEnabled(false);
321
322 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
323 move_down_button.setEnabled(false);
324
325 set_default_button = new GLIButton(Dictionary.get("CDM.LanguageManager.Set_Default"), Dictionary.get("CDM.LanguageManager.Set_Default_Tooltip"));
326 set_default_button.setEnabled(false);
327
328 JPanel button_panel = new JPanel();
329
330 add_button = new GLIButton(Dictionary.get("CDM.SubcollectionIndexManager.Add_Subindex"), Dictionary.get("CDM.LanguageManager.Add_Tooltip"));
331 add_button.setEnabled(false);
332
333 replace_button = new GLIButton(Dictionary.get("CDM.SubcollectionIndexManager.Replace_Subindex"), Dictionary.get("CDM.LanguageManager.Replace_Tooltip"));
334 replace_button.setEnabled(false);
335
336 remove_button = new GLIButton(Dictionary.get("CDM.SubcollectionIndexManager.Remove_Subindex"), Dictionary.get("CDM.LanguageManager.Remove_Tooltip"));
337 remove_button.setEnabled(false);
338
339 // Set up and connect listeners.
340 add_button.addActionListener(new AddListener());
341 add_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
342 move_down_button.addActionListener(new MoveListener(false));
343 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
344 move_up_button.addActionListener(new MoveListener(true));
345 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
346 remove_button.addActionListener(new RemoveListener());
347 remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
348 replace_button.addActionListener(new ReplaceListener());
349 replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
350
351 language_list.addListSelectionListener(new LanguageListListener());
352
353 set_default_button.addActionListener(new SetDefaultListener());
354 set_default_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
355 selected_languages_list.addListSelectionListener(new AssignedListListener());
356
357 // Layout components
358 button_panel.setLayout(new GridLayout(1,3));
359 button_panel.add(add_button);
360 button_panel.add(replace_button);
361 button_panel.add(remove_button);
362
363 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
364 movement_pane.setLayout(new GridLayout(3,1));
365 movement_pane.add(move_up_button);
366 movement_pane.add(move_down_button);
367 movement_pane.add(set_default_button);
368
369 control_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
370 control_panel.setLayout(new BorderLayout());
371 control_panel.add(selector_label, BorderLayout.WEST);
372 control_panel.add(new JScrollPane(language_list), BorderLayout.CENTER);
373 control_panel.add(button_panel, BorderLayout.SOUTH);
374
375 center_panel.setLayout(new BorderLayout());
376 center_panel.add(selected_languages_list_label, BorderLayout.NORTH);
377 center_panel.add(new JScrollPane(selected_languages_list), BorderLayout.CENTER);
378 center_panel.add(movement_pane, BorderLayout.EAST);
379
380 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
381 setLayout(new BorderLayout());
382 add(center_panel, BorderLayout.NORTH);
383 add(control_panel, BorderLayout.CENTER);
384 }
385
386 /** Destructor. */
387 public void destroy() {
388 }
389
390 public void gainFocus() {
391 }
392
393 public void loseFocus() {
394 }
395
396
397 private void clearControls() {
398 selected_languages_list.clearSelection();
399 language_list.clearTicked();
400 add_button.setEnabled(false);
401 remove_button.setEnabled(false);
402 replace_button.setEnabled(false);
403 set_default_button.setEnabled(false);
404 move_down_button.setEnabled(false);
405 move_up_button.setEnabled(false);
406
407 }
408
409 private void updateControlsWithSelectedLanguage()
410 {
411 Language selected_lang = (Language) selected_languages_list.getSelectedValue();
412 if (selected_lang == null) {
413 clearControls();
414 return;
415 }
416
417 // Display the selected subcollection index's sources
418 language_list.clearTicked();
419 language_list.setTickedObjects(selected_lang.getCode().split(","));
420
421 }
422
423 private void validateButtons() {
424 boolean add_enabled = false;
425 boolean replace_enabled = false;
426
427 if (!language_list.isNothingTicked()) {
428 // Create a dummy Langauge and see if its in the collection
429 ArrayList langs = language_list.getTicked();
430 StringBuffer code_str = new StringBuffer();
431 boolean first = true;
432 for (int i=0; i<langs.size(); i++) {
433 if (!first) {
434 code_str.append(",");
435 } else {
436 first = false;
437 }
438 code_str.append(langs.get(i));
439 }
440 String lang_str = code_str.toString();
441
442 if (!model.contains(lang_str)) {
443 add_enabled = true;
444 if (!selected_languages_list.isSelectionEmpty()) {
445 replace_enabled = true;
446 }
447 }
448
449 }
450 add_button.setEnabled(add_enabled);
451 replace_button.setEnabled(replace_enabled);
452 }
453
454 /** Listens for actions apon the 'add' button in the LanguageManager controls, and if detected calls the add method of the manager with a newly created language. */
455 private class AddListener
456 implements ActionListener {
457 /** Add a new language support.
458 * @param event an ActionEvent
459 * @see org.greenstone.gatherer.cdm.Language
460 */
461 public void actionPerformed(ActionEvent event) {
462 if (!language_list.isNothingTicked()) {
463 addLanguage(new Language(language_list.getTicked()));
464 clearControls();
465 }
466 }
467 }
468
469 /** Listens for actions apon the 'remove' button in the LanguageManager controls, and if detected calls the remove method of the manager with the language selected for removal. */
470 private class RemoveListener
471 implements ActionListener {
472 /** Remove the currently selected language, if any.
473 * @param event An <strong>ActionEvent</strong>.
474 * @see org.greenstone.gatherer.cdm.Language
475 */
476 public void actionPerformed(ActionEvent event) {
477 Language delete_me = (Language)selected_languages_list.getSelectedValue();
478 if(delete_me != null) {
479 removeLanguage(delete_me);
480 }
481 }
482 }
483
484 private class ReplaceListener
485 implements ActionListener {
486
487 public void actionPerformed(ActionEvent event) {
488 if (selected_languages_list.isSelectionEmpty() || language_list.isNothingTicked()) {
489 // This should never happen, but just in case...
490 replace_button.setEnabled(false);
491 return;
492 }
493 Language old_language = (Language) selected_languages_list.getSelectedValue();
494 Language new_language = new Language(language_list.getTicked());
495 replaceLanguage(old_language, new_language);
496
497 }
498 }
499
500 private class LanguageListListener
501 implements ListSelectionListener {
502
503 public void valueChanged(ListSelectionEvent event) {
504 if (event.getValueIsAdjusting()) {
505 return;
506 }
507 validateButtons();
508 }
509 }
510
511 /** Listens for actions apon the 'set default' button in the LanguageManager controls, and if detected calls the <i>setDefault()</i> method of the manager with the language selected for default. */
512 private class SetDefaultListener
513 implements ActionListener {
514 /** Set the default index to the one currently selected, if any.
515 * @param event An <strong>ActionEvent</strong>.
516 * @see org.greenstone.gatherer.cdm.Language
517 */
518 public void actionPerformed(ActionEvent event) {
519 Language selected_language = (Language) selected_languages_list.getSelectedValue();
520 if(selected_language != null) {
521 setDefault(selected_language);
522 // This should cause a repaint of just the desired row
523 selected_languages_list.setSelectedValue(selected_language, true);
524 }
525 set_default_button.setEnabled(false);
526 }
527 }
528
529 private class MoveListener
530 implements ActionListener
531 {
532 private boolean move_up;
533
534 public MoveListener(boolean move_up)
535 {
536 this.move_up = move_up;
537 }
538
539 public void actionPerformed(ActionEvent event)
540 {
541 // Retrieve the selected language
542 Language language = (Language) selected_languages_list.getSelectedValue();
543 if (language != null) {
544 int new_position = moveLanguage(language, move_up);
545 // Ensure the language that moved is still selected
546 selected_languages_list.setSelectedIndex(new_position);
547 }
548 }
549 }
550
551
552 /** Listens for selections within the list on the LanguageManager controls, and if a change is detected enables, or disables, controls appropriately. */
553 private class AssignedListListener
554 implements ListSelectionListener {
555 /** Enable or disable controls depending on the current list selection.
556 * @param event A <strong>ListSelectionEvent</strong>.
557 */
558 public void valueChanged(ListSelectionEvent event) {
559 if (event.getValueIsAdjusting()) {
560 return;
561 }
562 if(selected_languages_list.isSelectionEmpty()) {
563 clearControls();
564 return;
565 }
566
567 int i = selected_languages_list.getSelectedIndex();
568 int size = selected_languages_list.getModel().getSize();
569 Language selected_lang = (Language)selected_languages_list.getSelectedValue();
570 remove_button.setEnabled(true);
571 replace_button.setEnabled(false);
572 add_button.setEnabled(false);
573 set_default_button.setEnabled(default_language == null || !default_language.equals(selected_lang));
574
575 if (i > 0) {
576 move_up_button.setEnabled(true);
577 }
578 else {
579 move_up_button.setEnabled(false);
580 }
581 if (i < size-1){
582 move_down_button.setEnabled(true);
583 }
584 else {
585 move_down_button.setEnabled(false);
586 }
587 updateControlsWithSelectedLanguage();
588 }
589 }
590
591 private class MyLanguageListCellRenderer
592 extends DefaultListCellRenderer
593 {
594 /** Return a component that has been configured to display the specified value. */
595 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
596 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
597 if (default_language != null && default_language.equals(value)) {
598 component.setText(component.getText() + " [" + Dictionary.get("CDM.LanguageManager.Default_Language")+"]");
599 }
600 return component;
601 }
602 }
603 }
604
605 /** A custom list cell renderer for producing rows which contain clickable check boxes. */
606 private class LanguageCheckListCellRenderer
607 implements ListCellRenderer
608 {
609 /** Return a component that has been configured to display the specified value. That component's paint method is then called to "render" the cell. If it is necessary to compute the dimensions of a list because the list cells do not have a fixed size, this method is called to generate a component on which getPreferredSize can be invoked.
610 * @param list The </strong>JList</strong> we're painting.
611 * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
612 * @param index The cells index as an <i>int</i>.
613 * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
614 * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
615 * @return A <strong>Component</strong> whose paint() method will render the specified value.
616 */
617 public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
618 JCheckBox checkbox = (JCheckBox) value;
619 checkbox.setBackground(list.getBackground());
620 checkbox.setForeground(list.getForeground());
621 checkbox.setBorderPainted(false);
622 checkbox.setEnabled(list.isEnabled());
623 checkbox.setFont(list.getFont());
624 checkbox.setFocusPainted(false);
625 checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : new EmptyBorder(1, 1, 1, 1));
626
627 String code = (String)((CheckListEntry)list.getModel().getElementAt(index)).getObject();
628 checkbox.setText((String)known_languages.get(code));
629
630 return checkbox;
631 }
632 }
633
634}
Note: See TracBrowser for help on using the repository browser.