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

Last change on this file since 31566 was 31566, checked in by kjdon, 7 years ago

modified VList format statement to match default in code

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