Customize document tabs in NetBeans

There is a new API in upcoming NetBeans 8.0 which allows customization of document tabs. The API uses alternate implementation of document tabs which is backed up by a plain JTable component. Each table cell is a single document tab. The API lets you plug in your custom decorators to change the default rendering of document tabs.

For example being a Java developer it is expected that you mostly work with Java source files. In that case it doesn't make much sense to waste space on the default .java file extension and having the 'Java' icon in each document tab isn't helping much either. So let's create a new TabDecorator to remove the file extension and icon from all Java sources:

@ServiceProvider(service = TabDecorator.class, position = 100)
public class NoJavaIconDecorator extends TabDecorator {

    @Override
    public Icon getIcon(TabData tab) {
        String displayName = tab.getText();
        displayName = stripHtml( displayName );
        if( displayName.endsWith(".java") )
            return NO_ICON;
        return super.getIcon(tab);
    }

    @Override
    public String getText(TabData tab) {
        String displayName = tab.getText();
        displayName = stripHtml( displayName );
        if( displayName.endsWith(".java") ) {
            displayName = tab.getText().replace(displayName, displayName.substring(0, displayName.length()-".java".length()));
            return displayName;
        }
        return super.getText(tab);
    }
    
    private static String stripHtml( String text ) {
        if( null == text )
            return text;
        text = text.replaceAll( "<[^>]*>", "" ); //NOI18N
        text = text.replaceAll( " ", " " ); //NOI18N
        return text;
    }
    
    private static final Icon NO_ICON = new Icon() {  

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
        }

        @Override
        public int getIconWidth() {
            return 1;
        }

        @Override
        public int getIconHeight() {
            return 1;
        }
    };
}

Note: You can even plug in your own implementation of the whole document tabs component - just provide implementations of TabDisplayer and TabDisplayerFactory classes. It is somewhat easier than starting with Tabbed class as the Multi-tabs module does a lot of work for you already.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Oracle Blogs Admin-Oracle

Search

Top Tags
Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today