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

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

Major CDM rewrite so it uses DOM.

  • Property svn:keywords set to Author Date Id Revision
File size: 26.4 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 * Written: 06/05/02
30 * Revised: 04/10/02 - Commented
31 * 14/07/03 - DOM support
32 **************************************************************************************/
33import java.awt.*;
34import java.awt.event.*;
35import java.util.*;
36import javax.swing.*;
37import javax.swing.event.*;
38import org.greenstone.gatherer.Gatherer;
39import org.greenstone.gatherer.cdm.Classifier;
40import org.greenstone.gatherer.cdm.ClassifierManager;
41import org.greenstone.gatherer.cdm.CollectionConfiguration;
42import org.greenstone.gatherer.cdm.CollectionDesignManager;
43import org.greenstone.gatherer.cdm.CommandTokenizer;
44import org.greenstone.gatherer.cdm.Control;
45import org.greenstone.gatherer.cdm.DOMProxyListModel;
46import org.greenstone.gatherer.cdm.Format;
47import org.greenstone.gatherer.msm.ElementWrapper;
48import org.greenstone.gatherer.util.Utility;
49import org.w3c.dom.*;
50/** This class maintains a list of format statements, and allows the addition and removal of these statements.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class FormatManager
55 extends DOMProxyListModel {
56
57 /** The controls used to edit the format commands. */
58 private Control controls = null;
59 /** A reference to ourselves so inner classes can get at the model. */
60 private DOMProxyListModel model = null;
61
62 /** Constructor. */
63 public FormatManager() {
64 super(CollectionDesignManager.collect_config.getDocumentElement(), CollectionConfiguration.FORMAT_ELEMENT, new Format());
65 this.model = this;
66 Gatherer.println("FormatManager: parsed " + getSize() + " format statements.");
67 // 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.
68 int size = getSize();
69 for(int i = 0; i < size; i++) {
70 getElementAt(i);
71 }
72 }
73 /** Method to add a new format to this manager.
74 * @param format The <strong>Format</strong> to add.
75 */
76 public void addFormat(Format format) {
77 if(!contains(format)) {
78 Element element = format.getElement();
79 // Locate where we should insert this new classifier.
80 Node target_node = CollectionConfiguration.findInsertionPoint(element);
81 add(root, format, target_node);
82 Gatherer.c_man.configurationChanged();
83 }
84 }
85
86 public void destroy() {
87 if(controls != null) {
88 controls.destroy();
89 controls = null;
90 }
91 }
92
93 /** Gets the format indicated by the index.
94 * @param index The location of the desired format, as an <i>int</i>.
95 */
96 public Format getFormat(int index) {
97 Format result = null;
98 if(0 < index && index < getSize()) {
99 result = (Format) getElementAt(index);
100 }
101 return result;
102 }
103
104 /** Method to retrieve this managers controls.
105 * @return the Control for this collection.
106 */
107 public Control getControls() {
108 if(controls == null) {
109 controls = new FormatControl();
110 }
111 return controls;
112 }
113
114 /** Method to remove a format.
115 * @param format The <strong>Format</strong> to remove.
116 */
117 public void removeFormat(Format format) {
118 remove(format);
119 Gatherer.c_man.configurationChanged();
120 }
121
122 /** Overloaded to call get with both a key and an empty argument array.
123 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
124 * @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.
125 */
126 private String get(String key) {
127 return get(key, null);
128 }
129
130 /** 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>
131 * 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>.
132 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
133 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
134 * @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.
135 * @see org.greenstone.gatherer.Gatherer
136 * @see org.greenstone.gatherer.Dictionary
137 */
138 private String get(String key, String args[]) {
139 if(key.indexOf('.') == -1) {
140 key = "CDM.FormatManager." + key;
141 }
142 return Gatherer.dictionary.get(key, args);
143 }
144
145 private class FormatControl
146 extends JPanel
147 implements Control {
148 /** Do we ignore selection changing events (mainly because we're generating them!) */
149 private boolean ignore = false;
150 private boolean new_entry = true;
151 private boolean ready = false;
152 private ButtonGroup button_group = null;
153 private CardLayout card_layout = null;
154 private Dimension LABEL_SIZE = new Dimension(175,25);
155 private Format current_format = null;
156 private String view_type = "custom";
157 private JButton add = null;
158 private JButton insert = null;
159 private JButton preview = null;
160 private JButton remove = null;
161 private JComboBox feature = null;
162 private JComboBox part = null;
163 private JComboBox special = null;
164 private JLabel editor_label = null;
165 private JLabel feature_label = null;
166 private JLabel format_list_label = null;
167 private JLabel part_label = null;
168 private JLabel special_label = null;
169 private JLabel title = null;
170 private JLabel value_label = null;
171 private JList format_list = null;
172 private JPanel blank_pane = null;
173 private JPanel control_pane = null;
174 private JPanel editor_pane = null;
175 private JPanel feature_pane = null;
176 private JPanel format_list_pane = null;
177 private JPanel header_pane = null;
178 private JPanel inner_button_pane = null;
179 private JPanel inner_state_pane = null;
180 private JPanel inner_value_pane = null;
181 private JPanel options_pane = null;
182 private JPanel outer_button_pane = null;
183 private JPanel part_pane = null;
184 private JPanel special_pane = null;
185 private JPanel state_pane = null;
186 private JPanel value_pane = null;
187 private JPanel view_pane = null;
188 private JTextArea editor = null;
189 private JTextArea instructions = null;
190 private JTextField value = null;
191 private JToggleButton off = null;
192 private JToggleButton on = null;
193 private String BLANK = "blank";
194 private String CUSTOM = "custom";
195 private String FLAG = "flag";
196 private String PARAM = "param";
197 private Vector part_model = null;
198 private Vector special_model = null;
199 public FormatControl() {
200 ArrayList feature_model = buildFeatureModel();
201 part_model = new Vector();
202 part_model.add("");//get("Custom"));
203 part_model.add("DateList");
204 part_model.add("HList");
205 part_model.add("Invisible");
206 part_model.add("VList");
207 special_model = new Vector();
208 special_model.add("[Text]");
209 special_model.add("[link]");
210 special_model.add("[/link]");
211 special_model.add("[icon]");
212 special_model.add("[num]");
213 special_model.add("[parent():_]");
214 special_model.add("[parent(Top):_]");
215 special_model.add("[parent(All'_'):_]");
216 Vector elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
217 for(int i = 0; i < elements.size(); i++) {
218 special_model.add("[" + ((ElementWrapper)elements.get(i)).toString() + "]");
219 }
220 Collections.sort(special_model);
221 // Create
222 add = new JButton(get("Add"));
223 add.setEnabled(false);
224 blank_pane = new JPanel();
225 button_group = new ButtonGroup();
226 card_layout = new CardLayout();
227 control_pane = new JPanel();
228 editor = new JTextArea();
229 editor.setBackground(Gatherer.config.getColor("coloring.editable", false));
230 editor.setCaretPosition(0);
231 editor.setLineWrap(true);
232 editor.setWrapStyleWord(false);
233 editor_label = new JLabel(get("Editor"));
234 editor_label.setHorizontalAlignment(JLabel.CENTER);
235 editor_pane = new JPanel();
236 feature = new JComboBox(feature_model.toArray());
237 feature.setEditable(false);
238 feature_label = new JLabel(get("Feature"));
239 feature_label.setPreferredSize(LABEL_SIZE);
240 format_list = new JList(model);
241 format_list_label = new JLabel(get("Assigned_Formats"));
242 format_list_pane = new JPanel();
243 feature_pane = new JPanel();
244 header_pane = new JPanel();
245 inner_button_pane = new JPanel();
246 inner_state_pane = new JPanel();
247 inner_value_pane = new JPanel();
248 insert = new JButton(get("Insert"));
249 instructions = new JTextArea(get("Instructions"));
250 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
251 instructions.setEditable(false);
252 instructions.setLineWrap(true);
253 instructions.setRows(4);
254 instructions.setWrapStyleWord(true);
255 off = new JToggleButton(get("Off"));
256 off.setSelected(false);
257 on = new JToggleButton(get("On"));
258 on.setSelected(true);
259 options_pane = new JPanel();
260 outer_button_pane = new JPanel();
261 part = new JComboBox(part_model);
262 part.setEditable(false);
263 part_label = new JLabel(get("Part"));
264 part_label.setPreferredSize(LABEL_SIZE);
265 part_pane = new JPanel();
266 preview = new JButton(get("Preview"));
267 preview.setEnabled(false);
268 remove = new JButton(get("Remove"));
269 remove.setEnabled(false);
270 special = new JComboBox(special_model);
271 special_label = new JLabel(get("Special"));
272 special_label.setHorizontalAlignment(JLabel.CENTER);
273 special_pane = new JPanel();
274 state_pane = new JPanel();
275 title = new JLabel(get("Title"));
276 title.setHorizontalAlignment(JLabel.CENTER);
277 title.setOpaque(true);
278 value = new JTextField();
279 value_label = new JLabel(get("Value"));
280 value_pane = new JPanel();
281 view_pane = new JPanel();
282 // Connect
283 add.addActionListener(new AddListener());
284 button_group.add(on);
285 button_group.add(off);
286 editor.getDocument().addDocumentListener(new EditorListener());
287 feature.addActionListener(new FeatureListener());
288 format_list.addListSelectionListener(new FormatListListener());
289 insert.addActionListener(new InsertListener());
290 off.addActionListener(new StateListener());
291 on.addActionListener(new StateListener());
292 part.addActionListener(new PartListener());
293 preview.addActionListener(new PreviewListener());
294 remove.addActionListener(new RemoveListener());
295 value.addKeyListener(new ValueListener());
296 // Layout
297 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
298
299 header_pane.setLayout(new BorderLayout());
300 header_pane.add(title, BorderLayout.NORTH);
301 header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
302
303 format_list_label.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
304
305 format_list_pane.setLayout(new BorderLayout());
306 format_list_pane.add(format_list_label, BorderLayout.NORTH);
307 format_list_pane.add(new JScrollPane(format_list), BorderLayout.CENTER);
308
309 feature_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
310
311 feature_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
312 feature_pane.setLayout(new BorderLayout());
313 feature_pane.add(feature_label, BorderLayout.WEST);
314 feature_pane.add(feature, BorderLayout.CENTER);
315
316 part_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
317
318 part_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
319 part_pane.setLayout(new BorderLayout());
320 part_pane.add(part_label, BorderLayout.WEST);
321 part_pane.add(part, BorderLayout.CENTER);
322
323 options_pane.setLayout(new GridLayout(2,1));
324 options_pane.add(feature_pane);
325 options_pane.add(part_pane);
326
327 inner_state_pane = new JPanel(new GridLayout(1,2));
328 inner_state_pane.add(on);
329 inner_state_pane.add(off);
330
331 state_pane.setLayout(new BorderLayout());
332 state_pane.add(inner_state_pane, BorderLayout.NORTH);
333 state_pane.add(new JPanel(), BorderLayout.CENTER);
334
335 value_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
336
337 inner_value_pane.setLayout(new BorderLayout());
338 inner_value_pane.add(value_label, BorderLayout.WEST);
339 inner_value_pane.add(value, BorderLayout.CENTER);
340
341 value_pane.setLayout(new BorderLayout());
342 value_pane.add(inner_value_pane, BorderLayout.NORTH);
343 value_pane.add(new JPanel(), BorderLayout.CENTER);
344
345 special_pane.setLayout(new GridLayout(3,1));
346 special_pane.add(special_label);
347 special_pane.add(special);
348 special_pane.add(insert);
349
350 editor_pane.setLayout(new BorderLayout());
351 editor_pane.add(editor_label, BorderLayout.NORTH);
352 editor_pane.add(new JScrollPane(editor), BorderLayout.CENTER);
353 editor_pane.add(special_pane, BorderLayout.EAST);
354
355 // Magic for view_pane card layout.
356 view_pane.setLayout(card_layout);
357 view_pane.add(editor_pane, CUSTOM);
358 view_pane.add(state_pane, FLAG);
359 //view_pane.add(value_pane, PARAM);
360
361 inner_button_pane.setLayout(new GridLayout(1,2));
362 inner_button_pane.add(add);
363 inner_button_pane.add(remove);
364
365 outer_button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
366 outer_button_pane.setLayout(new GridLayout(1,1));
367 outer_button_pane.add(inner_button_pane);
368 //outer_button_pane.add(preview);
369
370 control_pane.setLayout(new BorderLayout());
371 control_pane.add(options_pane, BorderLayout.NORTH);
372 control_pane.add(view_pane, BorderLayout.CENTER);
373 control_pane.add(outer_button_pane, BorderLayout.SOUTH);
374
375 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
376 setLayout(new BorderLayout());
377 add(header_pane, BorderLayout.NORTH);
378 add(format_list_pane, BorderLayout.CENTER);
379 add(control_pane, BorderLayout.SOUTH);
380 ready = true;
381 }
382
383 public void destroy() {
384 }
385
386 /** Overriden to ensure that the instructions pane is scrolled to the top.
387 */
388 public void gainFocus() {
389 // This is only necessary if the components have been realized
390 if(ready) {
391 model.refresh();
392 ArrayList feature_model = buildFeatureModel();
393 // Remember the current selection
394 Object selected_object = feature.getSelectedItem();
395 feature.setModel(new DefaultComboBoxModel(feature_model.toArray()));
396 // Now resotre the selected object as best as possible
397 feature.setSelectedItem(selected_object);
398 if(instructions != null) {
399 instructions.setCaretPosition(0);
400 }
401 }
402 }
403
404 public void loseFocus() {
405 // Force all of the Formats to update their names with the correct values.
406 int size = model.getSize();
407 for(int i = 0; i < size; i++) {
408 Format format = (Format) model.getElementAt(i);
409 format.update();
410 }
411 }
412
413 private ArrayList buildFeatureModel() {
414 // Rebuild feature model.
415 ArrayList feature_model = new ArrayList();
416 // Add the set options
417 for(int i = 0; i < Format.DEFAULT_FEATURES.length; i++) {
418 feature_model.add(new Entry(Format.DEFAULT_FEATURES[i]));
419 }
420 // Now the classifiers.
421 for(int j = 0; j < CollectionDesignManager.classifier_manager.getSize(); j++) {
422 feature_model.add(new Entry(CollectionDesignManager.classifier_manager.getClassifier(j)));
423 }
424 Collections.sort(feature_model);
425 return feature_model;
426 }
427
428 /** Formats the formatting string so that it contains safe characters and isn't enclosed in speech marks etc. (Ironic eh?)
429 * @see java.lang.StringBuffer
430 */
431 private String format(String raw) {
432 String safe = null;
433 if(raw != null && raw.length() > 0) {
434 StringBuffer temp = new StringBuffer(raw);
435 // Remove quotes at start and end. Look at my wiggly save three lines of code skills.
436 char start = ' ';
437 while(temp.length() > 0 && (start = temp.charAt(0)) == '\"' || start == '\'') {
438 temp.delete(0, 1);
439 }
440 int length = 0;
441 char end = ' ';
442 while((length = temp.length()) > 0 && (end = temp.charAt(length - 1)) == '\"' || end == '\'') {
443 temp.delete(length - 1, length);
444 }
445 // Now escape quotes within the format string
446 int quote_index = -1;
447 while((quote_index = temp.indexOf("\"", quote_index + 1)) != -1) {
448 temp.replace(quote_index, quote_index + 1, "\\\"");
449 quote_index = quote_index + 1;
450 }
451 // Done.
452 safe = temp.toString();
453 }
454 return safe;
455 }
456 /** Remove safe characters from string replacing them with unsafe ones.
457 * @see java.lang.StringBuffer
458 */
459 private String unformat(String safe) {
460 String raw = null;
461 if(safe != null && safe.length() > 0) {
462 StringBuffer temp = new StringBuffer(safe);
463 int quote_index = -1;
464 while((quote_index = temp.indexOf("\\\"")) != -1) {
465 temp.replace(quote_index, quote_index + 2, "\"");
466 }
467 raw = temp.toString();
468 }
469 return raw;
470 }
471
472 /** Listens for clicks on the add button, and if the relevant details are provided adds a new format. */
473 private class AddListener
474 implements ActionListener {
475 public void actionPerformed(ActionEvent event) {
476 //ignore = true;
477 Entry entry = (Entry)feature.getSelectedItem();
478 Object f = entry.getFeature();
479 String p = (String)part.getSelectedItem();
480 if(view_type.equals(FLAG)) {
481 current_format = new Format(f, p, on.isSelected());
482 }
483 else {
484 current_format = new Format(f, p, format(Utility.stripNL(editor.getText())));
485 }
486 addFormat(current_format);
487 add.setEnabled(false);
488 remove.setEnabled(true);
489 // Update list selection
490 format_list.setSelectedValue(current_format, true);
491 new_entry = false;
492 //ignore = false;
493 }
494 }
495
496 private class EditorListener
497 implements DocumentListener {
498
499 public void changedUpdate(DocumentEvent e) {
500 update();
501 }
502
503 public void insertUpdate(DocumentEvent e) {
504 update();
505 }
506
507 public void removeUpdate(DocumentEvent e) {
508 update();
509 }
510
511 public void update() {
512 String safe = format(editor.getText());
513 if(!ignore && current_format != null) {
514 // We have just performed an edit. Immediately update the format and model.
515 if(safe != null) {
516 current_format.setValue(safe);
517 }
518 else {
519 current_format.setValue("");
520 }
521 Gatherer.c_man.configurationChanged();
522 model.refresh(current_format);
523 }
524 Entry entry = (Entry) feature.getSelectedItem();
525 String name = entry.toString();
526 if(!(name.length() == 0 && ((String)part.getSelectedItem()).length() == 0) && safe != null && safe.length() != 0 && new_entry) {
527 add.setEnabled(true);
528 }
529 else {
530 add.setEnabled(false);
531 }
532 }
533 }
534
535 /** 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. */
536 private class Entry
537 implements Comparable {
538 private Classifier classifier = null;
539 private CustomClassifier custom_classifier = null;
540 private String text = null;
541 public Entry(Object object) {
542 if(object instanceof Classifier) {
543 classifier = (Classifier)object;
544 }
545 if(object instanceof CustomClassifier) {
546 custom_classifier = (CustomClassifier)object;
547 }
548 else if(object instanceof String) {
549 text = (String)object;
550 }
551 else {
552 text = "";
553 }
554 }
555 public Entry(String text) {
556 this.text = text;
557 }
558 public int compareTo(Object object) {
559 if(object == null) {
560 return 1;
561 }
562 if(toString() == null) {
563 return -1;
564 }
565 else {
566 String object_str = object.toString();
567 if(object_str == null) {
568 return 1;
569 }
570 return toString().compareTo(object_str);
571 }
572 }
573 public boolean equals(Object object) {
574 if(compareTo(object) == 0) {
575 return true;
576 }
577 return false;
578 }
579 public Classifier getClassifier() {
580 return classifier;
581 }
582 public CustomClassifier getCustomClassifier() {
583 return custom_classifier;
584 }
585 public Object getFeature() {
586 if(classifier != null) {
587 return classifier;
588 }
589 return text;
590 }
591 public String toString() {
592 if(classifier != null) {
593 String name = classifier.toString();
594 return name.substring(9);
595 }
596 if(custom_classifier != null) {
597 String name = custom_classifier.toString();
598 return name;//.substring(17);
599 }
600 return text;
601 }
602 }
603
604 private class FeatureListener
605 implements ActionListener {
606 public void actionPerformed(ActionEvent event) {
607 if(!ignore) {
608 current_format = null;
609 Entry entry = (Entry) feature.getSelectedItem();
610 String name = entry.toString();
611 if(Format.isParamType(name)) {
612 // Flags first.
613 part.setEnabled(false);
614 part_pane.remove(part);
615 card_layout.show(view_pane, FLAG);
616 view_type = FLAG;
617 add.setEnabled(true); // One of the options must be selected.
618 }
619 else {
620 part.setEnabled(true);
621 part_pane.add(part, BorderLayout.CENTER);
622 card_layout.show(view_pane, CUSTOM);
623 view_type = CUSTOM;
624 if(!(name.length() == 0 && ((String)part.getSelectedItem()).length() == 0) && editor.getText().length() != 0) {
625 add.setEnabled(true);
626 }
627 else {
628 add.setEnabled(false);
629 }
630 }
631 control_pane.updateUI();
632 new_entry = true;
633 }
634 }
635 }
636
637 private class FormatListListener
638 implements ListSelectionListener {
639 public void valueChanged(ListSelectionEvent event) {
640 if(!ignore) {
641 if(!format_list.isSelectionEmpty()) {
642 ignore = true;
643 current_format = (Format)format_list.getSelectedValue();
644 // Try to match the target, remembering the entries within are Entry's
645 Entry an_entry = new Entry(current_format.getFeature());
646 feature.setSelectedItem(an_entry);
647 // If we didn't match anything, add it and select it again
648 Entry result_entry = (Entry) feature.getSelectedItem();
649 if(!an_entry.equals(result_entry)) {
650 feature.insertItemAt(an_entry, feature.getItemCount());
651 feature.setSelectedItem(an_entry);
652 }
653
654 if(current_format.canHavePart()) {
655 part.setEnabled(true);
656 // Try to match the part.
657 String part_entry = current_format.getPart();
658 part.setSelectedItem(part_entry);
659 // If we didn't match anything, add it and select it again
660 String selected_part = (String)part.getSelectedItem();
661 if(!part_entry.equals(selected_part)) {
662 part.insertItemAt(part_entry, part.getItemCount());
663 feature.setSelectedItem(part_entry);
664 }
665 }
666 else {
667 part.setEnabled(false);
668 }
669 // Now use type to determine what controls are visible, and what have initial values.
670 if(current_format.isParamType()) {
671 // Flags first.
672 part.setEnabled(false);
673 part_pane.remove(part);
674 card_layout.show(view_pane, FLAG);
675 view_type = FLAG;
676 // Initial value
677 on.setSelected(current_format.getState());
678 off.setSelected(!current_format.getState());
679 add.setEnabled(false); // Can only update
680 }
681 else {
682 part.setEnabled(true);
683 part_pane.add(part, BorderLayout.CENTER);
684 card_layout.show(view_pane, CUSTOM);
685 view_type = CUSTOM;
686 // Initial value
687 editor.setText(unformat(current_format.getValue()));
688 add.setEnabled(false);
689 }
690 control_pane.updateUI();
691 ignore = false;
692 preview.setEnabled(true);
693 remove.setEnabled(true);
694 new_entry = false;
695 }
696 else {
697 preview.setEnabled(false);
698 remove.setEnabled(false);
699 }
700 }
701 }
702 }
703 private class InsertListener
704 implements ActionListener {
705 public void actionPerformed(ActionEvent event) {
706 editor.insert((String)special.getSelectedItem(), editor.getCaretPosition());
707 }
708 }
709 private class PartListener
710 implements ActionListener {
711 public void actionPerformed(ActionEvent event) {
712 if(!ignore) {
713 current_format = null;
714 Entry entry = (Entry) feature.getSelectedItem();
715 String name = entry.toString();
716 if(!(name.length() == 0 && ((String)part.getSelectedItem()).length() == 0) && editor.getText().length() != 0) {
717 add.setEnabled(true);
718 }
719 else {
720 add.setEnabled(false);
721 }
722 new_entry = true;
723 }
724 }
725 }
726 private class PreviewListener
727 implements ActionListener {
728 public void actionPerformed(ActionEvent event) {
729 }
730 }
731 private class RemoveListener
732 implements ActionListener {
733 public void actionPerformed(ActionEvent event) {
734 if(!format_list.isSelectionEmpty()) {
735 removeFormat((Format)format_list.getSelectedValue());
736 // Change buttons
737 add.setEnabled(true);
738 remove.setEnabled(false);
739 }
740 }
741 }
742 private class StateListener
743 implements ActionListener {
744 public void actionPerformed(ActionEvent event) {
745 if(!ignore && current_format != null) {
746 // We have just performed an edit. Immediately update the format and model.
747 current_format.setState(on.isSelected());
748 model.refresh(current_format);
749 }
750 }
751 }
752 private class ValueListener
753 extends KeyAdapter {
754 public void keyReleased(KeyEvent event) {
755 if(!ignore && current_format != null) {
756 // We have just performed an edit. Immediately update the format and model.
757 current_format.setValue(value.getText());
758 model.refresh(current_format);
759 }
760 }
761 }
762 }
763}
Note: See TracBrowser for help on using the repository browser.