KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
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 /**
37  * Preview design action.
38  *
39  * @author Tomas Pavek, Jan Stola
40  */

41 public class TestAction extends CallableSystemAction implements Runnable JavaDoc {
42
43     private static String JavaDoc name;
44
45     public TestAction() {
46         setEnabled(false);
47     }
48
49     protected boolean asynchronous() {
50         return false;
51     }
52
53     /** Human presentable name of the action. This should be
54      * presented as an item in a menu.
55      * @return the name of the action
56      */

57     public String JavaDoc getName() {
58         if (name == null)
59             name = org.openide.util.NbBundle.getBundle(TestAction.class)
60                      .getString("ACT_TestMode"); // NOI18N
61
return name;
62     }
63
64     /** Help context where to find more about the action.
65      * @return the help context for this action
66      */

67     public HelpCtx getHelpCtx() {
68         return new HelpCtx("gui.testing"); // NOI18N
69
}
70
71     /** @return resource for the action icon */
72     protected String JavaDoc iconResource() {
73         return "org/netbeans/modules/form/resources/test_form.png"; // NOI18N
74
}
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         // a JFrame or Frame will be used (depending on form is Swing or AWT)
103
Object JavaDoc formInstance = topComp.getBeanInstance();
104         Class JavaDoc 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             // create a copy of form
119
final Frame frame = (Frame)
120                 FormDesigner.createFormView(topComp, frameClass, selectedLaf);
121
122             // set title
123
String JavaDoc title = frame.getTitle();
124             if (title == null || "".equals(title)) { // NOI18N
125
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"), // NOI18N
130
new Object JavaDoc[] { title }
131                 ));
132             }
133
134             // prepare close operation
135
if (frame instanceof JFrame) {
136                 ((JFrame)frame).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
137                 HelpCtx.setHelpIDString(((JFrame)frame).getRootPane(),
138                                         "gui.modes"); // NOI18N
139
}
140             else {
141                 frame.addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
142                     public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
143                         frame.dispose();
144                     }
145                 });
146             }
147  
148             // set size
149
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             // Issue 66594 and 12084
160
final boolean pack = shouldPack;
161             EventQueue.invokeLater(new Runnable JavaDoc() {
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 JavaDoc 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     // -------
188

189     private FormDesigner formDesigner;
190
191     public void setFormDesigner(FormDesigner designer) {
192         formDesigner = designer;
193         setEnabled(formDesigner != null && formDesigner.getTopDesignComponent() != null);
194     }
195     
196     // LAFMenu
197

198     private Class JavaDoc selectedLaf;
199     
200     private class LAFMenu extends JMenu implements ActionListener JavaDoc {
201         private boolean initialized = false;
202
203         private LAFMenu(String JavaDoc 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                 // Swing L&Fs
214
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())); // NOI18N
218
mi.addActionListener(this);
219                     popup.add(mi);
220                 }
221
222                 // L&Fs from the Palette
223
Node[] cats = PaletteUtils.getCategoryNodes(PaletteUtils.getPaletteNode(), false);
224                 for (int i=0; i<cats.length; i++) {
225                     if ("LookAndFeels".equals(cats[i].getName())) { // NOI18N
226
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 JavaDoc 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 JavaDoc ex) {
241                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
242                             } catch (LinkageError JavaDoc 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)); // NOI18N
248
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 JavaDoc e) {
261             Object JavaDoc o = e.getSource();
262             if (o instanceof JComponent) {
263                 JComponent source = (JComponent)o;
264                 LookAndFeelItem item = (LookAndFeelItem)source.getClientProperty("lafInfo"); // NOI18N
265
try {
266                     selectedLaf = item.getLAFClass();
267                     run();
268                 } catch (Exception JavaDoc ex) {
269                     ErrorManager.getDefault().notify(ex);
270                 }
271             }
272         }
273
274     }
275
276     /**
277      * Information about one look and feel.
278      */

279     static class LookAndFeelItem {
280         /** Name of the look and feel's class. */
281         private String JavaDoc className;
282         /** The corresponding PaletteItem, if exists. */
283         private PaletteItem pitem;
284
285         public LookAndFeelItem(String JavaDoc 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 JavaDoc getClassName() {
295             return className;
296         }
297
298         public Class JavaDoc getLAFClass() throws ClassNotFoundException JavaDoc {
299             Class JavaDoc 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 JavaDoc 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