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-2007 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: package org.netbeans.modules.visualweb.propertyeditors;
042:
043: import java.awt.Dimension;
044: import java.awt.FontMetrics;
045: import java.awt.GridBagConstraints;
046: import java.util.ResourceBundle;
047:
048: import javax.swing.JEditorPane;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JScrollPane;
052: import javax.swing.JTextArea;
053: import javax.swing.UIManager;
054:
055: /**
056: * A custom property editor for JavaScript, that displays code using basic
057: * syntax highlighting.
058: *
059: * @author eric
060: * @author gjmurphy
061: */
062: public class JavaScriptPropertyPanel extends PropertyPanelBase {
063:
064: private static String instructions = ResourceBundle.getBundle(
065: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
066: .getString("JavaScriptPropertyPanel.instructions");
067:
068: protected static Class codeClipsPanelClass;
069: protected JEditorPane editorPane;
070:
071: // This is done to get around some module loading logic I dont
072: // want to deal with at the moment. How to get visibility to
073: // com.sun.rave.toolbox.CodeClipsTab, from toolbox module
074: public static void setCodeClipsPanelClass(Class clazz) {
075: codeClipsPanelClass = clazz;
076: }
077:
078: /**
079: *
080: */
081: public JavaScriptPropertyPanel(
082: JavaScriptPropertyEditor propertyEditor) {
083: super (propertyEditor);
084: initComponents((String) propertyEditor.getValue(), instructions);
085: }
086:
087: protected JPanel getNewCodeClipsPanel() {
088: // For now I do not want to have a hard reference and change the build order
089: // return new CodeClipsTab();
090: if (codeClipsPanelClass == null) {
091: return null;
092: }
093: try {
094: JPanel panel = (JPanel) codeClipsPanelClass.newInstance();
095: return panel;
096: } catch (Exception e) {
097: // e.printStackTrace();
098: // shouldn't totally hide this, but quicky jucky code anyway
099: return null;
100: }
101: }
102:
103: public Object getPropertyValue() throws IllegalStateException {
104: return editorPane.getText();
105: }
106:
107: // public void addNotify() {
108: // super.addNotify();
109: // if (isEnabled() && isFocusable()) {
110: // editorPane.requestFocus();
111: // }
112: // }
113:
114: protected void initComponents(String string, String instructions) {
115: GridBagConstraints gridBagConstraints;
116: setLayout(new java.awt.GridBagLayout());
117: // Label with instructions
118: final JLabel label = new JLabel(instructions);
119: label.setFont(getFont());
120: label
121: .setDisplayedMnemonic(ResourceBundle
122: .getBundle(
123: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
124: .getString(
125: "JavaScriptPropertyPanel.label.mnemonic")
126: .charAt(0));
127: gridBagConstraints = new java.awt.GridBagConstraints();
128: gridBagConstraints.gridx = 1;
129: gridBagConstraints.gridy = 0;
130: gridBagConstraints.gridwidth = 1;
131: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
132: gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
133: gridBagConstraints.weightx = 0;
134: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
135: add(label, gridBagConstraints);
136: // Code clips panel
137: // JPanel codeClips = getNewCodeClipsPanel();
138: // if (codeClips != null) {
139: // gridBagConstraints = new java.awt.GridBagConstraints();
140: // gridBagConstraints.gridx = 0;
141: // gridBagConstraints.gridy = 1;
142: // gridBagConstraints.gridwidth = 1;
143: // gridBagConstraints.fill = GridBagConstraints.BOTH;
144: // gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
145: // gridBagConstraints.weightx = 1.0;
146: // gridBagConstraints.weighty = 1.0;
147: // gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
148: // add(codeClips, gridBagConstraints);
149: // }
150: // Editor pane
151: JScrollPane scrollPane = new javax.swing.JScrollPane();
152: editorPane = new JEditorPane();
153: editorPane.setContentType("text/javascript"); // NOI18N
154: editorPane.setText(string);
155: editorPane
156: .getAccessibleContext()
157: .setAccessibleName(
158: ResourceBundle
159: .getBundle(
160: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
161: .getString(
162: "JavaScriptPropertyPanel.editor.accessibleName"));
163: label.setLabelFor(editorPane);
164:
165: //"Javascript editor"
166: scrollPane.setViewportView(editorPane);
167: gridBagConstraints = new java.awt.GridBagConstraints();
168: gridBagConstraints.gridx = 1;
169: gridBagConstraints.gridy = 1;
170: gridBagConstraints.gridwidth = 1;
171: gridBagConstraints.fill = GridBagConstraints.BOTH;
172: gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
173: gridBagConstraints.weightx = 1.0;
174: gridBagConstraints.weighty = 1.0;
175: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
176: FontMetrics metrics = editorPane.getFontMetrics(editorPane
177: .getFont());
178: int columnWidth = metrics.charWidth('m'); // cloned code from JTextArea
179: int rowHeight = metrics.getHeight();
180: editorPane.setPreferredSize(new Dimension(columnWidth * 80,
181: rowHeight * 15));
182: editorPane.addFocusListener(new java.awt.event.FocusListener() {
183: public void focusGained(java.awt.event.FocusEvent e) {
184: editorPane.setSelectionStart(0);
185: editorPane.setSelectionEnd(editorPane.getText()
186: .length());
187: }
188:
189: public void focusLost(java.awt.event.FocusEvent e) {
190: editorPane.setSelectionStart(0);
191: editorPane.setSelectionEnd(0);
192: }
193: });
194: add(scrollPane, gridBagConstraints);
195: }
196: }
|