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.JEditorPane;
051: import javax.swing.JLabel;
052: import javax.swing.JPanel;
053: import javax.swing.JScrollPane;
054: import javax.swing.SwingUtilities;
055: import javax.swing.text.Document;
056: import org.netbeans.api.java.source.JavaSource;
057: import org.netbeans.api.java.source.ui.DialogBinding;
058: import org.netbeans.modules.vmd.api.io.DataObjectContext;
059: import org.netbeans.modules.vmd.api.io.ProjectUtils;
060: import org.netbeans.modules.vmd.api.model.DesignComponent;
061: import org.netbeans.modules.vmd.api.model.PropertyValue;
062: import org.netbeans.modules.vmd.api.model.TypeID;
063: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
064: import org.netbeans.modules.vmd.midp.components.MidpTypes;
065: import org.netbeans.modules.vmd.midp.components.points.CallPointCD;
066: import org.netbeans.modules.vmd.midp.components.points.IfPointCD;
067: import org.netbeans.modules.vmd.midp.components.points.MethodPointCD;
068: import org.netbeans.modules.vmd.midp.components.points.SwitchPointCD;
069: import org.netbeans.modules.vmd.midp.components.sources.SwitchCaseEventSourceCD;
070: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.CodeUtils;
071: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
072: import org.openide.util.NbBundle;
073:
074: /**
075: *
076: * @author Anton Chechel
077: */
078: public final class PropertyEditorJavaString extends
079: DesignPropertyEditor {
080:
081: private static final String JAVA_CODE = NbBundle.getMessage(
082: PropertyEditorJavaString.class, "LBL_JAVA_CODE_STR"); // NOI18N
083: private static final String METHOD_NAME = NbBundle.getMessage(
084: PropertyEditorJavaString.class, "LBL_METHOD_NAME_STR"); // NOI18N
085: private static final String CONDITION_EXPRESSION = NbBundle
086: .getMessage(PropertyEditorJavaString.class,
087: "LBL_CONDITION_EXPRESSION_STR"); // NOI18N
088: private static final String SWITCH_OPERAND = NbBundle.getMessage(
089: PropertyEditorJavaString.class, "LBL_SWITCH_OPERAND_STR"); // NOI18N
090: private static final String CASE_OPERAND = NbBundle.getMessage(
091: PropertyEditorJavaString.class, "LBL_CASE_OPERAND_STR"); // NOI18N
092: private static final String JAVA_EXPRESSION = NbBundle.getMessage(
093: PropertyEditorJavaString.class, "LBL_JAVA_EXPRESSION_STR"); // NOI18N
094: protected WeakReference<DesignComponent> component;
095: private TypeID typeID;
096: private final CustomEditor customEditor;
097:
098: private PropertyEditorJavaString(TypeID typeID) {
099: this .typeID = typeID;
100: customEditor = new CustomEditor();
101: }
102:
103: public static final PropertyEditorJavaString createInstance(
104: TypeID typeID) {
105: return new PropertyEditorJavaString(typeID);
106: }
107:
108: @Override
109: public Component getCustomEditor() {
110: PropertyValue value = (PropertyValue) super .getValue();
111: if (value != null) {
112: customEditor.setText(MidpTypes.getJavaCode(value));
113: }
114: customEditor.init();
115: return customEditor;
116: }
117:
118: @Override
119: public String getAsText() {
120: PropertyValue value = (PropertyValue) super .getValue();
121: if (value == null) {
122: return PropertyEditorUserCode.NULL_TEXT;
123: }
124: return MidpTypes.getJavaCode(value);
125: }
126:
127: @Override
128: public void setAsText(String text) {
129: PropertyValue value = (PropertyValue) super .getValue();
130: if (value == null) {
131: return;
132: }
133: Object pv = value.getPrimitiveValue();
134: if (pv != null && pv.equals(text)) {
135: return;
136: }
137: saveValue(text);
138: }
139:
140: private void saveValue(String text) {
141: if (text != null) {
142: super .setValue(MidpTypes.createJavaCodeValue(text));
143: }
144: }
145:
146: @Override
147: public void customEditorOKButtonPressed() {
148: String text = customEditor.getText();
149: saveValue(text);
150: }
151:
152: @Override
153: public boolean supportsDefaultValue() {
154: return true;
155: }
156:
157: @Override
158: public String getCustomEditorTitle() {
159: return getLabelName();
160: }
161:
162: @Override
163: public void init(DesignComponent component) {
164: if (component != null) {
165: this .component = new WeakReference<DesignComponent>(
166: component);
167: }
168: }
169:
170: private String getLabelName() {
171: if (typeID.equals(CallPointCD.TYPEID)) {
172: return JAVA_CODE;
173: } else if (typeID.equals(MethodPointCD.TYPEID)) {
174: return METHOD_NAME;
175: } else if (typeID.equals(IfPointCD.TYPEID)) {
176: return CONDITION_EXPRESSION;
177: } else if (typeID.equals(SwitchPointCD.TYPEID)) {
178: return SWITCH_OPERAND;
179: } else if (typeID.equals(SwitchCaseEventSourceCD.TYPEID)) {
180: return CASE_OPERAND;
181: }
182: return JAVA_EXPRESSION;
183: }
184:
185: private final class CustomEditor extends JPanel {
186:
187: private JEditorPane textPane;
188:
189: public CustomEditor() {
190: initComponents();
191: }
192:
193: private void initComponents() {
194: setLayout(new GridBagLayout());
195: GridBagConstraints constraints = new GridBagConstraints();
196: JLabel label = new JLabel(getLabelName());
197: constraints.insets = new Insets(12, 12, 3, 12);
198: constraints.anchor = GridBagConstraints.NORTHWEST;
199: constraints.gridx = 0;
200: constraints.gridy = 0;
201: constraints.weightx = 0.0;
202: constraints.weighty = 0.0;
203: constraints.fill = GridBagConstraints.BOTH;
204: add(label, constraints);
205:
206: textPane = new JEditorPane();
207: SwingUtilities.invokeLater(new Runnable() {
208:
209: //otherwise we get: java.lang.AssertionError: BaseKit.install() incorrectly called from non-AWT thread.
210: public void run() {
211: textPane.setContentType("text/x-java"); // NOI18N
212: }
213: });
214: textPane.setPreferredSize(new Dimension(400, 100));
215: JScrollPane jsp = new JScrollPane();
216: jsp.setViewportView(textPane);
217: constraints.insets = new Insets(0, 12, 12, 12);
218: constraints.anchor = GridBagConstraints.NORTHWEST;
219: constraints.gridx = 0;
220: constraints.gridy = 1;
221: constraints.weightx = 1.0;
222: constraints.weighty = 1.0;
223: constraints.fill = GridBagConstraints.BOTH;
224: add(jsp, constraints);
225: }
226:
227: public void setText(String text) {
228: textPane.setText(text);
229: }
230:
231: public String getText() {
232: return textPane.getText();
233: }
234:
235: public void init() {
236: if (component == null || component.get() == null) {
237: return;
238: }
239: DesignComponent _component = component.get();
240:
241: javax.swing.text.Document swingDoc = textPane.getDocument();
242: if (swingDoc.getProperty(JavaSource.class) == null) {
243: DataObjectContext context = ProjectUtils
244: .getDataObjectContextForDocument(_component
245: .getDocument());
246: swingDoc.putProperty(
247: Document.StreamDescriptionProperty, context
248: .getDataObject());
249: int offset = CodeUtils.getMethodOffset(context);
250: DialogBinding.bindComponentToFile(context
251: .getDataObject().getPrimaryFile(), offset, 0,
252: textPane);
253: }
254: }
255: }
256: }
|