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

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

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

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