1 18 19 package org.apache.jmeter.gui.util; 20 21 import java.awt.Component ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.FocusEvent ; 24 import java.awt.event.FocusListener ; 25 import java.awt.event.ItemEvent ; 26 import java.awt.event.MouseEvent ; 27 import java.io.Serializable ; 28 import java.util.EventObject ; 29 30 import javax.swing.AbstractCellEditor ; 31 import javax.swing.JScrollPane ; 32 import javax.swing.JTable ; 33 import javax.swing.JTextArea ; 34 import javax.swing.JTree ; 35 import javax.swing.table.TableCellEditor ; 36 import javax.swing.tree.TreeCellEditor ; 37 38 42 public class TextAreaTableCellEditor 43 extends AbstractCellEditor 44 implements TableCellEditor , TreeCellEditor 45 { 46 50 51 protected JTextArea editorComponent; 52 53 57 protected EditorDelegate delegate; 58 59 64 protected int clickCountToStart = 1; 65 66 70 73 public TextAreaTableCellEditor() 74 { 75 editorComponent = new JTextArea (); 76 editorComponent.setRows(3); 77 this.clickCountToStart = 2; 78 delegate = new EditorDelegate() 79 { 80 public void setValue(Object value) 81 { 82 editorComponent.setText( 83 (value != null) ? value.toString() : ""); 84 } 85 86 public Object getCellEditorValue() 87 { 88 return editorComponent.getText(); 89 } 90 }; 91 editorComponent.addFocusListener(delegate); 92 } 93 94 99 public Component getComponent() 100 { 101 return editorComponent; 102 } 103 104 108 115 public void setClickCountToStart(int count) 116 { 117 clickCountToStart = count; 118 } 119 120 124 public int getClickCountToStart() 125 { 126 return clickCountToStart; 127 } 128 129 134 139 public Object getCellEditorValue() 140 { 141 return delegate.getCellEditorValue(); 142 } 143 144 149 public boolean isCellEditable(EventObject anEvent) 150 { 151 return delegate.isCellEditable(anEvent); 152 } 153 154 159 public boolean shouldSelectCell(EventObject anEvent) 160 { 161 return delegate.shouldSelectCell(anEvent); 162 } 163 164 169 public boolean stopCellEditing() 170 { 171 return delegate.stopCellEditing(); 172 } 173 174 179 public void cancelCellEditing() 180 { 181 delegate.cancelCellEditing(); 182 } 183 184 188 189 public Component getTreeCellEditorComponent( 190 JTree tree, 191 Object value, 192 boolean isSelected, 193 boolean expanded, 194 boolean leaf, 195 int row) 196 { 197 String 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 (editorComponent); 208 } 209 210 214 public Component getTableCellEditorComponent( 215 JTable table, 216 Object value, 217 boolean isSelected, 218 int row, 219 int column) 220 { 221 delegate.setValue(value); 222 return new JScrollPane (editorComponent); 223 } 224 225 229 232 protected class EditorDelegate implements FocusListener , Serializable 233 { 234 235 protected Object value; 236 237 241 public Object getCellEditorValue() 242 { 243 return value; 244 } 245 246 250 public void setValue(Object value) 251 { 252 this.value = value; 253 } 254 255 266 public boolean isCellEditable(EventObject anEvent) 267 { 268 if (anEvent instanceof MouseEvent ) 269 { 270 return ((MouseEvent ) anEvent).getClickCount() 271 >= clickCountToStart; 272 } 273 return true; 274 } 275 276 284 public boolean shouldSelectCell(EventObject anEvent) 285 { 286 return true; 287 } 288 289 294 public boolean startCellEditing(EventObject anEvent) 295 { 296 return true; 297 } 298 299 306 public boolean stopCellEditing() 307 { 308 fireEditingStopped(); 309 return true; 310 } 311 312 315 public void cancelCellEditing() 316 { 317 fireEditingCanceled(); 318 } 319 320 325 public void actionPerformed(ActionEvent e) 326 { 327 TextAreaTableCellEditor.this.stopCellEditing(); 328 } 329 330 335 public void itemStateChanged(ItemEvent e) 336 { 337 TextAreaTableCellEditor.this.stopCellEditing(); 338 } 339 public void focusLost(FocusEvent ev) 340 { 341 TextAreaTableCellEditor.this.stopCellEditing(); 342 } 343 344 public void focusGained(FocusEvent ev) 345 { 346 } 347 } 348 } 349 | Popular Tags |