KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.event.*;
25 import javax.swing.*;
26 import org.netbeans.modules.form.layoutdesign.LayoutModel;
27 import org.netbeans.modules.form.layoutsupport.LayoutSupportManager;
28 import org.netbeans.modules.form.palette.PaletteUtils;
29
30 import org.openide.util.HelpCtx;
31 import org.openide.util.NbBundle;
32 import org.openide.util.actions.CallableSystemAction;
33 import org.openide.nodes.Node;
34 import org.netbeans.modules.form.palette.PaletteItem;
35 import org.netbeans.modules.form.*;
36
37 /**
38  * Action for setting layout on selected container(s). Presented only in
39  * contextual menus within the Form Editor.
40  */

41
42 public class SelectLayoutAction extends CallableSystemAction {
43
44     private static String JavaDoc name;
45
46      /** Human presentable name of the action. This should be
47      * presented as an item in a menu.
48      * @return the name of the action
49      */

50     public String JavaDoc getName() {
51         if (name == null)
52             name = org.openide.util.NbBundle.getBundle(SelectLayoutAction.class)
53                      .getString("ACT_SelectLayout"); // NOI18N
54
return name;
55     }
56
57     /** Help context where to find more about the action.
58      * @return the help context for this action
59      */

60     public HelpCtx getHelpCtx() {
61         return HelpCtx.DEFAULT_HELP;
62     }
63
64     public boolean isEnabled() {
65         Node[] nodes = getNodes();
66         for (int i=0; i < nodes.length; i++) {
67             RADVisualContainer container = getContainer(nodes[i]);
68             if (container == null || container.hasDedicatedLayoutSupport())
69                 return false;
70         }
71         return true;
72     }
73
74     public JMenuItem getMenuPresenter() {
75         return getPopupPresenter();
76     }
77
78     public JMenuItem getPopupPresenter() {
79         JMenu layoutMenu = new LayoutMenu(getName());
80         layoutMenu.setEnabled(isEnabled());
81         HelpCtx.setHelpIDString(layoutMenu, SelectLayoutAction.class.getName());
82         return layoutMenu;
83     }
84
85     protected boolean asynchronous() {
86         return false;
87     }
88
89     public void performAction() {
90     }
91
92     // -------
93

94     private static Node[] getNodes() {
95         // using NodeAction and global activated nodes is not reliable
96
// (activated nodes are set with a delay after selection in
97
// ComponentInspector)
98
return ComponentInspector.getInstance().getExplorerManager().getSelectedNodes();
99     }
100
101     private static RADVisualContainer getContainer(Node node) {
102         RADComponentCookie radCookie = (RADComponentCookie)
103             node.getCookie(RADComponentCookie.class);
104         if (radCookie != null) {
105             RADComponent metacomp = radCookie.getRADComponent();
106             if (metacomp instanceof RADVisualContainer)
107                 return (RADVisualContainer) metacomp;
108         }
109         return null;
110     }
111
112     private static PaletteItem[] getAllLayouts() {
113         PaletteItem[] allItems = PaletteUtils.getAllItems();
114         ArrayList JavaDoc layoutsList = new ArrayList JavaDoc();
115         for (int i = 0; i < allItems.length; i++) {
116             if (allItems[i].isLayout()) {
117                 layoutsList.add(allItems[i]);
118             }
119         }
120
121         PaletteItem[] layouts = new PaletteItem[layoutsList.size()];
122         layoutsList.toArray(layouts);
123         return layouts;
124     }
125
126     private static class LayoutMenu extends org.openide.awt.JMenuPlus {
127         private boolean initialized = false;
128
129         private LayoutMenu(String JavaDoc name) {
130             super(name);
131         }
132
133         public JPopupMenu getPopupMenu() {
134             JPopupMenu popup = super.getPopupMenu();
135             Node[] nodes = getNodes();
136
137             if (nodes.length != 0 && !initialized) {
138                 popup.removeAll();
139
140                 JMenuItem mi = new JMenuItem(NbBundle.getMessage(SelectLayoutAction.class, "NAME_FreeDesign")); // NOI18N
141
popup.add(mi);
142                 mi.addActionListener(new LayoutActionListener(null));
143                 popup.addSeparator();
144
145                 RADVisualContainer container = getContainer(nodes[0]);
146                 boolean hasFreeDesignSupport = RADVisualContainer.isFreeDesignContainer(container);
147                 if(hasFreeDesignSupport){
148                     setBoldFontForMenuText(mi);
149                 }
150                 
151                 PaletteItem[] layouts = getAllLayouts();
152                 for (int i = 0; i < layouts.length; i++) {
153                     mi = new JMenuItem(layouts[i].getNode().getDisplayName());
154                     HelpCtx.setHelpIDString(mi, SelectLayoutAction.class.getName());
155                     addSortedMenuItem(popup, mi);
156                     mi.addActionListener(new LayoutActionListener(layouts[i]));
157                     if(!hasFreeDesignSupport && isContainersLayout(container, layouts[i])){
158                         setBoldFontForMenuText(mi);
159                     }
160                 }
161                 initialized = true;
162             }
163             return popup;
164         }
165         
166         private boolean isContainersLayout(RADVisualContainer container, PaletteItem layout){
167             return container != null
168                    && (container.getLayoutSupport().getLayoutDelegate().getSupportedClass() == layout.getComponentClass()
169                        || (container.getLayoutSupport().getLayoutDelegate().getClass() == org.netbeans.modules.form.layoutsupport.delegates.NullLayoutSupport.class &&
170                            layout.getComponentClass() == org.netbeans.modules.form.layoutsupport.delegates.NullLayoutSupport.class));
171         }
172
173         private static void addSortedMenuItem(JPopupMenu menu, JMenuItem menuItem) {
174             int n = menu.getComponentCount();
175             String JavaDoc text = menuItem.getText();
176             for (int i = 2; i < n; i++) { // 2 -> Free Design Item & Separator shouldn't be sorted
177
if(menu.getComponent(i) instanceof JMenuItem){
178                     String JavaDoc tx = ((JMenuItem)menu.getComponent(i)).getText();
179                     if (text.compareTo(tx) < 0) {
180                         menu.add(menuItem, i);
181                         return;
182                     }
183                 }
184             }
185             menu.add(menuItem);
186         }
187
188         private static void setBoldFontForMenuText(JMenuItem mi) {
189             java.awt.Font JavaDoc font = mi.getFont();
190             mi.setFont(font.deriveFont(font.getStyle() | java.awt.Font.BOLD));
191         }
192     
193     }
194
195     private static class LayoutActionListener implements ActionListener {
196         private PaletteItem paletteItem;
197
198         LayoutActionListener(PaletteItem paletteItem) {
199             this.paletteItem = paletteItem;
200         }
201
202         public void actionPerformed(ActionEvent evt) {
203             Node[] nodes = getNodes();
204             for (int i = 0; i < nodes.length; i++) {
205                 RADVisualContainer container = getContainer(nodes[i]);
206                 if (container == null)
207                     continue;
208
209                 FormModel formModel = container.getFormModel();
210                 if (paletteItem != null) {
211                     // set the selected layout on the container
212
formModel.getComponentCreator().createComponent(
213                         paletteItem.getComponentClassSource(), container, null);
214                 }
215                 else {
216                     LayoutSupportManager currentLS = container.getLayoutSupport();
217                     boolean convertToNew = (currentLS != null) && (currentLS.getLayoutDelegate() != null);
218                     LayoutModel layoutModel = formModel.getLayoutModel();
219                     Object JavaDoc layoutUndoMark = layoutModel.getChangeMark();
220                     javax.swing.undo.UndoableEdit JavaDoc ue = layoutModel.getUndoableEdit();
221                     boolean autoUndo = true;
222                     try {
223                         formModel.setNaturalContainerLayout(container);
224                         if (convertToNew) {
225                             RADVisualComponent[] components = container.getSubComponents();
226                             java.util.Map JavaDoc idToComponent = new java.util.HashMap JavaDoc();
227                             FormDesigner formDesigner = FormEditor.getFormDesigner(formModel);
228                             for (int j=0; j<components.length; j++) {
229                                 Component JavaDoc comp = (Component JavaDoc)formDesigner.getComponent(components[j]);
230                                 if (comp == null) comp = (Component JavaDoc)components[j].getBeanInstance(); // Issue 65919
231
idToComponent.put(components[j].getId(), comp);
232                             }
233                             layoutModel.createModel(container.getId(), (java.awt.Container JavaDoc)formDesigner.getComponent(container), idToComponent);
234                         }
235                         autoUndo = false;
236                     } finally {
237                         if (!layoutUndoMark.equals(layoutModel.getChangeMark())) {
238                             formModel.addUndoableEdit(ue);
239                         }
240                         if (autoUndo) {
241                             formModel.forceUndoOfCompoundEdit();
242                         }
243                     }
244                     FormEditor.getFormEditor(formModel).updateProjectForNaturalLayout();
245                 }
246             }
247         }
248     }
249 }
250
Popular Tags