KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > StringInplaceEditor


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.openide.explorer.propertysheet;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.Toolkit JavaDoc;
30 import java.awt.event.FocusEvent JavaDoc;
31 import java.awt.event.InputEvent JavaDoc;
32 import java.awt.event.KeyEvent JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.beans.PropertyEditor JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37 import javax.swing.KeyStroke JavaDoc;
38
39 /**
40  * A JTextField implementation of the InplaceEditor interface.
41  * @author Tim Boudreau
42  */

43 class StringInplaceEditor extends JTextField JavaDoc implements InplaceEditor {
44
45     protected PropertyEditor JavaDoc editor;
46     protected PropertyEnv env;
47     private boolean added;
48     private String JavaDoc valFromEditor;
49     private String JavaDoc valFromTextField;
50     private PropertyModel pm;
51     
52     private KeyStroke JavaDoc[] strokes = new KeyStroke JavaDoc[] {
53             KeyStroke.getKeyStroke(
54                 KeyEvent.VK_HOME, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | KeyEvent.SHIFT_DOWN_MASK
55             ),
56             KeyStroke.getKeyStroke(
57                 KeyEvent.VK_END, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | KeyEvent.SHIFT_DOWN_MASK
58             ), KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false)
59         };
60
61     public void removeNotify() {
62         super.removeNotify();
63     }
64
65     public void clear() {
66         editor = null;
67         setEditable(true);
68         setEnabled(true);
69         setText("");
70         pm = null;
71         env = null;
72         valFromEditor = null;
73         valFromTextField = null;
74     }
75
76     public void connect(PropertyEditor JavaDoc p, PropertyEnv env) {
77         setActionCommand(COMMAND_SUCCESS);
78         this.env = env;
79
80         if (editor == p) {
81             return;
82         }
83
84         editor = p;
85
86         boolean editable = PropUtils.checkEnabled(this, p, env);
87         setEnabled(editable);
88
89         //Undocumented, but in NB 3.5 and earlier, getAsText() returning null for
90
//paintable editors was yet another way to disable a property editor
91
if ((p.getTags() == null) && (p.getAsText() == null) && p.isPaintable()) {
92             editable = false;
93         }
94
95         setEditable(editable);
96         reset();
97         added = false;
98     }
99
100     public void addNotify() {
101         super.addNotify();
102         added = true;
103     }
104
105     public JComponent JavaDoc getComponent() {
106         return this;
107     }
108
109     public Object JavaDoc getValue() {
110         if ((valFromTextField != null) && valFromTextField.equals(getText())) {
111             //#47430 - JTextField will strip \n's from edited text. If no
112
//change to the text field value, return what we originally got
113
return valFromEditor;
114         } else {
115             return getText();
116         }
117     }
118
119     public void reset() {
120         String JavaDoc txt;
121         txt = editor.getAsText();
122
123         //don't want an editor with the text "different values" in it //NOI18N
124
if (editor instanceof PropUtils.DifferentValuesEditor) {
125             txt = ""; //NOI18N
126
}
127
128         valFromEditor = txt;
129         //issue 26367, form editor needs ability to set a custom value
130
//when editing is initiated (event handler combos, part of them
131
//cleaning up their EnhancedPropertyEditors).
132
if ((getClass() == StringInplaceEditor.class) && (env != null) && (env.getFeatureDescriptor() != null)) {
133             String JavaDoc initialEditValue = (String JavaDoc) env.getFeatureDescriptor().getValue("initialEditValue"); //NOI18N
134

135             if (initialEditValue != null) {
136                 txt = initialEditValue;
137                 valFromEditor = txt;
138             }
139         }
140
141         if (txt == null) {
142             txt = "";
143         }
144
145         setText(txt);
146         valFromTextField = getText();
147         setSelectionStart(0);
148         setSelectionEnd(txt.length());
149     }
150
151     public KeyStroke JavaDoc[] getKeyStrokes() {
152         return strokes;
153     }
154
155     public PropertyEditor JavaDoc getPropertyEditor() {
156         return editor;
157     }
158
159     private void handleInitialInputEvent(InputEvent JavaDoc e) {
160         //issue 35296, select all the text
161
String JavaDoc txt = getText();
162
163         if (txt.length() > 0) {
164             setSelectionStart(0);
165             setSelectionEnd(getText().length());
166         }
167     }
168
169     public void setValue(Object JavaDoc o) {
170         if ((null != o) && (null != editor) && editor.supportsCustomEditor()) {
171             editor.setValue(o);
172             setText(editor.getAsText());
173         } else {
174             setText((o != null) ? o.toString() : ""); //NOI18N
175
}
176     }
177
178     public boolean supportsTextEntry() {
179         return true;
180     }
181
182     public PropertyModel getPropertyModel() {
183         return pm;
184     }
185
186     public void setPropertyModel(PropertyModel pm) {
187         this.pm = pm;
188     }
189
190     public boolean isKnownComponent(Component JavaDoc c) {
191         return false;
192     }
193
194     public Dimension JavaDoc getPreferredSize() {
195         Graphics JavaDoc g = PropUtils.getScratchGraphics(this);
196         String JavaDoc s = getText();
197
198         if (s.length() > 1000) {
199             //IZ 44152, debugger can return 512K+ long strings
200
return new Dimension JavaDoc(4196, g.getFontMetrics(getFont()).getHeight());
201         }
202
203         FontMetrics JavaDoc fm = g.getFontMetrics(getFont());
204         Dimension JavaDoc result = new Dimension JavaDoc(fm.stringWidth(s), fm.getHeight());
205         result.width = Math.max(result.width, PropUtils.getMinimumPropPanelWidth());
206         result.height = Math.max(result.height, PropUtils.getMinimumPropPanelHeight());
207
208         if (getBorder() != null) {
209             Insets JavaDoc i = getBorder().getBorderInsets(this);
210             result.width += (i.right + i.left);
211             result.height += (i.top + i.bottom);
212         }
213
214         return result;
215     }
216
217     public void processMouseEvent(MouseEvent JavaDoc me) {
218         super.processMouseEvent(me);
219
220         if (added) {
221             handleInitialInputEvent(me);
222         }
223
224         added = false;
225     }
226
227     protected void processFocusEvent(FocusEvent JavaDoc fe) {
228         super.processFocusEvent(fe);
229         repaint();
230     }
231
232     public void paintComponent(Graphics JavaDoc g) {
233         //For property panel usage, allow the editor to paint
234
if ((editor != null) && !hasFocus() && editor.isPaintable()) {
235             Insets JavaDoc ins = getInsets();
236             Color JavaDoc c = g.getColor();
237
238             try {
239                 g.setColor(getBackground());
240                 g.fillRect(0, 0, getWidth(), getHeight());
241             } finally {
242                 g.setColor(c);
243             }
244
245             ins.left += PropUtils.getTextMargin();
246             editor.paintValue(
247                 g,
248                 new Rectangle JavaDoc(
249                     ins.left, ins.top, getWidth() - (ins.right + ins.left), getHeight() - (ins.top + ins.bottom)
250                 )
251             );
252         } else {
253             super.paintComponent(g);
254         }
255     }
256 }
257
Popular Tags