KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > viewer > tableviewer > AbstractCellEditor


1 package com.ca.directory.jxplorer.viewer.tableviewer;
2
3 import java.awt.*;
4 import java.util.*;
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.table.*;
8 import javax.swing.tree.*;
9 import java.awt.event.MouseEvent JavaDoc;
10 import java.util.EventObject JavaDoc;
11
12      
13 abstract public class AbstractCellEditor implements TableCellEditor
14 {
15     protected EventListenerList listenerList = new EventListenerList();
16     protected Object JavaDoc value;
17     protected ChangeEvent changeEvent = null;
18     protected int clickCountToStart = 1;
19
20 //
21
// Handle the event listener bookkeeping
22
//
23
// implements javax.swing.CellEditor
24
public void addCellEditorListener(CellEditorListener l)
25     {
26         listenerList.add(CellEditorListener.class, l);
27     }
28
29     // implements javax.swing.CellEditor
30
public void removeCellEditorListener(CellEditorListener l)
31     {
32         listenerList.remove(CellEditorListener.class, l);
33     }
34
35     /*
36      * Notify all listeners that have registered interest for
37      * notification on this event type. The event instance
38      * is lazily created using the parameters passed into
39      * the fire method.
40      * @see EventListenerList
41      */

42     protected void fireEditingStopped()
43     {
44         // Guaranteed to return a non-null array
45
Object JavaDoc[] listeners = listenerList.getListenerList();
46         // Process the listeners last to first, notifying
47
// those that are interested in this event
48
for (int i = listeners.length-2; i>=0; i-=2)
49         {
50             if (listeners[i]==CellEditorListener.class)
51             {
52                 // Lazily create the event:
53
if (changeEvent == null)
54                     changeEvent = new ChangeEvent(this);
55                 ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
56             }
57         }
58     }
59
60     /*
61      * Notify all listeners that have registered interest for
62      * notification on this event type. The event instance
63      * is lazily created using the parameters passed into
64      * the fire method.
65      * @see EventListenerList
66      */

67     protected void fireEditingCanceled()
68     {
69         // Guaranteed to return a non-null array
70
Object JavaDoc[] listeners = listenerList.getListenerList();
71         // Process the listeners last to first, notifying
72
// those that are interested in this event
73
for (int i = listeners.length-2; i>=0; i-=2)
74         {
75             if (listeners[i]==CellEditorListener.class)
76             {
77                 // Lazily create the event:
78
if (changeEvent == null)
79                 changeEvent = new ChangeEvent(this);
80                 ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
81             }
82         }
83     }
84
85
86     public Object JavaDoc getCellEditorValue()
87     {
88         return value;
89     }
90     
91     public void setCellEditorValue(Object JavaDoc value)
92     {
93         this.value = value;
94     }
95
96  
97     /**
98      * Specifies the number of clicks needed to start editing.
99      *
100      * @param count an int specifying the number of clicks needed to start editing
101      * @see #getClickCountToStart
102      */

103     public void setClickCountToStart(int count)
104     {
105         clickCountToStart = count;
106     }
107
108     /**
109      * ClickCountToStart controls the number of clicks required to start
110      * editing.
111      */

112     public int getClickCountToStart()
113     {
114         return clickCountToStart;
115     }
116     
117     // implements javax.swing.CellEditor
118
public boolean isCellEditable(EventObject JavaDoc anEvent)
119     {
120         if (anEvent instanceof MouseEvent JavaDoc)
121         {
122             return ((MouseEvent JavaDoc)anEvent).getClickCount() >= clickCountToStart;
123         }
124         return true;
125     }
126  
127    
128     // implements javax.swing.CellEditor
129
public boolean stopCellEditing()
130     {
131         fireEditingStopped();
132         return true;
133     }
134
135     // implements javax.swing.CellEditor
136
public void cancelCellEditing()
137     {
138         fireEditingCanceled();
139     }
140
141    // implements javax.swing.CellEditor
142
public boolean shouldSelectCell(EventObject JavaDoc anEvent)
143     {
144         return true;
145     }
146
147 }
Popular Tags