CoolEditorActions V1.0.5 released

I’ve been updated my CoolEditorActions NetBeans Plugin which is now compatible with NetBeans 7.1.

Other improvements:

  • The Button’s Popup Menu is lazy loaded
  • Improved Open & Select Project action
  • Added Close Project action
  • minor bug fixes

The Plugin can be downloaded from here:

Categories: Java, NetBeans Tags: , , ,

New plugin CoolEditorActions released

Today I released the first version of the CoolEditorActions NetBeans plugin. This plugin adds a drop down button to the editor’s toolbar. From the popup menu, some useful actions are available for all the file path objects:

The plugin can be downloaded from the Kenai Project site. Feel free to write me some feedback!

I will submit this plugin in the next few days also to the NetBeans Plugin Portal.

Categories: Java, NetBeans Tags: , , ,

Dynamically show/hide individual toolbars

In NetBeans you can define your own toolbar configurations (a set of visible/invisible toolbars). DevFaqHideShowToolbar shows you how you can activate a specific toolbar configuration at runtime. But how can I show an individual toolbar dynamically? For example, to show/hide certain toolbar(s) by the visibility of a TopComponent.

An explicit Toolbar API isn’t available in the NetBeans Platform. The class org.openide.awt.ToolbarPool is used to install and update the configured toolbars. With the constructor public ToolbarPool(DataFolder df) an own toolbar configuration at the specified location in the layer.xml (DataFolder) can be created.

But I don’t want create a new toolbar configuration for each TopComponent who should have their own toolbars. The idea is:

  1. Declare the toolbar for the TopComponent at a certain location (!= Toolbars/) in the module’s layer.xml
  2. Register the toolbar in the active toolbar configuration
  3. show the toolbar on TopComponent#componentOpened()
  4. hide the toolbar on TopComponent#componentClosed()

All these steps should be performed dynamically at runtime. How can I achieve this? Let’s go ahead…

Declare the toolbar

<!-- file: layer.xml -->
<filesystem>
   ...
   <folder name="MyModule">
      <folder name="Toolbars">
         <folder name="MyToolbar">
            <attr name="displayName" stringvalue="My Toolbar" />
            <file name="my-action-1.instance">
               ...
            </file>
         </folder>
      </folder>
   </folder>
</filesystem>

Register the toolbar in the active toolbar configuration
Using a shadow entry any custom toolbar is registered once in the default toolbar pool (at Toolbars/):

public static boolean registerToolbar(String toolbarConfigPath) {
   try {
      FileObject fo = FileUtil.getConfigFile(toolbarConfigPath);
      if (fo == null) {
         return false;
      }
      DataFolder df = DataFolder.findFolder(fo);
      DataFolder target = ToolbarPool.getDefault().getFolder();
      FileObject targetFO = target.getPrimaryFile().getFileObject(fo.getNameExt() + ".shadow");

      if (df != null && targetFO == null) {
         DataShadow ds = df.createShadow(target);
         return true;
      }
   } catch (IOException ex) {
      Exceptions.printStackTrace(ex);
   }
   return false;
}

Show/hide the toolbar
For programatically hide and show a toolbar you can write:

Toolbar toolbar = ToolbarPool.getDefault().findToolbar("MyToolbar");
toolbar.getParent().setVisible(false);

The drawback is that the visible state of the toolbar will not be propagated to the toolbar context menu actions.

After debugging I found this solution (or hack):

public static void setToolbarVisible(Toolbar toolbar, boolean visible) {
   try {
      ClassLoader cl = Lookup.getDefault().lookup(ClassLoader.class);
      Class cToolbarConfiguration = cl.loadClass("org.netbeans.core.windows.view.ui.toolbars.ToolbarConfiguration");
      // invoke static ToolbarConfiguration.findConfiguration( String name)
      Object toolbarConfig = cToolbarConfiguration.getMethod("findConfiguration", String.class).
              invoke(cToolbarConfiguration, "Standard");
      // invoke ToolbarConfiguration#setToolbarVisible( Toolbar tb, boolean visible)
      toolbarConfig.getClass().getMethod("setToolbarVisible", Toolbar.class, boolean.class).invoke(toolbarConfig, toolbar, visible);
   } catch (Exception ex) {
      throw new IllegalArgumentException(ex);
   }
}

Link the toolbar together with the TopComponent’s visibility state (simplified):

String toolbarName = "MyToolbar";

public final void componentOpened() {
   ToolbarUtil.registerToolbar("MyModule/Toolbars/" + toolbarName);
   ToolbarUtil.setToolbarVisible(toolbarName, true);
}

public final void componentClosed() {
  ToolbarUtil.setToolbarVisible(toolbarName, false);
}
Categories: Java, NB Platform, NetBeans Tags: ,

StatusLine in menu bar

With a simple NetBeans startup parameter the StatusLine can be placed in the menu bar: add the property

netbeans.winsys.statusLine.in.menuBar=true

to the NetBeans configuration file installRoot/etc/netbeans.conf:

# Options used by NetBeans launcher by default, can be overridden by explicit
# command line switches:
netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-Xmx1500m -J-XX:PermSize=32m -J-XX:MaxPermSize=400m -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true -J-Dnetbeans.winsys.statusLine.in.menuBar=true"

Don’t forget to add -J-D before the property name!

See also FaqNetbeansConf

Categories: NetBeans Tags:

Simple Validation API & DialogDescriptor

This post shows you how the Simple Validation API (HowTo, Project@Kenai) can be usedĀ together with a NetBeans Platform standard dialog (using a DialogDescriptor).

The Simple Validation API gives you a convenience method to show a dialog like (excerpt from the BasicDemo):

JPanel panel = new JPanel();
... // setup panel

ValidationPanel vp = new ValidationPanel();
vp.setInnerComponent(panel);
... // setup validation

// Convenience method to show a simple dialog
if (vp.showOkCancelDialog("URL")) {
   System.exit(0);
} else {
   System.exit(1);
}

To use a standard NetBeans Platform dialog, the sample above can be replaced with:

... // as above
DialogDescriptor dd = ValidationUtil.createDialogDescriptor(vp, panel, "URL");
Object ret = DialogDisplayer.getDefault().notify(dd);
if (DialogDescriptor.OK_OPTION.equals(ret)) {
   System.exit(0);
} else {
   System.exit(1);
}
public class ValidationUtil {

   private ValidationUtil() {
      // default constructor suppressed for non-instantiability
   }

   public static DialogDescriptor createDialogDescriptor(ValidationPanel vp, Object innerPane, String title) {
      final DialogDescriptor dd = new DialogDescriptor(innerPane, title);

      ValidationUI okButtonEnabler = new ValidationUI() {
         private NotificationLineSupport nls = dd.createNotificationLineSupport();

         public void showProblem(Problem problem) {
            if (problem != null) {
               switch (problem.severity()) {
                  case FATAL:
                     nls.setErrorMessage(problem.getMessage());
                     dd.setValid(false);
                     break;
                  case WARNING:
                     nls.setWarningMessage(problem.getMessage());
                     dd.setValid(true);
                     break;
                  default:
                     nls.setInformationMessage(problem.getMessage());
                     dd.setValid(true);
               }
            } else {
               nls.clearMessages();
               dd.setValid(true);
            }
         }

         public void clearProblem() {
            showProblem(null);
         }
      };

      vp.getValidationGroup().addUI(okButtonEnabler);
      vp.getValidationGroup().performValidation();
      return dd;
   }
}

Welcome

Welcome to my Java Blog. Here I will post some hopefully interesting articles about Java in general and NetBeans Platform related topics. The focus will be on the NetBeans Platform.

Categories: Article

Custom Toolbar Configuration

2010-08-19 6 comments

In my first NetBeans Platform post I show you how you can change the default toolbar configuration of your NetBeans Platform application:

  • Change the default toolbar icon size to 16px
  • Right aligned toolbar
  • Non-floating toolbar
  • Create your own set of Toolbar configuration

After reading this article about general toolbar configurations and some investigation in the NetBeans Platform source code I found the solution for the items above. The improved DTD for toolbar configuration (version 1.1) introduced with NetBeans V6.7 allows you to use the new attributesĀ ”align” and “dragable”, e.g.:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configuration PUBLIC "-//NetBeans IDE//DTD toolbar 1.1//EN"
 "http://www.netbeans.org/dtds/toolbar1_1.dtd">
<Configuration>
   <Row>
      <Toolbar name="File" visible="true" />
      <Toolbar name="Clipboard" visible="false"/>
      <Toolbar name="UndoRedo" visible="false"/>
      <Toolbar name="Memory" visible="true" draggable="false" align="right"/>
      <Toolbar name="Picture" visible="true" draggable="false" align="right"/>
   </Row>
</Configuration>

Overwrite the default toolbar configuration in the layer.xml:

<!-- http://wiki.netbeans.org/DevFaqHideShowToolbar -->
<!-- http://blogs.sun.com/geertjan/entry/toolbar_configurations -->
<folder name="Toolbars">
   <file name="MyToolbars.xml" url="res/MyToolbars.xml">
      <attr name="position" intvalue="100"/>
   </file>
</folder>

A NetBeans Platform application comes with the two toolbar sets:

  • Default
  • Debugging

In the example configuration above I created a new one named “MyToolbars”.

Now I want “MyToolbars” as my default toolbar configuration. Additionally I need to have 16px icons as default.
To achieve this, write a custom Window Manager Properties file:

<!-- File: res/windowManager.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE windowmanager PUBLIC
          "-//NetBeans//DTD Window Manager Properties 2.1//EN"
          "http://www.netbeans.org/dtds/windowmanager-properties2_1.dtd">

<windowmanager version="2.1">
   <main-window> 
      <joined-properties centered-horizontally="true" centered-vertically="true"
                           relative-width="0.9" relative-height="0.9" />
      <separated-properties centered-horizontally="true" relative-y="0.1"
                           relative-width="0.8" relative-height="0.08" />
   </main-window>
   <editor-area state="joined">
      <constraints>
         <path orientation="horizontal" number="60" weight="0.7" />
         <path orientation="vertical" number="40" weight="0.7" />
         <path orientation="horizontal" number="40" weight="0.7" />
      </constraints>
      <relative-bounds x="33" y="24" width="42" height="44"/>
   </editor-area>
   <screen width="1280" height="1024" />
   <active-mode name="explorer" />
   <maximized-mode name="" />
   <toolbar configuration="MyToolbars" preferred-icon-size="16" />
</windowmanager>

… and registered in the layer.xml:

<folder name="Windows2">          
   <file name="WindowManager.wswmgr" url="res/windowManager.xml" />
</folder>
Categories: NB Platform Tags: ,
Follow

Get every new post delivered to your Inbox.