KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > StringCustomEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.beaninfo.editors;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyEditor JavaDoc;
25 import org.openide.explorer.propertysheet.PropertyEnv;
26 import org.openide.util.NbBundle;
27 import javax.swing.*;
28 import javax.swing.border.*;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import java.awt.BorderLayout JavaDoc;
31 /** A custom editor for Strings.
32 *
33 * @author Ian Formanek
34 * @version 1.00, Sep 21, 1998
35 */

36 public class StringCustomEditor extends javax.swing.JPanel JavaDoc implements PropertyChangeListener JavaDoc {
37     
38     static final long serialVersionUID =7348579663907322425L;
39     
40     boolean oneline=false;
41     String JavaDoc instructions = null;
42
43     private PropertyEnv env;
44
45     private PropertyEditor JavaDoc editor;
46
47
48     //enh 29294, provide one line editor on request
49
/** Create a StringCustomEditor.
50      * @param value the initial value for the string
51      * @param editable whether to show the editor in read only or read-write mode
52      * @param oneline whether the text component should be a single-line or multi-line component
53      * @param instructions any instructions that should be displayed
54      */

55     StringCustomEditor (String JavaDoc value, boolean editable, boolean oneline, String JavaDoc instructions, PropertyEditor JavaDoc editor, PropertyEnv env) {
56         this.oneline = oneline;
57         this.instructions = instructions;
58         this.env = env;
59         this.editor = editor;
60
61         this.env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
62         this.env.addPropertyChangeListener(this);
63
64         init (value, editable);
65    }
66     
67     /** Initializes the Form
68      * @deprecated Nothing should be using this constructor */

69     public StringCustomEditor(String JavaDoc s, boolean editable) {
70         init (s, editable);
71     }
72     
73     private void init (String JavaDoc s, boolean editable) {
74         setLayout (new java.awt.BorderLayout JavaDoc ());
75         if (oneline) {
76             textArea = new javax.swing.JTextField JavaDoc();
77             add (textArea, BorderLayout.CENTER);
78         } else {
79             textAreaScroll = new javax.swing.JScrollPane JavaDoc ();
80             textArea = new javax.swing.JTextArea JavaDoc ();
81             textAreaScroll.setViewportView (textArea);
82             add (textAreaScroll, BorderLayout.CENTER);
83         }
84         //original constructor code
85
textArea.setEditable(editable);
86         
87         int from = 0;
88         int to = 0;
89         int ctn = 1;
90         while ((to = s.indexOf ("\n", from)) > 0) {
91             ctn ++;
92             from = to + 1;
93         }
94
95         textArea.setText (s);
96         if (textArea instanceof JTextArea && s.length () < 1024) {
97             ((JTextArea) textArea).setWrapStyleWord( true );
98             ((JTextArea)textArea).setLineWrap( true );
99             setPreferredSize (new java.awt.Dimension JavaDoc(500, 300));
100         } else {
101             textArea.setMinimumSize (new java.awt.Dimension JavaDoc (100, 20));
102             if (textArea instanceof JTextArea) {
103                 //Some gargantuan string value - do something that will
104
//show it. Line wrap is off, otherwise it will spend
105
//minutes trying to calculate preferred size, etc.
106
setPreferredSize (new java.awt.Dimension JavaDoc(500, 300));
107             }
108         }
109         
110         if ( !editable ) {
111             // hack to fix #9219
112
//TODO Fix this to use UIManager values, this is silly
113
JTextField hack = new JTextField();
114             hack.setEditable( false );
115             textArea.setBackground( hack.getBackground() );
116             textArea.setForeground( hack.getForeground() );
117         }
118         
119         setBorder (BorderFactory.createEmptyBorder(12,12,0,11));
120         
121         textArea.getAccessibleContext().setAccessibleName(NbBundle.getBundle(StringCustomEditor.class).getString("ACS_TextArea")); //NOI18N
122
if (instructions == null) {
123             textArea.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(StringCustomEditor.class).getString("ACSD_TextArea")); //NOI18N
124
} else {
125             textArea.getAccessibleContext().setAccessibleDescription(instructions);
126         }
127         getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(StringCustomEditor.class).getString("ACSD_CustomStringEditor")); //NOI18N
128
//Layout is not quite smart enough about text field along with variable
129
//size text area
130
int prefHeight;
131         
132         //IZ 44152, Debugger can produce 512K+ length strings, avoid excessive
133
//iterations (which textArea.getPreferredSize() will definitely do)
134
if (s.length () < 1024) {
135             prefHeight = textArea.getPreferredSize().height + 8;
136         } else {
137             prefHeight = ctn * 8;
138         }
139         
140         if (instructions != null) {
141             final JTextArea jta = new JTextArea(instructions);
142             jta.setEditable (false);
143             java.awt.Color JavaDoc c = UIManager.getColor("control"); //NOI18N
144
if (c != null) {
145                 jta.setBackground (c);
146             } else {
147                 jta.setBackground (getBackground());
148             }
149             jta.setLineWrap(true);
150             jta.setWrapStyleWord(true);
151             jta.setFont (getFont());
152             add (jta, BorderLayout.NORTH, 0);
153             jta.getAccessibleContext().setAccessibleName(
154                 NbBundle.getMessage(StringCustomEditor.class,
155                 "ACS_Instructions")); //NOI18N
156
jta.getAccessibleContext().setAccessibleDescription(
157                 NbBundle.getMessage(StringCustomEditor.class,
158                 "ACSD_Instructions")); //NOI18N
159
prefHeight += jta.getPreferredSize().height;
160             //jlf guidelines - auto select text when clicked
161
jta.addFocusListener(new java.awt.event.FocusListener JavaDoc() {
162                 public void focusGained(java.awt.event.FocusEvent JavaDoc e) {
163                     jta.setSelectionStart(0);
164                     jta.setSelectionEnd(jta.getText().length());
165                 }
166                 public void focusLost(java.awt.event.FocusEvent JavaDoc e) {
167                     jta.setSelectionStart(0);
168                     jta.setSelectionEnd(0);
169                 }
170             });
171         }
172         if (textArea instanceof JTextField) {
173             setPreferredSize (new java.awt.Dimension JavaDoc (300,
174                 prefHeight));
175         }
176     }
177     
178     public void addNotify () {
179         super.addNotify();
180         //force focus to the editable area
181
if (isEnabled() && isFocusable()) {
182             textArea.requestFocus();
183         }
184     }
185
186     /**
187     * @return Returns the property value that is result of the CustomPropertyEditor.
188     * @exception InvalidStateException when the custom property editor does not represent valid property value
189     * (and thus it should not be set)
190     */

191     private Object JavaDoc getPropertyValue () throws IllegalStateException JavaDoc {
192         return textArea.getText ();
193     }
194
195
196
197     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
198         if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
199             editor.setValue(getPropertyValue());
200         }
201     }
202
203     private javax.swing.JScrollPane JavaDoc textAreaScroll;
204     private JTextComponent JavaDoc textArea;
205 }
206
Popular Tags