Custom Corporate Menu Bar for NetBeans Platform Applications

http://blogs.oracle.com/geertjan/entry/customizable_corporate_menu_bar_for

————————————————————————————————————————————————————————————————————————

Today let's create a highly customized menu bar in our NetBeans Platform application:

What's special in the above screenshot is that the menu bar has an image, the menu items are set to the right, and the toolbar is gone. That's all. But the effect is quite nice and professional for corporate applications.

Get to the above point as follows:

  1. The key to everything is "netbeans.winsys.menu_bar.path"  which is a NetBeans property pointing to a folder in the central registry where a custom menu bar can optionally be registered. You can set the above via -J-D or via System.setProperty in your Installer:
    public class Installer extends ModuleInstall {

    @Override
    public void restored() {
    System.setProperty("netbeans.winsys.menu_bar.path", "LookAndFeel/MenuBar.instance");
    }

    }

  2. And here is the registered custom menu bar referred to above:
    <folder name="LookAndFeel">
    <file name="MenuBar.instance">
    <attr name="instanceOf" stringvalue="org.openide.awt.MenuBar"/>
    <attr name="instanceCreate" newvalue="org.menu.bar.MyMenuBar"/>
    </file>
    </folder>

  3. So I have a package named "org.menu.bar", where "MyMenuBar" is registered. A lot of the code below comes from this interview with Tonny Kohar. Here's my custom menu bar:
    public class MyMenuBar extends JMenuBar {

    //Load content from Menu folder and populate the custom menubar,
    //refer to the interview with Tonny Kohar:

    public MyMenuBar() {
    //Let the menus appear from the right:
    add(Box.createHorizontalGlue());
    //Now we'll get the "Menu" folder and process its content:
    FileObject menuFolder = FileUtil.getConfigFile("Menu");
    FileObject[] menuKids = menuFolder.getChildren();
    for (FileObject menuKid : FileUtil.getOrder(Arrays.asList(menuKids), true)) {
    JMenu m = new JMenu(menuKid.getName());
    if (m != null) {
    add(m);
    }
    buildPopup(menuKid, m);
    }
    setPreferredSize(new Dimension(50, 50));
    }

    private void buildPopup(FileObject fo, JComponent comp) {

    DataFolder df = DataFolder.findFolder(fo);
    DataObject[] childs = df.getChildren();
    DataObject dob;
    Object instanceObj;
    for (int i = 0; i < childs.length; i++) {
    dob = childs[i];
    if (dob.getPrimaryFile().isFolder()) {
    FileObject childFo = childs[i].getPrimaryFile();
    JMenu menu = new JMenu();
    Mnemonics.setLocalizedText(menu, dob.getNodeDelegate().getDisplayName());
    comp.add(menu);
    buildPopup(childFo, menu);
    } else {
    //Cookie or Lookup API discovery:
    InstanceCookie ck = (InstanceCookie) dob.getCookie(InstanceCookie.class);
    try {
    instanceObj = ck.instanceCreate();
    } catch (Exception ex) {
    instanceObj = null;
    ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
    }
    if (instanceObj == null) {
    continue;
    }
    if (instanceObj instanceof JSeparator) {
    comp.add((JSeparator) instanceObj);
    } else if (instanceObj instanceof BooleanStateAction) {
    JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem();
    Actions.connect(menuItem, (BooleanStateAction) instanceObj, true);
    } else if (instanceObj instanceof Action) {
    final Action a = (Action) instanceObj;
    String name = (String) a.getValue(Action.NAME);
    String cutAmpersand = Actions.cutAmpersand(name);
    JMenuItem menuItem = new JMenuItem();
    menuItem.setAction(new AbstractAction(cutAmpersand) {
    @Override
    public void actionPerformed(ActionEvent e) {
    a.actionPerformed(e);
    }
    });
    //Should be like this, but somehow didn't work in this case:
    //Actions.connect(menuItem, (Action) instanceObj, false);
    comp.add(menuItem);
    }
    }
    }

    }

    //Paint the banner image into the menu bar:

    private final Paint bannerPaint = makeBannerPaint();

    @Override
    protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(bannerPaint);
    g2.fillRect(0, 0, getWidth(), getHeight());
    }

    private Paint makeBannerPaint() {
    //Pointing to an image in org/menu/bar:
    BufferedImage img = (BufferedImage) ImageUtilities.loadImage("org/menu/bar/banner.jpg");
    return new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight()));
    }

    }

  4. Finally, in the layer, hide the toolbar:
    <file name="Toolbars_hidden"/>

That's it, now you have a nice corporate menu bar.

 

原文地址:https://www.cnblogs.com/cuizhf/p/2189945.html