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.beaninfo.editors;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyEditor;
047: import org.openide.explorer.propertysheet.PropertyEnv;
048: import org.openide.util.NbBundle;
049: import javax.swing.*;
050: import javax.swing.border.*;
051: import javax.swing.text.JTextComponent;
052: import java.awt.BorderLayout;
053:
054: /** A custom editor for Strings.
055: *
056: * @author Ian Formanek
057: * @version 1.00, Sep 21, 1998
058: */
059: public class StringCustomEditor extends javax.swing.JPanel implements
060: PropertyChangeListener {
061:
062: static final long serialVersionUID = 7348579663907322425L;
063:
064: boolean oneline = false;
065: String instructions = null;
066:
067: private PropertyEnv env;
068:
069: private PropertyEditor editor;
070:
071: //enh 29294, provide one line editor on request
072: /** Create a StringCustomEditor.
073: * @param value the initial value for the string
074: * @param editable whether to show the editor in read only or read-write mode
075: * @param oneline whether the text component should be a single-line or multi-line component
076: * @param instructions any instructions that should be displayed
077: */
078: StringCustomEditor(String value, boolean editable, boolean oneline,
079: String instructions, PropertyEditor editor, PropertyEnv env) {
080: this .oneline = oneline;
081: this .instructions = instructions;
082: this .env = env;
083: this .editor = editor;
084:
085: this .env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
086: this .env.addPropertyChangeListener(this );
087:
088: init(value, editable);
089: }
090:
091: /** Initializes the Form
092: * @deprecated Nothing should be using this constructor */
093: public StringCustomEditor(String s, boolean editable) {
094: init(s, editable);
095: }
096:
097: private void init(String s, boolean editable) {
098: setLayout(new java.awt.BorderLayout());
099: if (oneline) {
100: textArea = new javax.swing.JTextField();
101: add(textArea, BorderLayout.CENTER);
102: } else {
103: textAreaScroll = new javax.swing.JScrollPane();
104: textArea = new javax.swing.JTextArea();
105: textAreaScroll.setViewportView(textArea);
106: add(textAreaScroll, BorderLayout.CENTER);
107: }
108: //original constructor code
109: textArea.setEditable(editable);
110:
111: int from = 0;
112: int to = 0;
113: int ctn = 1;
114: while ((to = s.indexOf("\n", from)) > 0) {
115: ctn++;
116: from = to + 1;
117: }
118:
119: textArea.setText(s);
120: if (textArea instanceof JTextArea && s.length() < 1024) {
121: ((JTextArea) textArea).setWrapStyleWord(true);
122: ((JTextArea) textArea).setLineWrap(true);
123: setPreferredSize(new java.awt.Dimension(500, 300));
124: } else {
125: textArea.setMinimumSize(new java.awt.Dimension(100, 20));
126: if (textArea instanceof JTextArea) {
127: //Some gargantuan string value - do something that will
128: //show it. Line wrap is off, otherwise it will spend
129: //minutes trying to calculate preferred size, etc.
130: setPreferredSize(new java.awt.Dimension(500, 300));
131: }
132: }
133:
134: if (!editable) {
135: // hack to fix #9219
136: //TODO Fix this to use UIManager values, this is silly
137: JTextField hack = new JTextField();
138: hack.setEditable(false);
139: textArea.setBackground(hack.getBackground());
140: textArea.setForeground(hack.getForeground());
141: }
142:
143: setBorder(BorderFactory.createEmptyBorder(12, 12, 0, 11));
144:
145: textArea.getAccessibleContext().setAccessibleName(
146: NbBundle.getBundle(StringCustomEditor.class).getString(
147: "ACS_TextArea")); //NOI18N
148: if (instructions == null) {
149: textArea.getAccessibleContext().setAccessibleDescription(
150: NbBundle.getBundle(StringCustomEditor.class)
151: .getString("ACSD_TextArea")); //NOI18N
152: } else {
153: textArea.getAccessibleContext().setAccessibleDescription(
154: instructions);
155: }
156: getAccessibleContext().setAccessibleDescription(
157: NbBundle.getBundle(StringCustomEditor.class).getString(
158: "ACSD_CustomStringEditor")); //NOI18N
159: //Layout is not quite smart enough about text field along with variable
160: //size text area
161: int prefHeight;
162:
163: //IZ 44152, Debugger can produce 512K+ length strings, avoid excessive
164: //iterations (which textArea.getPreferredSize() will definitely do)
165: if (s.length() < 1024) {
166: prefHeight = textArea.getPreferredSize().height + 8;
167: } else {
168: prefHeight = ctn * 8;
169: }
170:
171: if (instructions != null) {
172: final JTextArea jta = new JTextArea(instructions);
173: jta.setEditable(false);
174: java.awt.Color c = UIManager.getColor("control"); //NOI18N
175: if (c != null) {
176: jta.setBackground(c);
177: } else {
178: jta.setBackground(getBackground());
179: }
180: jta.setLineWrap(true);
181: jta.setWrapStyleWord(true);
182: jta.setFont(getFont());
183: add(jta, BorderLayout.NORTH, 0);
184: jta.getAccessibleContext().setAccessibleName(
185: NbBundle.getMessage(StringCustomEditor.class,
186: "ACS_Instructions")); //NOI18N
187: jta.getAccessibleContext().setAccessibleDescription(
188: NbBundle.getMessage(StringCustomEditor.class,
189: "ACSD_Instructions")); //NOI18N
190: prefHeight += jta.getPreferredSize().height;
191: //jlf guidelines - auto select text when clicked
192: jta.addFocusListener(new java.awt.event.FocusListener() {
193: public void focusGained(java.awt.event.FocusEvent e) {
194: jta.setSelectionStart(0);
195: jta.setSelectionEnd(jta.getText().length());
196: }
197:
198: public void focusLost(java.awt.event.FocusEvent e) {
199: jta.setSelectionStart(0);
200: jta.setSelectionEnd(0);
201: }
202: });
203: }
204: if (textArea instanceof JTextField) {
205: setPreferredSize(new java.awt.Dimension(300, prefHeight));
206: }
207: }
208:
209: public void addNotify() {
210: super .addNotify();
211: //force focus to the editable area
212: if (isEnabled() && isFocusable()) {
213: textArea.requestFocus();
214: }
215: }
216:
217: /**
218: * @return Returns the property value that is result of the CustomPropertyEditor.
219: * @exception InvalidStateException when the custom property editor does not represent valid property value
220: * (and thus it should not be set)
221: */
222: private Object getPropertyValue() throws IllegalStateException {
223: return textArea.getText();
224: }
225:
226: public void propertyChange(PropertyChangeEvent evt) {
227: if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
228: && evt.getNewValue() == PropertyEnv.STATE_VALID) {
229: editor.setValue(getPropertyValue());
230: }
231: }
232:
233: private javax.swing.JScrollPane textAreaScroll;
234: private JTextComponent textArea;
235: }
|