source: main/trunk/gli/src/org/greenstone/gatherer/cdm/Format4gs3Manager.java@ 32628

Last change on this file since 32628 was 32628, checked in by kjdon, 5 years ago

making default templates in the interface xsl files, the format statements in the model col collectionConfig.xml, and GLI default formats in Format4gs3Manager all match up

  • Property svn:keywords set to Author Date Id Revision
File size: 28.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 * Copyright (C) 1999 New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *########################################################################
24 */
25package org.greenstone.gatherer.cdm;
26
27import java.awt.*;
28import java.awt.event.*;
29import java.util.*;
30import javax.swing.*;
31import javax.swing.event.*;
32import javax.swing.undo.*;
33import javax.xml.parsers.DocumentBuilderFactory;
34
35import org.fife.ui.rsyntaxtextarea.*;
36
37import org.greenstone.gatherer.Configuration;
38import org.greenstone.gatherer.DebugStream;
39import org.greenstone.gatherer.Dictionary;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.gui.DesignPaneHeader;
42import org.greenstone.gatherer.gui.GLIButton;
43import org.greenstone.gatherer.gui.FormatPane;
44import org.greenstone.gatherer.gui.NumberedJTextArea;
45import org.greenstone.gatherer.metadata.MetadataElement;
46import org.greenstone.gatherer.metadata.MetadataSetManager;
47import org.greenstone.gatherer.util.StaticStrings;
48import org.greenstone.gatherer.util.Utility;
49import org.greenstone.gatherer.util.XMLTools;
50import org.w3c.dom.*;
51import org.xml.sax.InputSource;
52
53import java.awt.FontMetrics;
54import java.awt.Graphics;
55import java.awt.Insets;
56import java.awt.Rectangle;
57import java.io.ByteArrayInputStream;
58
59/**
60 * This class maintains a list of format statements, and allows the addition and
61 * removal of these statements. This is the greenstone 3 equivalent class of
62 * FormatManager.java which is used by greenstone 2
63 */
64public class Format4gs3Manager implements SharedByTwoFormatManager
65{
66 // The default global format
67 static final private String GLOBAL_FORMAT;
68 static
69 {
70 // @formatter:off
71 String GLOBAL_FORMAT_TEMP = "" +
72 "<gsf:template name=\"choose-title\">" +
73 "<gsf:choose-metadata>" +
74 "<gsf:metadata name=\"dc.Title\"/>" +
75 "<gsf:metadata name=\"exp.Title\"/>" +
76 "<gsf:metadata name=\"ex.dc.Title\"/>" +
77 "<gsf:metadata name=\"Title\"/>" +
78 "<gsf:default>Untitled</gsf:default>" +
79 "</gsf:choose-metadata>" +
80 "</gsf:template>";
81 GLOBAL_FORMAT = GLOBAL_FORMAT_TEMP;
82 // @formatter:on
83 }
84 static final private String GLOBAL = "global";
85 //The default search format
86 static final private String SEARCH_FORMAT;
87 static
88 {
89 // @formatter:off
90 String SEARCH_FORMAT_TEMP = "" +
91 "<gsf:template match=\"documentNode\">" +
92 "<td valign=\"top\">" +
93 "<gsf:link type=\"document\">" +
94 "<gsf:icon type=\"document\"/>" +
95 "</gsf:link>" +
96 "</td>" +
97 "<td>" +
98 "<gsf:link type=\"document\">" +
99 "<xsl:call-template name=\"choose-title\"/>" +
100 "</gsf:link>" +
101 "</td>" +
102 "</gsf:template>";
103 SEARCH_FORMAT = SEARCH_FORMAT_TEMP;
104 // @formatter:on
105 }
106 static final private String SEARCH = "search";
107 static final private String DISPLAY_DEFAULT_FORMAT;
108 static
109 {
110 // @formatter:off
111 String DISPLAY_DEFAULT_FORMAT_TEMP = "" +
112 "<gsf:option name=\"TOC\" value=\"true\"/>" +
113 "<gsf:option name=\"AllowUserComments\" value=\"false\"/>" +
114 "<!--" +
115 "Overwriting this template allows you to change the heading of the document." +
116 "-->" +
117 "<!--" +
118 "<gsf:template name=\"documentHeading\">" +
119 "<span style=\"font-weight:bold; font-size: 120%;\">" +
120 "<xsl:call-template name=\"choose-title\"/>" +
121 "</span>" +
122 "</gsf:template>" +
123 "-->" +
124 "<!--" +
125 "Overwriting this template can be used to redefine the content of the whole document." +
126 "This is useful for simple documents, but not recommended for more complex documents" +
127 "(e.g. hierachical and paged documents) as it can prevent any sub-sections from showing." +
128 "-->" +
129 "<!--" +
130 "<gsf:template name=\"documentContent\">" +
131 "<div id=\"gs-document\">" +
132 "<xsl:call-template name=\"documentPre\"/>" +
133 "<xsl:call-template name=\"wrappedSectionImage\"/>" +
134 "<div id=\"gs-document-text\">" +
135 "<xsl:call-template name=\"documentNodeText\"/>" +
136 "</div>" +
137 "</gsf:template>" +
138 "-->" +
139 "<!--" +
140 "Overwriting this template can be used to change the content of section headings." +
141 "-->" +
142 "<!--" +
143 "<gsf:template name=\"sectionHeading\">" +
144 "<xsl:call-template name=\"choose-title\"/>" +
145 "</gsf:template>" +
146 "-->" +
147 "<!--" +
148 "Overwriting this template can be used to change the content of the top-level section." +
149 "-->" +
150 "<!--" +
151 "<gsf:template name=\"topLevelSectionContent\">" +
152 "<xsl:call-template name=\"wrappedSectionImage\"/>" +
153 "<xsl:call-template name=\"wrappedSectionText\"/>" +
154 "</gsf:template>" +
155 "-->" +
156 "<!--" +
157 "Overwriting this template can be used to change the content of sections." +
158 "-->" +
159 "<!--" +
160 "<gsf:template name=\"sectionContent\">" +
161 "<xsl:call-template name=\"wrappedSectionImage\"/>" +
162 "<xsl:call-template name=\"wrappedSectionText\"/>" +
163 "</gsf:template>" +
164 "-->";
165 // @formatter:on
166
167 DISPLAY_DEFAULT_FORMAT = DISPLAY_DEFAULT_FORMAT_TEMP;
168 }
169 static final private String DISPLAY = "display";
170
171 //The default browse format
172 static final private String CLASSIFIER_DEFAULT_FORMAT;
173 static
174 {
175 // @formatter:off
176 String CLASSIFIER_DEFAULT_FORMAT_TEMP = "" +
177 "<gsf:template match=\"documentNode\">" +
178 "<td valign=\"top\">" +
179 "<gsf:link type=\"document\">" +
180 "<gsf:icon type=\"document\"/>" +
181 "</gsf:link>" +
182 "</td>" +
183 "<td valign=\"top\">" +
184 "<gsf:link type=\"source\">" +
185 "<gsf:choose-metadata>" +
186 "<gsf:metadata name=\"thumbicon\"/>" +
187 "<gsf:metadata name=\"srcicon\"/>" +
188 "</gsf:choose-metadata>" +
189 "</gsf:link>" +
190 "</td>" +
191 "<td valign=\"top\">" +
192 "<gsf:link type=\"document\">" +
193 "<xsl:call-template name=\"choose-title\"/>" +
194 "</gsf:link>" +
195 "<gsf:switch>" +
196 "<gsf:metadata name=\"Source\"/>" +
197 "<gsf:when test=\"exists\"><br/><i>(<gsf:metadata name=\"Source\"/>)</i></gsf:when>" +
198 "</gsf:switch>" +
199 "</td>" +
200 "</gsf:template>" +
201 "<gsf:template match=\"classifierNode[@classifierStyle = 'VList']\">" +
202 "<td valign=\"top\">" +
203 "<gsf:link type=\"classifier\" style=\"static\">" +
204 "<gsf:icon type=\"classifier\"/>" +
205 "</gsf:link>" +
206 "</td>" +
207 "<td valign=\"top\">" +
208 "<gsf:link type=\"classifier\">" +
209 "<gsf:metadata name=\"Title\"/>" +
210 "</gsf:link>" +
211 "</td>" +
212 "</gsf:template>" +
213 "<gsf:template match=\"classifierNode[@classifierStyle = 'HList']\">" +
214 "<gsf:link type=\"classifier\">" +
215 "<gsf:metadata name=\"Title\"/>" +
216 "</gsf:link>" +
217 "</gsf:template>";
218 CLASSIFIER_DEFAULT_FORMAT = CLASSIFIER_DEFAULT_FORMAT_TEMP;
219 // @formatter:on
220 }
221 static final private String CLASSIFIER_DEFAULT = "browse";
222 static final private String SEARCHTYPE_FORMAT = "plain,simpleform,advancedform";
223 static final private String SEARCHTYPE = "searchType";
224 static final private String[] FEATURE_NAME = { SEARCH, GLOBAL, DISPLAY, CLASSIFIER_DEFAULT, SEARCHTYPE };
225 static final private String[] FEATURE_FORMAT = { SEARCH_FORMAT, GLOBAL_FORMAT, DISPLAY_DEFAULT_FORMAT, CLASSIFIER_DEFAULT_FORMAT, SEARCHTYPE_FORMAT };
226
227 static private HashMap default_format_map = null;
228 static private HashMap default_format_formated_map = null;
229
230 /** The controls used to edit the format commands. */
231 private Control controls = null;// an interface
232 /** A reference */
233 private DOMProxyListModel format_list_model = null;
234
235 //private DOMProxyListModel feature_list_model = null;
236
237 /** Constructor. */
238 public Format4gs3Manager()
239 {//pass the internal structure
240 Element root = CollectionDesignManager.collect_config.getDocumentElement();
241 format_list_model = new DOMProxyListModel(root, StaticStrings.FORMAT_STR, new Format4gs3());
242 initDefault(format_list_model, FEATURE_NAME, FEATURE_FORMAT);
243 initFormatMap(FEATURE_NAME, FEATURE_FORMAT);
244
245 }
246
247 private void initFormatMap(String[] feature_name, String[] feature_format)
248 {
249 default_format_map = new HashMap();
250 default_format_formated_map = new HashMap();
251 for (int i = 0; i < feature_name.length; i++)
252 {
253 default_format_map.put(feature_name[i], feature_format[i]);
254 default_format_formated_map.put(feature_name[i], Format4gs3.toFormatedFormat(feature_format[i]));
255 }
256
257 }
258
259 public void initDefault(DOMProxyListModel model, String[] feature_name, String[] feature_format)
260 {
261 // Establish all of the format objects.
262 for (int i = 0; i < model.getSize(); i++)
263 {
264 model.getElementAt(i);//get those objects cached
265 }
266 for (int i = 0; i < feature_name.length; i++)
267 {
268 if (getFormat(model, feature_name[i]) == null)
269 {
270 model.add(new Format4gs3(feature_name[i], feature_format[i]));
271
272 }
273 }
274 }
275
276 /**
277 * Method to remove a format.
278 *
279 * @param format
280 * The <strong>Format</strong> to remove.
281 */
282 private void removeFormat(DOMProxyListModel model, Format4gs3 format)
283 {
284 model.remove(format);
285 }
286
287 private Format4gs3 getFormat(DOMProxyListModel model, String name)
288 {
289
290 for (int index = 0; index < model.getSize(); index++)
291 {
292 Format4gs3 format = (Format4gs3) model.getElementAt(index);
293 if (format.getFeatureName().equals(name))
294 {
295 return format;
296 }
297 }
298 return null;
299 }
300
301 private void addFormat(Element parent, Format4gs3 format)
302 {
303 if (!format_list_model.contains(format))
304 {
305
306 format_list_model.add(parent, format, null);
307 }
308 }
309
310 public void destroy()
311 {
312 if (controls != null)
313 {
314 controls.destroy();
315 controls = null;
316 }
317 }
318
319 /**
320 * Method to retrieve this managers controls.
321 *
322 * @return the Control for this collection.
323 */
324 public Control getControls()
325 {
326 if (controls == null)
327 {
328 controls = new FormatControl();
329 }
330 //controls.gainFocus();
331 return controls;
332 }
333
334 /**
335 * Called when the detail mode has changed which in turn may cause several
336 * design elements to be available/hidden
337 *
338 * @param mode
339 * the new mode as an int
340 */
341 public void modeChanged(int mode)
342 {
343
344 }
345
346 /** updates the format and feature model */
347 public synchronized void refresh()
348 {
349 for (int i = 0; i < format_list_model.getSize(); i++)
350 {
351 Format4gs3 format = (Format4gs3) format_list_model.getElementAt(i);
352 format.update();
353 format = null;
354 }
355 //call the gainFocus() and in turn the buildFeatureModel() method to get the feature combobox refreshed as well
356 if (controls == null)
357 {
358 controls = new FormatControl();
359 }
360 controls.gainFocus();
361
362 //format_list_model.refresh(); //this call is not necessary as it is included in gainFocus()
363 }
364
365 private ArrayList buildFeatureModel()
366 {
367 // Rebuild feature model.
368 ArrayList feature_model = new ArrayList();
369 //This will display 'choose a feature' and is used when the format feature panel is first gained focus
370 feature_model.add(new Entry(""));
371
372 for (int i = 0; i < format_list_model.getSize(); i++)
373 {
374 Format4gs3 format = (Format4gs3) format_list_model.getElementAt(i);
375 String feature_name = format.getFeatureName();
376 if (!feature_name.startsWith(Classifier.CLASSIFIER_PREFIX))
377 {
378 feature_model.add(new Entry(format.getFeatureName()));
379
380 }
381 }
382 // Now the classifiers.
383 Element root = CollectionDesignManager.collect_config.getDocumentElement();
384 NodeList classifier_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);
385 for (int j = 0; j < classifier_list.getLength(); j++)
386 {
387 feature_model.add(new Entry(CollectionDesignManager.classifier_manager.getClassifier(j)));
388
389 }
390 //Collections.sort (feature_model);
391 return feature_model;
392 }
393
394 private String addSurroundingTags(String xml)
395 {
396 return "<ROOTELEMENT>" + xml + "</ROOTELEMENT>";
397 }
398
399 private String removeSurroundingTags(String xml)
400 {
401 return xml.replace("<ROOTELEMENT>\n", "").replace("<ROOTELEMENT>", "").replace("</ROOTELEMENT>", "").replace("<ROOTELEMENT/>","");
402 }
403
404 public class FormatControl extends JPanel implements Control
405 {
406
407 private ArrayList feature_model;
408 private boolean ignore_event = false;
409 private boolean ready = false; // Are these controls available to be refreshed
410 private JButton add_button;
411 private JButton remove_button;
412 private JButton default_button;
413 private JComboBox feature_combobox;
414 private JList format_list;
415 private NumberedJTextArea editor_textarea;
416 private JTextArea editor_msgarea;
417 private JPanel validation_msg_panel;
418 private JPanel selection_pane;
419 private final Dimension FIELD_SIZE = new Dimension(200, 30);
420 private boolean newtext = true;
421 private Format4gs3 previousFormat = null;
422 private Format4gs3 currentFormat = null;
423 private boolean fresh = true;
424
425 public FormatControl()
426 {
427 feature_model = buildFeatureModel();
428
429 // Create
430 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Formats", "formatstatements");
431
432 format_list = new JList(format_list_model);
433
434 selection_pane = new JPanel();
435 JPanel feature_pane = new JPanel();
436 JLabel feature_label = new JLabel(Dictionary.get("CDM.FormatManager.Feature"));
437
438 feature_combobox = new JComboBox(feature_model.toArray());
439 feature_combobox.setOpaque(!Utility.isMac());
440 feature_combobox.setPreferredSize(FIELD_SIZE);
441 feature_combobox.setEditable(false);
442 feature_combobox.setToolTipText(Dictionary.get("CDM.FormatManager.Feature_Tooltip"));
443
444 JPanel center_pane = new JPanel();
445 JPanel editor_pane = new JPanel();
446
447 // NumberedJTextArea comes with undo and redo buttons already hooked up to listeners
448 editor_textarea = new NumberedJTextArea(Dictionary.get("CDM.FormatManager.Add_Tooltip"));
449
450 default_button = new GLIButton(Dictionary.get("CDM.FormatManager.Default"), Dictionary.get("CDM.FormatManager.Default_Tooltip"));
451 JPanel button_pane = new JPanel();
452 add_button = new GLIButton(Dictionary.get("CDM.FormatManager.Add"), Dictionary.get("CDM.FormatManager.Add_Tooltip"));
453 add_button.setEnabled(false);
454
455 remove_button = new GLIButton(Dictionary.get("CDM.FormatManager.Remove"), Dictionary.get("CDM.FormatManager.Remove_Tooltip"));
456 remove_button.setEnabled(false);
457
458 // Connect
459 add_button.addActionListener(new AddListener());
460 remove_button.addActionListener(new RemoveListener());
461 default_button.addActionListener(new DefaultListener());
462 feature_combobox.addActionListener(new FeatureListener());
463 editor_textarea.getDocument().addDocumentListener(new EditorListener());
464
465 format_list.addListSelectionListener(new FormatListListener());
466
467 // Layout
468 JPanel format_list_pane = new JPanel();
469 format_list_pane.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
470 format_list_pane.setLayout(new BorderLayout());
471 format_list_pane.add(new JScrollPane(format_list), BorderLayout.CENTER);
472
473 feature_pane.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
474 feature_pane.setLayout(new BorderLayout(5, 0));
475 feature_pane.add(feature_label, BorderLayout.WEST);
476 feature_pane.add(feature_combobox, BorderLayout.CENTER);
477
478 JPanel rupanel = new JPanel();
479 rupanel.setLayout(new GridLayout(1, 2));
480 rupanel.add(editor_textarea.undoButton);
481 rupanel.add(editor_textarea.redoButton);
482
483 editor_pane.setLayout(new BorderLayout());
484 editor_pane.add(new JScrollPane(editor_textarea), BorderLayout.CENTER);
485
486 validation_msg_panel = new JPanel();
487 JLabel validation_label = new JLabel(Dictionary.get("CDM.FormatManager.MessageBox"));
488 editor_msgarea = new JTextArea();
489
490 editor_msgarea.setCaretPosition(0);
491 editor_msgarea.setLineWrap(true);
492 editor_msgarea.setRows(3);
493 editor_msgarea.setWrapStyleWord(false);
494 editor_msgarea.setEditable(false);
495 editor_msgarea.setToolTipText(Dictionary.get("CDM.FormatManager.MessageBox_Tooltip"));
496 validation_msg_panel.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
497 validation_msg_panel.setLayout(new BorderLayout(5, 0));
498 validation_msg_panel.add(validation_label, BorderLayout.WEST);
499 validation_msg_panel.add(new JScrollPane(editor_msgarea), BorderLayout.CENTER);
500
501 selection_pane.setLayout(new BorderLayout());
502 selection_pane.add(validation_msg_panel, BorderLayout.NORTH);
503 selection_pane.add(rupanel, BorderLayout.SOUTH);
504 selection_pane.add(editor_pane, BorderLayout.CENTER);
505
506 button_pane.setLayout(new GridLayout(1, 3));
507 button_pane.add(add_button);
508 button_pane.add(remove_button);
509 button_pane.add(default_button);
510
511 center_pane.setLayout(new BorderLayout());
512 center_pane.add(feature_pane, BorderLayout.NORTH);
513 center_pane.add(selection_pane, BorderLayout.CENTER);
514 center_pane.add(button_pane, BorderLayout.SOUTH);
515
516 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
517 splitPane.add(format_list_pane, JSplitPane.TOP);
518 splitPane.add(center_pane, JSplitPane.BOTTOM);
519 splitPane.setDividerLocation(150);
520
521 setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
522 setLayout(new BorderLayout());
523 add(header_pane, BorderLayout.NORTH);
524 add(splitPane, BorderLayout.CENTER);
525 ready = true;
526 }
527
528 public void destroy()
529 {
530 }
531
532 /**
533 * Overriden to ensure that the instructions pane is scrolled to the
534 * top.
535 */
536 public void gainFocus()
537 {
538 if (ready)
539 {
540
541 format_list_model.refresh();
542 // Update the feature model, trying to maintain the same selected object
543 Object selected_feature = feature_combobox.getSelectedItem();
544 feature_combobox.setSelectedItem(selected_feature);
545 feature_model = buildFeatureModel();
546 feature_combobox.setModel(new DefaultComboBoxModel(feature_model.toArray()));
547 }
548 }
549
550 public void loseFocus()
551 {
552 //validate the templates. If not wellformed, pop up an alert; otherwise, do nothing.
553 String msg = XMLTools.parse(editor_textarea.getText());
554 if (msg.startsWith(XMLTools.NOTWELLFORMED))
555 {
556 JOptionPane.showMessageDialog(null, msg, XMLTools.NOTWELLFORMED, JOptionPane.ERROR_MESSAGE);
557 }
558 format_list_model.refresh();
559 }
560
561 public Format4gs3 getCurrentFormat()
562 {
563 return (Format4gs3) format_list.getSelectedValue();
564
565 }
566
567 /**
568 * Listens for clicks on the add button, and if the relevant details are
569 * provided adds a new format. Note that formats are responsible for
570 * codecing the values into something that can be a) stored in a DOM and
571 * b) written to file
572 */
573 private class AddListener implements ActionListener
574 {
575
576 public void actionPerformed(ActionEvent event)
577 {
578
579 ignore_event = true; // Prevent format_list excetera propagating events
580
581 String format_str = editor_textarea.getText();
582 Entry entry = (Entry) feature_combobox.getSelectedItem();
583 String feature_name = entry.getClassifier().getPositionString();
584
585 Format4gs3 format = new Format4gs3(feature_name, format_str);
586 Element e = format.getClassifyElement();
587 addFormat(e, format);
588 existingFormat(format.getFeatureName().startsWith(Classifier.CLASSIFIER_PREFIX)); // set the appropriate enable/disable on the interface
589
590 // Update list selection (make the new added format highlighted on the format list panel)
591 format_list.setSelectedValue(format, true);
592
593 format = null;
594
595 ignore_event = false;
596 }
597 }
598
599 private class EditorListener implements DocumentListener
600 {
601
602 public void changedUpdate(DocumentEvent e)
603 {
604 update();
605 }
606
607 public void insertUpdate(DocumentEvent e)
608 {
609 update();
610 updateUndo("insert");
611
612 }
613
614 public void removeUpdate(DocumentEvent e)
615 {
616 update();
617 updateUndo("remove");
618
619 }
620
621 private void updateUndo(String from)
622 {
623
624 if (!newtext)
625 {
626 editor_textarea.undoButton.setEnabled(true);
627 }
628
629 if (editor_textarea.getText().length() != 0 && newtext)
630 {
631 newtext = false;
632 }
633 }
634
635 public void update()
636 {
637 if (!format_list.isSelectionEmpty())
638 {
639 Format4gs3 format = (Format4gs3) format_list.getSelectedValue();
640 String format_str = editor_textarea.getText();
641 String msg = XMLTools.parse(format_str);
642 editor_msgarea.setText(msg);
643
644 if (msg.startsWith(XMLTools.WELLFORMED))
645 {
646 format.setPureFormat(format_str);
647 format.update();
648 format_list_model.refresh(format);
649 editor_msgarea.setBackground(Color.white);
650 FormatPane.setPreviewButton(true);
651 }
652 else
653 {
654 editor_msgarea.setBackground(Color.red);
655 FormatPane.setPreviewButton(false);
656 }
657 }
658 else
659 {
660 add_button.setEnabled(false);
661 }
662 }
663 }
664
665 private class FeatureListener implements ActionListener
666 {
667 public void actionPerformed(ActionEvent event)
668 {
669 editor_textarea.undoButton.setEnabled(false);
670 editor_textarea.redoButton.setEnabled(false);
671 default_button.setEnabled(true);
672 newtext = true;
673
674 if (ignore_event == true)
675 {
676 editor_textarea.discardAllEdits();
677 return;
678 }
679
680 ignore_event = true;
681 // Add is only enabled if there isn't already a format for the choosen feature and part.
682 //Create a dummy format and test if itsa already in the model
683 Entry entry = (Entry) feature_combobox.getSelectedItem();
684 String feature_name = entry.getFeatureName();
685
686 Format4gs3 format = getFormat(format_list_model, feature_name);
687
688 if (format != null)
689 {
690 ///ystem.err.println("There is an existing format!");
691 format_list.setSelectedValue(format, true);
692 Element formatElem = null;
693 try
694 {
695 InputSource is = new InputSource(new ByteArrayInputStream(addSurroundingTags(format.getPureFormat()).getBytes("utf-8")));
696 formatElem = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is).getDocumentElement();
697 }
698 catch (Exception ex)
699 {
700 ex.printStackTrace();
701 }
702
703 if (formatElem != null)
704 {
705 StringBuffer sb = new StringBuffer();
706 XMLTools.xmlNodeToString(sb, formatElem, true, " ", 0);
707 editor_textarea.setText(removeSurroundingTags(sb.toString()));
708 }
709 else
710 {
711 editor_textarea.setText(format.getPureFormat());
712 }
713 editor_textarea.setCaretPosition(0);
714
715 existingFormat(feature_name.startsWith(Classifier.CLASSIFIER_PREFIX));
716 }
717 // Otherwise there is no existing format, then this feature must be a classifier (CL1, 2, ...)
718 // we display the ClassifierDefault for this format
719 else
720 {
721 //Fist reset the format list panel
722 format_list.clearSelection();
723
724 if (feature_name.equals(""))
725 {
726 editor_textarea.setText("");
727
728 }
729 else
730 {
731 //Only for debugging purposes
732 if (entry.getClassifier() == null)
733 {
734 DebugStream.println("It should be a classifier or choose a feature. What is it? " + entry.toString());
735 }
736
737 editor_textarea.setText(Format4gs3.toFormatedFormat(CLASSIFIER_DEFAULT_FORMAT));
738 editor_textarea.setCaretPosition(0);
739 newFormat();
740 }
741 }
742 ignore_event = false;
743 editor_textarea.discardAllEdits();
744 }
745 }
746
747 private class FormatListListener implements ListSelectionListener
748 {
749 public void valueChanged(ListSelectionEvent event)
750 {
751 editor_textarea.undoButton.setEnabled(false);
752 editor_textarea.redoButton.setEnabled(false);
753 default_button.setEnabled(true);
754 newtext = true;
755
756 if (!ignore_event && !event.getValueIsAdjusting())
757 {
758
759 if (!format_list.isSelectionEmpty())
760 {
761 ignore_event = true;
762 Format4gs3 format = (Format4gs3) format_list.getSelectedValue();
763 String feature_name = format.getFeatureName();
764 Entry entry = null;
765 if (feature_name.startsWith(Classifier.CLASSIFIER_PREFIX))
766 {
767 entry = new Entry(format.getClassifier());
768 }
769 else
770 {
771 entry = new Entry(feature_name);
772 }
773 feature_combobox.setSelectedItem(entry);
774
775 existingFormat(format.getFeatureName().startsWith(Classifier.CLASSIFIER_PREFIX));
776
777 Element formatElem = null;
778 try
779 {
780 InputSource is = new InputSource(new ByteArrayInputStream(addSurroundingTags(format.getPureFormat()).getBytes("utf-8")));
781 formatElem = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is).getDocumentElement();
782 }
783 catch (Exception ex)
784 {
785 ex.printStackTrace();
786 }
787
788 if (formatElem != null)
789 {
790 StringBuffer sb = new StringBuffer();
791 XMLTools.xmlNodeToString(sb, formatElem, true, " ", 0);
792 editor_textarea.setText(removeSurroundingTags(sb.toString()));
793 }
794 else
795 {
796 editor_textarea.setText(format.getPureFormat());
797 }
798 editor_textarea.setCaretPosition(0);
799
800 ignore_event = false;
801 }
802
803 }
804 editor_textarea.discardAllEdits();
805 }
806
807 }
808
809 private class RemoveListener implements ActionListener
810 {
811 public void actionPerformed(ActionEvent event)
812 {
813 if (!format_list.isSelectionEmpty())
814 {
815 // Remove the current format
816 Format4gs3 format = (Format4gs3) format_list.getSelectedValue();
817 removeFormat(format_list_model, format);
818 // Change buttons
819 add_button.setEnabled(true);
820 feature_combobox.setSelectedItem(new Entry(""));
821 newFormat();
822 }
823 }
824 }
825
826 private class DefaultListener implements ActionListener
827 {
828 public void actionPerformed(ActionEvent event)
829 {
830 newtext = false;
831 if (!ignore_event)
832 {
833 Entry entry = (Entry) feature_combobox.getSelectedItem();
834 String feature_name = entry.getFeatureName();
835 Format4gs3 format = getFormat(format_list_model, feature_name);
836
837 if (format != null)
838 {
839 if (format.isClassifier() == true)
840 {
841 editor_textarea.setText((String) default_format_formated_map.get(CLASSIFIER_DEFAULT));
842 editor_textarea.setCaretPosition(0);
843 remove_button.setEnabled(true);
844 }
845 else
846 {
847 editor_textarea.setText((String) default_format_formated_map.get(format.getFeatureName()));
848 editor_textarea.setCaretPosition(0);
849 remove_button.setEnabled(false);
850 }
851 }
852 else
853 {
854 editor_textarea.setText((String) default_format_formated_map.get(CLASSIFIER_DEFAULT));
855 editor_textarea.setCaretPosition(0);
856 remove_button.setEnabled(false);
857 add_button.setEnabled(true);
858 }
859 }
860 }
861 }
862
863 private void newFormat()
864 {
865 editor_textarea.setEditable(false);
866 editor_textarea.setBackground(Color.lightGray);
867 editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Editor_Disabled_Tooltip"));
868
869 editor_textarea.undoButton.setEnabled(false);
870 editor_textarea.redoButton.setEnabled(false);
871 add_button.setEnabled(true);
872 remove_button.setEnabled(false);
873 default_button.setEnabled(false);
874 FormatPane.setPreviewButton(true);
875 }
876
877 private void existingFormat(boolean enableRemoveButton)
878 {
879 editor_textarea.setEditable(true);
880 editor_textarea.setBackground(Color.white);
881 editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Editor_Tooltip"));
882 add_button.setEnabled(false);
883 remove_button.setEnabled(enableRemoveButton);
884 default_button.setEnabled(true);
885 FormatPane.setPreviewButton(true);
886 }
887 }
888
889 /**
890 * This object provides a wrapping around an entry in Format4gs3, which is
891 * tranparent.
892 */
893 // This class is used for display in the feature combobox
894 private class Entry implements Comparable
895 {
896 private Classifier classifier = null;
897 private String feature_name = null;
898
899 public Entry(Object object)
900 {
901 if (object instanceof Classifier)
902 {
903 classifier = (Classifier) object;
904 feature_name = classifier.getPositionString();
905 }
906 else if (object instanceof String)
907 {
908 feature_name = (String) object;
909 }
910 else
911 {
912 feature_name = "";
913 }
914 }
915
916 public Entry(String text)
917 {
918 this.feature_name = text;
919 }
920
921 public int compareTo(Object object)
922 {
923 if (object == null)
924 {
925 return 1;
926 }
927 if (toString() == null)
928 {
929 return -1;
930 }
931 else
932 {
933 String object_str = object.toString();
934 if (object_str == null)
935 {
936 return 1;
937 }
938 return toString().compareTo(object_str);
939 }
940 }
941
942 public boolean equals(Object object)
943 {
944 if (compareTo(object) == 0)
945 {
946 return true;
947 }
948 return false;
949 }
950
951 public Classifier getClassifier()
952 {
953 return classifier;
954 }
955
956 public String toString()
957 {
958 if (classifier != null)
959 {
960 // Return the classifier name - with its CL index shown, and all its metadata options as well
961 return classifier.getPositionString() + StaticStrings.SPACE_CHARACTER + classifier.toString();
962 }
963 if (feature_name.equals(""))
964 {
965 return "<html><body><i>" + "Choose a feature" + "</i></body></html>";
966 }
967 return feature_name;
968 }
969
970 public String getFeatureName()
971 {
972 return feature_name;
973 }
974 }
975}
Note: See TracBrowser for help on using the repository browser.