Search in sources :

Example 1 with PopupTrigger

use of com.ramussoft.gui.common.PopupTrigger in project ramus by Vitaliy-Yakovchuk.

the class GUIPluginFactory method addActions.

private void addActions(String id, final View view, final DefaultCDockable dockable, Action[] actions) {
    for (final Action action : actions) if (action == null) {
        dockable.addSeparator();
    } else {
        ImageIcon icon = (ImageIcon) action.getValue(Action.SMALL_ICON);
        if (icon == null) {
            icon = new ImageIcon(getClass().getResource("/com/ramussoft/gui/icon.png"));
        }
        final String command = (String) action.getValue(Action.ACTION_COMMAND_KEY);
        String text = null;
        if (view instanceof TabView) {
            text = ((TabView) view).getString(command);
        }
        if (text == null)
            text = findPluginForViewId(id).getString(command);
        if (text == null) {
            StringGetter getter = (StringGetter) action.getValue(StringGetter.ACTION_STRING_GETTER);
            if (getter != null)
                text = getter.getString(command);
        }
        final CDecorateableAction button;
        if (action.getValue(Action.SELECTED_KEY) == null) {
            final PopupTrigger trigger = (PopupTrigger) action.getValue(POPUP_MENU);
            if (trigger == null)
                button = new CButton(text, icon) {

                    @Override
                    protected void action() {
                        java.awt.event.ActionEvent e = new java.awt.event.ActionEvent(view, 0, command);
                        action.actionPerformed(e);
                    }
                };
            else {
                button = new CPanelPopup() {

                    @Override
                    public void openPopup(PanelPopupWindow window) {
                        super.openPopup(window);
                    }

                    @Override
                    protected void openDialog(JComponent item, Orientation orientation) {
                        JPanel menu = (JPanel) getContent();
                        menu.removeAll();
                        for (Action action : trigger.getPopupActions()) menu.add(new JButton(action));
                        menu.revalidate();
                        menu.repaint();
                        super.openDialog(item, orientation);
                    }
                };
                ((CPanelPopup) button).setContent(new JPanel(new GridLayout(0, 1, 0, 0)));
                button.setText(text);
                button.setIcon(icon);
            }
        } else {
            button = new CCheckBox(text, icon) {

                @Override
                protected void changed() {
                    java.awt.event.ActionEvent e = new java.awt.event.ActionEvent(view, 0, command);
                    action.putValue(Action.SELECTED_KEY, isSelected());
                    action.actionPerformed(e);
                }
            };
            action.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (Action.SELECTED_KEY.equals(evt.getPropertyName())) {
                        ((CCheckBox) button).setSelected((Boolean) evt.getNewValue());
                    }
                }
            });
            ((CCheckBox) button).setSelected((Boolean) action.getValue(Action.SELECTED_KEY));
        }
        String tooltip = (String) action.getValue(Action.LONG_DESCRIPTION);
        if (tooltip == null)
            tooltip = (String) action.getValue(Action.SHORT_DESCRIPTION);
        if (tooltip == null)
            tooltip = text;
        if (tooltip != null)
            button.setTooltip(tooltip);
        KeyStroke stroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
        if (stroke != null)
            button.setAccelerator(stroke);
        actionButtons.put(action, button);
        button.setEnabled(action.isEnabled());
        dockable.addAction(button);
        action.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("enabled")) {
                    button.setEnabled((Boolean) evt.getNewValue());
                }
            }
        });
    }
}
Also used : ImageIcon(javax.swing.ImageIcon) JPanel(javax.swing.JPanel) AbstractAction(javax.swing.AbstractAction) CDecorateableAction(bibliothek.gui.dock.common.intern.action.CDecorateableAction) Action(javax.swing.Action) PropertyChangeListener(java.beans.PropertyChangeListener) PanelPopupWindow(bibliothek.gui.dock.common.action.panel.PanelPopupWindow) ActionEvent(com.ramussoft.gui.common.event.ActionEvent) JButton(javax.swing.JButton) StringGetter(com.ramussoft.gui.common.StringGetter) CPanelPopup(bibliothek.gui.dock.common.action.CPanelPopup) GridLayout(java.awt.GridLayout) CCheckBox(bibliothek.gui.dock.common.action.CCheckBox) CDecorateableAction(bibliothek.gui.dock.common.intern.action.CDecorateableAction) CButton(bibliothek.gui.dock.common.action.CButton) PropertyChangeEvent(java.beans.PropertyChangeEvent) TabView(com.ramussoft.gui.common.TabView) PopupTrigger(com.ramussoft.gui.common.PopupTrigger) JComponent(javax.swing.JComponent) Orientation(bibliothek.gui.dock.title.DockTitle.Orientation) KeyStroke(javax.swing.KeyStroke)

Aggregations

CButton (bibliothek.gui.dock.common.action.CButton)1 CCheckBox (bibliothek.gui.dock.common.action.CCheckBox)1 CPanelPopup (bibliothek.gui.dock.common.action.CPanelPopup)1 PanelPopupWindow (bibliothek.gui.dock.common.action.panel.PanelPopupWindow)1 CDecorateableAction (bibliothek.gui.dock.common.intern.action.CDecorateableAction)1 Orientation (bibliothek.gui.dock.title.DockTitle.Orientation)1 PopupTrigger (com.ramussoft.gui.common.PopupTrigger)1 StringGetter (com.ramussoft.gui.common.StringGetter)1 TabView (com.ramussoft.gui.common.TabView)1 ActionEvent (com.ramussoft.gui.common.event.ActionEvent)1 GridLayout (java.awt.GridLayout)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 ImageIcon (javax.swing.ImageIcon)1 JButton (javax.swing.JButton)1 JComponent (javax.swing.JComponent)1 JPanel (javax.swing.JPanel)1 KeyStroke (javax.swing.KeyStroke)1