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.Dimension;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.Insets;
048: import java.awt.event.ActionEvent;
049: import java.awt.event.ActionListener;
050: import java.awt.event.FocusEvent;
051: import java.awt.event.FocusListener;
052: import java.util.Collections;
053: import java.util.regex.Pattern;
054: import javax.swing.JCheckBox;
055: import javax.swing.JComponent;
056: import javax.swing.JPanel;
057: import javax.swing.JRadioButton;
058: import javax.swing.JTextField;
059: import javax.swing.event.DocumentEvent;
060: import javax.swing.event.DocumentListener;
061: import org.netbeans.modules.vmd.api.model.DesignComponent;
062: import org.netbeans.modules.vmd.api.model.PropertyValue;
063: import org.netbeans.modules.vmd.midp.components.MidpTypes;
064: import org.netbeans.modules.vmd.midp.components.items.GaugeCD;
065: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
066: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
067: import org.openide.awt.Mnemonics;
068: import org.openide.util.NbBundle;
069:
070: /**
071: *
072: * @author Anton Chechel
073: */
074: public final class PropertyEditorGaugeMaxValue extends
075: PropertyEditorUserCode implements PropertyEditorElement {
076:
077: private static final String INDEFINITE_TEXT = NbBundle.getMessage(
078: PropertyEditorGaugeMaxValue.class,
079: "LBL_MAX_VALUE_INDEFINITE_TXT"); // NOI18N
080: private static final String INDEFINITE_NUM_TEXT = String
081: .valueOf(GaugeCD.VALUE_INDEFINITE);
082:
083: private CustomEditor customEditor;
084: private JRadioButton radioButton;
085:
086: private PropertyEditorGaugeMaxValue() {
087: super (NbBundle.getMessage(PropertyEditorGaugeMaxValue.class,
088: "LBL_GAUGE_MAX_VALUE_UCLABEL")); // NOI18N
089: initComponents();
090:
091: initElements(Collections
092: .<PropertyEditorElement> singleton(this ));
093: }
094:
095: public static final PropertyEditorGaugeMaxValue createInstance() {
096: return new PropertyEditorGaugeMaxValue();
097: }
098:
099: private void initComponents() {
100: radioButton = new JRadioButton();
101: Mnemonics.setLocalizedText(radioButton, NbBundle.getMessage(
102: PropertyEditorGaugeMaxValue.class,
103: "LBL_GAUGE_MAX_VALUE_STR")); // NOI18N
104: customEditor = new CustomEditor();
105: }
106:
107: public JComponent getCustomEditorComponent() {
108: return customEditor;
109: }
110:
111: public JRadioButton getRadioButton() {
112: return radioButton;
113: }
114:
115: public boolean isInitiallySelected() {
116: return true;
117: }
118:
119: public boolean isVerticallyResizable() {
120: return false;
121: }
122:
123: @Override
124: public String getAsText() {
125: String super Text = super .getAsText();
126: if (super Text != null) {
127: return super Text;
128: }
129:
130: PropertyValue value = (PropertyValue) super .getValue();
131: if (value == null) {
132: return INDEFINITE_TEXT;
133: }
134: int intValue = MidpTypes.getInteger(value);
135: if (intValue == GaugeCD.VALUE_INDEFINITE) {
136: return INDEFINITE_TEXT;
137: }
138: return String.valueOf(intValue);
139: }
140:
141: public void setTextForPropertyValue(String text) {
142: saveValue(text);
143: }
144:
145: public String getTextForPropertyValue() {
146: return null;
147: }
148:
149: public void updateState(PropertyValue value) {
150: if (isCurrentValueANull() || value == null) {
151: customEditor.unsetForever(true);
152: } else if (MidpTypes.getInteger(value) == GaugeCD.VALUE_INDEFINITE) {
153: customEditor.setForever(true);
154: } else {
155: customEditor.unsetForever(true);
156: customEditor.setText(String.valueOf(value
157: .getPrimitiveValue()));
158: }
159: radioButton.setSelected(!isCurrentValueAUserCodeType());
160: }
161:
162: private void saveValue(String text) {
163: if (text.length() <= 0) {
164: return;
165: }
166:
167: if (INDEFINITE_TEXT.equals(text)
168: || INDEFINITE_NUM_TEXT.equals(text)) {
169: if (component != null && component.get() != null) {
170: final DesignComponent _component = component.get();
171: _component.getDocument().getTransactionManager()
172: .writeAccess(new Runnable() {
173:
174: public void run() {
175: _component
176: .writeProperty(
177: GaugeCD.PROP_VALUE,
178: MidpTypes
179: .createIntegerValue(GaugeCD.VALUE_CONTINUOUS_IDLE));
180: }
181: });
182: }
183: super .setValue(MidpTypes
184: .createIntegerValue(GaugeCD.VALUE_INDEFINITE));
185: } else {
186: int intValue = 0;
187: try {
188: text = text.replaceAll("[^0-9\\-]+", ""); // NOI18N
189: intValue = Integer.parseInt(text);
190: } catch (NumberFormatException e) {
191: }
192: super .setValue(MidpTypes.createIntegerValue(intValue));
193: }
194: }
195:
196: @Override
197: public void customEditorOKButtonPressed() {
198: super .customEditorOKButtonPressed();
199: if (radioButton.isSelected()) {
200: saveValue(customEditor.getText());
201: }
202: }
203:
204: @Override
205: public Boolean canEditAsText() {
206: if (!isCurrentValueAUserCodeType()) {
207: PropertyValue value = (PropertyValue) super .getValue();
208: if (value == null) {
209: return false;
210: }
211: int intValue = MidpTypes.getInteger(value);
212: return intValue != GaugeCD.VALUE_INDEFINITE;
213: }
214: return false;
215: }
216:
217: private class CustomEditor extends JPanel implements
218: ActionListener, DocumentListener, FocusListener {
219:
220: private JTextField textField;
221: private JCheckBox foreverCheckBox;
222:
223: public CustomEditor() {
224: radioButton.addFocusListener(this );
225: initComponents();
226: }
227:
228: private void initComponents() {
229: setLayout(new GridBagLayout());
230:
231: textField = new JTextField();
232: textField.getDocument().addDocumentListener(this );
233: textField.addFocusListener(this );
234: textField.setPreferredSize(new Dimension(100, textField
235: .getPreferredSize().height));
236: add(textField, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
237: GridBagConstraints.WEST, GridBagConstraints.NONE,
238: new Insets(0, 0, 0, 0), 0, 0));
239:
240: foreverCheckBox = new JCheckBox();
241: Mnemonics.setLocalizedText(foreverCheckBox, NbBundle
242: .getMessage(PropertyEditorGaugeMaxValue.class,
243: "LBL_MAX_VALUE_INDEFINITE")); // NOI18N
244: foreverCheckBox.addActionListener(this );
245: foreverCheckBox.addFocusListener(this );
246: add(foreverCheckBox, new GridBagConstraints(1, 0, 1, 1,
247: 0.0, 0.0, GridBagConstraints.WEST,
248: GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0,
249: 0));
250:
251: add(new JPanel(), new GridBagConstraints(2, 0, 1, 1, 1.0,
252: 0.0, GridBagConstraints.WEST,
253: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0,
254: 0));
255: }
256:
257: public void setText(String text) {
258: textField.setText(text);
259: }
260:
261: public String getText() {
262: return textField.getText();
263: }
264:
265: public void setForever(boolean changeCheckBox) {
266: setText(INDEFINITE_NUM_TEXT);
267: textField.setEditable(false);
268: if (changeCheckBox) {
269: foreverCheckBox.setSelected(true);
270: }
271: }
272:
273: public void unsetForever(boolean changeCheckBox) {
274: setText(null);
275: textField.setEditable(true);
276: if (changeCheckBox) {
277: foreverCheckBox.setSelected(false);
278: }
279: }
280:
281: public void actionPerformed(ActionEvent evt) {
282: if (foreverCheckBox.isSelected()) {
283: setForever(false);
284: } else {
285: unsetForever(false);
286: }
287: }
288:
289: public void insertUpdate(DocumentEvent evt) {
290: if (textField.hasFocus()) {
291: radioButton.setSelected(true);
292: checkNumberStatus();
293: }
294: }
295:
296: public void removeUpdate(DocumentEvent evt) {
297: if (textField.hasFocus()) {
298: radioButton.setSelected(true);
299: checkNumberStatus();
300: }
301: }
302:
303: public void changedUpdate(DocumentEvent evt) {
304: }
305:
306: private void checkNumberStatus() {
307: if (!Pattern.matches("[\\d\\-]+", textField.getText())) { // NOI18N
308: displayWarning(PropertyEditorNumber.NON_DIGITS_TEXT);
309: } else {
310: clearErrorStatus();
311: }
312: }
313:
314: public void focusGained(FocusEvent e) {
315: if (e.getSource() == radioButton
316: || e.getSource() == textField
317: || e.getSource() == foreverCheckBox) {
318: checkNumberStatus();
319: }
320: }
321:
322: public void focusLost(FocusEvent e) {
323: clearErrorStatus();
324: }
325: }
326: }
|