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

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

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 27.7 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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43package org.greenstone.gatherer.cdm;
44/**************************************************************************************
45 * Title: Gatherer
46 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
47 * Copyright: Copyright (c) 2001
48 * Company: The University of Waikato
49 * Written: 08/05/02
50 * Revised: 17/11/02 - Commented
51 **************************************************************************************/
52import java.awt.BorderLayout;
53import java.awt.Color;
54import java.awt.Component;
55import java.awt.Dimension;
56import java.awt.GridLayout;
57import java.awt.event.ActionEvent;
58import java.awt.event.ActionListener;
59import java.awt.event.KeyEvent;
60import java.io.BufferedReader;
61import java.io.File;
62import java.io.FileReader;
63import java.util.ArrayList;
64import java.util.Collections;
65import java.util.Enumeration;
66import java.util.Iterator;
67import java.util.LinkedHashMap;
68import java.util.StringTokenizer;
69import java.util.Vector;
70import javax.swing.BorderFactory;
71import javax.swing.DefaultListCellRenderer;
72import javax.swing.DefaultListModel;
73import javax.swing.ListModel;
74import javax.swing.JButton;
75import javax.swing.JComboBox;
76import javax.swing.JLabel;
77import javax.swing.JList;
78import javax.swing.JPanel;
79import javax.swing.JScrollPane;
80import javax.swing.JTextArea;
81import javax.swing.JTextField;
82import javax.swing.event.ListSelectionEvent;
83import javax.swing.event.ListSelectionListener;
84import org.greenstone.gatherer.Gatherer;
85import org.greenstone.gatherer.cdm.Language;
86/** This class manages the language commands, remembering both a list of languages to build indexes in, plus the default language.
87* @author John Thompson, Greenstone Digital Library, University of Waikato
88* @version 2.3
89*/
90public class LanguageManager
91 extends DefaultListModel {
92 /** A reference to the collection design manager. */
93 private CollectionDesignManager manager = null;
94 /** The visual controls for this manager. */
95 private Control controls = null;
96 /** A reference to the Gatherer. */
97 private Gatherer gatherer = null;
98 /** A reference to this class as a model, for the inner controls class. */
99 private ListModel model = null;
100 /** A hashtable of code->name mappings of known languages. */
101 private LinkedHashMap known_languages = null;
102 /** The default language object. */
103 private Language default_language = null;
104 /** Constructor.
105 * @param gatherer A reference to the <strong>Gatherer</strong>.
106 * @param manager A reference to the <strong>CollectionDesignManager</strong>.
107 */
108 public LanguageManager(Gatherer gatherer, CollectionDesignManager manager) {
109 super();
110 this.gatherer = gatherer;
111 this.known_languages = new LinkedHashMap();
112 this.manager = manager;
113 this.model = this;
114 loadLanguages();
115 }
116 /** Method to add a new language.
117 * @param language The <strong>Language</strong> to add.
118 * @see org.greenstone.gatherer.Gatherer
119 * @see org.greenstone.gatherer.collection.CollectionManager
120 */
121 public void addLanguage(Language language) {
122 if(!contains(language)) {
123 // Add alphabetically.
124 for(int index = 0; index < size(); index++) {
125 Language sibling = (Language) get(index);
126 int position = language.compareTo(sibling);
127 // Sibling is before language.
128 if(position > 0) {
129 // Carry on.
130 }
131 // Language is equal to, or before sibling. Insert it.
132 else if(position == 0 || position < 0) {
133 add(index, language);
134 gatherer.c_man.configurationChanged();
135 return;
136 }
137 }
138 // If we got this far, we haven't inserted language, and we are out of model so.
139 addElement(language);
140 gatherer.c_man.configurationChanged();
141 }
142 }
143 /** Method to retrieve the control for this manager.
144 * @return A <strong>JPanel</strong> containing the controls.
145 */
146 public JPanel getControls() {
147 if(controls == null) {
148 controls = new Control();
149 }
150 return controls;
151 }
152 /** Method to retrieve the default language code.
153 * @return A <strong>Language</strong> containing a two letter code.
154 */
155 public Language getDefaultLanguage() {
156 // If no default is set...
157 if(default_language == null) {
158 // And we have other assigned languages, use the first one of them.
159 if(size() >= 1) {
160 default_language = (Language) get(0);
161 }
162 // And we have nothing else, use English.
163 else {
164 default_language = getLanguage("en", false);
165 // Remember to add it.
166 addLanguage(default_language);
167 }
168 }
169 return default_language;
170 }
171 /** Method to retrieve a certain language object by its code.
172 * @param code The two letter code of a language, as a <strong>String</strong>.
173 * @param assigned_only If <i>true</i> only those languages currently having indexes built for them are checked, otherwise the known languages buffer is checked.
174 * @return The <strong>Language</strong> that matches the given code, or <i>null</i> if no such language exists.
175 */
176 public Language getLanguage(String code, boolean assigned_only) {
177 if(assigned_only) {
178 for(int i = 0; i < size(); i++) {
179 Language pos = (Language)get(i);
180 ///ystem.err.println("Comparing " + pos.getCode() + " and " + code);
181 if(pos.getCode().equals(code)) {
182 return pos;
183 }
184 }
185 }
186 else {
187 if(known_languages.containsKey(code)) {
188 return new Language((Language)known_languages.get(code));
189 }
190 }
191 return null;
192 }
193 /** Method to return a list of the known language codes.
194 * @return An <strong>ArrayList</strong> containing a series of alphabetically sorted two letter codes.
195 */
196 public ArrayList getLanguageCodes() {
197 ArrayList result = new ArrayList();
198 Iterator key_iter = known_languages.keySet().iterator();
199 while(key_iter.hasNext()) {
200 result.add(known_languages.get(key_iter.next()));
201 }
202 //for(Enumeration keys = known_languages.keys(); keys.hasMoreElements(); ) {
203 // result.add(known_languages.get(keys.nextElement()));
204 //}
205 //Collections.sort(result);
206 return result;
207 }
208 /** Mark the current set of controls, if any, as obsolete and deallocate them. If further need of the controls will cause new controls to be created.
209 * @see org.greenstone.gatherer.cdm.IndexManager.Control
210 */
211 public void invalidateControls() {
212 if(controls != null) {
213 controls.destroy();
214 }
215 controls = null;
216 }
217
218 /** Determine if the given language is the current default language.
219 * @param language The <strong>Language</strong> to test.
220 * @return <i>true</i> if the language is the default one, <i>false</i> otherwise.
221 */
222 public boolean isDefaultLanguage(Language language) {
223 return (language.equals(default_language));
224 }
225 /** This method attempts to parse a language command from the given string. If such a command is parsed, it is immediately added to the list of languages.
226 * @param command The <strong>String</strong> containing a possible language command.
227 * @return A <i>boolean</i> which is <i>true</i> if a command was parsed, <i>false</i> otherwise.
228 * @see org.greenstone.gatherer.cdm.Language
229 */
230 public boolean parse(String command) {
231 String command_lc = command.toLowerCase();
232 if(command_lc.startsWith("languages")) {
233 StringTokenizer tokenizer = new StringTokenizer(command);
234 tokenizer.nextToken();
235 while(tokenizer.hasMoreTokens()) {
236 String code = tokenizer.nextToken();
237 if(known_languages.containsKey(code)) {
238 Language language = (Language)known_languages.get(code);
239 addElement(new Language(language));
240 }
241 else {
242 addElement(new Language(code, code, false));
243 }
244 }
245 return true;
246 }
247 if(command_lc.startsWith("defaultlanguage")) {
248 StringTokenizer tokenizer = new StringTokenizer(command);
249 tokenizer.nextToken();
250 if(tokenizer.hasMoreTokens()) {
251 String code = tokenizer.nextToken();
252 Language language = getLanguage(code, true);
253 if(language == null) {
254 language = new Language(code, (String)known_languages.get(code), true);
255 addElement(language);
256 }
257 else {
258 language.setDefault(true);
259 }
260 setDefault(language);
261 }
262 return true;
263 }
264 return false;
265 }
266 /** Method to cause the list appearance to update if the selection changes. */
267 public void refreshAppearance() {
268 fireContentsChanged(this, 0, size());
269 }
270 /** Method to remove a certain language.
271 * @param language The <strong>Language</strong> to remove.
272 * @see org.greenstone.gatherer.Gatherer
273 * @see org.greenstone.gatherer.collection.CollectionManager
274 */
275 public void removeLanguage(Language language) {
276 removeElement(language);
277 if(default_language != null && default_language.equals(language)) {
278 default_language = null;
279 }
280 gatherer.c_man.configurationChanged();
281 }
282 /** Method to set the default language.
283 * @param language The <strong>Language</strong> to use as a default, or <i>null</i> for no default.
284 * @see org.greenstone.gatherer.Gatherer
285 * @see org.greenstone.gatherer.collection.CollectionManager
286 */
287 public void setDefault(Language language) {
288 // Unset existing.
289 Language old = null;
290 if(default_language != null) {
291 old = default_language;
292 default_language.setDefault(false);
293 default_language = null;
294 }
295 // Now set the new if its not null.
296 if(language != null) {
297 default_language = language;
298 default_language.setDefault(true);
299 }
300 // Now cause the model to refresh any lists that are listening.
301 int start = 0;
302 int end = size() - 1;
303 if(default_language != null && old != null) {
304 int index_default = indexOf(default_language);
305 int index_old = indexOf(old);
306 if(index_default < index_old) {
307 start = index_default;
308 end = index_old;
309 }
310 else {
311 start = index_old;
312 end = index_default;
313 }
314 }
315 else if(default_language != null) {
316 start = end = indexOf(default_language);
317 }
318 else {
319 start = end = indexOf(old);
320 }
321 fireContentsChanged(this, 0, size());
322 gatherer.c_man.configurationChanged();
323 }
324 /** Method to translate this object into a block of commands as you you expect to find in the collection configuration file.
325 * @return A <strong>String</string> containing a series of commands.
326 */
327 public String toString() {
328 StringBuffer text = new StringBuffer();
329 if(size() > 1) {
330 text.append("languages ");
331 for(int i = 0; i < size(); i++) {
332 Language language = (Language) get(i);
333 text.append(language.getCode());
334 if(i < size() - 1) {
335 text.append(" ");
336 }
337 else {
338 text.append("\n");
339 }
340 }
341 // Only bother with default language if there is more than one language.
342 if(default_language != null) {
343 text.append("defaultlanguage ");
344 text.append(default_language.getCode());
345 text.append("\n");
346 }
347 text.append("\n");
348 }
349 return text.toString();
350 }
351 /** Overloaded to call get with both a key and an empty argument array.
352 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
353 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
354 */
355 private String get(String key) {
356 return get(key, null);
357 }
358 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
359 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.
360 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
361 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
362 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
363 * @see org.greenstone.gatherer.Gatherer
364 * @see org.greenstone.gatherer.Dictionary
365 */
366 private String get(String key, String args[]) {
367 if(key.indexOf('.') == -1) {
368 key = "CDM.LanguageManager." + key;
369 }
370 return gatherer.dictionary.get(key, args);
371 }
372 /** This method loads a series of code->language mappings into known_languages, by reading from the 'languages.dat' file, which is essentially a verbatim copy of the ISO 639 Standard.
373 * @see org.greenstone.gatherer.cdm.Language
374 */
375 private void loadLanguages() {
376 try {
377 File in_file = new File("languages.dat");
378 FileReader in_reader = new FileReader(in_file);
379 BufferedReader in = new BufferedReader(in_reader);
380 String entry = null;
381 while((entry = in.readLine()) != null) {
382 if(!entry.startsWith("#")) {
383 StringTokenizer tokenizer = new StringTokenizer(entry);
384 String name = tokenizer.nextToken();
385 String code = tokenizer.nextToken().toLowerCase();
386 Language language = new Language(code, name, false);
387 known_languages.put(code, language);
388 }
389 }
390 in.close();
391 }
392 catch (Exception error) {
393 error.printStackTrace();
394 }
395 }
396
397 /** This class represents the visual component of the Language Manager. */
398 private class Control
399 extends JPanel {
400 /** The button to add a new language support. */
401 private JButton add = null;
402 /** The button to clear the current default language. */
403 private JButton clear_default = null;
404 /** The button to remove a supported language. */
405 private JButton remove = null;
406 /** The button to set the current language as the default one. */
407 private JButton set_default = null;
408 /** A button to active the translation manager prompt. */
409 private JButton translate = null;
410 /** A combobox listing the available supported languages. */
411 private JComboBox selector = null;
412 /** The label denoting the default language. */
413 private JLabel default_language_label = null;
414 /** The label denoting the language selector. */
415 private JLabel selector_label = null;
416 /** The title label of this control view. */
417 private JLabel title = null;
418 /** A list of currently supported languages. */
419 private JList list = null;
420 /** The pane in which the control buttons are placed. */
421 private JPanel button_pane = null;
422 /** The pane onto which all other panes are placed. */
423 private JPanel central_pane = null;
424 /** The pane to the left containing editing controls. */
425 private JPanel control_pane = null;
426 /** A panel used to correctly layout the default language box. */
427 private JPanel default_language_pane = null;
428 /** The display pane contains the title and instruction controls. */
429 private JPanel display_pane = null;
430 /** The pane which holds everything the display pane doesn't! */
431 private JPanel lower_pane = null;
432 /** The pane containing the selector combobox. */
433 private JPanel selector_pane = null;
434 /** A text area displaying inline instructions. */
435 private JTextArea instructions = null;
436 /** A description of the language currently selected. */
437 private JTextArea description = null;
438 /** The text field showing the currently name of the default language. Non-editable. */
439 private JTextField default_language_value = null;
440 /** Constructor.
441 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.AddListener
442 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.ClearDefaultListener
443 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.ListListener
444 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.RemoveListener
445 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.SelectorListener
446 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.SetDefaultListener
447 * @see org.greenstone.gatherer.cdm.LanguageManager.Control.TranslateListener
448 */
449 public Control() {
450 super();
451 // Creation.
452 title = new JLabel(get("Title"));
453 title.setHorizontalAlignment(JLabel.CENTER);
454 title.setOpaque(true);
455
456 central_pane = new JPanel();
457
458 control_pane = new JPanel();
459
460 instructions = new JTextArea(get("Instructions"));
461 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
462 instructions.setEditable(false);
463 instructions.setLineWrap(true);
464 instructions.setWrapStyleWord(true);
465
466 selector_pane = new JPanel();
467
468 selector_label = new JLabel(get("Selector"));
469 selector = new JComboBox(getLanguageCodes().toArray());
470
471 display_pane = new JPanel();
472
473 list = new JList(model);
474 list.setCellRenderer(new ListRenderer());
475
476 default_language_pane = new JPanel();
477
478 default_language_label = new JLabel(get("Default_Language"));
479 default_language_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
480 if(default_language == null) {
481 default_language_value = new JTextField();
482 }
483 else {
484 default_language_value = new JTextField(default_language.toString());
485 }
486 default_language_value.setBackground(Color.white);
487 default_language_value.setEditable(false);
488
489 button_pane = new JPanel();
490
491 add = new JButton(get("Add"));
492 add.setMnemonic(KeyEvent.VK_A);
493 if(selector.getSelectedItem() != null) {
494 add.setEnabled(true);
495 }
496 else {
497 add.setEnabled(false);
498 }
499
500 remove = new JButton(get("Remove"));
501 remove.setMnemonic(KeyEvent.VK_R);
502 remove.setEnabled(false);
503
504 clear_default = new JButton(get("Clear_Default"));
505 clear_default.setMnemonic(KeyEvent.VK_C);
506 clear_default.setEnabled(false);
507
508 set_default = new JButton(get("Set_Default"));
509 set_default.setMnemonic(KeyEvent.VK_S);
510 set_default.setEnabled(false);
511
512 lower_pane = new JPanel();
513 translate = new JButton(get("Translate"));
514 translate.setMnemonic(KeyEvent.VK_T);
515
516 // Set up and connect listeners.
517 add.addActionListener(new AddListener());
518 clear_default.addActionListener(new ClearDefaultListener());
519 remove.addActionListener(new RemoveListener());
520 selector.addActionListener(new SelectorListener());
521 set_default.addActionListener(new SetDefaultListener());
522 translate.addActionListener(new TranslateListener());
523 list.addListSelectionListener(new ListListener());
524 // Layout components.
525 selector_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
526 selector_pane.setLayout(new BorderLayout());
527 selector_pane.add(selector_label, BorderLayout.WEST);
528 selector_pane.add(selector, BorderLayout.CENTER);
529
530 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
531
532 control_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
533 control_pane.setLayout(new BorderLayout());
534 control_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
535 control_pane.add(selector_pane, BorderLayout.SOUTH);
536
537 default_language_pane.setBorder
538 (BorderFactory.createCompoundBorder
539 (BorderFactory.createCompoundBorder
540 (BorderFactory.createEmptyBorder(5,5,5,5),
541 BorderFactory.createRaisedBevelBorder()),
542 BorderFactory.createEmptyBorder(2,2,2,2)));
543 default_language_pane.setLayout(new BorderLayout());
544 default_language_pane.add(default_language_label, BorderLayout.WEST);
545 default_language_pane.add(default_language_value, BorderLayout.CENTER);
546
547 display_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,5));
548 display_pane.setLayout(new BorderLayout());
549 display_pane.add(new JScrollPane(list), BorderLayout.CENTER);
550 display_pane.add(default_language_pane, BorderLayout.SOUTH);
551
552 central_pane.setLayout(new GridLayout(1,2));
553 central_pane.add(control_pane);
554 central_pane.add(display_pane);
555
556 button_pane.setLayout(new GridLayout(2,2));
557 button_pane.add(add);
558 button_pane.add(remove);
559 button_pane.add(clear_default);
560 button_pane.add(set_default);
561
562 lower_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
563 lower_pane = new JPanel(new BorderLayout());
564 lower_pane.add(button_pane, BorderLayout.NORTH);
565 lower_pane.add(translate, BorderLayout.CENTER);
566
567 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
568 setLayout(new BorderLayout());
569 add(title, BorderLayout.NORTH);
570 add(central_pane, BorderLayout.CENTER);
571 add(lower_pane, BorderLayout.SOUTH);
572 }
573 /** Destructor.
574 */
575 public void destroy() {
576 }
577 /** We override the updateUI method so that we can ensure we are scrolled to the top of the instructions box first.
578 */
579 public void updateUI() {
580 if(instructions != null) {
581 instructions.setCaretPosition(0);
582 }
583 super.updateUI();
584 }
585 /** 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. */
586 private class AddListener
587 implements ActionListener {
588 /** Add a new language support.
589 * @param event An <strong>ActionEvent</strong>.
590 * @see org.greenstone.gatherer.cdm.Language
591 */
592 public void actionPerformed(ActionEvent event) {
593 Language language = (Language)selector.getSelectedItem();
594 if(language != null) {
595 addLanguage(new Language(language));
596 }
597 }
598 }
599 /** Listens for actions apon the 'clear default' button in the LanguageManager controls, and if detected calls the setDefault method of the manager with <i>null</i>. */
600 private class ClearDefaultListener
601 implements ActionListener {
602 /** Clear the default index.
603 * @param event An <strong>ActionEvent</strong>.
604 */
605 public void actionPerformed(ActionEvent event) {
606 setDefault(null);
607 clear_default.setEnabled(false);
608 default_language_value.setText("");
609 }
610 }
611 /** 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. */
612 private class RemoveListener
613 implements ActionListener {
614 /** Remove the currently selected language, if any.
615 * @param event An <strong>ActionEvent</strong>.
616 * @see org.greenstone.gatherer.cdm.Language
617 */
618 public void actionPerformed(ActionEvent event) {
619 if(!list.isSelectionEmpty()) {
620 removeLanguage((Language)list.getSelectedValue());
621 if(default_language == null) {
622 clear_default.setEnabled(false);
623 default_language_value.setText("");
624 }
625 }
626 }
627 }
628 /** Listens for selections wihtin the combobox on the LanguageManager controls, and if a change is detected enables, or disables, controls appropriately. */
629 private class SelectorListener
630 implements ActionListener {
631 /** Enable or disable controls depeding on selection.
632 * @param event An <strong>ActionEvent</strong>.
633 */
634 public void actionPerformed(ActionEvent event) {
635 if(selector.getSelectedItem() != null) {
636 add.setEnabled(true);
637 //description.setText((String)known_languages.get(code));
638 }
639 else {
640 add.setEnabled(false);
641 //description.setText("");
642 }
643 }
644 }
645 /** 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. */
646 private class SetDefaultListener
647 implements ActionListener {
648 /** Set the default index to the one currently selected, if any.
649 * @param event An <strong>ActionEvent</strong>.
650 * @see org.greenstone.gatherer.cdm.Language
651 */
652 public void actionPerformed(ActionEvent event) {
653 if(!list.isSelectionEmpty()) {
654 setDefault((Language)list.getSelectedValue());
655 clear_default.setEnabled(true);
656 default_language_value.setText(default_language.toString());
657 }
658 }
659 }
660 /** Listens for actions apon the 'translate' button in the LanguageManager controls, and if detected creates a new <strong>TranslationManager</strong> to allow the user to tranlate. */
661 private class TranslateListener
662 implements ActionListener {
663 /** Create a new translation manager, but remember to clean up after it disposes.
664 * @param event An <strong>ActionEvent</strong>.
665 * @see org.greenstone.gatherer.cdm.TranslationManager
666 */
667 public void actionPerformed(ActionEvent event) {
668 TranslationManager tm = new TranslationManager(gatherer, manager);
669 tm.destroy();
670 }
671 }
672 /** Listens for selections within the list on the LanguageManager controls, and if a change is detected enables, or disables, controls appropriately. */
673 private class ListListener
674 implements ListSelectionListener {
675 /** Enable or disable controls depending on the current list selection.
676 * @param event A <strong>ListSelectionEvent</strong>.
677 */
678 public void valueChanged(ListSelectionEvent event) {
679 if(list.isSelectionEmpty()) {
680 remove.setEnabled(false);
681 set_default.setEnabled(false);
682 }
683 else {
684 remove.setEnabled(true);
685 set_default.setEnabled(true);
686 }
687 refreshAppearance();
688 }
689 }
690 /** Our list cel renderer which renders the default cell just a little different. */
691 private class ListRenderer
692 extends DefaultListCellRenderer {
693 /** Method to produce the component used to display an entry in the list.
694 * @param list The <strong>JList</strong> the component will be placed in.
695 * @param value A value to be displayed for this component, as a <strong>Object</strong>.
696 * @param index The position in the list it will occupy as an <i>int</i>.
697 * @param isSelected Whether the user has currently selected this component, as a <i>boolean</i>.
698 * @param cellHasFocus Whether the component currently has focus, again as a <i>boolean</i>.
699 * @return A <strong>Component</strong> to be used as the entry in the list.
700 * @see org.greenstone.gatherer.cdm.Language
701 */
702 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
703 Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
704 Language language = (Language) value;
705 if(language.isDefault() && !isSelected) {
706 component.setBackground(Gatherer.config.getColor("coloring.workspace_selection_background", false));
707 }
708 if(component instanceof JLabel) {
709 ((JLabel)component).setOpaque(true);
710 }
711 return component;
712 }
713 }
714 }
715}
716
717
718
719
720
721
722
Note: See TracBrowser for help on using the repository browser.