KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.openide.util.HelpCtx;
22 import org.openide.nodes.*;
23 import org.openide.util.actions.NodeAction;
24
25 import org.netbeans.modules.form.*;
26 import org.netbeans.modules.form.layoutdesign.LayoutDesigner;
27 import org.netbeans.modules.form.layoutdesign.LayoutModel;
28
29 public class DefaultSizeAction extends NodeAction {
30
31     protected boolean asynchronous() {
32         return false;
33     }
34
35     protected boolean enable(Node[] nodes) {
36         for (int i=0; i < nodes.length; i++) {
37             if (getValidComponent(nodes[i]) == null)
38                 return false; // all components must be valid
39
}
40         return true;
41     }
42
43     protected void performAction(Node[] nodes) {
44         FormModel formModel = null;
45         FormDesigner formDesigner = null;
46         LayoutDesigner layoutDesigner = null;
47         RADVisualComponent topDesignComponent = null;
48         LayoutModel layoutModel = null;
49         Object JavaDoc layoutUndoMark = null;
50         javax.swing.undo.UndoableEdit JavaDoc layoutUE = null;
51         boolean autoUndo = true;
52
53         try {
54             for (int i=0; i < nodes.length; i++) {
55                 RADVisualComponent metacomp = getValidComponent(nodes[i]);
56                 if (metacomp == null)
57                     return; // all components must be valid
58

59                 if (layoutDesigner == null) {
60                     formModel = metacomp.getFormModel();
61                     formDesigner = FormEditor.getFormDesigner(formModel);
62                     layoutDesigner = formDesigner.getLayoutDesigner();
63                     layoutModel = formModel.getLayoutModel();
64                     layoutUndoMark = layoutModel.getChangeMark();
65                     layoutUE = layoutModel.getUndoableEdit();
66                 }
67                 layoutDesigner.setDefaultSize(metacomp.getId());
68                 if (metacomp instanceof RADVisualContainer) {
69                     formModel.fireContainerLayoutChanged((RADVisualContainer)metacomp, null, null, null);
70                     // [should be recursive]
71
}
72                 if (topDesignComponent == null && metacomp == formDesigner.getTopDesignComponent()) {
73                     topDesignComponent = metacomp;
74                 }
75                 else { // update container the component is in
76
formModel.fireContainerLayoutChanged(metacomp.getParentContainer(), null, null, null);
77                 }
78             }
79
80             if (topDesignComponent != null) {
81                 formDesigner.resetDesignerSize();
82             }
83             autoUndo = false;
84         } finally {
85             if (layoutUE != null && !layoutUndoMark.equals(layoutModel.getChangeMark())) {
86                 formModel.addUndoableEdit(layoutUE);
87             }
88             if (autoUndo) {
89                 formModel.forceUndoOfCompoundEdit();
90             }
91         }
92     }
93
94     public String JavaDoc getName() {
95         return org.openide.util.NbBundle.getBundle(DefaultSizeAction.class)
96                 .getString("ACT_DefaultSize"); // NOI18N
97
}
98
99     public HelpCtx getHelpCtx() {
100         return HelpCtx.DEFAULT_HELP;
101     }
102
103     private static RADVisualComponent getValidComponent(Node node) {
104         RADComponentCookie radCookie = (RADComponentCookie)
105                 node.getCookie(RADComponentCookie.class);
106         if (radCookie != null) {
107             RADComponent metacomp = radCookie.getRADComponent();
108             if (metacomp instanceof RADVisualComponent) {
109                 RADVisualComponent visualMetaComp = (RADVisualComponent)metacomp;
110                 RADVisualContainer parent = visualMetaComp.getParentContainer();
111                 if ((parent != null) && javax.swing.JScrollPane JavaDoc.class.isAssignableFrom(parent.getBeanInstance().getClass())) {
112                     visualMetaComp = parent;
113                     parent = parent.getParentContainer();
114                 }
115                 if (FormUtils.isInTopDesignComponent(metacomp) &&
116                     ((parent != null && parent.getLayoutSupport() == null)
117                     || (visualMetaComp instanceof RADVisualContainer
118                         && ((RADVisualContainer)visualMetaComp).getLayoutSupport() == null
119                         && (!(visualMetaComp instanceof RADVisualFormContainer)
120                             || ((RADVisualFormContainer)visualMetaComp).getFormSizePolicy()
121                                  != RADVisualFormContainer.GEN_BOUNDS))))
122                     return visualMetaComp;
123             }
124         }
125         return null;
126     }
127 }
128
Popular Tags