001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.actions;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import javax.swing.*;
047: import java.awt.*;
048:
049: import org.openide.ErrorManager;
050: import org.openide.util.HelpCtx;
051: import org.openide.nodes.*;
052: import org.openide.util.actions.*;
053:
054: import org.netbeans.modules.form.*;
055: import org.netbeans.modules.form.palette.PaletteItem;
056: import org.netbeans.modules.form.palette.PaletteUtils;
057: import org.netbeans.modules.form.project.ClassPathUtils;
058: import org.openide.filesystems.FileObject;
059:
060: /**
061: * Preview design action.
062: *
063: * @author Tomas Pavek, Jan Stola
064: */
065: public class TestAction extends CallableSystemAction implements
066: Runnable {
067:
068: private static String name;
069:
070: public TestAction() {
071: setEnabled(false);
072: }
073:
074: @Override
075: protected boolean asynchronous() {
076: return false;
077: }
078:
079: /** Human presentable name of the action. This should be
080: * presented as an item in a menu.
081: * @return the name of the action
082: */
083: public String getName() {
084: if (name == null)
085: name = org.openide.util.NbBundle
086: .getBundle(TestAction.class).getString(
087: "ACT_TestMode"); // NOI18N
088: return name;
089: }
090:
091: /** Help context where to find more about the action.
092: * @return the help context for this action
093: */
094: public HelpCtx getHelpCtx() {
095: return new HelpCtx("gui.testing"); // NOI18N
096: }
097:
098: /** @return resource for the action icon */
099: @Override
100: protected String iconResource() {
101: return "org/netbeans/modules/form/resources/test_form.png"; // NOI18N
102: }
103:
104: public void performAction() {
105: if (formDesigner != null) {
106: selectedLaf = null;
107: if (java.awt.EventQueue.isDispatchThread())
108: run();
109: else
110: java.awt.EventQueue.invokeLater(this );
111: }
112: }
113:
114: public void run() {
115: RADVisualComponent topComp = formDesigner
116: .getTopDesignComponent();
117: if (topComp == null)
118: return;
119:
120: RADVisualComponent parent = topComp.getParentContainer();
121: while (parent != null) {
122: topComp = parent;
123: parent = topComp.getParentContainer();
124: }
125:
126: FormModel formModel = formDesigner.getFormModel();
127: RADVisualFormContainer formContainer = topComp instanceof RADVisualFormContainer ? (RADVisualFormContainer) topComp
128: : null;
129:
130: try {
131: if (selectedLaf == null) {
132: selectedLaf = UIManager.getLookAndFeel().getClass();
133: }
134:
135: // create a copy of form
136: FileObject formFile = FormEditor.getFormDataObject(
137: formModel).getFormFile();
138: final ClassLoader classLoader = ClassPathUtils
139: .getProjectClassLoader(formFile);
140: final FormLAF.PreviewInfo previewInfo = FormLAF
141: .initPreviewLaf(selectedLaf, classLoader);
142: final Frame frame = (Frame) FormDesigner.createFormView(
143: topComp, previewInfo);
144:
145: // set title
146: String title = frame.getTitle();
147: if (title == null || "".equals(title)) { // NOI18N
148: title = topComp == formModel.getTopRADComponent() ? formModel
149: .getName()
150: : topComp.getName();
151: frame.setTitle(java.text.MessageFormat.format(
152: org.openide.util.NbBundle.getBundle(
153: TestAction.class).getString(
154: "FMT_TestingForm"), // NOI18N
155: new Object[] { title }));
156: }
157:
158: // prepare close operation
159: if (frame instanceof JFrame) {
160: ((JFrame) frame)
161: .setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
162: HelpCtx.setHelpIDString(((JFrame) frame).getRootPane(),
163: "gui.modes"); // NOI18N
164: } else {
165: frame
166: .addWindowListener(new java.awt.event.WindowAdapter() {
167: @Override
168: public void windowClosing(
169: java.awt.event.WindowEvent evt) {
170: frame.dispose();
171: }
172: });
173: }
174:
175: // set size
176: boolean shouldPack = false;
177: if (formContainer != null
178: && formContainer.getFormSizePolicy() == RADVisualFormContainer.GEN_BOUNDS
179: && formContainer.getGenerateSize()) {
180: Dimension size = formContainer.getFormSize();
181: if (frame.isUndecorated()) { // will be shown as decorated anyway
182: Dimension diffSize = RADVisualFormContainer
183: .getDecoratedWindowContentDimensionDiff();
184: size = new Dimension(size.width + diffSize.width,
185: size.height + diffSize.height);
186: }
187: frame.setSize(size);
188: } else {
189: shouldPack = true;
190: }
191: frame.setUndecorated(false);
192: frame.setFocusableWindowState(true);
193:
194: // Issue 66594 and 12084
195: final boolean pack = shouldPack;
196: EventQueue.invokeLater(new Runnable() {
197: public void run() {
198: if (pack) {
199: try {
200: FormLAF.setUsePreviewDefaults(classLoader,
201: previewInfo);
202: frame.pack();
203: } finally {
204: FormLAF.setUsePreviewDefaults(null, null);
205: }
206: }
207: frame.setBounds(org.openide.util.Utilities
208: .findCenterBounds(frame.getSize()));
209: frame.setVisible(true);
210: }
211: });
212: } catch (Exception ex) {
213: org.openide.ErrorManager.getDefault().notify(
214: org.openide.ErrorManager.INFORMATIONAL, ex);
215: }
216: }
217:
218: @Override
219: public JMenuItem getMenuPresenter() {
220: return getPopupPresenter();
221: }
222:
223: @Override
224: public JMenuItem getPopupPresenter() {
225: JMenu layoutMenu = new LAFMenu(getName());
226: layoutMenu.setEnabled(isEnabled());
227: HelpCtx.setHelpIDString(layoutMenu, SelectLayoutAction.class
228: .getName());
229: return layoutMenu;
230: }
231:
232: // -------
233:
234: private FormDesigner formDesigner;
235:
236: public void setFormDesigner(FormDesigner designer) {
237: formDesigner = designer;
238: setEnabled(formDesigner != null
239: && formDesigner.getTopDesignComponent() != null);
240: }
241:
242: // LAFMenu
243:
244: private Class selectedLaf;
245:
246: private class LAFMenu extends JMenu implements ActionListener {
247: private boolean initialized = false;
248:
249: private LAFMenu(String name) {
250: super (name);
251: }
252:
253: @Override
254: public JPopupMenu getPopupMenu() {
255: JPopupMenu popup = super .getPopupMenu();
256: JMenuItem mi;
257: if (!initialized) {
258: popup.removeAll();
259:
260: // Swing L&Fs
261: UIManager.LookAndFeelInfo[] lafs = UIManager
262: .getInstalledLookAndFeels();
263: for (int i = 0; i < lafs.length; i++) {
264: mi = new JMenuItem(lafs[i].getName());
265: mi
266: .putClientProperty("lafInfo",
267: new LookAndFeelItem(lafs[i]
268: .getClassName())); // NOI18N
269: mi.addActionListener(this );
270: popup.add(mi);
271: }
272:
273: // L&Fs from the Palette
274: Node[] cats = PaletteUtils.getCategoryNodes(
275: PaletteUtils.getPaletteNode(), false);
276: for (int i = 0; i < cats.length; i++) {
277: if ("LookAndFeels".equals(cats[i].getName())) { // NOI18N
278: final Node lafNode = cats[i];
279: Node[] items = PaletteUtils.getItemNodes(
280: lafNode, true);
281: if (items.length != 0) {
282: popup.add(new JSeparator());
283: }
284: for (int j = 0; j < items.length; j++) {
285: PaletteItem pitem = items[j].getLookup()
286: .lookup(PaletteItem.class);
287: boolean supported = false;
288: try {
289: Class clazz = pitem.getComponentClass();
290: if ((clazz != null)
291: && (LookAndFeel.class
292: .isAssignableFrom(clazz))) {
293: LookAndFeel laf = (LookAndFeel) clazz
294: .newInstance();
295: supported = laf
296: .isSupportedLookAndFeel();
297: }
298: } catch (Exception ex) {
299: ErrorManager.getDefault().notify(
300: ErrorManager.INFORMATIONAL, ex);
301: } catch (LinkageError ex) {
302: ErrorManager.getDefault().notify(
303: ErrorManager.INFORMATIONAL, ex);
304: }
305: if (supported) {
306: mi = new JMenuItem(items[j]
307: .getDisplayName());
308: mi.putClientProperty("lafInfo",
309: new LookAndFeelItem(pitem)); // NOI18N
310: mi.addActionListener(this );
311: popup.add(mi);
312: }
313: }
314: }
315: }
316:
317: initialized = true;
318: }
319: return popup;
320: }
321:
322: public void actionPerformed(ActionEvent e) {
323: Object o = e.getSource();
324: if (o instanceof JComponent) {
325: JComponent source = (JComponent) o;
326: LookAndFeelItem item = (LookAndFeelItem) source
327: .getClientProperty("lafInfo"); // NOI18N
328: try {
329: selectedLaf = item.getLAFClass();
330: run();
331: } catch (Exception ex) {
332: ErrorManager.getDefault().notify(ex);
333: }
334: }
335: }
336:
337: }
338:
339: /**
340: * Information about one look and feel.
341: */
342: static class LookAndFeelItem {
343: /** Name of the look and feel's class. */
344: private String className;
345: /** The corresponding PaletteItem, if exists. */
346: private PaletteItem pitem;
347:
348: public LookAndFeelItem(String className) {
349: this .className = className;
350: }
351:
352: public LookAndFeelItem(PaletteItem pitem) {
353: this .pitem = pitem;
354: this .className = pitem.getComponentClassName();
355: }
356:
357: public String getClassName() {
358: return className;
359: }
360:
361: public Class getLAFClass() throws ClassNotFoundException {
362: Class clazz;
363: if (pitem == null) {
364: if (className == null) {
365: clazz = UIManager.getLookAndFeel().getClass();
366: } else {
367: clazz = Class.forName(className);
368: }
369: } else {
370: clazz = pitem.getComponentClass();
371: }
372: return clazz;
373: }
374:
375: @Override
376: public boolean equals(Object obj) {
377: if (!(obj instanceof LookAndFeelItem))
378: return false;
379: LookAndFeelItem item = (LookAndFeelItem) obj;
380: return (pitem == item.pitem)
381: && ((pitem != null) || ((className == null) ? (item.className == null)
382: : className.equals(item.className)));
383: }
384:
385: @Override
386: public int hashCode() {
387: return (className == null) ? pitem.hashCode() : className
388: .hashCode();
389: }
390:
391: }
392:
393: }
|