source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/FormatManager.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 47.3 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.lang.reflect.Field;
32import java.util.*;
33import javax.swing.*;
34import javax.swing.event.*;
35import javax.swing.undo.*;
36import org.greenstone.gatherer.Configuration;
37import org.greenstone.gatherer.DebugStream;
38import org.greenstone.gatherer.Dictionary;
39import org.greenstone.gatherer.Gatherer;
40import org.greenstone.gatherer.gui.DesignPaneHeader;
41import org.greenstone.gatherer.gui.GLIButton;
42import org.greenstone.gatherer.gui.WarningDialog;
43import org.greenstone.gatherer.metadata.MetadataElement;
44import org.greenstone.gatherer.metadata.MetadataSetManager;
45import org.greenstone.gatherer.util.StaticStrings;
46import org.greenstone.gatherer.util.Utility;
47import org.w3c.dom.*;
48
49import org.fife.ui.rsyntaxtextarea.*;
50
51
52/** This class maintains a list of format statements, and allows the addition and removal of these statements.
53 * @author John Thompson, Greenstone Digital Library, University of Waikato
54 * @version 2.3
55 */
56public class FormatManager
57 extends DOMProxyListModel implements SharedByTwoFormatManager {
58
59 static final private String BLANK = "blank";
60 static final private String FLAG = "flag";
61 static final private String VALUE = "value";
62
63 static final private String DATELIST = "DateList";
64 static final private String DATELIST_DEFAULT_FORMAT = "<td>[link][icon][/link]</td>\n<td>[highlight]{Or}{[dc.Title],[exp.Title],[ex.Title],Untitled}[/highlight]</td>\n<td>{Or}{[format:dc.Date],[format:exp.Date],[format:ex.Date]}</td>";
65 static final private String HLIST = "HList";
66 static final private String HLIST_DEFAULT_FORMAT = "[link][highlight][ex.Title][/highlight][/link]";
67 static final private String VLIST = "VList";
68 static final private String VLIST_DEFAULT_FORMAT = "<td valign=\"top\">[link][icon][/link]</td>\n<td valign=\"top\">[ex.srclink]{Or}{[ex.thumbicon],[ex.srcicon]}[ex./srclink]</td>\n<td valign=\"top\">[highlight]\n{Or}{[dc.Title],[exp.Title],[ex.Title],Untitled}\n[/highlight]{If}{[ex.Source],<br><i>([ex.Source])</i>}</td>";
69 // static final private String INVISIBLE = "Invisible";
70 //static final private String INVISIBLE_DEFAULT_FORMAT = "";
71
72 static final private String DOCUMENTHEADING = "DocumentHeading";
73 static final private String DOCUMENTHEADING_DEFAULT_FORMAT = "{Or}{[parent(Top):Title],[Title],untitled}<br>";
74
75 static final private String DOCUMENTTEXT = "DocumentText";
76 static final private String DOCUMENTTEXT_DEFAULT_FORMAT = "[Text]";
77
78 static final private String DOCUMENTBUTTONS = "DocumentButtons";
79 static final private String DOCUMENTBUTTONS_DEFAULT_FORMAT = "Detach|Highlight";
80 static final private String SEARCHTYPES = "SearchTypes";
81 static final private String SEARCHTYPES_DEFAULT_FORMAT = "plain,form";
82
83 static final private String NAVBAR = "NavigationBar";
84 static final private String NAVBAR_OPTION = "pulldown";
85
86 static private HashMap default_format_map = null;
87
88 /** The controls used to edit the format commands. */
89 private Control controls = null;
90 /** A reference to ourselves so inner classes can get at the model. */
91 private DOMProxyListModel model = null;
92
93
94 /** Constructor. */
95 public FormatManager() {
96 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.FORMAT_ELEMENT, new Format());
97 this.model = this;
98
99 default_format_map = new HashMap();
100 default_format_map.put(DATELIST, DATELIST_DEFAULT_FORMAT);
101 default_format_map.put(HLIST, HLIST_DEFAULT_FORMAT);
102 default_format_map.put(VLIST, VLIST_DEFAULT_FORMAT);
103 default_format_map.put(DOCUMENTHEADING, DOCUMENTHEADING_DEFAULT_FORMAT);
104 default_format_map.put(DOCUMENTTEXT, DOCUMENTTEXT_DEFAULT_FORMAT);
105 default_format_map.put(DOCUMENTBUTTONS, DOCUMENTBUTTONS_DEFAULT_FORMAT);
106 default_format_map.put(SEARCHTYPES, SEARCHTYPES_DEFAULT_FORMAT);
107 default_format_map.put(NAVBAR, NAVBAR_OPTION);
108 default_format_map.put("", "");
109
110 DebugStream.println("FormatManager: parsed " + getSize() + " format statements.");
111 // 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.
112 int size = getSize();
113 for(int i = 0; i < size; i++) {
114 getElementAt(i);
115 }
116
117 // Ensure the default formats for important format statements are assigned
118 if (getFormat(VLIST) == null) {
119 addFormat(new Format("",VLIST,VLIST_DEFAULT_FORMAT));
120 }
121 if (getFormat(HLIST) == null) {
122 addFormat(new Format("",HLIST,HLIST_DEFAULT_FORMAT));
123 }
124 // only if DateList is used
125 if (getFormat(DATELIST) == null && CollectionDesignManager.classifier_manager.isDateListClassifierAssigned()) {
126 addFormat(new Format("",DATELIST,DATELIST_DEFAULT_FORMAT));
127 }
128 if (getFormat(DOCUMENTHEADING) == null) {
129 addFormat(new Format(DOCUMENTHEADING, "", DOCUMENTHEADING_DEFAULT_FORMAT));
130 }
131 if (getFormat(DOCUMENTTEXT)== null) {
132 addFormat(new Format(DOCUMENTTEXT,"",DOCUMENTTEXT_DEFAULT_FORMAT));
133 }
134 if (getFormat(DOCUMENTBUTTONS) == null) {
135 addFormat(new Format(DOCUMENTBUTTONS,"",DOCUMENTBUTTONS_DEFAULT_FORMAT));
136 }
137 // only for mgpp and lucene colls
138 if (getFormat(SEARCHTYPES)==null && (CollectionDesignManager.index_manager.isMGPP()) || CollectionDesignManager.index_manager.isLucene()) {
139 addFormat(new Format(SEARCHTYPES, "", SEARCHTYPES_DEFAULT_FORMAT));
140 }
141
142
143 }
144
145 /** Method to add a new format to this manager.
146 * @param format The <strong>Format</strong> to add.
147 */
148 private void addFormat(Format format) {
149 if(!contains(format)) {
150 Element element = format.getElement();
151 // Locate where we should insert this new classifier.
152 Node target_node = CollectionConfiguration.findInsertionPoint(element);
153 add(root, format, target_node);
154 }
155 }
156
157
158 public void destroy() {
159 if(controls != null) {
160 controls.destroy();
161 controls = null;
162 }
163 }
164
165
166 private Format getFormat(String name) {
167 int model_size = getSize();
168 for(int index = 0; index < model_size; index++) {
169 Format format = (Format) getElementAt(index);
170 if(format.getName().equals(name)) {
171 return format;
172 }
173 }
174 return null;
175 }
176
177 public String getDefaultFormatString(String name){
178 String default_str = "";
179 try{
180 String field_name = name.toUpperCase()+"_DEFAULT_FORMAT";
181 Field field = this.getClass().getDeclaredField(field_name);
182 Class fieldType = field.getType();
183 default_str = (String) field.get(fieldType);
184 } catch (SecurityException e) {
185 //System.err.println("Debug: "+e.getMessage());
186 default_str = VLIST_DEFAULT_FORMAT;
187 } catch (NoSuchFieldException e) {
188 //System.err.println("Debug: "+e.getMessage());
189 default_str = VLIST_DEFAULT_FORMAT;
190 } catch (IllegalArgumentException e) {
191 //System.err.println("Debug: "+e.getMessage());
192 default_str = VLIST_DEFAULT_FORMAT;
193 } catch (IllegalAccessException e) {
194 //System.err.println("Debug: "+e.getMessage());
195 default_str = VLIST_DEFAULT_FORMAT;
196 }
197 return default_str;
198 }
199
200 /** Method to retrieve this managers controls.
201 * @return the Control for this collection.
202 */
203 public Control getControls() {
204 if(controls == null) {
205 controls = new FormatControl();
206 }
207 return controls;
208 }
209
210 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
211 * @param mode the new mode as an int
212 */
213 public void modeChanged(int mode) {
214
215 }
216
217
218 /** updates the format model */
219 public synchronized void refresh() {
220 for(int i = 0; i < getSize(); i++) {
221 Format format = (Format) getElementAt(i);
222 format.update();
223 format = null;
224 }
225 super.refresh();
226 }
227 /** Method to remove a format.
228 * @param format The <strong>Format</strong> to remove.
229 */
230 private void removeFormat(Format format) {
231 remove(format);
232 }
233
234 private ArrayList buildFeatureModel() {
235 // Rebuild feature model.
236 ArrayList feature_model = new ArrayList();
237 // Add the set options
238 for(int i = 0; i < Format.DEFAULT_FEATURES.length; i++) {
239 feature_model.add(new Entry(Format.DEFAULT_FEATURES[i]));
240 }
241 // Now the classifiers.
242 for(int j = 0; j < CollectionDesignManager.classifier_manager.getSize(); j++) {
243 feature_model.add(new Entry(CollectionDesignManager.classifier_manager.getClassifier(j)));
244 }
245 Collections.sort(feature_model);
246 return feature_model;
247 }
248
249 private ArrayList buildPartModel() {
250 DebugStream.println("buildPartModel(): replace me with something that reads in a data xml file.");
251 ArrayList part_model = new ArrayList();
252 part_model.add(new Part("", ""));
253 part_model.add(new Part(DATELIST, DATELIST_DEFAULT_FORMAT));
254 part_model.add(new Part(HLIST, HLIST_DEFAULT_FORMAT));
255 //part_model.add(new Part(INVISIBLE, INVISIBLE_DEFAULT_FORMAT));
256 part_model.add(new Part(VLIST, VLIST_DEFAULT_FORMAT));
257 return part_model;
258 }
259
260 private ArrayList buildVariableModel() {
261 ArrayList variable_model = new ArrayList();
262 variable_model.add(Dictionary.get("CDM.FormatManager.Insert_Variable"));
263 variable_model.add("[Text]");
264 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
265 for (int i = 0; i < every_metadata_set_element.size(); i++) {
266 variable_model.add("[" + ((MetadataElement) every_metadata_set_element.get(i)).getFullName() + "]");
267 }
268 variable_model.add("[link]");
269 variable_model.add("[/link]");
270 variable_model.add("[icon]");
271 variable_model.add("[numleafdocs]");
272 variable_model.add("[num]");
273 variable_model.add("[parent():_]");
274 variable_model.add("[parent(Top):_]");
275 variable_model.add("[parent(All'_'):_]");
276 variable_model.add("[child():_]");
277 variable_model.add("[child(All'_'):_]");
278 variable_model.add("[sibling():_]");
279 variable_model.add("[sibling(All'_'):_]");
280
281 return variable_model;
282 }
283
284 public class FormatControl
285 extends JPanel
286 implements Control{
287
288 private ArrayList feature_model;
289 private ArrayList part_model;
290 private ArrayList variable_model;
291 private boolean ignore_event = false;
292 private boolean ready = false; // Are these controls available to be refreshed
293 private CardLayout card_layout;
294 private JButton add_button;
295 private JButton insert_button;
296 private JButton remove_button;
297 private JButton default_button;
298 private JButton undo_button;
299 private JButton redo_button;
300 private JCheckBox enabled_checkbox;
301 private JComboBox feature_combobox;
302 private JComboBox part_combobox;
303 private JComboBox variable_combobox;
304 private JList format_list;
305 private RSyntaxTextArea editor_textarea;
306 private JPanel blank_pane;
307 private JPanel control_pane;
308 private JPanel part_pane;
309 private JPanel selection_pane;
310 private String view_type;
311 private final Dimension FIELD_SIZE = new Dimension(200, 30);
312 private final UndoManager undo = new UndoManager();
313 private boolean newtext = true;
314 private Format previousFormat = null;
315 private Format currentFormat = null;
316 private boolean fresh = true;
317
318 public FormatControl() {
319 this.setComponentOrientation(Dictionary.getOrientation());
320 feature_model = buildFeatureModel();
321 part_model = buildPartModel();
322 variable_model = buildVariableModel();
323
324 // Create
325 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Formats", "formatstatements");
326
327 format_list = new JList(model);
328
329 selection_pane = new JPanel();
330 JPanel feature_pane = new JPanel();
331 feature_pane.setComponentOrientation(Dictionary.getOrientation());
332
333 JLabel feature_label = new JLabel(Dictionary.get("CDM.FormatManager.Feature"));
334 feature_label.setComponentOrientation(Dictionary.getOrientation());
335
336 feature_combobox = new JComboBox(feature_model.toArray());
337 feature_combobox.setComponentOrientation(Dictionary.getOrientation());
338
339 feature_combobox.setOpaque(!Utility.isMac());
340 feature_combobox.setPreferredSize(FIELD_SIZE);
341 feature_combobox.setEditable(false);
342 feature_combobox.setToolTipText(Dictionary.get("CDM.FormatManager.Feature_Tooltip"));
343
344 part_pane = new JPanel();
345 part_pane.setComponentOrientation(Dictionary.getOrientation());
346 JLabel part_label = new JLabel(Dictionary.get("CDM.FormatManager.Part"));
347 part_label.setComponentOrientation(Dictionary.getOrientation());
348 part_combobox = new JComboBox(part_model.toArray());
349 part_combobox.setComponentOrientation(Dictionary.getOrientation());
350 part_combobox.setOpaque(!Utility.isMac());
351 part_combobox.setPreferredSize(FIELD_SIZE);
352 part_combobox.setEditable(false);
353 part_combobox.setToolTipText(Dictionary.get("CDM.FormatManager.Part_Tooltip"));
354
355 blank_pane = new JPanel();
356
357 JPanel center_pane = new JPanel();
358
359 card_layout = new CardLayout();
360 control_pane = new JPanel();
361
362 JPanel blank_pane = new JPanel();
363
364 JPanel editor_pane = new JPanel();
365 JPanel editor_header_pane = new JPanel();
366 JLabel editor_label = new JLabel(Dictionary.get("CDM.FormatManager.Editor"));
367 editor_label.setComponentOrientation(Dictionary.getOrientation());
368
369 editor_textarea = new RSyntaxTextArea();
370
371 /* Fields specific to RSyntaxQuery inherited class */
372 editor_textarea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
373 editor_textarea.setBracketMatchingEnabled(true);
374 editor_textarea.setAnimateBracketMatching(true);
375 editor_textarea.setAntiAliasingEnabled(true);
376 editor_textarea.setAutoIndentEnabled(true);
377 editor_textarea.setPaintMarkOccurrencesBorder(false);
378
379 /* Standard fields to JTextArea */
380 editor_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
381 editor_textarea.setCaretPosition(0);
382 editor_textarea.setLineWrap(true);
383 editor_textarea.setRows(6);
384 editor_textarea.setWrapStyleWord(false);
385 editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Add_Tooltip"));
386
387 variable_combobox = new JComboBox(variable_model.toArray());
388 variable_combobox.setComponentOrientation(Dictionary.getOrientation());
389 variable_combobox.setOpaque(!Utility.isMac());
390 variable_combobox.setToolTipText(Dictionary.get("CDM.FormatManager.Variable_Tooltip"));
391
392 insert_button = new GLIButton(Dictionary.get("CDM.FormatManager.Insert"), Dictionary.get("CDM.FormatManager.Insert_Tooltip"));
393
394 default_button = new GLIButton(Dictionary.get("CDM.FormatManager.Default"), Dictionary.get("CDM.FormatManager.Default_Tooltip"));
395
396 JPanel flag_pane = new JPanel();
397 enabled_checkbox = new JCheckBox(Dictionary.get("CDM.FormatManager.Enabled"));
398 enabled_checkbox.setComponentOrientation(Dictionary.getOrientation());
399
400 JPanel button_pane = new JPanel();
401 button_pane.setComponentOrientation(Dictionary.getOrientation());
402 add_button = new GLIButton(Dictionary.get("CDM.FormatManager.Add"), Dictionary.get("CDM.FormatManager.Add_Tooltip"));
403 add_button.setEnabled(false);
404
405 remove_button = new GLIButton(Dictionary.get("CDM.FormatManager.Remove"), Dictionary.get("CDM.FormatManager.Remove_Tooltip"));
406 remove_button.setEnabled(false);
407
408 undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
409 undo_button.setEnabled(false);
410
411 redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
412 redo_button.setEnabled(false);
413
414 // Connect
415 add_button.addActionListener(new AddListener());
416 remove_button.addActionListener(new RemoveListener());
417 default_button.addActionListener(new DefaultListener());
418 undo_button.addActionListener(new UndoListener());
419 redo_button.addActionListener(new RedoListener());
420 enabled_checkbox.addActionListener(new EnabledListener());
421 feature_combobox.addActionListener(new FeatureListener());
422 part_combobox.addActionListener(new PartListener());
423
424 editor_textarea.getDocument().addDocumentListener(new EditorListener());
425 // Listen for undo and redo events
426 editor_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
427 public void undoableEditHappened(UndoableEditEvent evt) {
428 undo.addEdit(evt.getEdit());
429 }
430 });
431
432
433 format_list.addListSelectionListener(new FormatListListener());
434 variable_combobox.addActionListener(new VariableListener());
435
436 // Layout
437 JPanel format_list_pane = new JPanel();
438 format_list_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
439 format_list_pane.setLayout(new BorderLayout());
440 format_list_pane.add(new JScrollPane(format_list), BorderLayout.CENTER);
441
442
443 feature_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
444 feature_pane.setLayout(new BorderLayout(5,0));
445 feature_pane.add(feature_label, BorderLayout.LINE_START);
446 feature_pane.add(feature_combobox, BorderLayout.CENTER);
447
448 part_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
449 part_pane.setLayout(new BorderLayout(5, 0));
450 part_pane.add(part_label, BorderLayout.LINE_START);
451 part_pane.add(part_combobox, BorderLayout.CENTER);
452
453
454 flag_pane.add(enabled_checkbox);
455
456 editor_header_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
457 editor_header_pane.setLayout(new GridLayout(1,3));
458 editor_header_pane.add(editor_label);
459
460 JPanel rupanel = new JPanel();
461 rupanel.setComponentOrientation(Dictionary.getOrientation());
462 rupanel.setLayout(new GridLayout(1,2));
463 rupanel.add(undo_button);
464 rupanel.add(redo_button);
465
466 JPanel variable_pane = new JPanel();
467 variable_pane.setComponentOrientation(Dictionary.getOrientation());
468 variable_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
469 variable_pane.setLayout(new GridLayout(1,3));
470 variable_pane.add(new JPanel());
471 variable_pane.add(variable_combobox);
472 variable_pane.add(rupanel);
473
474 editor_pane.setLayout(new BorderLayout());
475 editor_pane.add(editor_header_pane, BorderLayout.NORTH);
476 editor_pane.add(new JScrollPane(editor_textarea), BorderLayout.CENTER);
477
478 selection_pane.setLayout(new BorderLayout());
479 selection_pane.add(part_pane, BorderLayout.NORTH);
480 selection_pane.add(editor_pane, BorderLayout.CENTER);
481 selection_pane.add(variable_pane, BorderLayout.SOUTH);
482
483 control_pane.setLayout(card_layout);
484 control_pane.add(flag_pane, FLAG);
485 control_pane.add(selection_pane, VALUE);
486 control_pane.add(blank_pane, BLANK);
487
488
489 button_pane.setLayout(new GridLayout(1,3));
490 button_pane.add(add_button);
491 button_pane.add(remove_button);
492 button_pane.add(default_button);
493
494
495 center_pane.setLayout(new BorderLayout());
496 center_pane.add(feature_pane, BorderLayout.NORTH);
497 center_pane.add(control_pane, BorderLayout.CENTER);
498 center_pane.add(button_pane, BorderLayout.SOUTH);
499
500 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
501 setLayout(new BorderLayout());
502 add(header_pane, BorderLayout.NORTH);
503 add(format_list_pane, BorderLayout.CENTER);
504 add(center_pane, BorderLayout.SOUTH);
505 ready = true;
506 }
507
508 public void destroy() {
509 }
510
511
512
513 /** Overriden to ensure that the instructions pane is scrolled to the top.
514 */
515 public void gainFocus() {
516 // This is only necessary if the components have been realized
517 if(ready) {
518 model.refresh();
519
520 // Update the feature model, trying to maintain the same selected object
521 Object selected_feature = feature_combobox.getSelectedItem();
522 feature_model = buildFeatureModel();
523 feature_combobox.setModel(new DefaultComboBoxModel(feature_model.toArray()));
524 feature_combobox.setSelectedItem(selected_feature);
525
526 // Update the variable model,
527 variable_model = buildVariableModel();
528 variable_combobox.setModel(new DefaultComboBoxModel(variable_model.toArray()));
529 }
530 }
531
532 public void loseFocus() {
533 // Force all of the Formats to update their names with the correct values.
534 // do we need to do this on loseFocus???
535 model.refresh();
536 }
537
538 public Format getCurrentFormat() {
539 return (Format)format_list.getSelectedValue();
540
541 }
542
543 /** 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 */
544 private class AddListener
545 implements ActionListener {
546
547 public void actionPerformed(ActionEvent event) {
548
549 ignore_event = true; // Prevent format_list excetera propagating events
550
551 Entry entry = (Entry) feature_combobox.getSelectedItem();
552 Object f = entry.getFeature();
553 String p = "";
554 if (entry.canHavePart()) {
555 p = ((Part)part_combobox.getSelectedItem()).getName();
556 }
557
558 // Add a new format string of the appropriate type
559 Format format = null;
560 if (view_type.equals(FLAG)) {
561 format = new Format(f, p, enabled_checkbox.isSelected());
562 } else {
563 format = new Format(f, p, editor_textarea.getText());
564
565 }
566
567
568 addFormat(format);
569 existingFormat();
570
571 // Update list selection
572 format_list.setSelectedValue(format, true);
573 format = null;
574 p = null;
575 f = null;
576 entry = null;
577 ignore_event = false;
578 }
579 }
580
581 private class EditorListener
582 implements DocumentListener {
583
584 public void changedUpdate(DocumentEvent e) {
585
586 update();
587 }
588
589 public void insertUpdate(DocumentEvent e) {
590 update();
591 updateUndo("insert");
592
593 }
594
595 public void removeUpdate(DocumentEvent e) {
596 update();
597 updateUndo("remove");
598
599 }
600
601 private void updateUndo(String from){
602
603 if (!newtext){
604 undo_button.setEnabled(true);
605 }
606
607 if (editor_textarea.getText().length()!=0 && newtext){
608 newtext = false;
609 }
610 }
611
612 public void update() {
613
614 // Determine if replace should be enabled
615 if(!format_list.isSelectionEmpty()) {
616 Format format = (Format)format_list.getSelectedValue();
617 boolean shouldSave = !format.isParamType();
618
619 if (shouldSave){
620 // Changed so DocumentButtons statement can be set to empty
621 // to get rid of the Detach and NOHIGHLIGHT buttons
622 //String format_str = editor_textarea.getText().trim();
623 //format_str = (format_str.equals(""))? getDefaultFormatString(format.getName()) : format_str;
624 format.setValue(editor_textarea.getText().trim());
625 model.refresh((DOMProxyListEntry)format);
626 }
627
628 } else {
629 add_button.setEnabled(false);
630
631 }
632
633 }
634 }
635
636 private class EnabledListener
637 implements ActionListener {
638
639 public void actionPerformed(ActionEvent event) {
640 // 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.
641 if(!format_list.isSelectionEmpty()) {
642 Format format = (Format)format_list.getSelectedValue();
643 boolean shouldSave = format.isParamType();
644 if (shouldSave){
645 format.setState(enabled_checkbox.isSelected());
646 model.refresh((DOMProxyListEntry)format);
647 }
648 }
649
650 // Thats it. Add would have been enabled upon feature/part selection depending if no existing format, um, existed.
651 }
652 }
653
654 private class FeatureListener
655 implements ActionListener {
656 public void actionPerformed(ActionEvent event) {
657 undo_button.setEnabled(false);
658 redo_button.setEnabled(false);
659 default_button.setEnabled(true);
660 newtext = true;
661
662 if(!ignore_event) {
663 ignore_event = true;
664 Entry entry = (Entry) feature_combobox.getSelectedItem();
665
666 // Step one: reset part
667 if (entry.canHavePart()) {
668 // update AffectedComponents according to current entry
669 part_model = entry.getPartModel();
670 part_combobox.setModel(new DefaultComboBoxModel(part_model.toArray()));
671 part_combobox.updateUI();
672
673 part_combobox.setEnabled(true);
674 part_combobox.setSelectedIndex(part_combobox.getModel().getSize()-1);
675
676 } else {
677 part_combobox.setEnabled(false);
678 part_combobox.setSelectedIndex(0);
679 }
680 // Step two: the rest
681
682 String name = entry.toString();
683 // 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
684 Object f = entry.getFeature();
685 Part part = (Part)part_combobox.getSelectedItem();
686 String pname = part.getName();
687 // You can never add anything to blank-blank
688 if(f.toString().length() == 0 && pname.length() == 0) {
689 add_button.setEnabled(false);
690 remove_button.setEnabled(false);
691 } else {
692
693 Format format = getFormat(Format.generateName(f, pname));
694 // If there is an existing feature, select it, and use it to determine what controls are visible
695
696 if(format != null) {
697 ///ystem.err.println("There is an existing format!");
698 format_list.setSelectedValue(format, true);
699 // Now use type to determine what controls are visible, and what have initial values.
700 if(format.isParamType()) {
701 ///ystem.err.println("Flag type");
702 // Flags first.
703 ///election_pane.remove(part_pane);
704 card_layout.show(control_pane, FLAG);
705 view_type = FLAG;
706 // Initial value
707 enabled_checkbox.setSelected(format.getState());
708 } else {
709 ///ystem.err.println("Value type");
710 ///election_pane.add(part_pane);
711 card_layout.show(control_pane, VALUE);
712 view_type = VALUE;
713 // Initial value
714
715 editor_textarea.setText(format.getValue());
716 editor_textarea.setCaretPosition(0);
717
718 }
719 existingFormat();
720 control_pane.updateUI();
721 }
722 // Otherwise there is no existing format, so we proceed by checking against the feature name
723 else {
724 ///ystem.err.println("No existing format");
725 format_list.clearSelection();
726 if(Format.isParamType(name)) {
727 ///ystem.err.println("Flag type");
728 // Flags first.
729 ///election_pane.remove(part_pane);
730 card_layout.show(control_pane, FLAG);
731 view_type = FLAG;
732 // Initial value
733 enabled_checkbox.setSelected(false);
734 enabled_checkbox.setEnabled(false);
735 } else {
736 ///ystem.err.println("Value type");
737 ///election_pane.add(part_pane);
738 card_layout.show(control_pane, VALUE);
739 view_type = VALUE;
740 // Initial value
741
742 String feature_default_format = (String) default_format_map.get(f.toString());
743 if (feature_default_format != null) {
744
745 editor_textarea.setText(feature_default_format);
746 } else {
747 editor_textarea.setText(part.getDefaultFormat());
748 }
749 editor_textarea.setCaretPosition(0);
750
751 }
752 newFormat();
753 }
754 format = null;
755 name = null;
756 }
757 part = null;
758 pname = null;
759 f = null;
760 name = null;
761 entry = null;
762 ignore_event = false;
763 }
764 undo.discardAllEdits();
765 }
766 }
767
768 private class FormatListListener
769 implements ListSelectionListener {
770 public void valueChanged(ListSelectionEvent event) {
771 undo_button.setEnabled(false);
772 redo_button.setEnabled(false);
773 default_button.setEnabled(true);
774 newtext = true;
775
776 if(!ignore_event && !event.getValueIsAdjusting()) {
777
778 if(!format_list.isSelectionEmpty()) {
779 existingFormat();
780 ignore_event = true;
781 Format format = (Format)format_list.getSelectedValue();
782 // Try to match the target, remembering the entries within are Entry's
783 Entry an_entry = new Entry(format.getFeature());
784 feature_combobox.setSelectedItem(an_entry);
785 // If we didn't match anything, add it and select it again
786 Entry result_entry = (Entry) feature_combobox.getSelectedItem();
787 if(!an_entry.equals(result_entry)) {
788 feature_combobox.insertItemAt(an_entry, feature_combobox.getItemCount());
789 feature_combobox.setSelectedItem(an_entry);
790 }
791
792 // Now use type to determine what controls are visible, and what have initial values.
793 ///ystem.err.println("Determine the correct type.");
794 if(format.isParamType()) {
795 ///ystem.err.println("Flag type - remove part");
796 // Flags first.
797 ///SwingUtilities.invokeLater(new GUIChangeTask(/election_pane, part_pane, blank_pane, false));
798 card_layout.show(control_pane, FLAG);
799 view_type = FLAG;
800 // Initial value
801 enabled_checkbox.setSelected(format.getState());
802 } else {
803 ///ystem.err.println("Value type - show part");
804 ///SwingUtilities.invokeLater(new GUIChangeTask(/election_pane, part_pane, blank_pane, true));
805 card_layout.show(control_pane, VALUE);
806 view_type = VALUE;
807 // Initial values
808 // do we have a part?
809 if (format.canHavePart()) {
810 part_combobox.setEnabled(true);
811
812 // Should update the part list and re-render the part ComboBox here
813 part_model = result_entry.getPartModel();
814 part_combobox.setModel(new DefaultComboBoxModel(part_model.toArray()));
815 part_combobox.updateUI();
816
817 // Try to match the part.
818 String part_entry = format.getPart();
819 // Set Selected Item doesn't work so I'll do this manually
820 boolean found = false;
821 for(int i=0; i < part_combobox.getItemCount(); i++) {
822 Part a_part = (Part) part_combobox.getItemAt(i);
823 if(a_part.equals(part_entry)) {
824 part_combobox.setSelectedItem(a_part);
825 found = true;
826 }
827 a_part = null;
828 }
829 // If we didn't match anything, add it and select it again
830 if(!found) {
831 Part a_part = new Part(part_entry, "");
832 part_combobox.insertItemAt(a_part, part_combobox.getItemCount());
833 part_combobox.setSelectedItem(a_part);
834
835 }
836 } else {
837 part_combobox.setEnabled(false);
838 part_combobox.setSelectedIndex(0);
839 }
840 editor_textarea.setText(format.getValue());
841 editor_textarea.setCaretPosition(0);
842
843 }
844
845 //control_pane.updateUI();
846 ignore_event = false;
847 }
848
849 }
850 undo.discardAllEdits();
851 }
852
853 }
854
855 private void newFormat(){
856 editor_textarea.setEditable(false);
857 editor_textarea.setBackground(Color.lightGray);
858 editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Editor_Disabled_Tooltip"));
859
860 enabled_checkbox.setEnabled(false);
861 undo_button.setEnabled(false);
862 redo_button.setEnabled(false);
863 variable_combobox.setEnabled(false);
864 add_button.setEnabled(true);
865 remove_button.setEnabled(false);
866 default_button.setEnabled(false);
867
868 }
869
870 private void existingFormat(){
871 editor_textarea.setEditable(true);
872 editor_textarea.setBackground(Color.white);
873 editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Editor_Tooltip"));
874 enabled_checkbox.setEnabled(true);
875 variable_combobox.setEnabled(true);
876 add_button.setEnabled(false);
877 remove_button.setEnabled(true);
878 default_button.setEnabled(true);
879
880 }
881
882 private class VariableListener
883 implements ActionListener {
884 public void actionPerformed(ActionEvent event) {
885 int selected_index = variable_combobox.getSelectedIndex();
886 if (selected_index == 0) return;
887 String selected_value = (String)variable_combobox.getSelectedItem();
888 editor_textarea.insert(selected_value, editor_textarea.getCaretPosition());
889 undo_button.setEnabled(true);
890 variable_combobox.setSelectedIndex(0);
891 }
892
893 }
894
895 private class PartListener
896 implements ActionListener {
897 public void actionPerformed(ActionEvent event) {
898 undo_button.setEnabled(false);
899 redo_button.setEnabled(false);
900 default_button.setEnabled(true);
901 newtext = true;
902 if(!ignore_event) {
903 // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if its already in the model
904 Entry entry = (Entry) feature_combobox.getSelectedItem();
905 Object f = entry.getFeature();
906 Part part = (Part) part_combobox.getSelectedItem();
907 String pname = part.getName();
908 // You can never add anything to blank-blank
909 if(f.toString().length() == 0 && pname.length() == 0) {
910 add_button.setEnabled(false);
911 remove_button.setEnabled(false);
912 } else {
913 String name = Format.generateName(f, pname);
914 Format format = getFormat(name);
915 // If there is an existing feature, select it.
916 if(format != null) {
917 format_list.setSelectedValue(format, true);
918 // Now use type to determine what controls are visible, and what have initial values.
919 if(format.isParamType()) {
920 // Flags first.
921 ///election_pane.remove(part_pane);
922 card_layout.show(control_pane, FLAG);
923 view_type = FLAG;
924 // Initial value
925 enabled_checkbox.setSelected(format.getState());
926 } else {
927 ///election_pane.add(part_pane);
928 card_layout.show(control_pane, VALUE);
929 view_type = VALUE;
930 // Initial value
931
932 editor_textarea.setText(format.getValue());
933 editor_textarea.setCaretPosition(0);
934
935 }
936 control_pane.updateUI();
937 existingFormat();
938 } else {
939 format_list.clearSelection();
940 if(Format.isParamType(name)) {
941 // Flags first.
942 ///election_pane.remove(part_pane);
943 card_layout.show(control_pane, FLAG);
944 view_type = FLAG;
945 // Initial value
946 enabled_checkbox.setSelected(false);
947 enabled_checkbox.setEnabled(false);
948 } else {
949 ///election_pane.add(part_pane);
950 card_layout.show(control_pane, VALUE);
951 view_type = VALUE;
952 // Initial value
953
954 editor_textarea.setText(part.getDefaultFormat());
955 editor_textarea.setCaretPosition(0);
956
957 }
958 newFormat();
959 }
960 format = null;
961 name = null;
962 }
963
964 pname = null;
965 part = null;
966 f = null;
967 entry = null;
968 }
969 undo.discardAllEdits();
970 }
971 }
972
973 private class RemoveListener
974 implements ActionListener {
975
976 public void actionPerformed(ActionEvent event) {
977 if (!format_list.isSelectionEmpty()) {
978 // Remove the current format
979 removeFormat((Format)format_list.getSelectedValue());
980
981 // Change buttons
982 add_button.setEnabled(true);
983 newFormat();
984 }
985 }
986 }
987
988
989
990 private class DefaultListener
991 implements ActionListener {
992
993 public void actionPerformed(ActionEvent event) {
994
995 // ensure that the user didn't accidentally press the Reset to Default button
996 int user_choice = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("CDM.FormatManager.Default_Warning"), Dictionary.get("General.Warning"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
997 if(user_choice == JOptionPane.CANCEL_OPTION) {
998 return;
999 }
1000
1001 newtext = false;
1002 if(!ignore_event) {
1003 Entry entry = (Entry) feature_combobox.getSelectedItem();
1004 Object f = entry.getFeature();
1005 String name ="";
1006 String pname="";
1007 Part part = null;
1008
1009 if (entry.canHavePart()){
1010 part = (Part) part_combobox.getSelectedItem();
1011 pname = part.getName();
1012 name = Format.generateName(f, pname);
1013 } else{
1014 name = entry.toString();
1015 }
1016
1017 Format format = getFormat(name);
1018 // If there is an existing feature, select it.
1019 if(format != null) {
1020 remove_button.setEnabled(true);
1021 } else {
1022
1023 add_button.setEnabled(true);
1024 }//endif (format !=null)
1025
1026 if(Format.isParamType(name)) {
1027 // Flags first.
1028 card_layout.show(control_pane, FLAG);
1029 view_type = FLAG;
1030 // Initial value
1031 enabled_checkbox.setSelected(false);
1032 } else {
1033 card_layout.show(control_pane, VALUE);
1034 view_type = VALUE;
1035 // Initial value
1036 if (pname !=null && pname.length()!=0 ){
1037 editor_textarea.setText((String) default_format_map.get(pname));
1038 editor_textarea.setCaretPosition(0);
1039 } else{
1040 editor_textarea.setText((String) default_format_map.get(name));
1041 editor_textarea.setCaretPosition(0);
1042 }
1043 }
1044
1045 }
1046 }
1047
1048 }
1049
1050 private class UndoListener
1051 implements ActionListener {
1052
1053 public void actionPerformed(ActionEvent event) {
1054 try {
1055 if (undo.canUndo()) {
1056 int pos = editor_textarea.getCaretPosition();
1057 redo_button.setEnabled(true);
1058 undo.undo();
1059 if (pos > 0)
1060 editor_textarea.setCaretPosition(pos-1);
1061 else
1062 editor_textarea.setCaretPosition(pos);
1063 }
1064 if (!undo.canUndo()){
1065 undo_button.setEnabled(false);
1066 } else{
1067 undo_button.setEnabled(true);
1068 }
1069
1070 } catch (Exception e) {
1071
1072 }
1073 }
1074 }
1075
1076 private class RedoListener
1077 implements ActionListener {
1078 public void actionPerformed(ActionEvent evt) {
1079 try {
1080 if (undo.canRedo()) {
1081 int pos = editor_textarea.getCaretPosition();
1082 undo.redo();
1083 editor_textarea.setCaretPosition(pos);
1084 }
1085 if (!undo.canRedo()){
1086 redo_button.setEnabled(false);
1087 } else{
1088 redo_button.setEnabled(true);
1089 }
1090
1091 } catch (Exception e) {}
1092 }
1093 }
1094
1095 }
1096
1097 /** 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. */
1098 private class Entry
1099 implements Comparable {
1100 private Classifier classifier = null;
1101 private String text = null;
1102
1103 public Entry(Object object) {
1104 if(object instanceof Classifier) {
1105 classifier = (Classifier)object;
1106 } else if(object instanceof String) {
1107 text = (String)object;
1108 } else {
1109 text = "";
1110 }
1111 }
1112
1113 public Entry(String text) {
1114 this.text = text;
1115 }
1116
1117 public boolean canHavePart() {
1118 if (classifier !=null) return true;
1119 return Format.canHavePart(text);
1120 }
1121
1122 public int compareTo(Object object) {
1123 if(object == null) {
1124 return 1;
1125 }
1126 if(toString() == null) {
1127 return -1;
1128 } else {
1129 String object_str = object.toString();
1130 if(object_str == null) {
1131 return 1;
1132 }
1133 return toString().compareTo(object_str);
1134 }
1135 }
1136
1137 public boolean equals(Object object) {
1138 if(compareTo(object) == 0) {
1139 return true;
1140 }
1141 return false;
1142 }
1143
1144 public Classifier getClassifier() {
1145 return classifier;
1146 }
1147
1148 public Object getFeature() {
1149 if(classifier != null) {
1150 return classifier;
1151 }
1152
1153 if (text.startsWith("<html>")){
1154 return "";
1155 }
1156 return text;
1157 }
1158
1159 public ArrayList getPartModel(){
1160 DebugStream.println("getPartModel(): get appopriate affected components of this classifier");
1161 String classifier_name = classifier == null ? "" : classifier.toString();
1162 if(!classifier_name.equals("")){
1163 classifier_name = classifier_name.substring(0, classifier_name.indexOf("-")).trim();
1164 }
1165 ArrayList part_model = new ArrayList();
1166 // DateList only has DateList component
1167 if(classifier_name.equals("DateList")){
1168 part_model.add(new Part(DATELIST, DATELIST_DEFAULT_FORMAT));
1169 }
1170 // Other Format Entries, display the default Affected Components
1171 else if(classifier_name.equals("")){
1172 part_model = buildPartModel();
1173 }
1174 // Other Classifiers have HList and VList as Affected Component
1175 else {
1176 part_model.add(new Part(HLIST, HLIST_DEFAULT_FORMAT));
1177 part_model.add(new Part(VLIST, VLIST_DEFAULT_FORMAT));
1178 }
1179 return part_model;
1180 }
1181
1182 public String toString() {
1183 if(classifier != null) {
1184 // Return the classifier - with its CL index shown
1185 return classifier.getPositionString() + StaticStrings.COLON_CHARACTER + StaticStrings.SPACE_CHARACTER + classifier.toString();
1186 }
1187 if (text.equals("")) {
1188 return "<html><body><i>"+Dictionary.get("CDM.FormatManager.AllFeatures")+"</i></body></html>";
1189 }
1190 return text;
1191 }
1192 }
1193
1194 /*
1195 private class GUIChangeTask
1196 implements Runnable {
1197 private boolean to_add;
1198 private JPanel child;
1199 private JPanel parent;
1200 private JPanel replacement;
1201
1202 public GUIChangeTask(JPanel parent, JPanel child, JPanel replacement, boolean to_add) {
1203 this.child = child;
1204 this.parent = parent;
1205 this.replacement = replacement;
1206 this.to_add = to_add;
1207 }
1208
1209 public void run() {
1210 if(to_add) {
1211 parent.remove(replacement);
1212 parent.add(child);
1213 parent.updateUI();
1214 }
1215 else {
1216 parent.remove(child);
1217 parent.add(replacement);
1218 parent.updateUI();
1219 }
1220 }
1221 }
1222 */
1223
1224 /** This class encapsulates all of the information associated with a certain component part of a feature within a html page returned from the receptioninst. */
1225 private class Part
1226 implements Comparable {
1227 /** The default format string for this part */
1228 private String default_format = null;
1229 /** The name of this part */
1230 private String name = null;
1231 /** Constructor - must be provided with all of the details of a part as there are no other setter methods.
1232 * @param name the name of this part
1233 * @param default_format the default format string for this part
1234 */
1235 public Part(String name, String default_format) {
1236 this.default_format = default_format;
1237 this.name = name;
1238 }
1239 /** Compare this part to another object in terms of ordering
1240 * @param obj the other Object
1241 * @return <0 if the object is before, 0 if equal to, and >0 if the object is after this part
1242 */
1243 public int compareTo(Object obj) {
1244 if (obj instanceof Part) {
1245 return name.compareTo(((Part)obj).getName());
1246 }
1247 return name.compareTo(obj.toString());
1248 }
1249
1250 /** Determine if the part is equivelent to some other object
1251 * @param obj the other Object
1252 * @return true is the two objects are equal
1253 */
1254 public boolean equals(Object obj) {
1255 if (obj instanceof Part) {
1256 return name.equals(((Part)obj).getName());
1257 }
1258 return name.equals(obj.toString());
1259 }
1260
1261 /** Retrieve the default format string for this part
1262 * @return the default format String
1263 */
1264 public String getDefaultFormat() {
1265 // Retrieve the format for the super format - either VList or HList
1266 Format default_format_object = getFormat(name);
1267 if(default_format_object != null) {
1268 return default_format_object.getValue();
1269 } else {
1270 return this.default_format;
1271 }
1272 }
1273 /** Retrieve the name of this part
1274 * @return the name as a String
1275 */
1276 public String getName() {
1277 return name;
1278 }
1279 /** Produce a string representation of this part, which in this case is simply the name again
1280 * @return the name as a String
1281 */
1282 public String toString() {
1283 if (name.equals("")) {
1284 return "<html><body><i>"+Dictionary.get("CDM.FormatManager.AllParts")+"</i></body></html>";
1285 }
1286 return name;
1287 }
1288 }
1289}
Note: See TracBrowser for help on using the repository browser.