zoukankan      html  css  js  c++  java
  • Presenter.Toolbar Meets @Action Annotations

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

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

    Let's say you have a JPanel that you'd like to integrate into the NetBeans Platform toolbar. As an example, we imagine that this is the JPanel:

    public class NextPrevJPanel extends JPanel {

        JButton nextButton = new JButton();
        JButton prevButton = new JButton();

        public NextPrevJPanel() {
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            prevButton.setIcon(new ImageIcon(getClass().getResource("prev.png")));
            nextButton.setIcon(new ImageIcon(getClass().getResource("next.png")));
            prevButton.addActionListener(new PrevButtonActionListener());
            nextButton.addActionListener(new NextButtonActionListener());
            add(prevButton);
            add(nextButton);
        }

        private class NextButtonActionListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Simulate next action:
                JOptionPane.showMessageDialog(null, "Next");
            }
        }

        private class PrevButtonActionListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Simulate previous action:
                JOptionPane.showMessageDialog(null, "Previous");
            }
        }
        
    }

     As you can see, we have a panel that provides navigation buttons. The standard NetBeans Platform approach to integrating the above panel into the toolbar would be to have you create, essentially, a dummy action. The action implements "Presenter.Toolbar" and returns the JPanel, which represents the dummy action in the toolbar:

    @ActionID(category = "NextPrev", id = "org.demo.DummyAction")
    @ActionRegistration(displayName = "")
    @ActionReferences({
        @ActionReference(path = "Toolbars/File", position = 100)
    })
    public final class DummyAction extends AbstractAction implements Presenter.Toolbar {

        @Override
        public Component getToolbarPresenter() {
            return new NextPrevJPanel();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // Nothing to do here.
        }
        
    }

    It works, but it's not very nice. Aside from the fact that you have a meaningless "actionPerformed" in the code above, you also have a whole meaningless class, since it only exists to register the panel into the toolbar.

    So, with NetBeans Platform 7.0, we can simply delete the above class. And then we rewrite our JPanel as follows:

    public class NextPrevJPanel extends JPanel {

        @ActionID(category = "NextPrev", id = "org.demo.NextAction")
        @ActionRegistration(displayName = "", iconBase = "org/demo/next.png")
        @ActionReference(path = "Toolbars/NextPrev", position = 100)
        public static class NextButtonActionListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Simulate next action:
                JOptionPane.showMessageDialog(null, "Next");
            }
        }

        @ActionID(category = "NextPrev", id = "org.demo.PrevAction")
        @ActionRegistration(displayName = "", iconBase = "org/demo/prev.png")
        @ActionReference(path = "Toolbars/NextPrev", position = 200)
        public static class PrevButtonActionListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Simulate previous action:
                JOptionPane.showMessageDialog(null, "Previous");
            }
        }
        
    }

    Isn't that great? Action annotations on inner classes. It means you don't have to create a separate class file for each of your ActionListeners, which would be another approach to solving this problem.

    In the code above, note that all the stuff about constructing/decorating/adding the JButtons is now gone. All we're left with is the actual logic in our JPanel. And we register that via the new @Action annotations into the toolbar, which means that the NetBeans Platform will create buttons for us. The NetBeans Platform is responsible for everything except the logic, which is our domain-level code anyway, while all the rest is just standard stuff that the NetBeans Platform will handle for us. Another very nice side effect is that the small icon/large icon switching works (thanks to our "iconBase" attribute), which is not the case when you use Presenter.Toolbar to put your panel into the toolbar. Only not-so-nice thing is that our inner classes are now static, since under the hood the action performed calls in inner classes are reached statically from the layer.

    Finally, does this mean that Presenter.Toolbar (and friends) are now obsolete? I don't think so. For example, if you have a Google Toolbar, i.e., a panel containing a label and a text field, for example, you still need to put it into the toolbar somehow. In that particular scenario, you don't want the NetBeans Platform to create buttons for you and hence you will not use the approach outlined here.

  • 相关阅读:
    smobiler介绍(二)
    smobiler介绍(一)
    iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
    iOS 力学动画生成器UIKit Dynamics 之碰撞效果讲解
    iOS 解决tableView中headerView头部视图不跟随tableView滑动的方法
    iOS 当使用FD_FullscreenPopViewController的时候遇到scrollView右滑手势无法使用的解决
    iOS 点击左上角系统返回按钮方法
    解决右滑返回手势和UIScrollView中的手势冲突
    Git 常见的命令操作
    iOS Class 使用NSProxy和NSObject设计代理类的差异
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2189590.html
Copyright © 2011-2022 走看看