Example #1
0
  public XmlTextPane() {
    XMLEditorKit kit = new XMLEditorKit(true, this);

    kit.setLineWrappingEnabled(false);

    kit.setStyle(XMLStyleConstants.ELEMENT_NAME, new Color(128, 0, 0), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.ELEMENT_VALUE, new Color(0, 0, 0), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.ELEMENT_PREFIX, new Color(128, 0, 0), Font.PLAIN);

    kit.setStyle(XMLStyleConstants.ATTRIBUTE_NAME, new Color(255, 0, 0), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.ATTRIBUTE_VALUE, new Color(0, 0, 0), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.ATTRIBUTE_PREFIX, new Color(128, 0, 0), Font.PLAIN);

    kit.setStyle(XMLStyleConstants.NAMESPACE_NAME, new Color(102, 102, 102), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.NAMESPACE_VALUE, new Color(0, 51, 51), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.NAMESPACE_PREFIX, new Color(102, 102, 102), Font.PLAIN);

    kit.setStyle(XMLStyleConstants.ENTITY, new Color(0, 0, 0), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.COMMENT, new Color(153, 153, 153), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.CDATA, new Color(0, 0, 0), Font.PLAIN);
    kit.setStyle(XMLStyleConstants.SPECIAL, new Color(0, 0, 0), Font.PLAIN);

    this.setEditorKit(kit);

    this.setFont(new Font("Monospaced", Font.PLAIN, 12));

    this.registerKeyboardAction(
        shiftTabAction,
        KeyStroke.getKeyStroke(KeyEvent.VK_TAB, ActionEvent.SHIFT_MASK),
        JComponent.WHEN_FOCUSED);
    this.registerKeyboardAction(
        deleteLineAction,
        KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK),
        JComponent.WHEN_FOCUSED);
    ActionListener escAction =
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            clearMarkerLine();
            clearErrorLine();
          }
        };
    this.registerKeyboardAction(
        escAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED);
    this.getActionMap().put(tabAction.getValue(Action.NAME), tabAction);
  }
Example #2
0
  public NcmlEditor(JPanel buttPanel, PreferencesExt prefs) {
    this.prefs = prefs;
    fileChooser = new FileManager(null, null, null, (PreferencesExt) prefs.node("FileManager"));

    AbstractAction coordAction =
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            addCoords = (Boolean) getValue(BAMutil.STATE);
            String tooltip = addCoords ? "add Coordinates is ON" : "add Coordinates is OFF";
            coordButt.setToolTipText(tooltip);
          }
        };
    addCoords = prefs.getBoolean("coordState", false);
    String tooltip2 = addCoords ? "add Coordinates is ON" : "add Coordinates is OFF";
    BAMutil.setActionProperties(coordAction, "addCoords", tooltip2, true, 'C', -1);
    coordAction.putValue(BAMutil.STATE, Boolean.valueOf(addCoords));
    coordButt = BAMutil.addActionToContainer(buttPanel, coordAction);

    protoChooser = new ComboBox((PreferencesExt) prefs.node("protoChooser"));
    addProtoChoices();
    buttPanel.add(protoChooser);
    protoChooser.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String ptype = (String) protoChooser.getSelectedItem();
            String proto = protoMap.get(ptype);
            if (proto != null) {
              editor.setText(proto);
            }
          }
        });

    editor = new JEditorPane();

    // Instantiate a XMLEditorKit with wrapping enabled.
    XMLEditorKit kit = new XMLEditorKit(false);

    // Set the wrapping style.
    kit.setWrapStyleWord(true);

    editor.setEditorKit(kit);

    // Set the font style.
    editor.setFont(new Font("Monospaced", Font.PLAIN, 12));

    // Set the tab size
    editor.getDocument().putProperty(PlainDocument.tabSizeAttribute, 2);

    // Enable auto indentation.
    editor.getDocument().putProperty(XMLDocument.AUTO_INDENTATION_ATTRIBUTE, true);

    // Enable tag completion.
    editor.getDocument().putProperty(XMLDocument.TAG_COMPLETION_ATTRIBUTE, true);

    // Initialise the folding
    kit.setFolding(true);

    // Set a style
    kit.setStyle(XMLStyleConstants.ATTRIBUTE_NAME, Color.RED, Font.BOLD);

    // Put the editor in a panel that will force it to resize, when a different view is choosen.
    ScrollableEditorPanel editorPanel = new ScrollableEditorPanel(editor);

    JScrollPane scroller = new JScrollPane(editorPanel);

    // Add the number margin as a Row Header View
    scroller.setRowHeaderView(new LineNumberMargin(editor));

    AbstractAction wrapAction =
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            XMLEditorKit kit = (XMLEditorKit) editor.getEditorKit();
            kit.setLineWrappingEnabled(!kit.isLineWrapping());
            editor.updateUI();
          }
        };
    BAMutil.setActionProperties(wrapAction, "Wrap", "Toggle Wrapping", false, 'W', -1);
    BAMutil.addActionToContainer(buttPanel, wrapAction);

    AbstractAction saveAction =
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            String location = (ds == null) ? ncmlLocation : ds.getLocation();
            if (location == null) location = "test";
            int pos = location.lastIndexOf(".");
            if (pos > 0) location = location.substring(0, pos);
            String filename = fileChooser.chooseFilenameToSave(location + ".ncml");
            if (filename == null) return;
            if (doSaveNcml(editor.getText(), filename)) ncmlLocation = filename;
          }
        };
    BAMutil.setActionProperties(saveAction, "Save", "Save NcML", false, 'S', -1);
    BAMutil.addActionToContainer(buttPanel, saveAction);

    AbstractAction netcdfAction =
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            if (outChooser == null) {
              outChooser = new NetcdfOutputChooser((Frame) null);
              outChooser.addPropertyChangeListener(
                  "OK",
                  new PropertyChangeListener() {
                    public void propertyChange(PropertyChangeEvent evt) {
                      writeNetcdf((NetcdfOutputChooser.Data) evt.getNewValue());
                    }
                  });
            }

            String location = (ds == null) ? ncmlLocation : ds.getLocation();
            if (location == null) location = "test";
            int pos = location.lastIndexOf(".");
            if (pos > 0) location = location.substring(0, pos);

            outChooser.setOutputFilename(location);
            outChooser.setVisible(true);
          }
        };
    BAMutil.setActionProperties(netcdfAction, "netcdf", "Write netCDF file", false, 'N', -1);
    BAMutil.addActionToContainer(buttPanel, netcdfAction);

    AbstractAction transAction =
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            doTransform(editor.getText());
          }
        };
    BAMutil.setActionProperties(
        transAction,
        "Import",
        "read textArea through NcMLReader\n write NcML back out via resulting dataset",
        false,
        'T',
        -1);
    BAMutil.addActionToContainer(buttPanel, transAction);

    AbstractButton compareButton = BAMutil.makeButtcon("Select", "Check NcML", false);
    compareButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Formatter f = new Formatter();
            checkNcml(f);

            infoTA.setText(f.toString());
            infoTA.gotoTop();
            infoWindow.show();
          }
        });
    buttPanel.add(compareButton);

    setLayout(new BorderLayout());
    add(scroller, BorderLayout.CENTER);

    // the info window
    infoTA = new TextHistoryPane();
    infoWindow = new IndependentWindow("Extra Information", BAMutil.getImage("netcdfUI"), infoTA);
    infoWindow.setBounds(
        (Rectangle) prefs.getBean("InfoWindowBounds", new Rectangle(300, 300, 500, 300)));
  }