KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > actions > SetResizabilityAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.actions;
21
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import javax.swing.*;
27 import javax.swing.event.MenuListener JavaDoc;
28 import javax.swing.event.MenuEvent JavaDoc;
29
30 import org.openide.util.HelpCtx;
31 import org.openide.util.actions.*;
32 import org.openide.nodes.Node;
33 import org.openide.util.NbBundle;
34
35 import org.netbeans.modules.form.*;
36
37 /**
38  * Action class providing popup menu presenter for setresizability submenu.
39  *
40  * @author Martin Grebac
41  */

42
43 public class SetResizabilityAction extends NodeAction {
44
45     private JCheckBoxMenuItem[] items;
46     
47     protected boolean enable(Node[] nodes) {
48         List JavaDoc comps = FormUtils.getSelectedLayoutComponents(nodes);
49         return ((comps != null) && (comps.size() > 0));
50     }
51     
52     public String JavaDoc getName() {
53         return NbBundle.getMessage(SetResizabilityAction.class, "ACT_SetResizability"); // NOI18N
54
}
55
56     public HelpCtx getHelpCtx() {
57         return HelpCtx.DEFAULT_HELP;
58     }
59
60     protected void performAction(Node[] activatedNodes) { }
61
62     public JMenuItem getMenuPresenter() {
63         return getPopupPresenter();
64     }
65
66     /**
67      * Returns a JMenuItem that presents this action in a Popup Menu.
68      * @return the JMenuItem representation for the action
69      */

70     public JMenuItem getPopupPresenter() {
71         JMenu popupMenu = new JMenu(
72             NbBundle.getMessage(SetResizabilityAction.class, "ACT_SetResizability")); // NOI18N
73

74         popupMenu.setEnabled(isEnabled());
75         HelpCtx.setHelpIDString(popupMenu, SetResizabilityAction.class.getName());
76         
77         popupMenu.addMenuListener(new MenuListener JavaDoc() {
78             public void menuSelected(MenuEvent JavaDoc e) {
79                 JMenu menu = (JMenu) e.getSource();
80                 createResizabilitySubmenu(menu);
81             }
82             
83             public void menuDeselected(MenuEvent JavaDoc e) {}
84             
85             public void menuCanceled(MenuEvent JavaDoc e) {}
86         });
87         return popupMenu;
88     }
89
90     private void createResizabilitySubmenu(JMenu menu) {
91         Node[] nodes = getActivatedNodes();
92         List JavaDoc components = FormUtils.getSelectedLayoutComponents(nodes);
93         if ((components == null) || (components.size() < 1)) {
94             return;
95         }
96         if (!(menu.getMenuComponentCount() > 0)) {
97             ResourceBundle JavaDoc bundle = NbBundle.getBundle(SetResizabilityAction.class);
98
99             JCheckBoxMenuItem hItem = new ResizabilityMenuItem(
100                     bundle.getString("CTL_ResizabilityH"), // NOI18N
101
components,
102                     0);
103             JCheckBoxMenuItem vItem = new ResizabilityMenuItem(
104                     bundle.getString("CTL_ResizabilityV"), // NOI18N
105
components,
106                     1);
107             items = new JCheckBoxMenuItem[] {hItem, vItem};
108             
109             for (int i=0; i<2; i++) {
110                 items[i].addActionListener(getMenuItemListener());
111                 HelpCtx.setHelpIDString(items[i], SetResizabilityAction.class.getName());
112                 menu.add(items[i]);
113             }
114         }
115         updateState(components);
116     }
117
118     private void updateState(List JavaDoc components) {
119         if ((components == null) || (components.size()<1)) {
120             return;
121         }
122         RADComponent rc = (RADComponent)components.get(0);
123         FormDesigner formDesigner = FormEditor.getFormDesigner(rc.getFormModel());
124         formDesigner.updateResizabilityActions();
125         for (int i=0; i<2; i++) {
126             items[i].setEnabled(formDesigner.getResizabilityButtons()[i].isEnabled());
127             items[i].setSelected(formDesigner.getResizabilityButtons()[i].isSelected());
128         }
129     }
130     
131     private ActionListener JavaDoc getMenuItemListener() {
132         if (menuItemListener == null)
133             menuItemListener = new ResizabilityMenuItemListener();
134         return menuItemListener;
135     }
136
137     // --------
138

139     private static class ResizabilityMenuItem extends JCheckBoxMenuItem {
140         private int direction;
141         private List JavaDoc components;
142
143         ResizabilityMenuItem(String JavaDoc text, List JavaDoc components, int direction) {
144             super(text);
145             this.components = components;
146             this.direction = direction;
147         }
148         
149         int getDirection() {
150             return direction;
151         }
152
153         List JavaDoc getRADComponents() {
154             return components;
155         }
156     }
157
158     private static class ResizabilityMenuItemListener implements ActionListener JavaDoc {
159         public void actionPerformed(ActionEvent JavaDoc evt) {
160             Object JavaDoc source = evt.getSource();
161             if (!(source instanceof ResizabilityMenuItem)) {
162                 return;
163             }
164             ResizabilityMenuItem mi = (ResizabilityMenuItem) source;
165             if (!mi.isEnabled()) {
166                 return;
167             }
168             int index = mi.getDirection();
169             RADComponent radC = (RADComponent)mi.getRADComponents().get(0);
170             FormModel fm = radC.getFormModel();
171             FormDesigner fd = FormEditor.getFormDesigner(fm);
172             fd.getResizabilityButtons()[index].setSelected(!fd.getResizabilityButtons()[index].isSelected());
173             ((Action)fd.getResizabilityActions().toArray()[index]).actionPerformed(evt);
174         }
175     }
176         
177     private ActionListener JavaDoc menuItemListener;
178 }
179
Popular Tags