1 19 20 package org.netbeans.modules.form.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import javax.swing.*; 25 import java.awt.*; 26 27 import org.openide.ErrorManager; 28 import org.openide.util.HelpCtx; 29 import org.openide.nodes.*; 30 import org.openide.util.actions.*; 31 32 import org.netbeans.modules.form.*; 33 import org.netbeans.modules.form.palette.PaletteItem; 34 import org.netbeans.modules.form.palette.PaletteUtils; 35 36 41 public class TestAction extends CallableSystemAction implements Runnable { 42 43 private static String name; 44 45 public TestAction() { 46 setEnabled(false); 47 } 48 49 protected boolean asynchronous() { 50 return false; 51 } 52 53 57 public String getName() { 58 if (name == null) 59 name = org.openide.util.NbBundle.getBundle(TestAction.class) 60 .getString("ACT_TestMode"); return name; 62 } 63 64 67 public HelpCtx getHelpCtx() { 68 return new HelpCtx("gui.testing"); } 70 71 72 protected String iconResource() { 73 return "org/netbeans/modules/form/resources/test_form.png"; } 75 76 public void performAction() { 77 if (formDesigner != null) { 78 selectedLaf = null; 79 if (java.awt.EventQueue.isDispatchThread()) 80 run(); 81 else 82 java.awt.EventQueue.invokeLater(this); 83 } 84 } 85 86 public void run() { 87 RADVisualComponent topComp = formDesigner.getTopDesignComponent(); 88 if (topComp == null) 89 return; 90 91 RADVisualComponent parent = topComp.getParentContainer(); 92 while (parent != null) { 93 topComp = parent; 94 parent = topComp.getParentContainer(); 95 } 96 97 FormModel formModel = formDesigner.getFormModel(); 98 RADVisualFormContainer formContainer = 99 topComp instanceof RADVisualFormContainer ? 100 (RADVisualFormContainer) topComp : null; 101 102 Object formInstance = topComp.getBeanInstance(); 104 Class frameClass = formInstance instanceof JComponent 105 || formInstance instanceof JFrame 106 || formInstance instanceof JDialog 107 || formInstance instanceof JApplet 108 || formInstance instanceof JWindow 109 || (!(formInstance instanceof Window) 110 && !(formInstance instanceof Panel)) ? 111 JFrame.class : Frame.class; 112 113 try { 114 if (selectedLaf == null) { 115 selectedLaf = UIManager.getLookAndFeel().getClass(); 116 } 117 118 final Frame frame = (Frame) 120 FormDesigner.createFormView(topComp, frameClass, selectedLaf); 121 122 String title = frame.getTitle(); 124 if (title == null || "".equals(title)) { title = topComp == formModel.getTopRADComponent() ? 126 formModel.getName() : topComp.getName(); 127 frame.setTitle(java.text.MessageFormat.format( 128 org.openide.util.NbBundle.getBundle(TestAction.class) 129 .getString("FMT_TestingForm"), new Object [] { title } 131 )); 132 } 133 134 if (frame instanceof JFrame) { 136 ((JFrame)frame).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 137 HelpCtx.setHelpIDString(((JFrame)frame).getRootPane(), 138 "gui.modes"); } 140 else { 141 frame.addWindowListener(new java.awt.event.WindowAdapter () { 142 public void windowClosing(java.awt.event.WindowEvent evt) { 143 frame.dispose(); 144 } 145 }); 146 } 147 148 boolean shouldPack = false; 150 if (formContainer != null 151 && formContainer.getFormSizePolicy() 152 == RADVisualFormContainer.GEN_BOUNDS 153 && formContainer.getGenerateSize()) 154 { 155 frame.setSize(formContainer.getFormSize()); 156 } 157 else shouldPack = true; 158 159 final boolean pack = shouldPack; 161 EventQueue.invokeLater(new Runnable () { 162 public void run() { 163 if (pack) { 164 frame.pack(); 165 } 166 frame.setBounds(org.openide.util.Utilities.findCenterBounds(frame.getSize())); 167 frame.setVisible(true); 168 } 169 }); 170 } 171 catch (Exception ex) { 172 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 173 } 174 } 175 176 public JMenuItem getMenuPresenter() { 177 return getPopupPresenter(); 178 } 179 180 public JMenuItem getPopupPresenter() { 181 JMenu layoutMenu = new LAFMenu(getName()); 182 layoutMenu.setEnabled(isEnabled()); 183 HelpCtx.setHelpIDString(layoutMenu, SelectLayoutAction.class.getName()); 184 return layoutMenu; 185 } 186 187 189 private FormDesigner formDesigner; 190 191 public void setFormDesigner(FormDesigner designer) { 192 formDesigner = designer; 193 setEnabled(formDesigner != null && formDesigner.getTopDesignComponent() != null); 194 } 195 196 198 private Class selectedLaf; 199 200 private class LAFMenu extends JMenu implements ActionListener { 201 private boolean initialized = false; 202 203 private LAFMenu(String name) { 204 super(name); 205 } 206 207 public JPopupMenu getPopupMenu() { 208 JPopupMenu popup = super.getPopupMenu(); 209 JMenuItem mi; 210 if (!initialized) { 211 popup.removeAll(); 212 213 UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels(); 215 for (int i=0; i<lafs.length; i++) { 216 mi = new JMenuItem(lafs[i].getName()); 217 mi.putClientProperty("lafInfo", new LookAndFeelItem(lafs[i].getClassName())); mi.addActionListener(this); 219 popup.add(mi); 220 } 221 222 Node[] cats = PaletteUtils.getCategoryNodes(PaletteUtils.getPaletteNode(), false); 224 for (int i=0; i<cats.length; i++) { 225 if ("LookAndFeels".equals(cats[i].getName())) { final Node lafNode = cats[i]; 227 Node[] items = PaletteUtils.getItemNodes(lafNode, true); 228 if (items.length != 0) { 229 popup.add(new JSeparator()); 230 } 231 for (int j=0; j<items.length; j++) { 232 PaletteItem pitem = (PaletteItem)items[j].getLookup().lookup(PaletteItem.class); 233 boolean supported = false; 234 try { 235 Class clazz = pitem.getComponentClass(); 236 if ((clazz != null) && (LookAndFeel.class.isAssignableFrom(clazz))) { 237 LookAndFeel laf = (LookAndFeel)clazz.newInstance(); 238 supported = laf.isSupportedLookAndFeel(); 239 } 240 } catch (Exception ex) { 241 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 242 } catch (LinkageError ex) { 243 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 244 } 245 if (supported) { 246 mi = new JMenuItem(items[j].getDisplayName()); 247 mi.putClientProperty("lafInfo", new LookAndFeelItem(pitem)); mi.addActionListener(this); 249 popup.add(mi); 250 } 251 } 252 } 253 } 254 255 initialized = true; 256 } 257 return popup; 258 } 259 260 public void actionPerformed(ActionEvent e) { 261 Object o = e.getSource(); 262 if (o instanceof JComponent) { 263 JComponent source = (JComponent)o; 264 LookAndFeelItem item = (LookAndFeelItem)source.getClientProperty("lafInfo"); try { 266 selectedLaf = item.getLAFClass(); 267 run(); 268 } catch (Exception ex) { 269 ErrorManager.getDefault().notify(ex); 270 } 271 } 272 } 273 274 } 275 276 279 static class LookAndFeelItem { 280 281 private String className; 282 283 private PaletteItem pitem; 284 285 public LookAndFeelItem(String className) { 286 this.className = className; 287 } 288 289 public LookAndFeelItem(PaletteItem pitem) { 290 this.pitem = pitem; 291 this.className = pitem.getComponentClassName(); 292 } 293 294 public String getClassName() { 295 return className; 296 } 297 298 public Class getLAFClass() throws ClassNotFoundException { 299 Class clazz; 300 if (pitem == null) { 301 if (className == null) { 302 clazz = UIManager.getLookAndFeel().getClass(); 303 } else { 304 clazz = Class.forName(className); 305 } 306 } else { 307 clazz = pitem.getComponentClass(); 308 } 309 return clazz; 310 } 311 312 public boolean equals(Object obj) { 313 if (!(obj instanceof LookAndFeelItem)) return false; 314 LookAndFeelItem item = (LookAndFeelItem)obj; 315 return (pitem == item.pitem) && ((pitem != null) 316 || ((className == null) ? (item.className == null) : className.equals(item.className))); 317 } 318 319 public int hashCode() { 320 return (className == null) ? pitem.hashCode() : className.hashCode(); 321 } 322 323 } 324 325 } 326 | Popular Tags |