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