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

Last change on this file since 36272 was 36272, checked in by anupama, 22 months ago

Finally tracked down where the commented-out GS3 format statements are coming from. Not gs2build/collect's modelcol, but hardcoded into gli code. Fixing a missing div, which posed a problem for me when I tried uncommenting and editing the format stmt that lacked it.

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