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