1 19 20 package org.netbeans.modules.form.actions; 21 22 import java.util.ArrayList ; 23 import java.awt.Component ; 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 41 42 public class SelectLayoutAction extends CallableSystemAction { 43 44 private static String name; 45 46 50 public String getName() { 51 if (name == null) 52 name = org.openide.util.NbBundle.getBundle(SelectLayoutAction.class) 53 .getString("ACT_SelectLayout"); return name; 55 } 56 57 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 94 private static Node[] getNodes() { 95 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 layoutsList = new ArrayList (); 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 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")); 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 text = menuItem.getText(); 176 for (int i = 2; i < n; i++) { if(menu.getComponent(i) instanceof JMenuItem){ 178 String 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 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 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 layoutUndoMark = layoutModel.getChangeMark(); 220 javax.swing.undo.UndoableEdit 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 idToComponent = new java.util.HashMap (); 227 FormDesigner formDesigner = FormEditor.getFormDesigner(formModel); 228 for (int j=0; j<components.length; j++) { 229 Component comp = (Component )formDesigner.getComponent(components[j]); 230 if (comp == null) comp = (Component )components[j].getBeanInstance(); idToComponent.put(components[j].getId(), comp); 232 } 233 layoutModel.createModel(container.getId(), (java.awt.Container )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 |