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