source: trunk/gli/src/org/greenstone/gatherer/cdm/TranslationView.java@ 12072

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

all these managers now implement modeChanged()

  • 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 * 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 javax.swing.table.*;
35import org.greenstone.gatherer.Configuration;
36import org.greenstone.gatherer.DebugStream;
37import org.greenstone.gatherer.Dictionary;
38import org.greenstone.gatherer.Gatherer;
39import org.greenstone.gatherer.gui.DesignPaneHeader;
40import org.greenstone.gatherer.gui.GLIButton;
41
42/** This class provides a graphical interface to allow a user to quickly and conviently (ie all in one place) translate the text fragments associated with general metadata and indexes into each of the assigned languages in the collection. It should provide clear controls for the editing of these text fragments, plus clear indicate what languages still need further translation, which it will do through a combination of coloring and other visual indicators.
43 * @author John Thompson, Greenstone Digital Library, University of Waikato
44 * @version 2.3
45 */
46public class TranslationView {
47
48 private Control controls;
49
50 static final private Dimension COMPONENT_SIZE = new Dimension(225,25);
51 static final private int LANGUAGE_WIDTH = 75;
52 static final private String GENERAL_STR = "General:";
53 static final private String INDEX_STR = "Index:";
54 static final private String PARTITION_STR = "Partitions:";
55
56 public TranslationView() {
57 DebugStream.println("TranslationView: Initialized.");
58 }
59
60 public void destroy() {
61 if(controls != null) {
62 controls.destroy();
63 controls = null;
64 }
65 }
66
67 public Control getControls() {
68 if(controls == null) {
69 controls = new TranslationControl();
70 }
71 return controls;
72 }
73
74 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
75 * @param mode the new mode as an int
76 */
77 public void modeChanged(int mode) {
78
79 }
80
81 private Object[] getFeaturesList() {
82 ArrayList features_model = new ArrayList();
83
84 // Add the indexes
85 ArrayList indexes = CollectionDesignManager.index_manager.getIndexes();
86 int indexes_size = indexes.size();
87 for(int j = 0; j < indexes_size; j++) {
88 Object bob = new BobTheMagicalComparableWrapper(indexes.get(j));
89 if(!features_model.contains(bob)) {
90 features_model.add(bob);
91 }
92 }
93
94 // We also add subindexes.
95 ArrayList subindexes = CollectionDesignManager.subcollectionindex_manager.getSubcollectionIndexes();
96 int subindexes_size = subindexes.size();
97 for(int j = 0; j < subindexes_size; j++) {
98 Object bob = new BobTheMagicalComparableWrapper(subindexes.get(j));
99 if(!features_model.contains(bob)) {
100 features_model.add(bob);
101 }
102 }
103
104 // Finally we add any remaining metadata as general
105 ArrayList general_metadata = CollectionDesignManager.collectionmeta_manager.getMetadata();
106 for(int i = 0; i < general_metadata.size(); i++) {
107 CollectionMeta metadata = (CollectionMeta) general_metadata.get(i);
108 if(!metadata.isSpecial()) {
109 Object bob = new BobTheMagicalComparableWrapper(metadata.getName());
110 if(!features_model.contains(bob)) {
111 features_model.add(bob);
112 }
113 }
114 }
115
116 Collections.sort(features_model);
117
118 return features_model.toArray();
119 }
120
121 private class BobTheMagicalComparableWrapper
122 implements Comparable {
123 private Object content;
124 private String text;
125 BobTheMagicalComparableWrapper(Object content) {
126 this.content = content;
127 }
128 public int compareTo(Object object) {
129 if(object == null) {
130 object = "";
131 }
132 if(text == null) {
133 toString();
134 }
135 return text.compareTo(object.toString());
136 }
137
138 /** Equals is used by contains and since we want to prevent double up of metadata we compare differently than in compareTo. */
139 public boolean equals(Object object) {
140 return (object != null && content.toString().equals(((BobTheMagicalComparableWrapper)object).getContent().toString()));
141 }
142
143 public Object getContent() {
144 return content;
145 }
146
147 public String getName() {
148 if(content instanceof Index) {
149 return CollectionConfiguration.STOP_CHARACTER + ((Index)content).getID();
150 }
151 else if (content instanceof SubcollectionIndex) {
152 return CollectionConfiguration.STOP_CHARACTER + ((SubcollectionIndex)content).getID();
153 }
154 else {
155 return content.toString();
156 }
157 }
158
159 public String toString() {
160 if(text == null) {
161 String temp = content.toString();
162 if(temp.indexOf(CollectionConfiguration.SPACE_CHARACTER) != -1) {
163 temp = temp.substring(0, temp.indexOf(CollectionConfiguration.SPACE_CHARACTER));
164 }
165 if(content instanceof Index) {
166 text = INDEX_STR + temp;
167 }
168 else if(content instanceof SubcollectionIndex) {
169 text = PARTITION_STR + temp;
170 }
171 else {
172 text = GENERAL_STR + temp;
173 }
174 }
175 return text;
176 }
177 }
178
179 private class TranslationControl
180 extends JPanel
181 implements Control {
182
183 private boolean ignore_event = false;
184 private FragmentTableModel fragment_table_model;
185 private JButton add_button;
186 private JButton remove_button;
187 private JButton replace_button;
188 private JComboBox language_combobox;
189 private JList features_list;
190 private JTable fragment_table;
191 private JTextArea default_area;
192 private JTextArea translation_area;
193
194 TranslationControl() {
195 fragment_table_model = new FragmentTableModel("", new TreeSet(), new ArrayList());
196 // Creation
197 JPanel header_panel = new DesignPaneHeader("CDM.GUI.Translation", "translatetext");
198
199 JPanel center_panel = new JPanel();
200
201 JPanel selection_panel = new JPanel();
202
203 JPanel component_selection_panel = new JPanel();
204 JLabel component_label = new JLabel();
205 Dictionary.registerText(component_label, "CDM.TranslationManager.Affected_Features");
206 features_list = new JList(getFeaturesList());
207 features_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
208
209 JPanel fragment_selection_panel = new JPanel();
210 JLabel fragment_label = new JLabel();
211 Dictionary.registerText(fragment_label, "CDM.TranslationManager.Assigned_Fragments");
212
213 fragment_table = new JTable(fragment_table_model);
214 fragment_table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
215 fragment_table.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
216 fragment_table.setColumnSelectionAllowed(false);
217 fragment_table.setDefaultRenderer(Object.class, new FragmentTableRenderer());
218 fragment_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
219
220 JScrollPane table_scroll = new JScrollPane(fragment_table);
221 table_scroll.getViewport().setBackground(Configuration.getColor("coloring.collection_tree_background", false));
222 table_scroll.setOpaque(true);
223
224 JPanel south_panel = new JPanel();
225
226 JPanel text_panel = new JPanel();
227
228 JPanel language_panel = new JPanel();
229 JLabel language_label = new JLabel();
230 Dictionary.registerText(language_label, "CDM.TranslationManager.Language");
231 language_combobox = new JComboBox(CollectionDesignManager.language_manager.getLanguageCodes().toArray());
232 language_combobox.setPreferredSize(COMPONENT_SIZE);
233 language_combobox.setRenderer(new LanguageListCellRenderer());
234 Dictionary.registerTooltip(language_combobox, "CDM.TranslationManager.Language_Tooltip");
235
236 JPanel default_text_panel = new JPanel();
237 JLabel default_label = new JLabel();
238 Dictionary.registerText(default_label, "CDM.TranslationManager.Default_Text");
239 default_area = new JTextArea();
240 default_area.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
241 default_area.setEditable(false);
242 default_area.setLineWrap(true);
243 default_area.setWrapStyleWord(true);
244
245 JPanel translated_text_panel = new JPanel();
246 JLabel translation_label = new JLabel();
247 Dictionary.registerText(translation_label, "CDM.TranslationManager.Translation");
248 translation_area = new JTextArea();
249 translation_area.setBackground(Configuration.getColor("coloring.disabled", false));
250 translation_area.setEnabled(false);
251 translation_area.setLineWrap(true);
252 translation_area.setWrapStyleWord(true);
253 Dictionary.registerTooltip(translation_area, "CDM.TranslationManager.Translation_Tooltip");
254
255 JPanel button_pane = new JPanel();
256 add_button = new GLIButton();
257 add_button.setEnabled(false);
258 add_button.setMnemonic(KeyEvent.VK_A);
259 Dictionary.registerBoth(add_button, "CDM.TranslationManager.Add", "CDM.TranslationManager.Add_Tooltip");
260 replace_button = new GLIButton();
261 replace_button.setEnabled(false);
262 replace_button.setMnemonic(KeyEvent.VK_C);
263 Dictionary.registerBoth(replace_button, "CDM.TranslationManager.Replace", "CDM.TranslationManager.Replace_Tooltip");
264 remove_button = new GLIButton();
265 remove_button.setEnabled(false);
266 remove_button.setMnemonic(KeyEvent.VK_R);
267 Dictionary.registerBoth(remove_button, "CDM.TranslationManager.Remove", "CDM.TranslationManager.Remove_Tooltip");
268
269 // Connection
270 add_button.addActionListener(new AddListener());
271 add_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
272 remove_button.addActionListener(new RemoveListener());
273 remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
274 replace_button.addActionListener(new ReplaceListener());
275 replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
276 language_combobox.addActionListener(new LanguageActionListener());
277 translation_area.getDocument().addDocumentListener(new TranslationDocumentListener());
278 features_list.addListSelectionListener(new FeaturesListSelectionListener());
279 fragment_table.getSelectionModel().addListSelectionListener(new FragmentListSelectionListener());
280
281 // Layout
282 component_selection_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
283 component_selection_panel.setLayout(new BorderLayout());
284 component_selection_panel.add(component_label, BorderLayout.NORTH);
285 component_selection_panel.add(new JScrollPane(features_list), BorderLayout.CENTER);
286
287 fragment_selection_panel.setLayout(new BorderLayout());
288 fragment_selection_panel.add(fragment_label, BorderLayout.NORTH);
289 fragment_selection_panel.add(table_scroll, BorderLayout.CENTER);
290
291 selection_panel.setLayout(new GridLayout(2,1,0,5));
292 selection_panel.add(component_selection_panel);
293 selection_panel.add(fragment_selection_panel);
294
295 default_text_panel.setLayout(new BorderLayout());
296 default_text_panel.add(default_label, BorderLayout.NORTH);
297 default_text_panel.add(new JScrollPane(default_area), BorderLayout.CENTER);
298
299 translated_text_panel.setLayout(new BorderLayout());
300 translated_text_panel.add(translation_label, BorderLayout.NORTH);
301 translated_text_panel.add(new JScrollPane(translation_area), BorderLayout.CENTER);
302
303 text_panel.setLayout(new GridLayout(1,2,5,0));
304 text_panel.add(default_text_panel);
305 text_panel.add(translated_text_panel);
306
307 language_panel.setLayout(new BorderLayout(5,0));
308 language_panel.add(language_label, BorderLayout.WEST);
309 language_panel.add(language_combobox, BorderLayout.CENTER);
310
311 south_panel.setLayout(new BorderLayout());
312 south_panel.add(language_panel, BorderLayout.NORTH);
313 south_panel.add(text_panel, BorderLayout.CENTER);
314
315 center_panel.setLayout(new GridLayout(2,1,0,5));
316 center_panel.add(selection_panel);
317 center_panel.add(south_panel);
318
319 button_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
320 button_pane.setLayout(new GridLayout(1,3));
321 button_pane.add(add_button);
322 button_pane.add(replace_button);
323 button_pane.add(remove_button);
324
325 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
326 setLayout(new BorderLayout());
327 add(header_panel, BorderLayout.NORTH);
328 add(center_panel, BorderLayout.CENTER);
329 add(button_pane, BorderLayout.SOUTH);
330 }
331
332 public void destroy() {
333 }
334
335 public void gainFocus() {
336 // Rebuild the features model, just incase features have been added or removed.
337 if(features_list != null) {
338 Object selected_feature = features_list.getSelectedValue();
339 features_list.setListData(getFeaturesList());
340 if(selected_feature != null) {
341 features_list.setSelectedValue(selected_feature, true);
342 }
343 }
344 }
345
346 public void loseFocus() {
347 }
348
349
350 private class AddListener
351 implements ActionListener {
352
353 public void actionPerformed(ActionEvent event) {
354 ignore_event = true;
355 String translation_text = translation_area.getText();
356 if(translation_text.length() > 0) {
357 // If there is no current fragment table selection, but translation text is not an empty string, then this is a new fragment. Create new collection metadata, refresh the fragment table, then ensure that the new metadata is selected. Remember that it is the selected metadata to which changes will be applied.
358 BobTheMagicalComparableWrapper selected_feature = (BobTheMagicalComparableWrapper)features_list.getSelectedValue();
359 String language = (String) language_combobox.getSelectedItem();
360 CollectionMeta metadatum = new CollectionMeta(selected_feature.getName(), language);
361 metadatum.setValue(translation_text);
362 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
363 fragment_table.clearSelection();
364 ArrayList metadata = CollectionDesignManager.collectionmeta_manager.getMetadata(selected_feature.getName());
365 fragment_table_model.setData(selected_feature.getName(), CollectionDesignManager.collectionmeta_manager.getLanguages(), metadata);
366 int index = fragment_table_model.getMetadataIndexByLanguage(language);
367 ///ystem.err.println("I want to select the row " + index + " out of " + metadata.size());
368 fragment_table.setRowSelectionInterval(index, index);
369 metadatum = null;
370 language = null;
371 selected_feature = null;
372 translation_text = null;
373 remove_button.setEnabled(true);
374 Gatherer.c_man.configurationChanged();
375 }
376 add_button.setEnabled(false);
377 replace_button.setEnabled(false);
378 ignore_event = false;
379 }
380 }
381
382 private class RemoveListener
383 implements ActionListener {
384
385 public void actionPerformed(ActionEvent event) {
386 ignore_event = true;
387 int index = -1;
388 if((index = fragment_table.getSelectedRow()) != -1) {
389 CollectionMeta metadata = fragment_table_model.getMetadata(index);
390 CollectionDesignManager.collectionmeta_manager.removeMetadata(metadata);
391 fragment_table_model.remove(index);
392 // If a remove occured then enable add
393 add_button.setEnabled(true);
394 Gatherer.c_man.configurationChanged();
395 }
396 // Either way disable replace and remove
397 replace_button.setEnabled(false);
398 remove_button.setEnabled(false);
399 ignore_event = false;
400 }
401 }
402
403 private class ReplaceListener
404 implements ActionListener {
405
406 public void actionPerformed(ActionEvent event) {
407 ignore_event = true;
408 int index = -1;
409 if((index = fragment_table.getSelectedRow()) != -1) {
410 String translation_text = translation_area.getText();
411 // Get the selected collection metadata
412 CollectionMeta metadata = fragment_table_model.getMetadata(index);
413 // Update the fragment metadata and ask the table to repaint the appropriate row
414 metadata.setValue(translation_text);
415 fragment_table_model.fireTableRowsUpdated(index, index);
416 metadata = null;
417 remove_button.setEnabled(true);
418 Gatherer.c_man.configurationChanged();
419 }
420 // Either way disable replace
421 add_button.setEnabled(false);
422 replace_button.setEnabled(false);
423 ignore_event = false;
424 }
425 }
426
427 private class FeaturesListSelectionListener
428 implements ListSelectionListener {
429 public void valueChanged(ListSelectionEvent event) {
430 if(!event.getValueIsAdjusting() && !ignore_event) {
431 ///ystem.err.println("FeaturesListSelectionListener");
432 if(!features_list.isSelectionEmpty()) {
433 // Determine the features name. Remember to remove our helpful source prefix (delimited by ':').
434 BobTheMagicalComparableWrapper selected_feature = (BobTheMagicalComparableWrapper)features_list.getSelectedValue();
435 // Retrieve the default language translation, or otherwise the first match, to be default text.
436 CollectionMeta default_metadata = CollectionDesignManager.collectionmeta_manager.getMetadatum(selected_feature.getName());
437 if(default_metadata != null) {
438 default_area.setText(default_metadata.getValue(CollectionMeta.TEXT));
439 }
440 // Update the text fragment table.
441 fragment_table.clearSelection();
442 fragment_table_model.setData(selected_feature.getName(), CollectionDesignManager.collectionmeta_manager.getLanguages(), CollectionDesignManager.collectionmeta_manager.getMetadata(selected_feature.getName()));
443 selected_feature = null;
444 // Now we check whatever value is currently selected in the combobox to see if it is valid to add.
445 String language_name = (String) language_combobox.getSelectedItem();
446 int index = fragment_table_model.getMetadataIndexByLanguage(language_name);
447 if(index != -1) {
448 CollectionMeta metadata = fragment_table_model.getMetadata(index);
449 fragment_table.setRowSelectionInterval(index, index);
450 if(metadata != null) {
451 translation_area.setText(metadata.getValue(CollectionMeta.TEXT));
452 }
453 else {
454 translation_area.setText("");
455 }
456 metadata = null;
457 }
458 else {
459 translation_area.setText("");
460 }
461 // Update and enable the text area
462 translation_area.setEnabled(true);
463 translation_area.setBackground(Configuration.getColor("coloring.editable_background", false));
464 }
465 else {
466 default_area.setText("");
467 fragment_table.clearSelection();
468 fragment_table_model.setData("", new TreeSet(), new ArrayList());
469 translation_area.setText("");
470 // Update and disable the text area
471 translation_area.setText("");
472 translation_area.setEnabled(false);
473 translation_area.setBackground(Configuration.getColor("coloring.disabled", false));
474 }
475 }
476 }
477 }
478
479 private class FragmentListSelectionListener
480 implements ListSelectionListener {
481 public void valueChanged(ListSelectionEvent event) {
482 if(!event.getValueIsAdjusting() && !ignore_event) {
483 ignore_event = true;
484 ///ystem.err.println("FragmentListSelectionListener");
485 int index = -1;
486 if((index = fragment_table.getSelectedRow()) != -1) {
487 // A row has been selected. Retrieve the collection metadata.
488 CollectionMeta metadatum = fragment_table_model.getMetadata(index);
489 if(metadatum != null) {
490 // Update the combobox to show the current language.
491 String language = metadatum.getLanguage();
492 ///ystem.err.println("Searching for the language: " + language_name);
493 for(int i = 0; i < language_combobox.getItemCount(); i++) {
494 if(language_combobox.getItemAt(i).toString().equals(language)) {
495 language_combobox.setSelectedIndex(i);
496 }
497 }
498 translation_area.setText(metadatum.getValue(CollectionMeta.TEXT));
499 remove_button.setEnabled(!metadatum.isDummy());
500 }
501 else {
502 remove_button.setEnabled(false);
503 }
504 }
505 else {
506 translation_area.setText("");
507 remove_button.setEnabled(false);
508 }
509 ignore_event = false;
510 }
511 }
512 }
513
514 private class FragmentTableModel
515 extends AbstractTableModel {
516
517 private ArrayList metadata;
518 private String feature_name;
519 private TreeSet languages;
520
521 FragmentTableModel(String feature_name, TreeSet languages, ArrayList metadata) {
522 this.feature_name = feature_name;
523 this.languages = languages;
524 this.metadata = metadata;
525 }
526 public int getRowCount() {
527 // The row count is equal to the maximum number of languages currently assigned in the collection.
528 return languages.size();
529 }
530 public int getColumnCount() {
531 return 2;
532 }
533
534 public String getColumnName(int column) {
535 if (column == 0) {
536 return Dictionary.get("CDM.TranslationManager.Language_Column");
537 }
538 if (column == 1) {
539 return Dictionary.get("CDM.TranslationManager.Fragment_Column");
540 }
541 return "";
542 }
543
544 /** Attempt to retrieve the metadata associated with a certain row - however not all rows actually have metadata with them (say the French row where no metadata has been set).
545 * @param row the row number as an int
546 * @return the CollectionMeta requested, which may be a dummy metadata pair
547 */
548 public CollectionMeta getMetadata(int row) {
549 // Determine what language we are talking about
550 String language = null;
551 Iterator language_iterator = languages.iterator();
552 int current_row = 0;
553 while(current_row != row && language_iterator.hasNext()) {
554 language_iterator.next();
555 current_row++;
556 }
557 if(current_row == row) {
558 language = (String) language_iterator.next();
559 return getMetadata(language);
560 }
561 language_iterator = null;
562 return null;
563 }
564
565 public CollectionMeta getMetadata(String language) {
566 // Attempt to retrieve metadata with that language
567 for(int i = 0; i < metadata.size(); i++) {
568 CollectionMeta metadatum = (CollectionMeta) metadata.get(i);
569 if(metadatum.getLanguage().equals(language)) {
570 return metadatum;
571 }
572 }
573 return new CollectionMeta(feature_name, language, true);
574 }
575
576 public int getMetadataIndexByLanguage(String language) {
577 ///ystem.err.println("Find the index for the language " + language + " (out of " + languages.size() + ")");
578 // First we have to iterate to the correct place
579 Iterator language_iterator = languages.iterator();
580 int current_row = 0;
581 while(language_iterator.hasNext()) {
582 String target = (String)language_iterator.next();
583 if(language.equals(target)) {
584 ///ystem.err.println("Matches " + target);
585 return current_row;
586 }
587 else {
588 ///ystem.err.println("Doesn't match " + target);
589 current_row++;
590 }
591 }
592 ///ystem.err.println("No such language in model: -1");
593 return -1;
594 }
595
596 public Object getValueAt(int row, int column) {
597 if(0 <= row && row < languages.size()) {
598 // First we have to iterate to the correct place
599 Iterator language_iterator = languages.iterator();
600 int current_row = 0;
601 while(current_row != row && language_iterator.hasNext()) {
602 language_iterator.next();
603 current_row++;
604 }
605 if(current_row == row) {
606 String language = (String)language_iterator.next();
607 if(column == 0) {
608 return CollectionDesignManager.language_manager.getLanguageName(language);
609 }
610 else {
611 CollectionMeta metadatum = getMetadata(language);
612 return metadatum.getValue(CollectionMeta.TEXT);
613 }
614 }
615 language_iterator = null;
616 }
617 return "#Error";
618 }
619
620 public void remove(int index) {
621 metadata.remove(index);
622 fireTableRowsDeleted(index, index);
623 }
624
625 public void setData(String feature_name, TreeSet languages, ArrayList metadata) {
626 this.feature_name = feature_name;
627 this.languages = languages;
628 this.metadata = metadata;
629 fireTableDataChanged();
630 }
631
632 public int size() {
633 return metadata.size();
634 }
635 }
636
637 private class FragmentTableRenderer
638 extends DefaultTableCellRenderer {
639 /** Retrieve the component used to rubberstamp the table based on the given parameters.
640 * @param table The <strong>JTable</strong> to rendered.
641 * @param value The <strong>Object</strong> whose representation will be shown in the table row.
642 * @param isSelected <i>true</i> iff the column/row is selected.
643 * @param hasFocus <i>true</i> iff the column/row is in focus.
644 * @param row An <i>int</i> specifying the target row.
645 * @param column An <i>int</i> specifying the target column.
646 * @see org.greenstone.gatherer.cdm.Language
647 */
648 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
649 JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
650 if(isSelected) {
651 component.setBackground(Configuration.getColor("coloring.workspace_heading_background", false));
652 }
653 else {
654 component.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
655 }
656 return component;
657 }
658 }
659
660 private class LanguageActionListener
661 implements ActionListener {
662 public void actionPerformed(ActionEvent event) {
663 if(!ignore_event) {
664 ignore_event = true;
665 ///ystem.err.println("LanguageActionListener");
666 // If the newly selected language value already exists in the fragment table, and that row isn't the one selected, then select that row the text area.
667 String language_name = language_combobox.getSelectedItem().toString();
668 int index = fragment_table_model.getMetadataIndexByLanguage(language_name);
669 if(index != -1) {
670 CollectionMeta metadata = fragment_table_model.getMetadata(index);
671 fragment_table.setRowSelectionInterval(index, index);
672 translation_area.setText(metadata.getValue(CollectionMeta.TEXT));
673 }
674 else {
675 fragment_table.clearSelection();
676 translation_area.setText("");
677 }
678 // Ready the text area
679 translation_area.setEnabled(true);
680 translation_area.setBackground(Configuration.getColor("coloring.editable_background", false));
681 ignore_event = false;
682 }
683 }
684 }
685
686 private class TranslationDocumentListener
687 implements DocumentListener {
688 /** Gives notification that an attribute or set of attributes changed. */
689 public void changedUpdate(DocumentEvent e) {
690 update();
691 }
692 /** Gives notification that there was an insert into the document. */
693 public void insertUpdate(DocumentEvent e) {
694 update();
695 }
696 /** Gives notification that a portion of the document has been removed. */
697 public void removeUpdate(DocumentEvent e) {
698 update();
699 }
700 /** 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. */
701 private void update() {
702 String translation_text = translation_area.getText();
703 // Determine if add should be enabled. You can only add if the current text fragment doesn't already exist in the fragment table for the given language
704 String language = (String) language_combobox.getSelectedItem();
705 CollectionMeta metadatum = fragment_table_model.getMetadata(language);
706 add_button.setEnabled(translation_text.length() > 0 && (metadatum.isDummy() || fragment_table_model.getMetadataIndexByLanguage(language) == -1));
707 language = null;
708 // Determine if replace should be enabled. Replace is only enabled it the text fragment is different from the one in the current fragment table selection.
709 if(fragment_table.getSelectedRowCount() > 0) {
710 replace_button.setEnabled(!metadatum.isDummy() && !translation_text.equals(fragment_table.getValueAt(fragment_table.getSelectedRow(), 1)));
711 }
712 // Nothing selected, nothing to replace
713 else {
714 replace_button.setEnabled(false);
715 }
716 translation_text = null;
717 }
718 }
719 }
720}
Note: See TracBrowser for help on using the repository browser.