@NavigatorPanel.Registration

@NavigatorPanel.Registration is new in 7.2 and lets you register navigator panels for a MIME type. I.e., create a new module, use the annotation on a JPanel, and then your external module can enrich editor support for the MIME type of your choice, including existing MIME types, such as "text/x-java", for Java files, or "text/x-properties" for Properties files, as shown below.

import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.Lookup.Result;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;

@NavigatorPanel.Registration(mimeType = "text/x-properties", displayName = "Property File Content")
public class PropsNavigatorPanel extends JPanel implements NavigatorPanel, LookupListener {

    JTextArea jta = new JTextArea();

    public PropsNavigatorPanel() {
        setLayout(new BorderLayout());
        JScrollPane pane = new JScrollPane(jta);
        jta.setEditable(false);
        add(pane, BorderLayout.CENTER);
    }

    Result<DataObject> dataobjectsInLookup;

    @Override
    public void panelActivated(Lookup lkp) {
        dataobjectsInLookup = lkp.lookupResult(DataObject.class);
        dataobjectsInLookup.addLookupListener(this);
    }

    @Override
    public void panelDeactivated() {
        dataobjectsInLookup.removeLookupListener(this);
    }

    @Override
    public void resultChanged(LookupEvent le) {
        jta.setText("");
        if (dataobjectsInLookup.allInstances().iterator().hasNext()) {
            DataObject dobj = dataobjectsInLookup.allInstances().iterator().next();
            try {
                for (String line : dobj.getPrimaryFile().asLines()) {
                    if (!line.startsWith("#") && line.length() > 0) {
                        jta.append(line);
                        jta.append("\n");
                    }
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            };
        }
    }

    @Override
    public Lookup getLookup() {
        return Lookup.EMPTY;
    }

 
    @Override
    public String getDisplayName() {
        return "Property File Content";
    }

    @Override
    public String getDisplayHint() {
        return "Property File Content";
    }

    @Override
    public JComponent getComponent() {
        return this;
    }
 
}

Instead of Strings, would be better to have Nodes, which would be clicked to jump into the Properties editor onto the related line.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today