View Javadoc

1   package org.wcb.autohome.util;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   *  Project:   Alice X10 Interface
20   *
21   */ 
22  
23  import javax.swing.event.ChangeEvent;
24  import javax.swing.event.EventListenerList;
25  import javax.swing.event.CellEditorListener;
26  import javax.swing.table.TableCellEditor;
27  import java.awt.event.MouseEvent;
28  import java.util.EventObject;
29  
30  abstract public class AbstractCellEditor implements TableCellEditor {
31  	protected EventListenerList listenerList = new EventListenerList();
32  	protected Object value;
33  	protected ChangeEvent changeEvent = null;
34  	protected int clickCountToStart = 1;
35  
36  	public Object getCellEditorValue() {
37  		return value;
38  	}
39  
40  	public void setCellEditorValue(Object value) {
41  		this.value = value;
42  	}
43  
44  	public void setClickCountToStart(int count) {
45  		clickCountToStart = count;
46  	}
47  
48  	public int getClickCountToStart() {
49  		return clickCountToStart;
50  	}
51  
52  	public boolean isCellEditable(EventObject anEvent) {
53  		if (anEvent instanceof MouseEvent)
54          {
55  			if (((MouseEvent)anEvent).getClickCount() < clickCountToStart)
56              {
57  				return false;
58              }
59  		}
60  		return true;
61  	}
62  
63  	public boolean shouldSelectCell(EventObject anEvent) {
64  		return true;
65  	}
66  
67  	public boolean stopCellEditing() {
68  		fireEditingStopped();
69  		return true;
70  	}
71  
72  	public void cancelCellEditing() {
73  		fireEditingCanceled();
74  	}
75  
76  	public void addCellEditorListener(CellEditorListener l) {
77  		listenerList.add(CellEditorListener.class, l);
78  	}
79  
80  	public void removeCellEditorListener(CellEditorListener l) {
81  		listenerList.remove(CellEditorListener.class, l);
82  	}
83  
84  	protected void fireEditingStopped() {
85  		Object[] listeners = listenerList.getListenerList();
86  		for (int i = listeners.length - 2; i >= 0; i-=2)
87          {
88  			if (listeners[i] == CellEditorListener.class)
89              {
90  				if (changeEvent == null)
91                  {
92  					changeEvent = new ChangeEvent(this);
93                  }
94  				((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
95  			}	       
96  		}
97  	}
98  	protected void fireEditingCanceled() {
99  		Object[] listeners = listenerList.getListenerList();
100 		for (int i = listeners.length - 2; i >= 0; i-=2) {
101 			if (listeners[i] == CellEditorListener.class) {
102 				if (changeEvent == null)
103                 {
104 					changeEvent = new ChangeEvent(this);
105                 }
106 				((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
107 			}	       
108 		}
109 	}
110 }