001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.util;
020:
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025: import java.awt.event.ItemEvent;
026: import java.awt.event.MouseEvent;
027: import java.io.Serializable;
028: import java.util.EventObject;
029:
030: import javax.swing.AbstractCellEditor;
031: import javax.swing.JScrollPane;
032: import javax.swing.JTable;
033: import javax.swing.JTextArea;
034: import javax.swing.JTree;
035: import javax.swing.table.TableCellEditor;
036: import javax.swing.tree.TreeCellEditor;
037:
038: /**
039: * @author mstover
040: * @version $Revision: 571988 $
041: */
042: public class TextAreaTableCellEditor extends AbstractCellEditor
043: implements TableCellEditor, TreeCellEditor {
044: //
045: // Instance Variables
046: //
047:
048: /** The Swing component being edited. */
049: protected JTextArea editorComponent;
050:
051: /**
052: * The delegate class which handles all methods sent from the
053: * <code>CellEditor</code>.
054: */
055: protected EditorDelegate delegate;
056:
057: /**
058: * An integer specifying the number of clicks needed to start editing. Even
059: * if <code>clickCountToStart</code> is defined as zero, it will not
060: * initiate until a click occurs.
061: */
062: protected int clickCountToStart = 1;
063:
064: //
065: // Constructors
066: //
067:
068: /**
069: * Constructs a <code>TableCellEditor</code> that uses a text field.
070: */
071: public TextAreaTableCellEditor() {
072: editorComponent = new JTextArea();
073: editorComponent.setRows(3);
074: this .clickCountToStart = 2;
075: delegate = new EditorDelegate() {
076: public void setValue(Object value) {
077: editorComponent.setText((value != null) ? value
078: .toString() : "");
079: }
080:
081: public Object getCellEditorValue() {
082: return editorComponent.getText();
083: }
084: };
085: editorComponent.addFocusListener(delegate);
086: }
087:
088: /**
089: * Returns a reference to the editor component.
090: *
091: * @return the editor <code>Component</code>
092: */
093: public Component getComponent() {
094: return editorComponent;
095: }
096:
097: //
098: // Modifying
099: //
100:
101: /**
102: * Specifies the number of clicks needed to start editing.
103: *
104: * @param count
105: * an int specifying the number of clicks needed to start editing
106: * @see #getClickCountToStart
107: */
108: public void setClickCountToStart(int count) {
109: clickCountToStart = count;
110: }
111:
112: /**
113: * Returns the number of clicks needed to start editing.
114: *
115: * @return the number of clicks needed to start editing
116: */
117: public int getClickCountToStart() {
118: return clickCountToStart;
119: }
120:
121: //
122: // Override the implementations of the superclass, forwarding all methods
123: // from the CellEditor interface to our delegate.
124: //
125:
126: /**
127: * Forwards the message from the <code>CellEditor</code> to the
128: * <code>delegate</code>.
129: *
130: * @see EditorDelegate#getCellEditorValue
131: */
132: public Object getCellEditorValue() {
133: return delegate.getCellEditorValue();
134: }
135:
136: /**
137: * Forwards the message from the <code>CellEditor</code> to the
138: * <code>delegate</code>.
139: *
140: * @see EditorDelegate#isCellEditable(EventObject)
141: */
142: public boolean isCellEditable(EventObject anEvent) {
143: return delegate.isCellEditable(anEvent);
144: }
145:
146: /**
147: * Forwards the message from the <code>CellEditor</code> to the
148: * <code>delegate</code>.
149: *
150: * @see EditorDelegate#shouldSelectCell(EventObject)
151: */
152: public boolean shouldSelectCell(EventObject anEvent) {
153: return delegate.shouldSelectCell(anEvent);
154: }
155:
156: /**
157: * Forwards the message from the <code>CellEditor</code> to the
158: * <code>delegate</code>.
159: *
160: * @see EditorDelegate#stopCellEditing
161: */
162: public boolean stopCellEditing() {
163: return delegate.stopCellEditing();
164: }
165:
166: /**
167: * Forwards the message from the <code>CellEditor</code> to the
168: * <code>delegate</code>.
169: *
170: * @see EditorDelegate#cancelCellEditing
171: */
172: public void cancelCellEditing() {
173: delegate.cancelCellEditing();
174: }
175:
176: //
177: // Implementing the TreeCellEditor Interface
178: //
179:
180: /** Implements the <code>TreeCellEditor</code> interface. */
181: public Component getTreeCellEditorComponent(JTree tree,
182: Object value, boolean isSelected, boolean expanded,
183: boolean leaf, int row) {
184: String stringValue = tree.convertValueToText(value, isSelected,
185: expanded, leaf, row, false);
186:
187: delegate.setValue(stringValue);
188: return new JScrollPane(editorComponent);
189: }
190:
191: //
192: // Implementing the CellEditor Interface
193: //
194: /** Implements the <code>TableCellEditor</code> interface. */
195: public Component getTableCellEditorComponent(JTable table,
196: Object value, boolean isSelected, int row, int column) {
197: delegate.setValue(value);
198: return new JScrollPane(editorComponent);
199: }
200:
201: //
202: // Protected EditorDelegate class
203: //
204:
205: /**
206: * The protected <code>EditorDelegate</code> class.
207: */
208: protected class EditorDelegate implements FocusListener,
209: Serializable {
210: /** The value of this cell. */
211: protected Object value;
212:
213: /**
214: * Returns the value of this cell.
215: *
216: * @return the value of this cell
217: */
218: public Object getCellEditorValue() {
219: return value;
220: }
221:
222: /**
223: * Sets the value of this cell.
224: *
225: * @param value
226: * the new value of this cell
227: */
228: public void setValue(Object value) {
229: this .value = value;
230: }
231:
232: /**
233: * Returns true if <code>anEvent</code> is <b>not</b> a
234: * <code>MouseEvent</code>. Otherwise, it returns true if the
235: * necessary number of clicks have occurred, and returns false
236: * otherwise.
237: *
238: * @param anEvent
239: * the event
240: * @return true if cell is ready for editing, false otherwise
241: * @see #setClickCountToStart(int)
242: * @see #shouldSelectCell
243: */
244: public boolean isCellEditable(EventObject anEvent) {
245: if (anEvent instanceof MouseEvent) {
246: return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
247: }
248: return true;
249: }
250:
251: /**
252: * Returns true to indicate that the editing cell may be selected.
253: *
254: * @param anEvent
255: * the event
256: * @return true
257: * @see #isCellEditable
258: */
259: public boolean shouldSelectCell(EventObject anEvent) {
260: return true;
261: }
262:
263: /**
264: * Returns true to indicate that editing has begun.
265: *
266: * @param anEvent
267: * the event
268: */
269: public boolean startCellEditing(EventObject anEvent) {
270: return true;
271: }
272:
273: /**
274: * Stops editing and returns true to indicate that editing has stopped.
275: * This method calls <code>fireEditingStopped</code>.
276: *
277: * @return true
278: */
279: public boolean stopCellEditing() {
280: fireEditingStopped();
281: return true;
282: }
283:
284: /**
285: * Cancels editing. This method calls <code>fireEditingCanceled</code>.
286: */
287: public void cancelCellEditing() {
288: fireEditingCanceled();
289: }
290:
291: /**
292: * When an action is performed, editing is ended.
293: *
294: * @param e
295: * the action event
296: * @see #stopCellEditing
297: */
298: public void actionPerformed(ActionEvent e) {
299: TextAreaTableCellEditor.this .stopCellEditing();
300: }
301:
302: /**
303: * When an item's state changes, editing is ended.
304: *
305: * @param e
306: * the action event
307: * @see #stopCellEditing
308: */
309: public void itemStateChanged(ItemEvent e) {
310: TextAreaTableCellEditor.this .stopCellEditing();
311: }
312:
313: public void focusLost(FocusEvent ev) {
314: TextAreaTableCellEditor.this .stopCellEditing();
315: }
316:
317: public void focusGained(FocusEvent ev) {
318: }
319: }
320: }
|