1 19 20 21 23 package org.netbeans.modules.web.monitor.client; 24 25 26 28 import java.util.EventObject ; 29 30 import java.awt.Point ; 31 import java.awt.Rectangle ; 32 import java.awt.Component ; 33 import java.awt.BorderLayout ; 34 import java.awt.IllegalComponentStateException ; 35 36 import java.awt.event.MouseAdapter ; 37 import java.awt.event.MouseListener ; 38 import java.awt.event.MouseEvent ; 39 import java.awt.event.ActionEvent ; 40 import java.awt.event.ActionListener ; 41 42 import javax.swing.JPanel ; 43 import javax.swing.JTable ; 44 import javax.swing.JComboBox ; 45 import javax.swing.DefaultCellEditor ; 46 import javax.swing.ListCellRenderer ; 47 import javax.swing.SwingUtilities ; 48 49 import javax.swing.table.TableCellEditor ; 50 51 import javax.swing.event.ListSelectionEvent ; 52 import javax.swing.event.ListSelectionListener ; 53 54 55 63 65 public class ComboBoxTableCellEditor 66 extends DefaultCellEditor 67 implements TableCellEditor 68 { 69 70 73 private JPanel editor; 74 75 78 private boolean tableListenerAdded = false; 79 80 83 private JTable table; 84 85 88 private Runnable comboBoxFocusRequester; 89 90 93 private Runnable comboBoxOpener; 94 95 98 private int currentRow = -1; 99 100 103 private Object prevItem; 104 105 108 private Object initialValue; 109 110 113 private boolean consumeComboBoxActionEvent = true; 114 115 119 private EventObject startEditingEvent = null; 120 121 122 125 126 public ComboBoxTableCellEditor (Object [] values, 127 ListCellRenderer customRenderer) 128 { 129 130 super (new JComboBox ()); 131 132 setItems (values); 133 this.editor = new JPanel (new BorderLayout ()); 134 setClickCountToStart (1); 135 136 this.editor.addMouseListener (new MouseAdapter () 138 { 139 public final void mousePressed (MouseEvent evt) 140 { 141 eventEditorMousePressed (); 142 } 143 }); 144 145 JComboBox cb = getComboBox (); 146 147 if (customRenderer != null) 148 cb.setRenderer(customRenderer); 149 150 cb.addActionListener (new ActionListener () 151 { 152 public final void actionPerformed (ActionEvent evt) 153 { 154 eventComboBoxActionPerformed (); 155 } 156 }); 157 158 this.comboBoxFocusRequester = new Runnable () 159 { 160 public final void run () 161 { 162 getComboBox ().requestFocus (); 163 } 164 }; 165 this.comboBoxOpener = new Runnable () 166 { 167 public final void run () 168 { 169 if (startEditingEvent instanceof MouseEvent ) 170 { 171 try 172 { 173 getComboBox ().setPopupVisible (true); 174 } 175 catch (IllegalComponentStateException ex) 176 { 177 } 179 } 180 } 181 }; 182 183 } 184 185 public ComboBoxTableCellEditor (Object [] values) 186 { 187 188 this (values, null); 189 190 } 191 192 public ComboBoxTableCellEditor () 193 { 194 195 this (new Object [0], null); 196 197 } 198 199 200 public ComboBoxTableCellEditor (ListCellRenderer customRenderer) 201 { 202 203 this (new Object [0], customRenderer); 204 205 } 206 207 212 213 public final Component getTableCellEditorComponent (JTable table, 214 Object value, 215 boolean selected, 216 int row, 217 int col) 218 { 219 220 if ( ! this.tableListenerAdded) 222 { 223 this.tableListenerAdded = true; 224 this.table = table; 225 this.table.getSelectionModel ().addListSelectionListener (new ListSelectionListener () 226 { 227 public final void valueChanged (ListSelectionEvent evt) 228 { 229 eventTableSelectionChanged (); 230 } 231 }); 232 } 233 this.currentRow = row; 234 this.initialValue = value; 235 236 return getEditorComponent (table, value, selected, row, col); 237 238 } 239 240 protected Component getEditorComponent (JTable table, 241 Object value, 242 boolean selected, 243 int row, 244 int col) 245 { 246 247 setSelectedItem (value); 248 249 selected = table.isRowSelected (row); 251 if (selected) { 253 SwingUtilities.invokeLater (this.comboBoxFocusRequester); 254 SwingUtilities.invokeLater (this.comboBoxOpener); 255 return getComboBox (); 256 } 257 258 this.editor.removeAll (); 261 return this.editor; 262 263 } 264 265 266 274 275 public boolean isCellEditable (EventObject evt) 276 { 277 278 this.startEditingEvent = evt; 279 if (evt instanceof MouseEvent && evt.getSource () instanceof JTable ) 280 { 281 MouseEvent me = (MouseEvent ) evt; 282 JTable table = (JTable ) me.getSource (); 283 Point pt = new Point (me.getX (), me.getY ()); 284 int row = table.rowAtPoint (pt); 285 int col = table.columnAtPoint (pt); 286 Rectangle rec = table.getCellRect (row, col, false); 287 if (me.getY () >= rec.y + rec.height || me.getX () >= rec.x + rec.width) 288 { 289 return false; 290 } 291 } 292 293 return super.isCellEditable (evt); 294 295 } 296 297 public Object getCellEditorValue () 298 { 299 300 return prevItem ; 301 302 } 303 304 protected void setSelectedItem (Object item) 305 { 306 307 if (getComboBox ().getSelectedItem () != item) 308 { 309 this.consumeComboBoxActionEvent = false; 310 getComboBox ().setSelectedItem (item); 311 this.consumeComboBoxActionEvent = true; 312 } 313 314 } 315 316 public final void setItems (Object [] items) 317 { 318 319 JComboBox cb = getComboBox (); 320 cb.removeAllItems (); 321 final int n = (items != null ? items.length : 0); 322 for (int i = 0; i < n; i++) 323 { 324 cb.addItem (items [i]); 325 } 326 327 } 328 329 final void eventEditorMousePressed () 330 { 331 332 this.editor.add (getComboBox ()); 333 this.editor.revalidate (); 334 SwingUtilities.invokeLater (this.comboBoxFocusRequester); 335 336 } 337 338 protected void eventTableSelectionChanged () 339 { 340 341 if ( ! this.table.isRowSelected (this.currentRow)) 343 { 344 stopCellEditing (); 345 } 346 347 } 348 349 protected void eventComboBoxActionPerformed () 350 { 351 352 Object item = getComboBox ().getSelectedItem (); 353 if (item != null) prevItem = item; 354 if (consumeComboBoxActionEvent) stopCellEditing (); 355 356 } 357 358 public final JComboBox getComboBox () 359 { 360 361 return (JComboBox ) getComponent (); 362 363 } 364 365 366 public final Object getInitialValue () 367 { 368 369 return this.initialValue; 370 371 } 372 373 374 public final int getCurrentRow () 375 { 376 377 return this.currentRow; 378 379 } 380 381 382 } 383 | Popular Tags |