KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > FormCustomEditor


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;
21
22 import org.openide.*;
23 import org.openide.nodes.*;
24 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
25 import org.openide.explorer.propertysheet.PropertyEnv;
26 import org.openide.explorer.propertysheet.ExPropertyEditor;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.Utilities;
29
30 import java.awt.*;
31 import java.beans.PropertyEditor JavaDoc;
32 import javax.swing.*;
33
34 /**
35  *
36  * @author Ian Formanek, Vladimir Zboril
37  */

38 public class FormCustomEditor extends JPanel
39                               implements EnhancedCustomPropertyEditor
40 {
41     private static final int DEFAULT_WIDTH = 350;
42     private static final int DEFAULT_HEIGHT = 350;
43
44     // -----------------------------------------------------------------------------
45
// Private variables
46

47     private FormPropertyEditor editor;
48     private PropertyEditor[] allEditors;
49     private Component[] allCustomEditors;
50     private boolean[] validValues;
51
52     private String JavaDoc preCode;
53     private String JavaDoc postCode;
54
55     /** Creates new form FormCustomEditor */
56     public FormCustomEditor(FormPropertyEditor editor,
57                             Component currentCustomEditor)
58     {
59         initComponents();
60
61         advancedButton.setText(FormUtils.getBundleString("CTL_Advanced")); // NOI18N
62
// advancedButton.setMnemonic(FormUtils.getBundleString(
63
// "CTL_Advanced_mnemonic").charAt(0)); // NOI18N
64
if (editor.getProperty() instanceof RADProperty)
65             advancedButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
66                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
67                     showAdvancedSettings();
68                 }
69             });
70         else
71             advancedButton.setEnabled(false);
72         
73         jLabel1.setText(FormUtils.getBundleString("LAB_SelectMode")); //NOI18N
74
jLabel1.setDisplayedMnemonic((FormUtils.getBundleString(
75                                        "LAB_SelectMode.mnemonic").charAt(0))); //NOI18N
76
jLabel1.setLabelFor(editorsCombo);
77         
78         this.editor = editor;
79         preCode = editor.getProperty().getPreCode();
80         postCode = editor.getProperty().getPostCode();
81         allEditors = editor.getAllEditors();
82
83         PropertyEditor currentEditor = editor.getCurrentEditor();
84         int currentIndex;
85
86         if (currentEditor != null) {
87             currentIndex = -1;
88             for (int i=0; i < allEditors.length; i++)
89                 if (currentEditor.getClass().equals(allEditors[i].getClass())) {
90                     currentIndex = i;
91                     allEditors[i] = currentEditor;
92                     break;
93                 }
94             if (currentIndex == -1) {
95                 // this should not happen, but we cannot exclude it
96
PropertyEditor[] editors = new PropertyEditor[allEditors.length+1];
97                 editors[0] = currentEditor;
98                 System.arraycopy(allEditors, 0, editors, 1, allEditors.length);
99                 allEditors = editors;
100                 currentIndex = 0;
101             }
102         }
103         else currentIndex = 0;
104
105         allCustomEditors = new Component[allEditors.length];
106         validValues = new boolean[allEditors.length];
107
108         PropertyEnv env = editor.getPropertyEnv();
109         Object JavaDoc currentValue = editor.getValue();
110
111         // go through all available property editors, set their values and
112
// setup their custom editors
113
for (int i=0; i < allEditors.length; i++) {
114             PropertyEditor prEd = allEditors[i];
115             editor.getPropertyContext().initPropertyEditor(prEd);
116
117             boolean valueSet = false;
118             if (i == currentIndex) { // this is the currently used editor
119
valueSet = true;
120             }
121             else {
122                 if (env != null && prEd instanceof ExPropertyEditor)
123                     ((ExPropertyEditor)prEd).attachEnv(env);
124
125                 if (currentValue != null) {
126                     try {
127                         if (editor.getPropertyType().isAssignableFrom(
128                                                currentValue.getClass()))
129                         { // currentValue is a real property value corresponding
130
// to property editor value type
131
prEd.setValue(currentValue);
132                             valueSet = true;
133                         }
134                         else if (currentValue instanceof FormDesignValue) {
135                             Object JavaDoc realValue = // get real value of the design value
136
((FormDesignValue)currentValue).getDesignValue();
137                             if (realValue != FormDesignValue.IGNORED_VALUE) {
138                                 // there is a known real value
139
prEd.setValue(realValue);
140                                 valueSet = true;
141                             }
142                         }
143                     }
144                     catch (IllegalArgumentException JavaDoc ex) {} // ignore
145
}
146                 // [null value should not be set?]
147

148                 if (!valueSet) {
149                     // no reasonable value for this property editor, try to
150
// set the default value
151
Object JavaDoc defaultValue = editor.getProperty().getDefaultValue();
152                     if (defaultValue != BeanSupport.NO_VALUE) {
153                         prEd.setValue(defaultValue);
154                         valueSet = true;
155                     }
156                     // [but if there's no default value it is not possible to
157
// switch to this property editor and enter something - see
158
// getPropertyValue() - it returns BeanSupport.NO_VALUE]
159
}
160             }
161             validValues[i] = valueSet;
162
163             String JavaDoc editorName = prEd instanceof NamedPropertyEditor ?
164                         ((NamedPropertyEditor)prEd).getDisplayName() :
165                         Utilities.getShortClassName(prEd.getClass());
166
167             Component custEd = null;
168             if (i == currentIndex)
169                 custEd = currentCustomEditor;
170             else if (prEd.supportsCustomEditor())
171                 custEd = prEd.getCustomEditor();
172
173             if (custEd == null || custEd instanceof Window) {
174                 JPanel p = new JPanel(new GridBagLayout());
175                 JLabel label = new JLabel(
176                     FormUtils.getBundleString("CTL_PropertyEditorDoesNot")); // NOI18N
177
p.add(label);
178                 p.getAccessibleContext().setAccessibleDescription(label.getText());
179                 custEd = p;
180             }
181
182             allCustomEditors[i] = custEd;
183             cardPanel.add(editorName, custEd);
184             editorsCombo.addItem(editorName);
185         }
186
187         editorsCombo.setSelectedIndex(currentIndex);
188         CardLayout cl = (CardLayout) cardPanel.getLayout();
189         cl.show(cardPanel, (String JavaDoc) editorsCombo.getSelectedItem());
190
191         editorsCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
192             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
193                 CardLayout cl2 = (CardLayout) cardPanel.getLayout();
194                 cl2.show(cardPanel, (String JavaDoc) editorsCombo.getSelectedItem());
195
196                 int i = editorsCombo.getSelectedIndex();
197                 HelpCtx helpCtx = i < 0 ? null :
198                                   HelpCtx.findHelp(cardPanel.getComponent(i));
199                 String JavaDoc helpID = helpCtx != null ? helpCtx.getHelpID() : ""; // NOI18N
200
HelpCtx.setHelpIDString(FormCustomEditor.this, helpID);
201                 
202                 updateAccessibleDescription(i < 0 ? null : cardPanel.getComponent(i));
203             }
204         });
205
206         updateAccessibleDescription(cardPanel.getComponent(currentIndex));
207         advancedButton.getAccessibleContext().setAccessibleDescription(
208             FormUtils.getBundleString("ACSD_CTL_Advanced")); // NOI18N
209
editorsCombo.getAccessibleContext().setAccessibleDescription(
210             FormUtils.getBundleString("ACSD_BTN_SelectMode")); // NOI18N
211
}
212     
213     private void updateAccessibleDescription(Component comp) {
214         if (comp instanceof javax.accessibility.Accessible JavaDoc
215             && comp.getAccessibleContext().getAccessibleDescription() != null) {
216
217             getAccessibleContext().setAccessibleDescription(
218                 FormUtils.getFormattedBundleString(
219                     "ACSD_FormCustomEditor", // NOI18N
220
new Object JavaDoc[] {
221                         comp.getAccessibleContext().getAccessibleDescription()
222                     }
223                 )
224             );
225         } else {
226             getAccessibleContext().setAccessibleDescription(null);
227         }
228     }
229
230     /** This method is called from within the constructor to
231      * initialize the form.
232      * WARNING: Do NOT modify this code. The content of this method is
233      * always regenerated by the Form Editor.
234      */

235     private void initComponents() {//GEN-BEGIN:initComponents
236
editorsCombo = new javax.swing.JComboBox JavaDoc();
237         jLabel1 = new javax.swing.JLabel JavaDoc();
238         jPanel1 = new javax.swing.JPanel JavaDoc();
239         cardPanel = new javax.swing.JPanel JavaDoc();
240         advancedButton = new javax.swing.JButton JavaDoc();
241         
242         setLayout(new java.awt.GridBagLayout JavaDoc());
243         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
244         
245         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
246         gridBagConstraints1.gridx = 1;
247         gridBagConstraints1.gridy = 0;
248         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
249         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(12, 5, 0, 11);
250         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
251         add(editorsCombo, gridBagConstraints1);
252         
253         jLabel1.setText("jLabel1");
254         jLabel1.setLabelFor(editorsCombo);
255         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
256         gridBagConstraints1.gridx = 0;
257         gridBagConstraints1.gridy = 0;
258         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
259         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
260         add(jLabel1, gridBagConstraints1);
261         
262         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
263         java.awt.GridBagConstraints JavaDoc gridBagConstraints2;
264         
265         jPanel1.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
266         cardPanel.setLayout(new java.awt.CardLayout JavaDoc());
267         
268         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
269         gridBagConstraints2.gridx = 0;
270         gridBagConstraints2.gridy = 0;
271         gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
272         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(12, 12, 11, 11);
273         gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST;
274         gridBagConstraints2.weightx = 1.0;
275         gridBagConstraints2.weighty = 1.0;
276         jPanel1.add(cardPanel, gridBagConstraints2);
277         
278         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
279         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
280         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
281         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
282         gridBagConstraints1.weightx = 1.0;
283         gridBagConstraints1.weighty = 1.0;
284         add(jPanel1, gridBagConstraints1);
285         
286         advancedButton.setText("jButton1");
287         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
288         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
289         gridBagConstraints1.gridheight = java.awt.GridBagConstraints.REMAINDER;
290         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
291         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
292         add(advancedButton, gridBagConstraints1);
293         
294     }//GEN-END:initComponents
295

296
297     // Variables declaration - do not modify//GEN-BEGIN:variables
298
private javax.swing.JComboBox JavaDoc editorsCombo;
299     private javax.swing.JLabel JavaDoc jLabel1;
300     private javax.swing.JPanel JavaDoc jPanel1;
301     private javax.swing.JPanel JavaDoc cardPanel;
302     private javax.swing.JButton JavaDoc advancedButton;
303     // End of variables declaration//GEN-END:variables
304

305     public Dimension getPreferredSize() {
306         Dimension inh = super.getPreferredSize();
307         return new Dimension(Math.max(inh.width, DEFAULT_WIDTH), Math.max(inh.height, DEFAULT_HEIGHT));
308     }
309     
310     private void showAdvancedSettings() {
311         FormCustomEditorAdvanced fcea = new FormCustomEditorAdvanced(preCode, postCode);
312         DialogDescriptor dd = new DialogDescriptor(
313             fcea,
314             FormUtils.getFormattedBundleString(
315                 "FMT_CTL_AdvancedInitializationCode", // NOI18N
316
new Object JavaDoc[] { editor.getProperty().getName() }));
317
318         dd.setHelpCtx(new HelpCtx("gui.source.modifying.property")); // NOI18N
319
DialogDisplayer.getDefault().createDialog(dd).show();
320
321         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
322             preCode = fcea.getPreCode();
323             postCode = fcea.getPostCode();
324         }
325     }
326     
327     // -----------------------------------------------------------------------------
328
// EnhancedCustomPropertyEditor implementation
329

330     /** Get the customized property value.
331      * @return the property value
332      * @exception InvalidStateException when the custom property editor does
333      * not contain a valid property value (and thus it should not be set)
334      */

335     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
336         int currentIndex = editorsCombo.getSelectedIndex();
337         PropertyEditor currentEditor = currentIndex > -1 ?
338                                        allEditors[currentIndex] : null;
339         Component currentCustomEditor = currentIndex > -1 ?
340                                         allCustomEditors[currentIndex] : null;
341         Object JavaDoc value;
342
343         if (currentCustomEditor instanceof EnhancedCustomPropertyEditor) {
344             // current editor is EnhancedCustomPropertyEditor too
345
value = ((EnhancedCustomPropertyEditor) currentCustomEditor)
346                                                         .getPropertyValue();
347         }
348         else if (currentIndex > -1) {
349             value = validValues[currentIndex] ? currentEditor.getValue() :
350                                                 BeanSupport.NO_VALUE;
351         }
352         else value = editor.getValue();
353
354         // set the current property editor to FormPropertyEditor (to be used as
355
// the custom editor provider next time; and also for code generation);
356
// it should be set for all properties (of all nodes selected)
357
if (currentIndex > -1) {
358             Object JavaDoc[] nodes = editor.getPropertyEnv().getBeans();
359             if (nodes == null || nodes.length <= 1) {
360                 FormProperty prop = editor.getProperty();
361
362                 prop.setPreCode(preCode);
363                 prop.setPostCode(postCode);
364
365                 value = new FormProperty.ValueWithEditor(value, currentEditor);
366             }
367             else { // there are more nodes selected
368
String JavaDoc propName = editor.getProperty().getName();
369
370                 for (int i=0; i < nodes.length; i++) {
371                     if (!(nodes[i] instanceof Node))
372                         break; // these are not nodes...
373

374                     Node node = (Node) nodes[i];
375                     FormPropertyCookie propCookie = (FormPropertyCookie)
376                         node.getCookie(FormPropertyCookie.class);
377                     if (propCookie == null)
378                         break; // not form nodes...
379

380                     FormProperty prop = propCookie.getProperty(propName);
381                     if (prop == null)
382                         continue; // property not known
383

384                     prop.setPreCode(preCode);
385                     prop.setPostCode(postCode);
386                 }
387
388                 value = new FormProperty.ValueWithEditor(value, currentIndex);
389             }
390         }
391
392         return value;
393     }
394 }
395
Popular Tags