KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > NumberTableCellEditor


1 package snow.sortabletable;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.table.*;
8 import java.util.*;
9
10
11 /** our own table cell editor (based on DefaultCellEditor) using a textfield
12     keeping the types passed (Integer, Float, Double)
13     selet all when focused, nicer behaviour.
14 */

15 public final class NumberTableCellEditor extends JTextField implements TableCellEditor, ActionListener
16 {
17
18   protected final Vector<CellEditorListener> listeners = new Vector<CellEditorListener>();
19
20   // when a number is entered in a focused cell, it will be overwritten if true!
21
boolean selectAllWhenFocused = true;
22
23   // kept to test for class
24
protected Object JavaDoc oldValue = null;
25
26   public NumberTableCellEditor()
27   {
28      super();
29      //this.setInputVerifier(InputVerifier
30

31      // To stop cell editing
32
this.addActionListener(this);
33
34      if(selectAllWhenFocused)
35      {
36         this.addFocusListener(new FocusAdapter()
37         {
38            @Override JavaDoc public void focusGained(FocusEvent e)
39            {
40               selectAll();
41            }
42         });
43      }
44   } // Constructor
45

46
47
48   public Component getTableCellEditorComponent(JTable table,
49                                       Object JavaDoc value,
50                                       boolean isSelected,
51                                       int row,
52                                       int column)
53   {
54      // terminate editing
55
this.stopCellEditing();
56
57      oldValue = value;
58      setText(value.toString());
59
60      if(selectAllWhenFocused) this.selectAll();
61
62      return this;
63   }
64
65   /** called when enter is pressed
66   */

67   public void actionPerformed(ActionEvent e)
68   {
69      stopCellEditing();
70   }
71
72
73
74   //
75
// Celleditor interface impl
76
//
77

78  /** Adds a listener to the list that's notified when the editor stops, or cancels editing.
79  */

80  public void addCellEditorListener(CellEditorListener l)
81  {
82    listeners.addElement(l);
83  }
84
85  /** Removes a listener from the list that's notified
86  */

87  public void removeCellEditorListener(CellEditorListener l)
88  {
89    listeners.removeElement(l);
90  }
91
92  /** Tells the editor to cancel editing and not accept any partially edited value.
93  */

94  public void cancelCellEditing()
95  {
96     final ChangeEvent ce = new ChangeEvent(this);
97     for(int i=0; i<listeners.size(); i++)
98     {
99       CellEditorListener listener = listeners.elementAt(i);
100       listener.editingCanceled(ce);
101     }
102  }
103
104  /** Returns the value contained in the editor.
105  */

106  public Object JavaDoc getCellEditorValue()
107  {
108    if(oldValue==null)
109    {
110      return this.getText();
111    }
112
113    if(oldValue instanceof Integer JavaDoc)
114    {
115      try
116      {
117         return Integer.parseInt( this.getText() );
118      }
119      catch(Exception JavaDoc e)
120      {
121
122      }
123    }
124    if(oldValue instanceof Float JavaDoc)
125    {
126      try
127      {
128         return Float.parseFloat( this.getText() );
129      }
130      catch(Exception JavaDoc e)
131      {
132
133      }
134    }
135    if(oldValue instanceof Double JavaDoc)
136    {
137      try
138      {
139         return Double.parseDouble( this.getText() );
140      }
141      catch(Exception JavaDoc e)
142      {
143         System.out.println("NumberTableCellEditor.getCellEditorValue(): Bad double format: "+e.getMessage());
144      }
145    }
146
147    // IMPORTANT FOR ASE:
148
return this.getText();
149  }
150
151  /** Asks the editor if it can start editing using anEvent.
152      anEvent is in the invoking component coordinate system.
153      The editor can not assume the Component returned by getCellEditorComponent
154      is installed. This method is intended for the use of client to avoid
155      the cost of setting up and installing the editor component if editing
156      is not possible. If editing can be started this method returns true.
157  */

158  public boolean isCellEditable(EventObject anEvent)
159  {
160    return true;
161  }
162
163
164
165
166  /** Returns true if the editing cell should be selected, false otherwise.
167     Typically, the return value is true, because is most cases the editing cell
168     should be selected. However, it is useful to return false to keep the
169     selection from changing for some types of edits. eg.
170     A table that contains a column of check boxes, the user might want to
171     be able to change those checkboxes without altering the selection.
172     (See Netscape Communicator for just such an example) Of course, it is
173     up to the client of the editor to use the return value, but it
174     doesn't need to if it doesn't want to.
175  */

176  public boolean shouldSelectCell(EventObject anEvent)
177  {
178    return true;
179  }
180
181  /** Tells the editor to stop editing and accept any partially edited
182      value as the value of the editor.
183     @return true if editing was stopped; false otherwise
184  */

185  public boolean stopCellEditing()
186  {
187     final ChangeEvent ce = new ChangeEvent(this);
188     CellEditorListener[] listcopy = listeners.toArray(new CellEditorListener[listeners.size()]);
189     for(CellEditorListener cel : listcopy)
190     {
191       cel.editingStopped(ce);
192     }
193
194     // ok, we always accept.. ###
195
return true;
196  }
197
198
199
200
201
202 } // NumberTableCellEditor
Popular Tags