1 /* ******************************************************************** ** 2 ** Copyright notice ** 3 ** ** 4 ** (c) 2003 WiSim Development Team ** 5 ** http://wisim.sourceforge.net/ ** 6 ** ** 7 ** All rights reserved ** 8 ** ** 9 ** This script is part of the WiSim Business Game project. The WiSim ** 10 ** project is free software; you can redistribute it and/or modify ** 11 ** it under the terms of the GNU General Public License as published by ** 12 ** the Free Software Foundation; either version 2 of the License, or ** 13 ** (at your option) any later version. ** 14 ** ** 15 ** The GNU General Public License can be found at ** 16 ** http://www.gnu.org/copyleft/gpl.html. ** 17 ** A copy is found in the textfile GPL.txt and important notices to the ** 18 ** license from the team is found in the textfile LICENSE.txt distributed ** 19 ** in these package. ** 20 ** ** 21 ** This copyright notice MUST APPEAR in all copies of the file! ** 22 ** ******************************************************************** */ 23 24 package net.sourceforge.wisim.controller; 25 26 import java.awt.Component; 27 import java.awt.event; 28 import javax.swing.table; 29 import javax.swing.event; 30 import java.util.EventObject; 31 import javax.swing.tree; 32 import java.io.Serializable; 33 import javax.swing; 34 35 36 /** 37 * @author Zafir Anjum 38 * http://www.codeguru.com/java/articles/162.shtml 39 */ 40 public class JComponentCellEditor implements TableCellEditor, TreeCellEditor, 41 Serializable { 42 43 protected EventListenerList listenerList = new EventListenerList(); 44 transient protected ChangeEvent changeEvent = null; 45 46 protected JComponent editorComponent = null; 47 protected JComponent container = null; // Can be tree or table 48 49 /** 50 * @return editorComponent 51 */ 52 public Component getComponent() { 53 return editorComponent; 54 } 55 56 /** 57 * @return editorComponent 58 */ 59 public Object getCellEditorValue() { 60 return editorComponent; 61 } 62 63 /** 64 * @param anEvent 65 * @return boolean 66 */ 67 public boolean isCellEditable(EventObject anEvent) { 68 return true; 69 } 70 71 /** 72 * @param anEvent 73 * @return boolean 74 */ 75 public boolean shouldSelectCell(EventObject anEvent) { 76 if( editorComponent != null && anEvent instanceof MouseEvent 77 && ((MouseEvent)anEvent).getID() == MouseEvent.MOUSE_PRESSED ) { 78 Component dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3 ); 79 MouseEvent e = (MouseEvent)anEvent; 80 MouseEvent e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_RELEASED, 81 e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(), 82 e.isPopupTrigger() ); 83 dispatchComponent.dispatchEvent(e2); 84 e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_CLICKED, 85 e.getWhen() + 100001, e.getModifiers(), 3, 3, 1, 86 e.isPopupTrigger() ); 87 dispatchComponent.dispatchEvent(e2); 88 } 89 return false; 90 } 91 92 /** 93 * @return boolean 94 */ 95 public boolean stopCellEditing() { 96 fireEditingStopped(); 97 return true; 98 } 99 100 public void cancelCellEditing() { 101 fireEditingCanceled(); 102 } 103 104 /** 105 * @param l 106 */ 107 public void addCellEditorListener(CellEditorListener l) { 108 listenerList.add(CellEditorListener.class, l); 109 } 110 111 /** 112 * @param l 113 */ 114 public void removeCellEditorListener(CellEditorListener l) { 115 listenerList.remove(CellEditorListener.class, l); 116 } 117 118 protected void fireEditingStopped() { 119 Object[] listeners = listenerList.getListenerList(); 120 // Process the listeners last to first, notifying 121 // those that are interested in this event 122 for (int i = listeners.length-2; i>=0; i-=2) { 123 if (listeners[i]==CellEditorListener.class) { 124 // Lazily create the event: 125 if (changeEvent == null) 126 changeEvent = new ChangeEvent(this); 127 ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent); 128 } 129 } 130 } 131 132 protected void fireEditingCanceled() { 133 // Guaranteed to return a non-null array 134 Object[] listeners = listenerList.getListenerList(); 135 // Process the listeners last to first, notifying 136 // those that are interested in this event 137 for (int i = listeners.length-2; i>=0; i-=2) { 138 if (listeners[i]==CellEditorListener.class) { 139 // Lazily create the event: 140 if (changeEvent == null) 141 changeEvent = new ChangeEvent(this); 142 ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent); 143 } 144 } 145 } 146 147 // implements javax.swing.tree.TreeCellEditor 148 /** 149 * @param tree 150 * @param value 151 * @param isSelected 152 * @param expanded 153 * @param leaf 154 * @param row 155 * @return editorComponent 156 */ 157 public Component getTreeCellEditorComponent(JTree tree, Object value, 158 boolean isSelected, boolean expanded, boolean leaf, int row) { 159 String stringValue = tree.convertValueToText(value, isSelected, 160 expanded, leaf, row, false); 161 162 editorComponent = (JComponent)value; 163 container = tree; 164 return editorComponent; 165 } 166 167 // implements javax.swing.table.TableCellEditor 168 /** 169 * @param table 170 * @param value 171 * @param isSelected 172 * @param row 173 * @param column 174 * @return editorComponent 175 */ 176 public Component getTableCellEditorComponent(JTable table, Object value, 177 boolean isSelected, int row, int column) { 178 179 editorComponent = (JComponent)value; 180 container = table; 181 return editorComponent; 182 } 183 }