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.form.editors;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyEditor;
047: import javax.swing.undo.UndoManager;
048: import org.netbeans.editor.BaseDocument;
049: import org.openide.awt.Mnemonics;
050: import org.openide.explorer.propertysheet.PropertyEnv;
051:
052: /** Customizer for "code properties" used by JavaCodeGenerator.
053: *
054: * @author vzboril
055: */
056:
057: public class CustomCodeEditor extends javax.swing.JPanel implements
058: PropertyChangeListener {
059: private PropertyEditor propertyEditor;
060:
061: public CustomCodeEditor(PropertyEditor propertyEditor,
062: PropertyEnv env, javax.swing.JEditorPane editorPane) {
063: this .propertyEditor = propertyEditor;
064: this .editorPane = editorPane;
065: initComponents();
066: try {
067: codeEditorPane.setText((String) propertyEditor.getValue());
068: } catch (Exception ex) { // ignore - should not happen
069: ex.printStackTrace();
070: }
071: Object um = editorPane.getDocument().getProperty(
072: BaseDocument.UNDO_MANAGER_PROP);
073: if (um instanceof UndoManager) {
074: ((UndoManager) um).discardAllEdits();
075: }
076:
077: env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
078: env.addPropertyChangeListener(this );
079:
080: java.util.ResourceBundle bundle = org.openide.util.NbBundle
081: .getBundle(CustomCodeEditor.class);
082:
083: Mnemonics.setLocalizedText(jLabel1, bundle
084: .getString("CTL_InsertCode")); // NOI18N
085: jLabel1.setLabelFor(codeEditorPane);
086: codeEditorPane
087: .setPreferredSize(new java.awt.Dimension(440, 200));
088: codeEditorPane.requestFocus();
089: codeEditorPane.getCaret().setVisible(codeEditorPane.hasFocus());
090:
091: codeEditorPane.getAccessibleContext().setAccessibleDescription(
092: bundle.getString("ACSD_CustomCodeEditor.label1")); // NOI18N
093: getAccessibleContext().setAccessibleDescription(
094: bundle.getString("ACSD_CustomCodeEditor")); // NOI18N
095: }
096:
097: /** This method is called from within the constructor to
098: * initialize the form.
099: * WARNING: Do NOT modify this code. The content of this method is
100: * always regenerated by the Form Editor.
101: */
102: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
103: private void initComponents() {
104: java.awt.GridBagConstraints gridBagConstraints;
105:
106: jScrollPane1 = new javax.swing.JScrollPane();
107: codeEditorPane = this .editorPane;
108: jLabel1 = new javax.swing.JLabel();
109:
110: setLayout(new java.awt.GridBagLayout());
111:
112: addComponentListener(new java.awt.event.ComponentAdapter() {
113: public void componentShown(java.awt.event.ComponentEvent evt) {
114: formComponentShown(evt);
115: }
116: });
117: addFocusListener(new java.awt.event.FocusAdapter() {
118: public void focusGained(java.awt.event.FocusEvent evt) {
119: formFocusGained(evt);
120: }
121: });
122:
123: codeEditorPane
124: .addFocusListener(new java.awt.event.FocusAdapter() {
125: public void focusGained(
126: java.awt.event.FocusEvent evt) {
127: codeEditorPaneFocusGained(evt);
128: }
129:
130: public void focusLost(java.awt.event.FocusEvent evt) {
131: codeEditorPaneFocusLost(evt);
132: }
133: });
134:
135: jScrollPane1.setViewportView(codeEditorPane);
136:
137: gridBagConstraints = new java.awt.GridBagConstraints();
138: gridBagConstraints.gridx = 0;
139: gridBagConstraints.gridy = 1;
140: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
141: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
142: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
143: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
144: gridBagConstraints.weightx = 1.0;
145: gridBagConstraints.weighty = 1.0;
146: gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 11);
147: add(jScrollPane1, gridBagConstraints);
148:
149: jLabel1.setText("jLabel1");
150: gridBagConstraints = new java.awt.GridBagConstraints();
151: gridBagConstraints.gridx = 0;
152: gridBagConstraints.gridy = 0;
153: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
154: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
155: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 11);
156: add(jLabel1, gridBagConstraints);
157:
158: }// </editor-fold>//GEN-END:initComponents
159:
160: private void codeEditorPaneFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_codeEditorPaneFocusLost
161: // Add your handling code here:
162: codeEditorPane.getCaret().setVisible(codeEditorPane.hasFocus());
163: }//GEN-LAST:event_codeEditorPaneFocusLost
164:
165: private void codeEditorPaneFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_codeEditorPaneFocusGained
166: // Add your handling code here:
167: codeEditorPane.getCaret().setVisible(codeEditorPane.hasFocus());
168: }//GEN-LAST:event_codeEditorPaneFocusGained
169:
170: private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
171: // Add your handling code here:
172: codeEditorPane.requestFocus();
173: codeEditorPane.getCaret().setVisible(true); // true is HARDCODED here due to BUG in MAC OS X
174: }//GEN-LAST:event_formFocusGained
175:
176: private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
177: // Add your handling code here:
178: codeEditorPane.requestFocus();
179: codeEditorPane.getCaret().setVisible(codeEditorPane.hasFocus());
180: }//GEN-LAST:event_formComponentShown
181:
182: private javax.swing.JEditorPane editorPane;
183:
184: // Variables declaration - do not modify//GEN-BEGIN:variables
185: private javax.swing.JEditorPane codeEditorPane;
186: private javax.swing.JLabel jLabel1;
187: private javax.swing.JScrollPane jScrollPane1;
188:
189: // End of variables declaration//GEN-END:variables
190:
191: public void propertyChange(PropertyChangeEvent evt) {
192: if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
193: && evt.getNewValue() == PropertyEnv.STATE_VALID) {
194: propertyEditor.setValue(codeEditorPane.getText());
195: }
196: }
197: }
|