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;
043:
044: import org.openide.*;
045: import org.openide.nodes.*;
046: import org.openide.explorer.propertysheet.PropertyEnv;
047: import org.openide.explorer.propertysheet.ExPropertyEditor;
048: import org.openide.util.HelpCtx;
049: import org.openide.util.Utilities;
050:
051: import java.awt.*;
052: import java.beans.PropertyEditor;
053: import javax.swing.*;
054:
055: /**
056: *
057: * @author Ian Formanek, Vladimir Zboril
058: */
059: public class FormCustomEditor extends JPanel {
060: private static final int DEFAULT_WIDTH = 350;
061: private static final int DEFAULT_HEIGHT = 350;
062:
063: // -----------------------------------------------------------------------------
064: // Private variables
065:
066: private FormPropertyEditor editor;
067: private PropertyEditor[] allEditors;
068: private Component[] allCustomEditors;
069: private boolean[] validValues;
070:
071: private String preCode;
072: private String postCode;
073:
074: /** Creates new form FormCustomEditor */
075: public FormCustomEditor(FormPropertyEditor editor,
076: Component currentCustomEditor) {
077: initComponents();
078:
079: advancedButton.setText(FormUtils
080: .getBundleString("CTL_Advanced")); // NOI18N
081: // advancedButton.setMnemonic(FormUtils.getBundleString(
082: // "CTL_Advanced_mnemonic").charAt(0)); // NOI18N
083: if (editor.getProperty() instanceof RADProperty)
084: advancedButton
085: .addActionListener(new java.awt.event.ActionListener() {
086: public void actionPerformed(
087: java.awt.event.ActionEvent evt) {
088: showAdvancedSettings();
089: }
090: });
091: else
092: advancedButton.setEnabled(false);
093:
094: jLabel1.setText(FormUtils.getBundleString("LAB_SelectMode")); //NOI18N
095: jLabel1.setDisplayedMnemonic((FormUtils
096: .getBundleString("LAB_SelectMode.mnemonic").charAt(0))); //NOI18N
097: jLabel1.setLabelFor(editorsCombo);
098:
099: this .editor = editor;
100: preCode = editor.getProperty().getPreCode();
101: postCode = editor.getProperty().getPostCode();
102: allEditors = editor.getAllEditors();
103:
104: PropertyEditor currentEditor = editor.getCurrentEditor();
105: int currentIndex;
106:
107: if (currentEditor != null) {
108: currentIndex = -1;
109: for (int i = 0; i < allEditors.length; i++)
110: if (currentEditor.getClass().equals(
111: allEditors[i].getClass())) {
112: currentIndex = i;
113: allEditors[i] = currentEditor;
114: break;
115: }
116: if (currentIndex == -1) {
117: // this should not happen, but we cannot exclude it
118: PropertyEditor[] editors = new PropertyEditor[allEditors.length + 1];
119: editors[0] = currentEditor;
120: System.arraycopy(allEditors, 0, editors, 1,
121: allEditors.length);
122: allEditors = editors;
123: currentIndex = 0;
124: }
125: } else
126: currentIndex = 0;
127:
128: allCustomEditors = new Component[allEditors.length];
129: validValues = new boolean[allEditors.length];
130:
131: PropertyEnv env = editor.getPropertyEnv();
132: Object currentValue = editor.getValue();
133:
134: // go through all available property editors, set their values and
135: // setup their custom editors
136: for (int i = 0; i < allEditors.length; i++) {
137: PropertyEditor prEd = allEditors[i];
138: editor.getPropertyContext().initPropertyEditor(prEd);
139:
140: boolean valueSet = false;
141: if (i == currentIndex) { // this is the currently used editor
142: valueSet = true;
143: } else {
144: if (env != null && prEd instanceof ExPropertyEditor)
145: ((ExPropertyEditor) prEd).attachEnv(env);
146:
147: if (currentValue != null) {
148: try {
149: if (editor.getPropertyType().isAssignableFrom(
150: currentValue.getClass())) { // currentValue is a real property value corresponding
151: // to property editor value type
152: prEd.setValue(currentValue);
153: valueSet = true;
154: } else if (currentValue instanceof FormDesignValue) {
155: Object realValue = // get real value of the design value
156: ((FormDesignValue) currentValue)
157: .getDesignValue();
158: if (realValue != FormDesignValue.IGNORED_VALUE) {
159: // there is a known real value
160: prEd.setValue(realValue);
161: valueSet = true;
162: }
163: }
164: } catch (IllegalArgumentException ex) {
165: } // ignore
166: }
167: // [null value should not be set?]
168:
169: if (!valueSet) {
170: // no reasonable value for this property editor, try to
171: // set the default value
172: Object defaultValue = editor.getProperty()
173: .getDefaultValue();
174: if (defaultValue != BeanSupport.NO_VALUE) {
175: prEd.setValue(defaultValue);
176: valueSet = true;
177: }
178: // [but if there's no default value it is not possible to
179: // switch to this property editor and enter something - see
180: // getPropertyValue() - it returns BeanSupport.NO_VALUE]
181: }
182: }
183: validValues[i] = valueSet;
184:
185: String editorName = prEd instanceof NamedPropertyEditor ? ((NamedPropertyEditor) prEd)
186: .getDisplayName()
187: : Utilities.getShortClassName(prEd.getClass());
188:
189: Component custEd = null;
190: if (i == currentIndex)
191: custEd = currentCustomEditor;
192: else if (prEd.supportsCustomEditor())
193: custEd = prEd.getCustomEditor();
194:
195: if (custEd == null || custEd instanceof Window) {
196: JPanel p = new JPanel(new GridBagLayout());
197: JLabel label = new JLabel(FormUtils
198: .getBundleString("CTL_PropertyEditorDoesNot")); // NOI18N
199: p.add(label);
200: p.getAccessibleContext().setAccessibleDescription(
201: label.getText());
202: custEd = p;
203: }
204:
205: allCustomEditors[i] = custEd;
206: cardPanel.add(editorName, custEd);
207: editorsCombo.addItem(editorName);
208: }
209:
210: editorsCombo.setSelectedIndex(currentIndex);
211: CardLayout cl = (CardLayout) cardPanel.getLayout();
212: cl.show(cardPanel, (String) editorsCombo.getSelectedItem());
213:
214: editorsCombo
215: .addActionListener(new java.awt.event.ActionListener() {
216: public void actionPerformed(
217: java.awt.event.ActionEvent evt) {
218: CardLayout cl2 = (CardLayout) cardPanel
219: .getLayout();
220: cl2.show(cardPanel, (String) editorsCombo
221: .getSelectedItem());
222:
223: int i = editorsCombo.getSelectedIndex();
224: HelpCtx helpCtx = i < 0 ? null : HelpCtx
225: .findHelp(cardPanel.getComponent(i));
226: String helpID = helpCtx != null ? helpCtx
227: .getHelpID() : ""; // NOI18N
228: HelpCtx.setHelpIDString(FormCustomEditor.this ,
229: helpID);
230:
231: updateAccessibleDescription(i < 0 ? null
232: : cardPanel.getComponent(i));
233: }
234: });
235:
236: updateAccessibleDescription(cardPanel
237: .getComponent(currentIndex));
238: advancedButton.getAccessibleContext().setAccessibleDescription(
239: FormUtils.getBundleString("ACSD_CTL_Advanced")); // NOI18N
240: editorsCombo.getAccessibleContext().setAccessibleDescription(
241: FormUtils.getBundleString("ACSD_BTN_SelectMode")); // NOI18N
242: }
243:
244: private void updateAccessibleDescription(Component comp) {
245: if (comp instanceof javax.accessibility.Accessible
246: && comp.getAccessibleContext()
247: .getAccessibleDescription() != null) {
248:
249: getAccessibleContext().setAccessibleDescription(
250: FormUtils.getFormattedBundleString(
251: "ACSD_FormCustomEditor", // NOI18N
252: new Object[] { comp.getAccessibleContext()
253: .getAccessibleDescription() }));
254: } else {
255: getAccessibleContext().setAccessibleDescription(null);
256: }
257: }
258:
259: /** This method is called from within the constructor to
260: * initialize the form.
261: * WARNING: Do NOT modify this code. The content of this method is
262: * always regenerated by the Form Editor.
263: */
264: private void initComponents() {//GEN-BEGIN:initComponents
265: editorsCombo = new javax.swing.JComboBox();
266: jLabel1 = new javax.swing.JLabel();
267: jPanel1 = new javax.swing.JPanel();
268: cardPanel = new javax.swing.JPanel();
269: advancedButton = new javax.swing.JButton();
270:
271: setLayout(new java.awt.GridBagLayout());
272: java.awt.GridBagConstraints gridBagConstraints1;
273:
274: gridBagConstraints1 = new java.awt.GridBagConstraints();
275: gridBagConstraints1.gridx = 1;
276: gridBagConstraints1.gridy = 0;
277: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
278: gridBagConstraints1.insets = new java.awt.Insets(12, 5, 0, 11);
279: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
280: add(editorsCombo, gridBagConstraints1);
281:
282: jLabel1.setText("jLabel1");
283: jLabel1.setLabelFor(editorsCombo);
284: gridBagConstraints1 = new java.awt.GridBagConstraints();
285: gridBagConstraints1.gridx = 0;
286: gridBagConstraints1.gridy = 0;
287: gridBagConstraints1.insets = new java.awt.Insets(12, 12, 0, 0);
288: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
289: add(jLabel1, gridBagConstraints1);
290:
291: jPanel1.setLayout(new java.awt.GridBagLayout());
292: java.awt.GridBagConstraints gridBagConstraints2;
293:
294: jPanel1.setBorder(new javax.swing.border.EtchedBorder());
295: cardPanel.setLayout(new java.awt.CardLayout());
296:
297: gridBagConstraints2 = new java.awt.GridBagConstraints();
298: gridBagConstraints2.gridx = 0;
299: gridBagConstraints2.gridy = 0;
300: gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
301: gridBagConstraints2.insets = new java.awt.Insets(12, 12, 11, 11);
302: gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST;
303: gridBagConstraints2.weightx = 1.0;
304: gridBagConstraints2.weighty = 1.0;
305: jPanel1.add(cardPanel, gridBagConstraints2);
306:
307: gridBagConstraints1 = new java.awt.GridBagConstraints();
308: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
309: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
310: gridBagConstraints1.insets = new java.awt.Insets(12, 12, 0, 11);
311: gridBagConstraints1.weightx = 1.0;
312: gridBagConstraints1.weighty = 1.0;
313: add(jPanel1, gridBagConstraints1);
314:
315: advancedButton.setText("jButton1");
316: gridBagConstraints1 = new java.awt.GridBagConstraints();
317: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
318: gridBagConstraints1.gridheight = java.awt.GridBagConstraints.REMAINDER;
319: gridBagConstraints1.insets = new java.awt.Insets(12, 12, 0, 11);
320: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
321: add(advancedButton, gridBagConstraints1);
322:
323: }//GEN-END:initComponents
324:
325: // Variables declaration - do not modify//GEN-BEGIN:variables
326: private javax.swing.JComboBox editorsCombo;
327: private javax.swing.JLabel jLabel1;
328: private javax.swing.JPanel jPanel1;
329: private javax.swing.JPanel cardPanel;
330: private javax.swing.JButton advancedButton;
331:
332: // End of variables declaration//GEN-END:variables
333:
334: public Dimension getPreferredSize() {
335: Dimension inh = super .getPreferredSize();
336: return new Dimension(Math.max(inh.width, DEFAULT_WIDTH), Math
337: .max(inh.height, DEFAULT_HEIGHT));
338: }
339:
340: private void showAdvancedSettings() {
341: FormCustomEditorAdvanced fcea = new FormCustomEditorAdvanced(
342: preCode, postCode);
343: DialogDescriptor dd = new DialogDescriptor(fcea,
344: FormUtils
345: .getFormattedBundleString(
346: "FMT_CTL_AdvancedInitializationCode", // NOI18N
347: new Object[] { editor.getProperty()
348: .getName() }));
349:
350: dd.setHelpCtx(new HelpCtx("gui.source.modifying.property")); // NOI18N
351: DialogDisplayer.getDefault().createDialog(dd).show();
352:
353: if (dd.getValue() == DialogDescriptor.OK_OPTION) {
354: preCode = fcea.getPreCode();
355: postCode = fcea.getPostCode();
356: }
357: }
358:
359: }
|