Getting Started Extending VisualVM (Part 5)

Update on 30 May 2008: The code and steps in this blog entry have been updated to reflect the VisualVM 1.0 APIs. If you want to download the complete sources of the sample discussed here, get the VisualVM Sample Collection from the Plugin Portal.

Until now, we've only dealt with applications in the context of VisualVM. But applications are only one source of data for VisualVM. There are a few others, one of which is "Host". That is a class that provides the common interface for data sources that represent a localhost or remote host. In the same way as you can extend the Application view, by adding new tabs similar to the "Overview" tab and the "Monitor" tab, you can also extend the Host view. Here you see a new tab in the Host view, called "Hello World":

And here is all the code. First, here's the factory class that will create our tab in the Host view:

import com.sun.tools.visualvm.core.ui.DataSourceView;
import com.sun.tools.visualvm.core.ui.DataSourceViewProvider;
import com.sun.tools.visualvm.core.ui.DataSourceViewsManager;
import com.sun.tools.visualvm.host.Host;

public class DemoViewProvider extends DataSourceViewProvider<Host> {

    private static DataSourceViewProvider instance = new DemoViewProvider();

    @Override
    public boolean supportsViewFor(Host host) {
        //Always shown:
        return true;
    }

    @Override
    public synchronized DataSourceView createView(final Host host) {
        return new DemoView(host);
    }

    static void initialize() {
        DataSourceViewsManager.sharedInstance().addViewProvider(instance, Host.class);
    }

    static void unregister() {
        DataSourceViewsManager.sharedInstance().removeViewProvider(instance);
    }

}

Take note of the fact that above we refer to the "Host.class", instead of the "Application.class", as was done previously. Also, we are implementing DataSourceViewProvider<Host>, instead of DataSourceViewProvider<Application>. The view that is created is the same as before, except that the Host class is received instead of the Application class:

import com.sun.tools.visualvm.core.ui.DataSourceView;
import com.sun.tools.visualvm.core.ui.components.DataViewComponent;
import com.sun.tools.visualvm.host.Host;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import org.openide.util.Utilities;

public class DemoView extends DataSourceView {

    private DataViewComponent dvc;
    private static final String IMAGE_PATH = 
                "com/sun/tools/visualvm/coredump/resources/coredump.png"; // NOI18N

    public DemoView(Host host) {
        super(host, "Hello World", 
                new ImageIcon(Utilities.loadImage(IMAGE_PATH, true)).
                getImage(), 60, false);
    }

    @Override
    protected DataViewComponent createComponent() {

        //Data area for master view:
        JEditorPane generalDataArea = new JEditorPane();
        generalDataArea.setBorder(BorderFactory.createEmptyBorder(14, 8, 14, 8));

        //Panel, which we'll reuse in all four of our detail views for this sample:
        JPanel panel = new JPanel();

        //Master view:
        DataViewComponent.MasterView masterView = new 
                DataViewComponent.MasterView("Demo Overview", null, generalDataArea);

        //Configuration of master view:
        DataViewComponent.MasterViewConfiguration masterConfiguration =
                new DataViewComponent.MasterViewConfiguration(false);

        //Add the master view and configuration view to the component:
        dvc = new DataViewComponent(masterView, masterConfiguration);

        //Add detail views to the component:
        dvc.addDetailsView(new DataViewComponent.DetailsView(
                "Demo Details", null, 10, panel, null), DataViewComponent.TOP_LEFT);

        return dvc;

    }

}

Finally, initialize the factory, in an Installer class, created and registered via a wizard in the IDE:

public class Installer extends ModuleInstall {

    @Override
    public void restored() {
        DemoViewProvider.initialize();
    }

    @Override
    public void uninstalled() {
        DemoViewProvider.unregister();
    }

}

And that's how you can extend the Host view in VisualVM. The same principle would apply to creating new views for other data sources too.

Update on 30 May 2008: The code and steps in this blog entry have been updated to reflect the VisualVM 1.0 APIs. If you want to download the complete sources of the sample discussed here, get the VisualVM Sample Collection from the Plugin Portal.

Comments:

Hi Geertjan

I wanted to install the VisualVM plugins in NetBeans 6.5, but the versions currently in the Plugin Portal say
"Some plugins require plugin TAX Library to be installed.
The plugin TAX Library is requested in implementation version 4."

I can't find any reference to the TAX library in the plugin library, etc. Google indicates this might be an XML library but I can't find any downloads for it.

TIA
Patrick

Posted by guest on January 30, 2009 at 09:44 PM PST #

I get the same TAX library issue while trying to install ApplicationType module. I'm using Netbeans 6.7.1 and I'm planning to upgrade to 6.8 to see if its solved.

Does anyone know what the issue is?

Thanks.

Posted by guest on December 19, 2009 at 04:59 PM PST #

The sample was created for NetBeans IDE 6.1, the instructions clearly state that...

Posted by Geertjan on December 19, 2009 at 07:20 PM PST #

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
24
25
26
27
28
29
30
   
       
Today