2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.form;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.EventQueue;
47
import java.awt.event.ComponentAdapter;
48
import java.awt.event.ComponentEvent;
49
import javax.swing.BorderFactory;
50
import javax.swing.JEditorPane;
51
import javax.swing.JLabel;
52
import javax.swing.JScrollPane;
53
import javax.swing.JTextField;
54
import javax.swing.event.DocumentEvent;
55
import javax.swing.event.DocumentListener;
56
import org.jdesktop.layout.GroupLayout;
57
import org.netbeans.api.editor.guards.SimpleSection;
58
import org.openide.util.Utilities;
61
* Custom editor for RADConnectionPropertyEditor. Allows editing the custom code
66
class CodeCustomEditor extends javax.swing.JPanel implements DocumentListener, Runnable {
68
private RADConnectionPropertyEditor propertyEditor;
70
private JEditorPane codePane;
71
private boolean ignoreUpdate;
73
private JLabel headerLabel = new JLabel();
74
private JLabel footerLabel = new JLabel();
75
private JTextField typeField = new JTextField();
76
private JScrollPane jScrollPane = new JScrollPane() {
77
// We want the editor pane's height to accommodate to the actual number
78
// of lines. For that we also need to include the horizontal scrollbar
79
// height into the preferred height. See also invokeUpdate method.
80
public Dimension getPreferredSize() {
81
Dimension prefSize = super.getPreferredSize();
82
Component hBar = getHorizontalScrollBar();
83
if (hBar != null && hBar.isVisible()) {
84
prefSize = new Dimension(prefSize.width, prefSize.height + hBar.getPreferredSize().height);
90
public CodeCustomEditor(RADConnectionPropertyEditor propertyEditor,
91
FormModel formModel, FormProperty property)
93
this.propertyEditor = propertyEditor;
95
codePane = new JEditorPane();
96
jScrollPane.setViewportView(codePane);
101
FormDataObject dobj = FormEditor.getFormDataObject(formModel);
102
SimpleSection sec = dobj.getFormEditorSupport().getInitComponentSection();
103
if ((property instanceof RADProperty) && (property.getWriteMethod() != null)) {
104
RADComponent metacomp = ((RADProperty)property).getRADComponent();
105
headerLabel.setFont(codePane.getFont());
106
headerLabel.setText("<html>" + metacomp.getName() + ".<b>" // NOI18N
107
+ property.getWriteMethod().getName() + "</b>("); // NOI18N
108
footerLabel.setFont(codePane.getFont());
109
footerLabel.setText(");"); // NOI18N
112
if (metacomp != formModel.getTopRADComponent()) {
113
codeSnippet = " " + metacomp.getName() + "." + property.getWriteMethod().getName() + "("; // NOI18N
115
codeSnippet = " " + property.getWriteMethod().getName() + "("; // NOI18N
117
codePos = sec.getText().indexOf(codeSnippet);
119
codePos += codeSnippet.length();
123
headerLabel.setText(FormUtils.getBundleString("CodeCustomEditor.codeLabel")); // NOI18N
126
codePos = sec.getText().indexOf('{') + 2;
128
FormUtils.setupEditorPane(codePane, dobj.getPrimaryFile(), codePos + sec.getStartPosition().getOffset());
130
typeField.setBorder(BorderFactory.createEmptyBorder());
131
typeField.setEditable(false);
132
typeField.setFont(codePane.getFont());
133
typeField.setText(Utilities.getClassName(property.getValueType()));
135
codePane.getDocument().addDocumentListener(this);
138
addComponentListener(new ComponentAdapter() {
139
public void componentShown(ComponentEvent ev) {
146
private void resetLayout() {
147
GroupLayout layout = new GroupLayout(this);
148
this.setLayout(layout);
149
layout.setAutocreateContainerGaps(true);
150
layout.setHorizontalGroup(layout.createSequentialGroup()
151
.add(headerLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
153
.add(layout.createParallelGroup(GroupLayout.LEADING)
154
.add(jScrollPane, GroupLayout.PREFERRED_SIZE, 320, Short.MAX_VALUE)
162
if(jScrollPane.getPreferredSize().getHeight() > 300) {
164
maxHeight = GroupLayout.DEFAULT_SIZE;
166
prefHeight = GroupLayout.DEFAULT_SIZE;
167
maxHeight = GroupLayout.PREFERRED_SIZE;
170
layout.setVerticalGroup(layout.createSequentialGroup()
171
.add(layout.createParallelGroup(GroupLayout.LEADING)
172
.add(layout.createSequentialGroup()
173
.add(jScrollPane.getInsets().top)
174
.add(layout.createParallelGroup().add(headerLabel).add(footerLabel)))
175
.add(jScrollPane, GroupLayout.DEFAULT_SIZE, prefHeight, maxHeight))
176
.add(typeField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
179
void setValue(RADConnectionPropertyEditor.RADConnectionDesignValue value) {
180
if (value != null && value.getType() == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_CODE) {
182
codePane.setText(value.getCode());
183
ignoreUpdate = false;
190
public void insertUpdate(DocumentEvent e) {
195
public void removeUpdate(DocumentEvent e) {
200
public void changedUpdate(DocumentEvent e) {
203
private void invokeUpdate() {
206
EventQueue.invokeLater(this); // set the value
208
// also update the editor pane size according to the number of lines
209
// (can't just track line count changes because the preferred height
210
// also changes when the horizontal scrollbar appears/hides)
216
// updates the value in the property editor
218
propertyEditor.setValue(new RADConnectionPropertyEditor.RADConnectionDesignValue(codePane.getText()));
219
ignoreUpdate = false;