KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > TextAreaTableCellEditor


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/TextAreaTableCellEditor.java,v 1.7 2004/02/14 03:34:29 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.util;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.FocusEvent JavaDoc;
24 import java.awt.event.FocusListener JavaDoc;
25 import java.awt.event.ItemEvent JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.util.EventObject JavaDoc;
29
30 import javax.swing.AbstractCellEditor JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32 import javax.swing.JTable JavaDoc;
33 import javax.swing.JTextArea JavaDoc;
34 import javax.swing.JTree JavaDoc;
35 import javax.swing.table.TableCellEditor JavaDoc;
36 import javax.swing.tree.TreeCellEditor JavaDoc;
37
38 /**
39  * @author mstover
40  * @version $Revision: 1.7 $
41  */

42 public class TextAreaTableCellEditor
43     extends AbstractCellEditor JavaDoc
44     implements TableCellEditor JavaDoc, TreeCellEditor JavaDoc
45 {
46     //
47
//Instance Variables
48
//
49

50     /** The Swing component being edited. */
51     protected JTextArea JavaDoc editorComponent;
52     
53     /**
54      * The delegate class which handles all methods sent from the
55      * <code>CellEditor</code>.
56      */

57     protected EditorDelegate delegate;
58     
59     /**
60      * An integer specifying the number of clicks needed to start editing.
61      * Even if <code>clickCountToStart</code> is defined as zero, it
62      * will not initiate until a click occurs.
63      */

64     protected int clickCountToStart = 1;
65
66     //
67
//Constructors
68
//
69

70     /**
71      * Constructs a <code>TableCellEditor</code> that uses a text field.
72      */

73     public TextAreaTableCellEditor()
74     {
75         editorComponent = new JTextArea JavaDoc();
76         editorComponent.setRows(3);
77         this.clickCountToStart = 2;
78         delegate = new EditorDelegate()
79         {
80             public void setValue(Object JavaDoc value)
81             {
82                 editorComponent.setText(
83                     (value != null) ? value.toString() : "");
84             }
85
86             public Object JavaDoc getCellEditorValue()
87             {
88                 return editorComponent.getText();
89             }
90         };
91         editorComponent.addFocusListener(delegate);
92     }
93
94     /**
95      * Returns a reference to the editor component.
96      *
97      * @return the editor <code>Component</code>
98      */

99     public Component JavaDoc getComponent()
100     {
101         return editorComponent;
102     }
103
104     //
105
//Modifying
106
//
107

108     /**
109      * Specifies the number of clicks needed to start editing.
110      *
111      * @param count an int specifying the number of clicks needed to start
112      * editing
113      * @see #getClickCountToStart
114      */

115     public void setClickCountToStart(int count)
116     {
117         clickCountToStart = count;
118     }
119
120     /**
121      * Returns the number of clicks needed to start editing.
122      * @return the number of clicks needed to start editing
123      */

124     public int getClickCountToStart()
125     {
126         return clickCountToStart;
127     }
128
129     //
130
//Override the implementations of the superclass, forwarding all methods
131
//from the CellEditor interface to our delegate.
132
//
133

134     /**
135      * Forwards the message from the <code>CellEditor</code> to
136      * the <code>delegate</code>.
137      * @see EditorDelegate#getCellEditorValue
138      */

139     public Object JavaDoc getCellEditorValue()
140     {
141         return delegate.getCellEditorValue();
142     }
143
144     /**
145      * Forwards the message from the <code>CellEditor</code> to
146      * the <code>delegate</code>.
147      * @see EditorDelegate#isCellEditable(EventObject)
148      */

149     public boolean isCellEditable(EventObject JavaDoc anEvent)
150     {
151         return delegate.isCellEditable(anEvent);
152     }
153
154     /**
155      * Forwards the message from the <code>CellEditor</code> to
156      * the <code>delegate</code>.
157      * @see EditorDelegate#shouldSelectCell(EventObject)
158      */

159     public boolean shouldSelectCell(EventObject JavaDoc anEvent)
160     {
161         return delegate.shouldSelectCell(anEvent);
162     }
163
164     /**
165      * Forwards the message from the <code>CellEditor</code> to
166      * the <code>delegate</code>.
167      * @see EditorDelegate#stopCellEditing
168      */

169     public boolean stopCellEditing()
170     {
171         return delegate.stopCellEditing();
172     }
173
174     /**
175      * Forwards the message from the <code>CellEditor</code> to
176      * the <code>delegate</code>.
177      * @see EditorDelegate#cancelCellEditing
178      */

179     public void cancelCellEditing()
180     {
181         delegate.cancelCellEditing();
182     }
183
184     //
185
//Implementing the TreeCellEditor Interface
186
//
187

188     /** Implements the <code>TreeCellEditor</code> interface. */
189     public Component JavaDoc getTreeCellEditorComponent(
190         JTree JavaDoc tree,
191         Object JavaDoc value,
192         boolean isSelected,
193         boolean expanded,
194         boolean leaf,
195         int row)
196     {
197         String JavaDoc stringValue =
198             tree.convertValueToText(
199                 value,
200                 isSelected,
201                 expanded,
202                 leaf,
203                 row,
204                 false);
205
206         delegate.setValue(stringValue);
207         return new JScrollPane JavaDoc(editorComponent);
208     }
209
210     //
211
//Implementing the CellEditor Interface
212
//
213
/** Implements the <code>TableCellEditor</code> interface. */
214     public Component JavaDoc getTableCellEditorComponent(
215         JTable JavaDoc table,
216         Object JavaDoc value,
217         boolean isSelected,
218         int row,
219         int column)
220     {
221         delegate.setValue(value);
222         return new JScrollPane JavaDoc(editorComponent);
223     }
224
225     //
226
//Protected EditorDelegate class
227
//
228

229     /**
230      * The protected <code>EditorDelegate</code> class.
231      */

232     protected class EditorDelegate implements FocusListener JavaDoc, Serializable JavaDoc
233     {
234         /** The value of this cell. */
235         protected Object JavaDoc value;
236
237         /**
238          * Returns the value of this cell.
239          * @return the value of this cell
240          */

241         public Object JavaDoc getCellEditorValue()
242         {
243             return value;
244         }
245
246         /**
247          * Sets the value of this cell.
248          * @param value the new value of this cell
249          */

250         public void setValue(Object JavaDoc value)
251         {
252             this.value = value;
253         }
254
255         /**
256          * Returns true if <code>anEvent</code> is <b>not</b> a
257          * <code>MouseEvent</code>. Otherwise, it returns true
258          * if the necessary number of clicks have occurred, and
259          * returns false otherwise.
260          *
261          * @param anEvent the event
262          * @return true if cell is ready for editing, false otherwise
263          * @see #setClickCountToStart
264          * @see #shouldSelectCell
265          */

266         public boolean isCellEditable(EventObject JavaDoc anEvent)
267         {
268             if (anEvent instanceof MouseEvent JavaDoc)
269             {
270                 return ((MouseEvent JavaDoc) anEvent).getClickCount()
271                     >= clickCountToStart;
272             }
273             return true;
274         }
275
276         /**
277          * Returns true to indicate that the editing cell may
278          * be selected.
279          *
280          * @param anEvent the event
281          * @return true
282          * @see #isCellEditable
283          */

284         public boolean shouldSelectCell(EventObject JavaDoc anEvent)
285         {
286             return true;
287         }
288
289         /**
290          * Returns true to indicate that editing has begun.
291          *
292          * @param anEvent the event
293          */

294         public boolean startCellEditing(EventObject JavaDoc anEvent)
295         {
296             return true;
297         }
298
299         /**
300          * Stops editing and
301          * returns true to indicate that editing has stopped.
302          * This method calls <code>fireEditingStopped</code>.
303          *
304          * @return true
305          */

306         public boolean stopCellEditing()
307         {
308             fireEditingStopped();
309             return true;
310         }
311
312         /**
313          * Cancels editing. This method calls <code>fireEditingCanceled</code>.
314          */

315         public void cancelCellEditing()
316         {
317             fireEditingCanceled();
318         }
319
320         /**
321          * When an action is performed, editing is ended.
322          * @param e the action event
323          * @see #stopCellEditing
324          */

325         public void actionPerformed(ActionEvent JavaDoc e)
326         {
327             TextAreaTableCellEditor.this.stopCellEditing();
328         }
329
330         /**
331          * When an item's state changes, editing is ended.
332          * @param e the action event
333          * @see #stopCellEditing
334          */

335         public void itemStateChanged(ItemEvent JavaDoc e)
336         {
337             TextAreaTableCellEditor.this.stopCellEditing();
338         }
339         public void focusLost(FocusEvent JavaDoc ev)
340         {
341             TextAreaTableCellEditor.this.stopCellEditing();
342         }
343
344         public void focusGained(FocusEvent JavaDoc ev)
345         {
346         }
347     }
348 }
349
Popular Tags