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.vmd.midp.propertyeditors;
043:
044: import java.awt.event.ItemEvent;
045: import org.netbeans.modules.vmd.api.model.PropertyValue;
046: import org.netbeans.modules.vmd.midp.components.MidpTypes;
047: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
048: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
049: import org.openide.awt.Mnemonics;
050: import org.openide.explorer.propertysheet.InplaceEditor;
051: import org.openide.util.NbBundle;
052: import java.awt.BorderLayout;
053: import java.awt.Graphics;
054: import java.awt.Rectangle;
055: import java.awt.event.ActionEvent;
056: import java.awt.event.ActionListener;
057: import java.awt.event.ItemListener;
058: import java.util.Collections;
059: import javax.swing.*;
060: import org.netbeans.modules.vmd.api.model.DesignComponent;
061: import org.netbeans.modules.vmd.api.model.TypeID;
062:
063: /**
064: *
065: * @author Anton Chechel
066: */
067: public class PropertyEditorBooleanUC extends PropertyEditorUserCode
068: implements PropertyEditorElement {
069:
070: private static final PropertyValue TRUE_VALUE = MidpTypes
071: .createBooleanValue(true);
072: private static final PropertyValue FALSE_VALUE = MidpTypes
073: .createBooleanValue(false);
074: private CustomEditor customEditor;
075: private JRadioButton radioButton;
076: private BooleanInplaceEditor inplaceEditor;
077: private boolean supportsCustomEditor;
078: private TypeID parentTypeID;
079: private String rbLabel;
080:
081: private PropertyEditorBooleanUC(boolean supportsCustomEditor,
082: TypeID parentTypeID, String rbLabel) {
083: super (NbBundle.getMessage(PropertyEditorBooleanUC.class,
084: "LBL_VALUE_BOOLEAN_UCLABEL")); // NOI18N
085: this .supportsCustomEditor = supportsCustomEditor;
086: this .parentTypeID = parentTypeID;
087: this .rbLabel = rbLabel;
088:
089: initElements(Collections
090: .<PropertyEditorElement> singleton(this ));
091: }
092:
093: public static PropertyEditorBooleanUC createInstance() {
094: return new PropertyEditorBooleanUC(false, null, null);
095: }
096:
097: public static PropertyEditorBooleanUC createInstance(String rbLabel) {
098: return new PropertyEditorBooleanUC(true, null, rbLabel);
099: }
100:
101: public static PropertyEditorBooleanUC createInstance(
102: TypeID parentTypeID, String rbLabel) {
103: return new PropertyEditorBooleanUC(true, parentTypeID, rbLabel);
104: }
105:
106: @Override
107: public InplaceEditor getInplaceEditor() {
108: if (inplaceEditor == null) {
109: inplaceEditor = new BooleanInplaceEditor(this );
110: PropertyValue propertyValue = (PropertyValue) getValue();
111: Boolean value = (Boolean) propertyValue.getPrimitiveValue();
112: JCheckBox checkBox = (JCheckBox) inplaceEditor
113: .getComponent();
114: if (value != null) {
115: checkBox.setSelected(value);
116: }
117: checkBox.addItemListener(new ItemListener() {
118: public void itemStateChanged(ItemEvent e) {
119: JCheckBox checkBox = (JCheckBox) inplaceEditor
120: .getComponent();
121: PropertyValue value = MidpTypes
122: .createBooleanValue(checkBox.isSelected());
123: PropertyEditorBooleanUC.this .setValue(value);
124: PropertyEditorBooleanUC.this .invokeSaveToModel();
125: }
126: });
127: } else {
128: PropertyValue propertyValue = (PropertyValue) getValue();
129: Boolean value = (Boolean) propertyValue.getPrimitiveValue();
130: JCheckBox checkBox = (JCheckBox) inplaceEditor
131: .getComponent();
132: if (value == null || value == false) {
133: checkBox.setSelected(false);
134: } else {
135: checkBox.setSelected(true);
136: }
137: }
138: return inplaceEditor;
139: }
140:
141: @Override
142: public void paintValue(Graphics gfx, Rectangle box) {
143: JComponent _component = inplaceEditor.getComponent();
144: _component.setSize(box.width, box.height);
145: _component.doLayout();
146: _component.setBorder(BorderFactory
147: .createEmptyBorder(0, 0, 0, 0));
148: Graphics g = gfx.create(box.x, box.y, box.width, box.height);
149: _component.setOpaque(false);
150: _component.paint(g);
151: g.dispose();
152: }
153:
154: @Override
155: public boolean supportsCustomEditor() {
156: if (!isWriteableByParentType()) {
157: return false;
158: }
159:
160: return supportsCustomEditor ? super .supportsCustomEditor()
161: : false;
162: }
163:
164: public JComponent getCustomEditorComponent() {
165: if (customEditor == null) {
166: customEditor = new CustomEditor();
167: }
168: return customEditor;
169: }
170:
171: public JRadioButton getRadioButton() {
172: if (radioButton == null) {
173: radioButton = new JRadioButton();
174: Mnemonics.setLocalizedText(radioButton, NbBundle
175: .getMessage(PropertyEditorBooleanUC.class,
176: "LBL_VALUE_BOOLEAN")); // NOI18N
177: }
178: return radioButton;
179: }
180:
181: @Override
182: public boolean isPaintable() {
183: PropertyValue propertyValue = (PropertyValue) getValue();
184: return propertyValue.getKind() == PropertyValue.Kind.VALUE;
185: }
186:
187: public boolean isVerticallyResizable() {
188: return false;
189: }
190:
191: public boolean isInitiallySelected() {
192: return false;
193: }
194:
195: @Override
196: public String getAsText() {
197: if (isCurrentValueAUserCodeType()) {
198: return USER_CODE_TEXT;
199: } else if (isCurrentValueANull()) {
200: return "false"; // NOI18N
201: }
202: return MidpTypes.getBoolean((PropertyValue) super .getValue()) ? "true"
203: : "false"; // NOI18N
204: }
205:
206: public void setTextForPropertyValue(String text) {
207: saveValue(text);
208: }
209:
210: public String getTextForPropertyValue() {
211: return null;
212: }
213:
214: public void updateState(PropertyValue value) {
215: customEditor.setValue(value);
216: radioButton.setSelected(!isCurrentValueAUserCodeType());
217: }
218:
219: private void saveValue(String text) {
220: super .setValue("false".equals(text) ? FALSE_VALUE : TRUE_VALUE); // NOI18N
221: }
222:
223: @Override
224: public void customEditorOKButtonPressed() {
225: super .customEditorOKButtonPressed();
226: if (radioButton.isSelected()) {
227: saveValue(customEditor.getText());
228: if ("true".equals(customEditor.getText())) { // NOI18N
229: updateInplaceEditorComponent(true);
230: } else {
231: updateInplaceEditorComponent(false);
232: }
233: }
234: }
235:
236: @Override
237: public boolean canWrite() {
238: if (!isWriteableByParentType()) {
239: return false;
240: }
241:
242: return MidpPropertyEditorSupport
243: .singleSelectionEditAsTextOnly();
244: }
245:
246: @Override
247: public Object getDefaultValue() {
248: PropertyValue value = (PropertyValue) super .getDefaultValue();
249: if (value.getKind() == PropertyValue.Kind.VALUE
250: && value.getPrimitiveValue() instanceof Boolean) {
251: updateInplaceEditorComponent((Boolean) value
252: .getPrimitiveValue());
253: }
254: return super .getDefaultValue();
255: }
256:
257: private boolean isWriteableByParentType() {
258: if (component == null || component.get() == null) {
259: return false;
260: }
261:
262: if (parentTypeID != null) {
263: final DesignComponent _component = component.get();
264: final DesignComponent[] parent = new DesignComponent[1];
265: _component.getDocument().getTransactionManager()
266: .readAccess(new Runnable() {
267:
268: public void run() {
269: parent[0] = _component.getParentComponent();
270: }
271: });
272:
273: if (parent[0] != null
274: && parentTypeID.equals(parent[0].getType())) {
275: return false;
276: }
277: }
278: return true;
279: }
280:
281: private void updateInplaceEditorComponent(boolean selected) {
282: JCheckBox ic = (JCheckBox) inplaceEditor.getComponent();
283: ic.setSelected(selected);
284: }
285:
286: private class CustomEditor extends JPanel implements ActionListener {
287:
288: private JCheckBox checkBox;
289:
290: public CustomEditor() {
291: initComponents();
292: }
293:
294: private void initComponents() {
295: setLayout(new BorderLayout());
296: checkBox = new JCheckBox();
297: if (rbLabel != null) {
298: Mnemonics.setLocalizedText(checkBox, rbLabel);
299: }
300: checkBox.addActionListener(this );
301: add(checkBox, BorderLayout.CENTER);
302: }
303:
304: public void setValue(PropertyValue value) {
305: checkBox.setSelected(value != null
306: && value.getPrimitiveValue() != null
307: && MidpTypes.getBoolean(value));
308: }
309:
310: public String getText() {
311: return checkBox.isSelected() ? "true" : "false"; // NOI18N
312: }
313:
314: public void actionPerformed(ActionEvent evt) {
315: radioButton.setSelected(true);
316: }
317: }
318: }
|