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

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

added support for setting languagemetadata to be used with language partitions

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