~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/CodeCustomEditor.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.form;
 
43
 
 
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;
 
59
 
 
60
/**
 
61
 * Custom editor for RADConnectionPropertyEditor. Allows editing the custom code
 
62
 * of a property.
 
63
 *
 
64
 * @author Tomas Pavek
 
65
 */
 
66
class CodeCustomEditor extends javax.swing.JPanel implements DocumentListener, Runnable {
 
67
 
 
68
    private RADConnectionPropertyEditor propertyEditor;
 
69
 
 
70
    private JEditorPane codePane;
 
71
    private boolean ignoreUpdate;
 
72
 
 
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);
 
85
            }
 
86
            return prefSize;
 
87
        }
 
88
    };
 
89
        
 
90
    public CodeCustomEditor(RADConnectionPropertyEditor propertyEditor,
 
91
                            FormModel formModel, FormProperty property)
 
92
    {
 
93
        this.propertyEditor = propertyEditor;
 
94
        
 
95
        codePane = new JEditorPane();
 
96
        jScrollPane.setViewportView(codePane);
 
97
        
 
98
        resetLayout();
 
99
 
 
100
        int codePos = -1;
 
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
 
110
 
 
111
            String codeSnippet;
 
112
            if (metacomp != formModel.getTopRADComponent()) {
 
113
                codeSnippet = " " + metacomp.getName() + "." + property.getWriteMethod().getName() + "("; // NOI18N
 
114
            } else {
 
115
                codeSnippet = " " + property.getWriteMethod().getName() + "("; // NOI18N
 
116
            }
 
117
            codePos = sec.getText().indexOf(codeSnippet);
 
118
            if (codePos >= 0) {
 
119
                codePos += codeSnippet.length();
 
120
            }
 
121
        }
 
122
        else {
 
123
            headerLabel.setText(FormUtils.getBundleString("CodeCustomEditor.codeLabel")); // NOI18N
 
124
        }
 
125
        if (codePos < 0) {
 
126
            codePos = sec.getText().indexOf('{') + 2;
 
127
        }
 
128
        FormUtils.setupEditorPane(codePane, dobj.getPrimaryFile(), codePos + sec.getStartPosition().getOffset());
 
129
 
 
130
        typeField.setBorder(BorderFactory.createEmptyBorder());
 
131
        typeField.setEditable(false);
 
132
        typeField.setFont(codePane.getFont());
 
133
        typeField.setText(Utilities.getClassName(property.getValueType()));
 
134
 
 
135
        codePane.getDocument().addDocumentListener(this);
 
136
 
 
137
        // issue 103809
 
138
        addComponentListener(new ComponentAdapter() {
 
139
            public void componentShown(ComponentEvent ev) {
 
140
                revalidate();
 
141
                repaint();
 
142
            }
 
143
        });
 
144
    }
 
145
 
 
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)
 
152
            .add(3)
 
153
            .add(layout.createParallelGroup(GroupLayout.LEADING)
 
154
                .add(jScrollPane, GroupLayout.PREFERRED_SIZE, 320, Short.MAX_VALUE)
 
155
                .add(typeField))
 
156
            .add(3)
 
157
            .add(footerLabel));        
 
158
        
 
159
               
 
160
        int prefHeight;
 
161
        int maxHeight;
 
162
        if(jScrollPane.getPreferredSize().getHeight() > 300) {
 
163
            prefHeight = 300;
 
164
            maxHeight = GroupLayout.DEFAULT_SIZE;
 
165
        } else {
 
166
            prefHeight = GroupLayout.DEFAULT_SIZE;
 
167
            maxHeight = GroupLayout.PREFERRED_SIZE;
 
168
        }
 
169
        
 
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));
 
177
    }
 
178
    
 
179
    void setValue(RADConnectionPropertyEditor.RADConnectionDesignValue value) {
 
180
        if (value != null && value.getType() == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_CODE) {
 
181
            ignoreUpdate = true;
 
182
            codePane.setText(value.getCode());
 
183
            ignoreUpdate = false;
 
184
            
 
185
            resetLayout();
 
186
        }
 
187
    }
 
188
 
 
189
    // DocumentListener
 
190
    public void insertUpdate(DocumentEvent e) {
 
191
        invokeUpdate();
 
192
    }
 
193
 
 
194
    // DocumentListener
 
195
    public void removeUpdate(DocumentEvent e) {
 
196
        invokeUpdate();
 
197
    }
 
198
 
 
199
    // DocumentListener
 
200
    public void changedUpdate(DocumentEvent e) {
 
201
    }
 
202
 
 
203
    private void invokeUpdate() {
 
204
        if (!ignoreUpdate) {
 
205
            ignoreUpdate = true;
 
206
            EventQueue.invokeLater(this); // set the value
 
207
            
 
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)
 
211
            revalidate();
 
212
            repaint();
 
213
        }
 
214
    }
 
215
 
 
216
    // updates the value in the property editor
 
217
    public void run() {
 
218
        propertyEditor.setValue(new RADConnectionPropertyEditor.RADConnectionDesignValue(codePane.getText()));
 
219
        ignoreUpdate = false;
 
220
    }
 
221
}