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.Component;
045: import java.awt.Dimension;
046: import java.awt.GridBagConstraints;
047: import java.awt.GridBagLayout;
048: import java.awt.Insets;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JTextField;
052: import org.netbeans.modules.vmd.api.model.PropertyValue;
053: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
054: import org.netbeans.modules.vmd.midp.components.MidpTypes;
055: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
056: import org.openide.util.NbBundle;
057:
058: /**
059: *
060: * @author Anton Chechel
061: */
062: public class PropertyEditorVersion extends DesignPropertyEditor {
063:
064: private final CustomEditor customEditor;
065:
066: private PropertyEditorVersion() {
067: customEditor = new CustomEditor();
068: }
069:
070: public static final PropertyEditorVersion createInstance() {
071: return new PropertyEditorVersion();
072: }
073:
074: @Override
075: public Component getCustomEditor() {
076: PropertyValue value = (PropertyValue) super .getValue();
077: if (value != null && value.getKind() != PropertyValue.Kind.NULL) {
078: customEditor.setText((String) value.getPrimitiveValue());
079: }
080: return customEditor;
081: }
082:
083: @Override
084: public boolean supportsCustomEditor() {
085: return false;
086: }
087:
088: @Override
089: public Boolean canEditAsText() {
090: return false;
091: }
092:
093: @Override
094: public String getAsText() {
095: PropertyValue value = (PropertyValue) super .getValue();
096: if (value == null || value.getKind() == PropertyValue.Kind.NULL) {
097: return PropertyEditorUserCode.NULL_TEXT;
098: }
099: return (String) value.getPrimitiveValue();
100: }
101:
102: @Override
103: public void setAsText(String text) {
104: PropertyValue value = (PropertyValue) super .getValue();
105: if (value != null && value.getPrimitiveValue().equals(text)) {
106: return;
107: }
108: if (!PropertyEditorUserCode.NULL_TEXT.equals(text)) {
109: saveValue(text);
110: }
111: }
112:
113: private void saveValue(final String text) {
114: if (text.length() > 0) {
115: super .setValue(MidpTypes.createStringValue(text));
116: }
117: }
118:
119: @Override
120: public void customEditorOKButtonPressed() {
121: super .customEditorOKButtonPressed();
122: saveValue(customEditor.getText());
123: }
124:
125: @Override
126: public boolean supportsDefaultValue() {
127: return false;
128: }
129:
130: private final class CustomEditor extends JPanel {
131: private JTextField textField;
132:
133: public CustomEditor() {
134: initComponents();
135: }
136:
137: private void initComponents() {
138: setLayout(new GridBagLayout());
139: GridBagConstraints constraints = new GridBagConstraints();
140: JLabel label = new JLabel(NbBundle.getMessage(
141: PropertyEditorVersion.class, "LBL_VERSION_STR")); // NOI18N
142: constraints.insets = new Insets(12, 12, 12, 6);
143: constraints.anchor = GridBagConstraints.NORTHWEST;
144: constraints.gridx = 0;
145: constraints.gridy = 0;
146: constraints.weightx = 0.0;
147: constraints.weighty = 0.0;
148: constraints.fill = GridBagConstraints.BOTH;
149: add(label, constraints);
150:
151: textField = new JTextField();
152: constraints.insets = new Insets(12, 6, 12, 12);
153: constraints.anchor = GridBagConstraints.NORTHWEST;
154: constraints.gridx = 1;
155: constraints.gridy = 0;
156: constraints.weightx = 1.0;
157: constraints.weighty = 0.0;
158: constraints.fill = GridBagConstraints.BOTH;
159: add(textField, constraints);
160:
161: setPreferredSize(new Dimension(300, 40));
162: }
163:
164: public void setText(String text) {
165: textField.setText(text);
166: }
167:
168: public String getText() {
169: return textField.getText();
170: }
171: }
172: }
|