source: trunk/gli/src/org/greenstone/gatherer/cdm/FormatManager.java@ 5593

Last change on this file since 5593 was 5593, checked in by mdewsnip, 21 years ago

Changed calls to the Dictionary.

  • Property svn:keywords set to Author Date Id Revision
File size: 27.6 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/**************************************************************************************
30 * Written: 06/05/02
31 * Revised: 04/10/02 - Commented
32 * 14/07/03 - DOM support
33 **************************************************************************************/
34import java.awt.*;
35import java.awt.event.*;
36import java.util.*;
37import javax.swing.*;
38import javax.swing.event.*;
39import org.greenstone.gatherer.Dictionary;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.cdm.Classifier;
42import org.greenstone.gatherer.cdm.ClassifierManager;
43import org.greenstone.gatherer.cdm.CollectionConfiguration;
44import org.greenstone.gatherer.cdm.CollectionDesignManager;
45import org.greenstone.gatherer.cdm.CommandTokenizer;
46import org.greenstone.gatherer.cdm.Control;
47import org.greenstone.gatherer.cdm.DOMProxyListModel;
48import org.greenstone.gatherer.cdm.Format;
49import org.greenstone.gatherer.msm.ElementWrapper;
50import org.greenstone.gatherer.util.GSDLSiteConfig;
51import org.greenstone.gatherer.util.Utility;
52import org.w3c.dom.*;
53
54/** This class maintains a list of format statements, and allows the addition and removal of these statements.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.3
57 */
58public class FormatManager
59 extends DOMProxyListModel {
60
61 static final private String BLANK = "blank";
62 static final private String FLAG = "flag";
63 static final private String VALUE = "value";
64
65 /** This flag is set if some change has occured to the format commands. When a collection has been built for previewing, and the greenstone local library server is used, then we have to send commands to remove then add the new collection. */
66 private boolean formats_changed = false;
67 /** The controls used to edit the format commands. */
68 private Control controls = null;
69 /** A reference to ourselves so inner classes can get at the model. */
70 private DOMProxyListModel model = null;
71
72 /** Constructor. */
73 public FormatManager() {
74 super(CollectionDesignManager.collect_config.getDocumentElement(), CollectionConfiguration.FORMAT_ELEMENT, new Format());
75 this.model = this;
76 Gatherer.println("FormatManager: parsed " + getSize() + " format statements.");
77 // Establish all of the format objects, so that classifier indexes are initially correct (subsequent refreshes of the model will be sufficient to keep these up to date, as long as we start with a live reference to a classifier.
78 int size = getSize();
79 for(int i = 0; i < size; i++) {
80 getElementAt(i);
81 }
82 }
83
84 /** Method to add a new format to this manager.
85 * @param format The <strong>Format</strong> to add.
86 */
87 public void addFormat(Format format) {
88 if(!contains(format)) {
89 Element element = format.getElement();
90 // Locate where we should insert this new classifier.
91 Node target_node = CollectionConfiguration.findInsertionPoint(element);
92 add(root, format, target_node);
93 Gatherer.c_man.configurationChanged();
94 formats_changed = true;
95 }
96 }
97
98 public void destroy() {
99 if(controls != null) {
100 controls.destroy();
101 controls = null;
102 }
103 }
104
105 /** Gets the format indicated by the index.
106 * @param index The location of the desired format, as an <i>int</i>.
107 */
108 public Format getFormat(int index) {
109 Format result = null;
110 if(0 < index && index < getSize()) {
111 result = (Format) getElementAt(index);
112 }
113 return result;
114 }
115
116 public Format getFormat(String name) {
117 int model_size = getSize();
118 for(int index = 0; index < model_size; index++) {
119 Format format = (Format) getElementAt(index);
120 if(format.getName().equals(name)) {
121 return format;
122 }
123 }
124 return null;
125 }
126
127 /** Method to retrieve this managers controls.
128 * @return the Control for this collection.
129 */
130 public Control getControls() {
131 if(controls == null) {
132 controls = new FormatControl();
133 }
134 return controls;
135 }
136
137 /** Method to remove a format.
138 * @param format The <strong>Format</strong> to remove.
139 */
140 public void removeFormat(Format format) {
141 remove(format);
142 Gatherer.c_man.configurationChanged();
143 formats_changed = true;
144 }
145
146 private HashMap buildDefaultMappings(ArrayList features_model, ArrayList parts_model) {
147 System.err.println("buildDefaultMappings(): replace me with something that reads in a data xml file.");
148 return new HashMap();
149 }
150
151 private ArrayList buildFeatureModel() {
152 // Rebuild feature model.
153 ArrayList feature_model = new ArrayList();
154 // Add the set options
155 for(int i = 0; i < Format.DEFAULT_FEATURES.length; i++) {
156 feature_model.add(new Entry(Format.DEFAULT_FEATURES[i]));
157 }
158 // Now the classifiers.
159 for(int j = 0; j < CollectionDesignManager.classifier_manager.getSize(); j++) {
160 feature_model.add(new Entry(CollectionDesignManager.classifier_manager.getClassifier(j)));
161 }
162 Collections.sort(feature_model);
163 return feature_model;
164 }
165
166 private ArrayList buildPartModel() {
167 System.err.println("buildPartModel(): replace me with something that reads in a data xml file.");
168 ArrayList part_model = new ArrayList();
169 part_model.add("");
170 part_model.add("DateList");
171 part_model.add("HList");
172 part_model.add("Invisible");
173 part_model.add("VList");
174 return part_model;
175 }
176
177 private ArrayList buildVariableModel() {
178 System.err.println("buildVariableModel(): replace me with something that reads in a data xml file.");
179 ArrayList variable_model = new ArrayList();
180 variable_model.add("[Text]");
181 variable_model.add("[link]");
182 variable_model.add("[/link]");
183 variable_model.add("[icon]");
184 variable_model.add("[num]");
185 variable_model.add("[parent():_]");
186 variable_model.add("[parent(Top):_]");
187 variable_model.add("[parent(All'_'):_]");
188 Vector elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
189 for(int i = 0; i < elements.size(); i++) {
190 variable_model.add("[" + ((ElementWrapper)elements.get(i)).getName() + "]");
191 }
192 Collections.sort(variable_model);
193 return variable_model;
194 }
195
196 private class FormatControl
197 extends JPanel
198 implements Control {
199
200 private ArrayList feature_model;
201 private ArrayList part_model;
202 private ArrayList variable_model;
203 private boolean ignore_event = false;
204 private boolean ready = false; // Are these controls available to be refreshed
205 private CardLayout card_layout;
206 private HashMap default_mappings;
207 private JButton add_button;
208 private JButton insert_button;
209 private JButton remove_button;
210 private JButton replace_button;
211 private JCheckBox enabled_checkbox;
212 private JComboBox feature_combobox;
213 private JComboBox part_combobox;
214 private JComboBox variable_combobox;
215 private JList format_list;
216 private JTextArea instructions_textarea;
217 private JTextArea editor_textarea;
218 private JPanel control_pane;
219 private JPanel part_pane;
220 private JPanel selection_pane;
221 private String view_type;
222
223 public FormatControl() {
224 feature_model = buildFeatureModel();
225 part_model = buildPartModel();
226 variable_model = buildVariableModel();
227 default_mappings = buildDefaultMappings(feature_model, part_model);
228
229 // Create
230 JPanel instructions_pane = new JPanel();
231 JLabel title_label = new JLabel();
232 title_label.setHorizontalAlignment(JLabel.CENTER);
233 title_label.setOpaque(true);
234 Dictionary.registerText(title_label, "CDM.FormatManager.Title");
235
236 instructions_textarea = new JTextArea();
237 instructions_textarea.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
238 instructions_textarea.setEditable(false);
239 instructions_textarea.setLineWrap(true);
240 instructions_textarea.setRows(6);
241 instructions_textarea.setWrapStyleWord(true);
242 Dictionary.registerText(instructions_textarea, "CDM.FormatManager.Instructions");
243
244 JLabel format_label = new JLabel();
245 Dictionary.registerText(format_label, "CDM.FormatManager.Assigned_Formats");
246 format_list = new JList(model);
247
248 selection_pane = new JPanel();
249 JPanel feature_pane = new JPanel();
250 JLabel feature_label = new JLabel();
251 feature_label.setPreferredSize(Utility.LABEL_SIZE);
252 Dictionary.registerText(feature_label, "CDM.FormatManager.Feature");
253 feature_combobox = new JComboBox(feature_model.toArray());
254 feature_combobox.setEditable(false);
255 Dictionary.registerTooltip(feature_combobox, "CDM.FormatManager.Feature_Tooltip");
256
257 part_pane = new JPanel();
258 JLabel part_label = new JLabel();
259 part_label.setPreferredSize(Utility.LABEL_SIZE);
260 Dictionary.registerText(part_label, "CDM.FormatManager.Part");
261
262 part_combobox = new JComboBox(part_model.toArray());
263 part_combobox.setEditable(false);
264 Dictionary.registerTooltip(part_combobox, "CDM.FormatManager.Part_Tooltip");
265
266 JPanel center_pane = new JPanel();
267
268 card_layout = new CardLayout();
269 control_pane = new JPanel();
270
271 JPanel blank_pane = new JPanel();
272
273 JPanel editor_pane = new JPanel();
274 JPanel editor_header_pane = new JPanel();
275 JLabel editor_label = new JLabel();
276 Dictionary.registerText(editor_label, "CDM.FormatManager.Editor");
277
278 editor_textarea = new JTextArea();
279 editor_textarea.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
280 editor_textarea.setCaretPosition(0);
281 editor_textarea.setLineWrap(true);
282 editor_textarea.setRows(6);
283 editor_textarea.setWrapStyleWord(false);
284 Dictionary.registerTooltip(editor_textarea, "CDM.FormatManager.Editor_Tooltip");
285
286 JPanel variable_pane = new JPanel();
287 JLabel variable_label = new JLabel();
288 Dictionary.registerText(variable_label, "CDM.FormatManager.Variable");
289 variable_combobox = new JComboBox(variable_model.toArray());
290 Dictionary.registerTooltip(variable_combobox, "CDM.FormatManager.Variable_Tooltip");
291
292 insert_button = new JButton();
293 insert_button.setMnemonic(KeyEvent.VK_I);
294 Dictionary.registerBoth(insert_button, "CDM.FormatManager.Insert", "CDM.FormatManager.Insert_Tooltip");
295
296 JPanel flag_pane = new JPanel();
297 enabled_checkbox = new JCheckBox();
298 Dictionary.registerText(enabled_checkbox, "CDM.FormatManager.Enabled");
299
300 JPanel button_pane = new JPanel();
301 add_button = new JButton();
302 add_button.setEnabled(false);
303 add_button.setMnemonic(KeyEvent.VK_A);
304 Dictionary.registerBoth(add_button, "CDM.FormatManager.Add", "CDM.FormatManager.Add_Tooltip");
305 replace_button = new JButton();
306 replace_button.setEnabled(false);
307 replace_button.setMnemonic(KeyEvent.VK_C);
308 Dictionary.registerBoth(replace_button, "CDM.FormatManager.Replace", "CDM.FormatManager.Replace_Tooltip");
309 remove_button = new JButton();
310 remove_button.setEnabled(false);
311 remove_button.setMnemonic(KeyEvent.VK_R);
312 Dictionary.registerBoth(remove_button, "CDM.FormatManager.Remove", "CDM.FormatManager.Remove_Tooltip");
313
314 // Connect
315 add_button.addActionListener(new AddListener());
316 insert_button.addActionListener(new InsertListener());
317 remove_button.addActionListener(new RemoveListener());
318 replace_button.addActionListener(new ReplaceListener());
319 enabled_checkbox.addActionListener(new EnabledListener());
320 feature_combobox.addActionListener(new FeatureListener());
321 part_combobox.addActionListener(new PartListener());
322 editor_textarea.getDocument().addDocumentListener(new EditorListener());
323 format_list.addListSelectionListener(new FormatListListener());
324
325 // Layout
326 instructions_pane.setLayout(new BorderLayout());
327 instructions_pane.add(title_label, BorderLayout.NORTH);
328 instructions_pane.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
329 instructions_pane.add(format_label, BorderLayout.SOUTH);
330
331 feature_pane.setLayout(new BorderLayout());
332 feature_pane.add(feature_label, BorderLayout.WEST);
333 feature_pane.add(feature_combobox, BorderLayout.CENTER);
334
335 part_pane.setLayout(new BorderLayout());
336 part_pane.add(part_label, BorderLayout.WEST);
337 part_pane.add(part_combobox, BorderLayout.CENTER);
338
339 selection_pane.setLayout(new GridLayout(2,1,0,2));
340 selection_pane.add(feature_pane);
341 selection_pane.add(part_pane);
342
343 flag_pane.add(enabled_checkbox);
344
345 editor_header_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
346 editor_header_pane.setLayout(new GridLayout(1,3));
347 editor_header_pane.add(editor_label);
348 editor_header_pane.add(new JPanel());
349
350 variable_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
351 variable_pane.setLayout(new GridLayout(1,3));
352 variable_pane.add(variable_label);
353 variable_pane.add(variable_combobox);
354 variable_pane.add(insert_button);
355
356 editor_pane.setLayout(new BorderLayout());
357 editor_pane.add(editor_header_pane, BorderLayout.NORTH);
358 editor_pane.add(new JScrollPane(editor_textarea), BorderLayout.CENTER);
359 editor_pane.add(variable_pane, BorderLayout.SOUTH);
360
361 control_pane.setLayout(card_layout);
362 control_pane.add(flag_pane, FLAG);
363 control_pane.add(editor_pane, VALUE);
364 control_pane.add(blank_pane, BLANK);
365
366 button_pane.setLayout(new GridLayout(1,3));
367 button_pane.add(add_button);
368 button_pane.add(replace_button);
369 button_pane.add(remove_button);
370
371 center_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(Dictionary.get("CDM.FormatManager.Editing_Controls")), BorderFactory.createEmptyBorder(2,2,2,2)));
372 center_pane.setLayout(new BorderLayout());
373 center_pane.add(selection_pane, BorderLayout.NORTH);
374 center_pane.add(control_pane, BorderLayout.CENTER);
375 center_pane.add(button_pane, BorderLayout.SOUTH);
376
377 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
378 setLayout(new BorderLayout());
379 add(instructions_pane, BorderLayout.NORTH);
380 add(new JScrollPane(format_list), BorderLayout.CENTER);
381 add(center_pane, BorderLayout.SOUTH);
382 ready = true;
383 }
384
385 public void destroy() {
386 }
387
388 /** Overriden to ensure that the instructions pane is scrolled to the top.
389 */
390 public void gainFocus() {
391 // This is only necessary if the components have been realized
392 if(ready) {
393 formats_changed = false;
394 model.refresh();
395 feature_model = buildFeatureModel();
396 // Remember the current selection
397 Object selected_object = feature_combobox.getSelectedItem();
398 feature_combobox.setModel(new DefaultComboBoxModel(feature_model.toArray()));
399 // Now restore the selected object as best as possible
400 feature_combobox.setSelectedItem(selected_object);
401 selected_object = null;
402 if(instructions_textarea != null) {
403 instructions_textarea.setCaretPosition(0);
404 }
405 }
406 }
407
408 public void loseFocus() {
409 // Force all of the Formats to update their names with the correct values.
410 int size = model.getSize();
411 for(int i = 0; i < size; i++) {
412 Format format = (Format) model.getElementAt(i);
413 format.update();
414 format = null;
415 }
416
417 // Now if the formats have changed, and the greenstone local library is being used and the collection has been built for previewing, then remove the add the collection to Greenstone to ensure the formatting updates correctly.
418 if(formats_changed && Gatherer.c_man.built() && Gatherer.config.exec_file != null) {
419 // Release the collection
420 Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.RELEASE_COMMAND + Gatherer.c_man.getCollection().getName());
421 // Then re-add it to force format commands to be processed
422 Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.ADD_COMMAND + Gatherer.c_man.getCollection().getName());
423 }
424 formats_changed = false;
425 }
426
427 /** Listens for clicks on the add button, and if the relevant details are provided adds a new format. Note that formats are responsible for codecing the values into something that can be a) stored in a DOM and b) written to file */
428 private class AddListener
429 implements ActionListener {
430 public void actionPerformed(ActionEvent event) {
431 ignore_event = true; // Prevent format_list excetera propagating events
432 Entry entry = (Entry)feature_combobox.getSelectedItem();
433 Object f = entry.getFeature();
434 String p = (String)part_combobox.getSelectedItem();
435 Format format = null;
436 if(view_type.equals(FLAG)) {
437 format = new Format(f, p, enabled_checkbox.isSelected());
438 }
439 else {
440 format = new Format(f, p, editor_textarea.getText());
441 }
442 addFormat(format);
443 add_button.setEnabled(false);
444 replace_button.setEnabled(false);
445 remove_button.setEnabled(true);
446 // Update list selection
447 format_list.setSelectedValue(format, true);
448 format = null;
449 p = null;
450 f = null;
451 entry = null;
452 ignore_event = false;
453 }
454 }
455
456 private class EditorListener
457 implements DocumentListener {
458
459 public void changedUpdate(DocumentEvent e) {
460 update();
461 }
462
463 public void insertUpdate(DocumentEvent e) {
464 update();
465 }
466
467 public void removeUpdate(DocumentEvent e) {
468 update();
469 }
470
471 public void update() {
472 // Determine if replace should be enabled
473 if(!format_list.isSelectionEmpty()) {
474 Format format = (Format)format_list.getSelectedValue();
475 replace_button.setEnabled(!format.isParamType() && editor_textarea.getText() != format.getValue());
476 }
477 else {
478 replace_button.setEnabled(false);
479 }
480 }
481 }
482
483 private class EnabledListener
484 implements ActionListener {
485
486 public void actionPerformed(ActionEvent event) {
487 // If there is a current format selected, and the value of enable_checkbox is now different than to value in it, then enable to replace button.
488 if(!format_list.isSelectionEmpty()) {
489 Format format = (Format)format_list.getSelectedValue();
490 replace_button.setEnabled(format.isParamType() && enabled_checkbox.isSelected() != format.getState());
491 }
492 // Thats it. Add would have been enabled upon feature/part selection depending if no existing format, um, existed.
493 }
494 }
495
496 private class FeatureListener
497 implements ActionListener {
498 public void actionPerformed(ActionEvent event) {
499 if(!ignore_event) {
500 ignore_event = true;
501 // Step one: reset part
502 part_combobox.setSelectedIndex(0);
503 // Step two: the rest
504 Entry entry = (Entry) feature_combobox.getSelectedItem();
505 String name = entry.toString();
506 if(Format.isParamType(name)) {
507 // Flags first.
508 selection_pane.remove(part_pane);
509 card_layout.show(control_pane, FLAG);
510 view_type = FLAG;
511 }
512 else {
513 selection_pane.add(part_pane);
514 card_layout.show(control_pane, VALUE);
515 view_type = VALUE;
516 }
517 control_pane.updateUI();
518 // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if itsa already in the model
519 Object f = entry.getFeature();
520 String p = (String)part_combobox.getSelectedItem();
521 // You can never add anything to blank-blank
522 if(f.toString().length() == 0 && p.length() == 0) {
523 add_button.setEnabled(false);
524 replace_button.setEnabled(false);
525 remove_button.setEnabled(false);
526 }
527 else {
528 name = Format.generateName(f, p);
529 Format format = getFormat(name);
530 // If there is an existing feature, select it.
531 if(format != null) {
532 format_list.setSelectedValue(format, true);
533 // Now use type to determine what controls are visible, and what have initial values.
534 if(format.isParamType()) {
535 // Flags first.
536 selection_pane.remove(part_pane);
537 card_layout.show(control_pane, FLAG);
538 view_type = FLAG;
539 // Initial value
540 enabled_checkbox.setSelected(format.getState());
541 }
542 else {
543 selection_pane.add(part_pane);
544 card_layout.show(control_pane, VALUE);
545 view_type = VALUE;
546 // Initial value
547 editor_textarea.setText(format.getValue());
548 }
549 control_pane.updateUI();
550 remove_button.setEnabled(true);
551 }
552 else {
553 format_list.clearSelection();
554 if(Format.isParamType(name)) {
555 // Flags first.
556 selection_pane.remove(part_pane);
557 card_layout.show(control_pane, FLAG);
558 view_type = FLAG;
559 // Initial value
560 enabled_checkbox.setSelected(false);
561 }
562 else {
563 selection_pane.add(part_pane);
564 card_layout.show(control_pane, VALUE);
565 view_type = VALUE;
566 // Initial value
567 editor_textarea.setText("");
568 }
569 add_button.setEnabled(true);
570 }
571 format = null;
572 name = null;
573 }
574 p = null;
575 f = null;
576 replace_button.setEnabled(false);
577 name = null;
578 entry = null;
579 ignore_event = false;
580 }
581 }
582 }
583
584 private class FormatListListener
585 implements ListSelectionListener {
586 public void valueChanged(ListSelectionEvent event) {
587 if(!ignore_event) {
588 if(!format_list.isSelectionEmpty()) {
589 ignore_event = true;
590 Format format = (Format)format_list.getSelectedValue();
591 // Try to match the target, remembering the entries within are Entry's
592 Entry an_entry = new Entry(format.getFeature());
593 feature_combobox.setSelectedItem(an_entry);
594 // If we didn't match anything, add it and select it again
595 Entry result_entry = (Entry) feature_combobox.getSelectedItem();
596 if(!an_entry.equals(result_entry)) {
597 feature_combobox.insertItemAt(an_entry, feature_combobox.getItemCount());
598 feature_combobox.setSelectedItem(an_entry);
599 }
600
601 if(format.canHavePart()) {
602 part_combobox.setEnabled(true);
603 // Try to match the part.
604 String part_entry = format.getPart();
605 part_combobox.setSelectedItem(part_entry);
606 // If we didn't match anything, add it and select it again
607 String selected_part = (String)part_combobox.getSelectedItem();
608 if(!part_entry.equals(selected_part)) {
609 part_combobox.insertItemAt(part_entry, part_combobox.getItemCount());
610 part_combobox.setSelectedItem(part_entry);
611 }
612 }
613 else {
614 part_combobox.setEnabled(false);
615 }
616 // Now use type to determine what controls are visible, and what have initial values.
617 if(format.isParamType()) {
618 // Flags first.
619 selection_pane.remove(part_pane);
620 card_layout.show(control_pane, FLAG);
621 view_type = FLAG;
622 // Initial value
623 enabled_checkbox.setSelected(format.getState());
624 }
625 else {
626 selection_pane.add(part_pane);
627 card_layout.show(control_pane, VALUE);
628 view_type = VALUE;
629 // Initial value
630 editor_textarea.setText(format.getValue());
631 }
632 control_pane.updateUI();
633 remove_button.setEnabled(true);
634 ignore_event = false;
635 }
636 else {
637 remove_button.setEnabled(false);
638 }
639 add_button.setEnabled(false);
640 replace_button.setEnabled(false);
641 }
642 }
643 }
644
645 private class InsertListener
646 implements ActionListener {
647 public void actionPerformed(ActionEvent event) {
648 editor_textarea.insert((String)variable_combobox.getSelectedItem(), editor_textarea.getCaretPosition());
649 }
650 }
651
652 private class PartListener
653 implements ActionListener {
654 public void actionPerformed(ActionEvent event) {
655 if(!ignore_event) {
656 // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if itsa already in the model
657 Entry entry = (Entry) feature_combobox.getSelectedItem();
658 Object f = entry.getFeature();
659 String p = (String)part_combobox.getSelectedItem();
660 // You can never add anything to blank-blank
661 if(f.toString().length() == 0 && p.length() == 0) {
662 add_button.setEnabled(false);
663 replace_button.setEnabled(false);
664 remove_button.setEnabled(false);
665 }
666 else {
667 String name = Format.generateName(f, p);
668 Format format = getFormat(name);
669 // If there is an existing feature, select it.
670 if(format != null) {
671 format_list.setSelectedValue(format, true);
672 // Now use type to determine what controls are visible, and what have initial values.
673 if(format.isParamType()) {
674 // Flags first.
675 selection_pane.remove(part_pane);
676 card_layout.show(control_pane, FLAG);
677 view_type = FLAG;
678 // Initial value
679 enabled_checkbox.setSelected(format.getState());
680 }
681 else {
682 selection_pane.add(part_pane);
683 card_layout.show(control_pane, VALUE);
684 view_type = VALUE;
685 // Initial value
686 editor_textarea.setText(format.getValue());
687 }
688 control_pane.updateUI();
689 remove_button.setEnabled(true);
690 }
691 else {
692 format_list.clearSelection();
693 if(Format.isParamType(name)) {
694 // Flags first.
695 selection_pane.remove(part_pane);
696 card_layout.show(control_pane, FLAG);
697 view_type = FLAG;
698 // Initial value
699 enabled_checkbox.setSelected(false);
700 }
701 else {
702 selection_pane.add(part_pane);
703 card_layout.show(control_pane, VALUE);
704 view_type = VALUE;
705 // Initial value
706 editor_textarea.setText("");
707 }
708 add_button.setEnabled(true);
709 }
710 format = null;
711 name = null;
712 }
713 p = null;
714 f = null;
715 entry = null;
716 replace_button.setEnabled(false);
717 }
718 }
719 }
720
721 private class RemoveListener
722 implements ActionListener {
723 public void actionPerformed(ActionEvent event) {
724 if(!format_list.isSelectionEmpty()) {
725 removeFormat((Format)format_list.getSelectedValue());
726 // Change buttons
727 add_button.setEnabled(true);
728 replace_button.setEnabled(false);
729 }
730 remove_button.setEnabled(false);
731 }
732 }
733
734 private class ReplaceListener
735 implements ActionListener {
736
737 public void actionPerformed(ActionEvent event) {
738 /** @todo **/
739 }
740 }
741 }
742
743 /** This object provides a wrapping around an entry in the format target control, which is tranparent as to whether it is backed by a String or a Classifier. */
744 private class Entry
745 implements Comparable {
746 private Classifier classifier = null;
747 private String text = null;
748
749 public Entry(Object object) {
750 if(object instanceof Classifier) {
751 classifier = (Classifier)object;
752 }
753 else if(object instanceof String) {
754 text = (String)object;
755 }
756 else {
757 text = "";
758 }
759 }
760
761 public Entry(String text) {
762 this.text = text;
763 }
764
765 public int compareTo(Object object) {
766 if(object == null) {
767 return 1;
768 }
769 if(toString() == null) {
770 return -1;
771 }
772 else {
773 String object_str = object.toString();
774 if(object_str == null) {
775 return 1;
776 }
777 return toString().compareTo(object_str);
778 }
779 }
780
781 public boolean equals(Object object) {
782 if(compareTo(object) == 0) {
783 return true;
784 }
785 return false;
786 }
787
788 public Classifier getClassifier() {
789 return classifier;
790 }
791
792 public Object getFeature() {
793 if(classifier != null) {
794 return classifier;
795 }
796 return text;
797 }
798
799 public String toString() {
800 if(classifier != null) {
801 // Return the classifier - less the 'classify ' prefix
802 return classifier.toString().substring(9);
803 }
804 return text;
805 }
806 }
807}
Note: See TracBrowser for help on using the repository browser.