KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import javax.swing.*;
24 import java.util.ResourceBundle JavaDoc;
25
26 import org.openide.*;
27 import org.openide.awt.Mnemonics;
28 import org.openide.util.HelpCtx;
29 import org.openide.util.NbBundle;
30 import org.openide.util.actions.*;
31 import org.openide.nodes.*;
32
33 import org.netbeans.modules.form.*;
34 import org.netbeans.modules.form.layoutdesign.*;
35
36 /**
37  * Customize empty space action.
38  *
39  * @author Jan Stola
40  */

41 public class CustomizeEmptySpaceAction extends CookieAction {
42     private static String JavaDoc name;
43     private Dialog dialog;
44
45     protected int mode() {
46         return MODE_EXACTLY_ONE;
47     }
48
49     protected Class JavaDoc[] cookieClasses() {
50         return new Class JavaDoc[] { RADComponentCookie.class };
51     }
52
53     protected boolean asynchronous() {
54         return false;
55     }
56
57     /**
58      * Human presentable name of the action.
59      *
60      * @return the name of the action
61      */

62     public String JavaDoc getName() {
63         if (name == null)
64             name = org.openide.util.NbBundle.getBundle(CustomizeEmptySpaceAction.class)
65                 .getString("ACT_CustomizeEmptySpace"); // NOI18N
66
return name;
67     }
68
69     public HelpCtx getHelpCtx() {
70         return HelpCtx.DEFAULT_HELP;
71     }
72
73     protected String JavaDoc iconResource() {
74         return "org/openide/resources/actions/empty.gif"; // NOI18N
75
}
76
77     /**
78      * Standard perform action extended by actually activated nodes.
79      *
80      * @param activatedNodes gives array of actually activated nodes.
81      */

82     protected void performAction(Node[] activatedNodes) {
83         java.util.List JavaDoc comps = FormUtils.getSelectedLayoutComponents(activatedNodes);
84         if ((comps == null) || (comps.size() != 1)) return;
85         RADComponent metacomp = (RADComponent)comps.get(0);
86         FormModel formModel = metacomp.getFormModel();
87         LayoutModel model = formModel.getLayoutModel();
88         final EmptySpaceCustomizer customizer = new EmptySpaceCustomizer(model, metacomp.getId());
89         DialogDescriptor dd = new DialogDescriptor(
90             customizer,
91             NbBundle.getMessage(CustomizeEmptySpaceAction.class, "TITLE_CustomizeEmptySpace"), // NOI18N
92
true,
93             NotifyDescriptor.OK_CANCEL_OPTION,
94             NotifyDescriptor.OK_OPTION,
95             DialogDescriptor.DEFAULT_ALIGN,
96             new HelpCtx(getClass().getName()),
97             new java.awt.event.ActionListener JavaDoc() {
98                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
99                     if (evt.getSource() == NotifyDescriptor.OK_OPTION) {
100                         if (customizer.checkValues()) {
101                             dialog.dispose();
102                         }
103                     }
104                 }
105             });
106         dd.setClosingOptions(new Object JavaDoc[] {NotifyDescriptor.CANCEL_OPTION});
107         dialog = DialogDisplayer.getDefault().createDialog(dd);
108         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CustomizeEmptySpaceAction.class, "ACSD_EmptySpace")); // NOI18N
109
dialog.setVisible(true);
110         dialog = null;
111         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
112             Object JavaDoc layoutUndoMark = model.getChangeMark();
113             javax.swing.undo.UndoableEdit JavaDoc ue = model.getUndoableEdit();
114             boolean autoUndo = true;
115             try {
116                 customizer.applyValues();
117                 autoUndo = false;
118             } finally {
119                 formModel.fireContainerLayoutChanged(((RADVisualComponent)metacomp).getParentContainer(), null, null, null);
120                 if (!layoutUndoMark.equals(model.getChangeMark())) {
121                     formModel.addUndoableEdit(ue);
122                 }
123                 if (autoUndo) {
124                     formModel.forceUndoOfCompoundEdit();
125                 }
126             }
127         }
128     }
129
130     protected boolean enable(Node[] activatedNodes) {
131         if (super.enable(activatedNodes)) {
132             java.util.List JavaDoc comps = FormUtils.getSelectedLayoutComponents(activatedNodes);
133             return ((comps != null) && (comps.size() == 1));
134         }
135         return false;
136     }
137     
138 private static class EmptySpaceCustomizer extends JPanel {
139     JComboBox leftSize = new JComboBox();
140     JComboBox rightSize = new JComboBox();
141     JComboBox topSize = new JComboBox();
142     JComboBox bottomSize = new JComboBox();
143     JCheckBox leftResizable = new JCheckBox();
144     JCheckBox rightResizable = new JCheckBox();
145     JCheckBox topResizable = new JCheckBox();
146     JCheckBox bottomResizable = new JCheckBox();
147     LayoutModel model;
148     String JavaDoc compId;
149     String JavaDoc padding;
150
151     EmptySpaceCustomizer(LayoutModel model, String JavaDoc compId) {
152         this.model = model;
153         this.compId = compId;
154         initComponents();
155         LayoutComponent comp = model.getLayoutComponent(compId);
156         initValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.LEADING, leftSize, leftResizable);
157         initValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.TRAILING, rightSize, rightResizable);
158         initValues(comp, LayoutConstants.VERTICAL, LayoutConstants.LEADING, topSize, topResizable);
159         initValues(comp, LayoutConstants.VERTICAL, LayoutConstants.TRAILING, bottomSize, bottomResizable);
160     }
161     
162     private void initValues(LayoutComponent comp, int dimension, int direction, JComboBox size, JCheckBox resizable) {
163         LayoutInterval space = LayoutUtils.getAdjacentEmptySpace(comp, dimension, direction);
164         if (space != null) {
165             int pref = space.getPreferredSize(false);
166             int max = space.getMaximumSize(false);
167             size.setSelectedItem((pref == LayoutConstants.NOT_EXPLICITLY_DEFINED) ? padding : ("" + pref));
168             resizable.setSelected((max != LayoutConstants.USE_PREFERRED_SIZE) && (max != pref));
169         } else {
170             size.setSelectedItem(NbBundle.getMessage(CustomizeEmptySpaceAction.class, "VALUE_NoEmptySpace"));
171             size.setEnabled(false);
172             resizable.setEnabled(false);
173         }
174     }
175     
176     boolean checkValues() {
177         return checkValue(leftSize) && checkValue(rightSize) && checkValue(topSize) && checkValue(bottomSize);
178     }
179
180     private boolean checkValue(JComboBox size) {
181         Object JavaDoc selSize = size.getSelectedItem();
182         if (size.isEnabled() && !selSize.equals(padding)) {
183             try {
184                 int newPref = Integer.parseInt((String JavaDoc)selSize);
185                 if (newPref < 0) {
186                     // Negative
187
notify("MSG_NegativeSpaceSize"); // NOI18N
188
return false;
189                 }
190                 if (newPref > Short.MAX_VALUE) {
191                     // Too large
192
notify("MSG_TooLargeSpaceSize"); // NOI18N
193
return false;
194                 }
195             } catch (NumberFormatException JavaDoc nfex) {
196                 // Not a nubmer
197
notify("MSG_CorruptedSpaceSize"); // NOI18N
198
return false;
199             }
200         }
201         return true;
202     }
203
204     private void notify(String JavaDoc messageKey) {
205         NotifyDescriptor descriptor = new NotifyDescriptor.Message(
206             NbBundle.getBundle(CustomizeEmptySpaceAction.class).getString(messageKey));
207         DialogDisplayer.getDefault().notify(descriptor);
208     }
209
210     void applyValues() {
211         LayoutComponent comp = model.getLayoutComponent(compId);
212         applyValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.LEADING, leftSize, leftResizable);
213         applyValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.TRAILING, rightSize, rightResizable);
214         applyValues(comp, LayoutConstants.VERTICAL, LayoutConstants.LEADING, topSize, topResizable);
215         applyValues(comp, LayoutConstants.VERTICAL, LayoutConstants.TRAILING, bottomSize, bottomResizable);
216     }
217
218     private void applyValues(LayoutComponent comp, int dimension, int direction, JComboBox size, JCheckBox resizable) {
219         LayoutInterval space = LayoutUtils.getAdjacentEmptySpace(comp, dimension, direction);
220         if (space != null) {
221             int pref = space.getPreferredSize(false);
222             int max = space.getMaximumSize(false);
223             boolean oldResizable = (max != LayoutConstants.USE_PREFERRED_SIZE) && (max != pref);
224             boolean newResizable = resizable.isSelected();
225             Object JavaDoc selSize = size.getSelectedItem();
226             int newPref;
227             if (selSize.equals(padding)){
228                 newPref = LayoutConstants.NOT_EXPLICITLY_DEFINED;
229             } else {
230                 try {
231                     newPref = Integer.parseInt((String JavaDoc)selSize);
232                     if (newPref < 0) {
233                         newPref = pref;
234                     }
235                 } catch (NumberFormatException JavaDoc nfex) {
236                     newPref = pref; // Use old value instead
237
}
238             }
239             if ((pref != newPref) || (oldResizable != newResizable)) {
240                 model.setIntervalSize(space,
241                     newResizable ? LayoutConstants.NOT_EXPLICITLY_DEFINED : LayoutConstants.USE_PREFERRED_SIZE,
242                     newPref,
243                     newResizable ? Short.MAX_VALUE : LayoutConstants.USE_PREFERRED_SIZE);
244             }
245         }
246     }
247
248     private void initComponents() {
249         ResourceBundle JavaDoc bundle = NbBundle.getBundle(EmptySpaceCustomizer.class);
250         setLayout(new GridBagLayout());
251         setBorder(new javax.swing.border.TitledBorder JavaDoc(bundle.getString("TITLE_EmptySpace"))); // NOI18N
252
JLabel leftLabel = new JLabel();
253         JLabel rightLabel = new JLabel();
254         JLabel topLabel = new JLabel();
255         JLabel bottomLabel = new JLabel();
256         JLabel sizeLabel = new JLabel(bundle.getString("NAME_SpaceSize")); // NOI18N
257
JLabel resizableLabel = new JLabel(bundle.getString("NAME_SpaceResizable")); // NOI18N
258

259         GridBagConstraints gridBagConstraints = new GridBagConstraints();
260         gridBagConstraints.gridx = 0;
261         gridBagConstraints.gridy = 1;
262         gridBagConstraints.anchor = GridBagConstraints.EAST;
263         gridBagConstraints.insets = new Insets(3, 6, 3, 0);
264         add(leftLabel, gridBagConstraints);
265
266         gridBagConstraints.gridy = 2;
267         add(rightLabel, gridBagConstraints);
268
269         gridBagConstraints.gridy = 3;
270         add(topLabel, gridBagConstraints);
271
272         gridBagConstraints.gridy = 4;
273         gridBagConstraints.insets = new Insets(3, 6, 6, 0);
274         add(bottomLabel, gridBagConstraints);
275
276         gridBagConstraints.gridx = 1;
277         gridBagConstraints.gridy = 0;
278         gridBagConstraints.anchor = GridBagConstraints.CENTER;
279         gridBagConstraints.insets = new Insets(0, 6, 3, 6);
280         add(sizeLabel, gridBagConstraints);
281
282         gridBagConstraints.gridx = 2;
283         gridBagConstraints.insets = new Insets(0, 6, 3, 6);
284         add(resizableLabel, gridBagConstraints);
285
286         gridBagConstraints.insets = new Insets(0, 0, 0, 0);
287         gridBagConstraints.gridx = 2;
288         gridBagConstraints.gridy = 1;
289         add(leftResizable, gridBagConstraints);
290
291         gridBagConstraints.gridy = 2;
292         add(rightResizable, gridBagConstraints);
293
294         gridBagConstraints.gridy = 3;
295         add(topResizable, gridBagConstraints);
296
297         gridBagConstraints.gridy = 4;
298         gridBagConstraints.insets = new Insets(0, 0, 3, 0);
299         add(bottomResizable, gridBagConstraints);
300
301         leftLabel.setLabelFor(leftSize);
302         rightLabel.setLabelFor(rightSize);
303         topLabel.setLabelFor(topSize);
304         bottomLabel.setLabelFor(bottomSize);
305
306         Mnemonics.setLocalizedText(leftLabel, bundle.getString("NAME_LeftSpace")); // NOI18N
307
Mnemonics.setLocalizedText(rightLabel, bundle.getString("NAME_RightSpace")); // NOI18N
308
Mnemonics.setLocalizedText(topLabel, bundle.getString("NAME_TopSpace")); // NOI18N
309
Mnemonics.setLocalizedText(bottomLabel, bundle.getString("NAME_BottomSpace")); // NOI18N
310

311         leftSize.setEditable(true);
312         rightSize.setEditable(true);
313         topSize.setEditable(true);
314         bottomSize.setEditable(true);
315
316         padding = bundle.getString("VALUE_DefaultPadding"); // NOI18N
317
leftSize.setModel(new DefaultComboBoxModel(new String JavaDoc[] {padding}));
318         rightSize.setModel(new DefaultComboBoxModel(new String JavaDoc[] {padding}));
319         topSize.setModel(new DefaultComboBoxModel(new String JavaDoc[] {padding}));
320         bottomSize.setModel(new DefaultComboBoxModel(new String JavaDoc[] {padding}));
321
322         leftResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LeftResizable")); // NOI18N
323
rightResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_RightResizable")); // NOI18N
324
topResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TopResizable")); // NOI18N
325
bottomResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_BottomResizable")); // NOI18N
326

327         gridBagConstraints.gridx = 1;
328         gridBagConstraints.gridy = 1;
329         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
330         gridBagConstraints.weightx = 1.0;
331         gridBagConstraints.weighty = 1.0;
332         gridBagConstraints.insets = new Insets(3, 6, 3, 6);
333         add(leftSize, gridBagConstraints);
334
335         gridBagConstraints.gridy = 2;
336         add(rightSize, gridBagConstraints);
337         
338         gridBagConstraints.gridy = 3;
339         add(topSize, gridBagConstraints);
340
341         gridBagConstraints.gridy = 4;
342         gridBagConstraints.insets = new Insets(3, 6, 6, 6);
343         add(bottomSize, gridBagConstraints);
344     }
345
346 }
347 }
348
Popular Tags