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 java.lang.ref.WeakReference;
050: import javax.swing.JLabel;
051: import javax.swing.JPanel;
052: import javax.swing.JTextField;
053: import org.netbeans.modules.vmd.api.model.DesignComponent;
054: import org.netbeans.modules.vmd.api.model.DesignDocument;
055: import org.netbeans.modules.vmd.api.model.PropertyValue;
056: import org.netbeans.modules.vmd.api.model.TypeID;
057: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
058: import org.netbeans.modules.vmd.midp.codegen.InstanceNameResolver;
059: import org.netbeans.modules.vmd.midp.components.MidpTypes;
060: import org.netbeans.modules.vmd.midp.components.points.MethodPointCD;
061: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author Karol Harezlak
067: * @author Anton Chechel
068: */
069: public final class PropertyEditorInstanceName extends
070: DesignPropertyEditor {
071:
072: private static final String METHOD_NAME_TEXT = NbBundle.getMessage(
073: PropertyEditorInstanceName.class, "LBL_METHOD_NAME"); // NOI18N
074: private static final String INSTANCE_NAME_TEXT = NbBundle
075: .getMessage(PropertyEditorInstanceName.class,
076: "LBL_INSTANCE_NAME"); // NOI18N
077:
078: private TypeID typeID;
079: private final CustomEditor customEditor;
080: private WeakReference<DesignComponent> component;
081:
082: private PropertyEditorInstanceName(TypeID typeID) {
083: this .typeID = typeID;
084: customEditor = new CustomEditor();
085: }
086:
087: public static final DesignPropertyEditor createInstance(
088: TypeID typeID) {
089: return new PropertyEditorInstanceName(typeID);
090: }
091:
092: @Override
093: public Component getCustomEditor() {
094: PropertyValue value = (PropertyValue) super .getValue();
095: if (value != null) {
096: customEditor.setText(MidpTypes.getString(value));
097: }
098: return customEditor;
099: }
100:
101: @Override
102: public String getAsText() {
103: PropertyValue value = (PropertyValue) super .getValue();
104: return value == null ? PropertyEditorUserCode.NULL_TEXT
105: : MidpTypes.getString(value);
106: }
107:
108: @Override
109: public void setAsText(String text) {
110: PropertyValue value = (PropertyValue) super .getValue();
111: if (value == null) {
112: return;
113: }
114: Object pv = value.getPrimitiveValue();
115: if (pv != null && pv.equals(text)) {
116: return;
117: }
118:
119: saveValue(text);
120: }
121:
122: private void saveValue(final String text) {
123: if (component != null && component.get() != null) {
124: final DesignComponent _component = component.get();
125: _component.getDocument().getTransactionManager()
126: .readAccess(new Runnable() {
127:
128: public void run() {
129: PropertyValue newInstanceName = InstanceNameResolver
130: .createFromSuggested(_component,
131: text);
132: PropertyEditorInstanceName.super
133: .setValue(newInstanceName);
134: }
135: });
136: }
137: }
138:
139: @Override
140: public void customEditorOKButtonPressed() {
141: String text = customEditor.getText();
142: if (text.length() > 0) {
143: saveValue(text);
144: }
145: }
146:
147: @Override
148: public void init(DesignComponent component) {
149: this .component = new WeakReference<DesignComponent>(component);
150: }
151:
152: @Override
153: public boolean supportsDefaultValue() {
154: return false;
155: }
156:
157: @Override
158: public boolean canWrite() {
159: if (component == null || component.get() == null) {
160: return false;
161: }
162:
163: final boolean[] canWrite = new boolean[1];
164: final DesignDocument document = component.get().getDocument();
165: document.getTransactionManager().readAccess(new Runnable() {
166:
167: public void run() {
168: canWrite[0] = document.getSelectedComponents().size() <= 1;
169: }
170: });
171:
172: return canWrite[0];
173: }
174:
175: private String getLabelName() {
176: return MethodPointCD.TYPEID.equals(typeID) ? METHOD_NAME_TEXT
177: : INSTANCE_NAME_TEXT;
178: }
179:
180: private final class CustomEditor extends JPanel {
181:
182: private JTextField textField;
183:
184: public CustomEditor() {
185: initComponents();
186: }
187:
188: private void initComponents() {
189: setLayout(new GridBagLayout());
190: GridBagConstraints constraints = new GridBagConstraints();
191: JLabel label = new JLabel(getLabelName());
192: constraints.insets = new Insets(12, 12, 3, 12);
193: constraints.anchor = GridBagConstraints.NORTHWEST;
194: constraints.gridx = 0;
195: constraints.gridy = 0;
196: constraints.weightx = 0.0;
197: constraints.weighty = 0.0;
198: constraints.fill = GridBagConstraints.NONE;
199: add(label, constraints);
200:
201: textField = new JTextField();
202: constraints.insets = new Insets(0, 12, 12, 12);
203: constraints.anchor = GridBagConstraints.NORTHWEST;
204: constraints.gridx = 0;
205: constraints.gridy = 1;
206: constraints.weightx = 1.0;
207: constraints.weighty = 0.0;
208: constraints.fill = GridBagConstraints.HORIZONTAL;
209: add(textField, constraints);
210:
211: JPanel spacer = new JPanel();
212: constraints.insets = new Insets(0, 0, 0, 0);
213: constraints.anchor = GridBagConstraints.NORTHWEST;
214: constraints.gridx = 0;
215: constraints.gridy = 2;
216: constraints.weightx = 1.0;
217: constraints.weighty = 1.0;
218: constraints.fill = GridBagConstraints.BOTH;
219: add(spacer, constraints);
220: setPreferredSize(new Dimension(300, 64));
221: }
222:
223: public void setText(String text) {
224: textField.setText(text);
225: }
226:
227: public String getText() {
228: return textField.getText();
229: }
230: }
231: }
|