X

Geertjan's Blog

  • October 26, 2011

org.netbeans.editor.SideBarFactory (Part 2)

Geertjan Wielenga
Product Manager
I created a button that's displayed at the top of all Java documents, containing the name of the current class, and when you click on it the document opens in an external text editor:

Here's how:

package org.sb;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.editor.SideBarFactory;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
public class HelloPanel extends JPanel {
    public HelloPanel(JTextComponent target) {
        setLayout(new FlowLayout(FlowLayout.LEFT, 14, 0));
        setBorder(new EmptyBorder(2,2,2,2));
        JButton button = new JButton();
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setBorderPainted(false);
        button.setOpaque(true);
        add(button);
        JavaSource js = JavaSource.forDocument(target.getDocument());
        if (js != null) {
            final FileObject fo = js.getFileObjects().iterator().next();
            button.setText(fo.getNameExt());
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        Desktop.getDesktop().open(FileUtil.toFile(fo));
                    } catch (Exception f) {
                    }
                }
            });
        }
    }
    public static final class NavigationSideBarFactory implements SideBarFactory {
        @Override
        public JComponent createSideBar(JTextComponent target) {
            return new HelloPanel(target);
        }
    }
}

Registered like this:

<folder name="Editors">
    <folder name="text">
        <folder name="x-java">
            <folder name="SideBar">
                <file name="org-sb-HelloPanel$NavigationSideBarFactory.instance">
                    <attr name="position" intvalue="1300"/>
                    <attr name="location" stringvalue="North"/>
                    <attr name="scrollable" boolvalue="false"/>
                </file>
            </folder>
        </folder>
    </folder>
</folder>

Now have a look at part 1, where there's something else you can do with the same API.

Join the discussion

Comments ( 2 )
  • Sanghai Swapnil Wednesday, October 26, 2011

    Hi i need more clarification on the follwing points:

    import org.netbeans.api.java.source.JavaSource;

    import org.netbeans.editor.SideBarFactory;

    import org.openide.filesystems.FileObject;

    import org.openide.filesystems.FileUtil;

    JavaSource, NavigationSideBarFactory.


  • Tom Wheeler Wednesday, October 26, 2011

    I wasn't familiar with the new SideBarFactory API, so it's interesting to see this example. It's pretty useful to modify a file using an external editor (for example, to edit protected sections in .java files NetBeans' own editor won't let you change).

    But in case someone is just seeking a plugin for that, it's not necessary to write one from scratch. Sandip Chitale's Path Tools module offers this and other helpful features. It seems that Sandip only maintains the Eclipse version now, but you can find a version that works in NetBeans 7.0.1 as an attachment to this issue:

    http://netbeans.org/bugzilla/show_bug.cgi?id=153728


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.