/[classpath]/classpath/javax/swing/JTable.java
ViewVC logotype

Diff of /classpath/javax/swing/JTable.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9.2.13 by gnu_andrew, Tue Sep 20 18:46:31 2005 UTC revision 1.9.2.14 by gnu_andrew, Wed Nov 2 00:43:47 2005 UTC
# Line 45  import java.awt.Point; Line 45  import java.awt.Point;
45  import java.awt.Rectangle;  import java.awt.Rectangle;
46  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
47  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
 import java.awt.event.KeyAdapter;  
 import java.awt.event.KeyEvent;  
48  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
49  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
50  import java.text.DateFormat;  import java.text.DateFormat;
# Line 58  import java.util.Vector; Line 56  import java.util.Vector;
56    
57  import javax.accessibility.Accessible;  import javax.accessibility.Accessible;
58  import javax.accessibility.AccessibleContext;  import javax.accessibility.AccessibleContext;
59    import javax.accessibility.AccessibleExtendedTable;
60    import javax.accessibility.AccessibleSelection;
61    import javax.accessibility.AccessibleTable;
62    import javax.accessibility.AccessibleTableModelChange;
63  import javax.swing.event.CellEditorListener;  import javax.swing.event.CellEditorListener;
64  import javax.swing.event.ChangeEvent;  import javax.swing.event.ChangeEvent;
65  import javax.swing.event.ListSelectionEvent;  import javax.swing.event.ListSelectionEvent;
# Line 78  import javax.swing.table.TableColumnMode Line 80  import javax.swing.table.TableColumnMode
80  import javax.swing.table.TableModel;  import javax.swing.table.TableModel;
81  import javax.swing.text.Caret;  import javax.swing.text.Caret;
82    
83  public class JTable extends JComponent  public class JTable
84      extends JComponent
85    implements TableModelListener, Scrollable, TableColumnModelListener,    implements TableModelListener, Scrollable, TableColumnModelListener,
86               ListSelectionListener, CellEditorListener, Accessible               ListSelectionListener, CellEditorListener, Accessible
87  {  {
88    /**    /**
89       * Provides accessibility support for <code>JTable</code>.
90       *
91       * @author Roman Kennke (kennke@aicas.com)
92       */
93      protected class AccessibleJTable
94        extends AccessibleJComponent
95        implements AccessibleSelection, ListSelectionListener, TableModelListener,
96        TableColumnModelListener, CellEditorListener, PropertyChangeListener,
97        AccessibleExtendedTable
98      {
99    
100        protected class AccessibleJTableModelChange
101          implements AccessibleTableModelChange
102        {
103          protected int type;
104          protected int firstRow;
105          protected int lastRow;
106          protected int firstColumn;
107          protected int lastColumn;
108    
109          protected AccessibleJTableModelChange(int type, int firstRow,
110                                                int lastRow, int firstColumn,
111                                                int lastColumn)
112          {
113            this.type = type;
114            this.firstRow = firstRow;
115            this.lastRow = lastRow;
116            this.firstColumn = firstColumn;
117            this.lastColumn = lastColumn;
118          }
119    
120          public int getType()
121          {
122            return type;
123          }
124    
125          public int getFirstRow()
126          {
127            return firstRow;
128          }
129    
130          public int getLastRow()
131          {
132            return lastRow;
133          }
134    
135          public int getFirstColumn()
136          {
137            return firstColumn;
138          }
139    
140          public int getLastColumn()
141          {
142            return lastColumn;
143          }
144        }
145    
146        /**
147         * Creates a new <code>AccessibleJTable</code>.
148         *
149         * @since JDK1.5
150         */
151        protected AccessibleJTable()
152        {
153          getModel().addTableModelListener(this);
154          getSelectionModel().addListSelectionListener(this);
155          getColumnModel().addColumnModelListener(this);
156          getCellEditor().addCellEditorListener(this);
157        }
158    
159        /**
160         * Returns the number of selected items in this table.
161         */
162        public int getAccessibleSelectionCount()
163        {
164          return getSelectedColumnCount();
165        }
166    
167        public Accessible getAccessibleSelection(int i)
168        {
169          // TODO Auto-generated method stub
170          return null;
171        }
172    
173        public boolean isAccessibleChildSelected(int i)
174        {
175          // TODO Auto-generated method stub
176          return false;
177        }
178    
179        public void addAccessibleSelection(int i)
180        {
181          // TODO Auto-generated method stub
182          
183        }
184    
185        public void removeAccessibleSelection(int i)
186        {
187          // TODO Auto-generated method stub
188          
189        }
190    
191        public void clearAccessibleSelection()
192        {
193          // TODO Auto-generated method stub
194          
195        }
196    
197        public void selectAllAccessibleSelection()
198        {
199          // TODO Auto-generated method stub
200          
201        }
202    
203        public void valueChanged(ListSelectionEvent event)
204        {
205          // TODO Auto-generated method stub
206          
207        }
208    
209        /**
210         * Receives notification when the table model changes. Depending on the
211         * type of change, this method calls {@link #tableRowsInserted} or
212         * {@link #tableRowsDeleted}.
213         *
214         * @param event the table model event
215         */
216        public void tableChanged(TableModelEvent event)
217        {
218          switch (event.getType())
219            {
220            case TableModelEvent.INSERT:
221              tableRowsInserted(event);
222              break;
223            case TableModelEvent.DELETE:
224              tableRowsDeleted(event);
225              break;
226            }
227        }
228    
229        /**
230         * Receives notification when one or more rows have been inserted into the
231         * table.
232         *
233         * @param event the table model event
234         */
235        public void tableRowsInserted(TableModelEvent event)
236        {
237          // TODO: What to do here, if anything? This might be a hook method for
238          // subclasses...
239        }
240    
241        /**
242         * Receives notification when one or more rows have been deleted from the
243         * table.
244         *
245         * @param event the table model event
246         */
247        public void tableRowsDeleted(TableModelEvent event)
248        {
249          // TODO: What to do here, if anything? This might be a hook method for
250          // subclasses...
251        }
252    
253        public void columnAdded(TableColumnModelEvent event)
254        {
255          // TODO Auto-generated method stub
256          
257        }
258    
259        public void columnMarginChanged(ChangeEvent event)
260        {
261          // TODO Auto-generated method stub
262          
263        }
264    
265        public void columnMoved(TableColumnModelEvent event)
266        {
267          // TODO Auto-generated method stub
268          
269        }
270    
271        public void columnRemoved(TableColumnModelEvent event)
272        {
273          // TODO Auto-generated method stub
274          
275        }
276    
277        public void columnSelectionChanged(ListSelectionEvent event)
278        {
279          // TODO Auto-generated method stub
280          
281        }
282    
283        public void editingCanceled(ChangeEvent event)
284        {
285          // TODO Auto-generated method stub
286          
287        }
288    
289        public void editingStopped(ChangeEvent event)
290        {
291          // TODO Auto-generated method stub
292          
293        }
294    
295        /**
296         * Receives notification when any of the JTable's properties changes. This
297         * is used to replace the listeners on the table's model, selection model,
298         * column model and cell editor.
299         *
300         * @param e the property change event
301         */
302        public void propertyChange(PropertyChangeEvent e)
303        {
304          String propName = e.getPropertyName();
305          if (propName.equals("tableModel"))
306            {
307              TableModel oldModel = (TableModel) e.getOldValue();
308              oldModel.removeTableModelListener(this);
309              TableModel newModel = (TableModel) e.getNewValue();
310              newModel.addTableModelListener(this);
311            }
312          else if (propName.equals("columnModel"))
313            {
314              TableColumnModel oldModel = (TableColumnModel) e.getOldValue();
315              oldModel.removeColumnModelListener(this);
316              TableColumnModel newModel = (TableColumnModel) e.getNewValue();
317              newModel.addColumnModelListener(this);
318            }
319          else if (propName.equals("selectionModel"))
320            {
321              ListSelectionModel oldModel = (ListSelectionModel) e.getOldValue();
322              oldModel.removeListSelectionListener(this);
323              ListSelectionModel newModel = (ListSelectionModel) e.getNewValue();
324              newModel.addListSelectionListener(this);
325            }
326          else if (propName.equals("cellEditor"))
327            {
328              CellEditor oldEd = (CellEditor) e.getOldValue();
329              oldEd.removeCellEditorListener(this);
330              CellEditor newEd = (CellEditor) e.getNewValue();
331              newEd.addCellEditorListener(this);
332            }
333        }
334    
335        public int getAccessibleRow(int index)
336        {
337          // TODO Auto-generated method stub
338          return 0;
339        }
340    
341        public int getAccessibleColumn(int index)
342        {
343          // TODO Auto-generated method stub
344          return 0;
345        }
346    
347        public int getAccessibleIndex(int r, int c)
348        {
349          // TODO Auto-generated method stub
350          return 0;
351        }
352    
353        public Accessible getAccessibleCaption()
354        {
355          // TODO Auto-generated method stub
356          return null;
357        }
358    
359        public void setAccessibleCaption(Accessible caption)
360        {
361          // TODO Auto-generated method stub
362          
363        }
364    
365        public Accessible getAccessibleSummary()
366        {
367          // TODO Auto-generated method stub
368          return null;
369        }
370    
371        public void setAccessibleSummary(Accessible summary)
372        {
373          // TODO Auto-generated method stub
374          
375        }
376    
377        public int getAccessibleRowCount()
378        {
379          // TODO Auto-generated method stub
380          return 0;
381        }
382    
383        public int getAccessibleColumnCount()
384        {
385          // TODO Auto-generated method stub
386          return 0;
387        }
388    
389        public Accessible getAccessibleAt(int r, int c)
390        {
391          // TODO Auto-generated method stub
392          return null;
393        }
394    
395        public int getAccessibleRowExtentAt(int r, int c)
396        {
397          // TODO Auto-generated method stub
398          return 0;
399        }
400    
401        public int getAccessibleColumnExtentAt(int r, int c)
402        {
403          // TODO Auto-generated method stub
404          return 0;
405        }
406    
407        public AccessibleTable getAccessibleRowHeader()
408        {
409          // TODO Auto-generated method stub
410          return null;
411        }
412    
413        public void setAccessibleRowHeader(AccessibleTable header)
414        {
415          // TODO Auto-generated method stub
416          
417        }
418    
419        public AccessibleTable getAccessibleColumnHeader()
420        {
421          // TODO Auto-generated method stub
422          return null;
423        }
424    
425        public void setAccessibleColumnHeader(AccessibleTable header)
426        {
427          // TODO Auto-generated method stub
428          
429        }
430    
431        public Accessible getAccessibleRowDescription(int r)
432        {
433          // TODO Auto-generated method stub
434          return null;
435        }
436    
437        public void setAccessibleRowDescription(int r, Accessible description)
438        {
439          // TODO Auto-generated method stub
440          
441        }
442    
443        public Accessible getAccessibleColumnDescription(int c)
444        {
445          // TODO Auto-generated method stub
446          return null;
447        }
448    
449        public void setAccessibleColumnDescription(int c, Accessible description)
450        {
451          // TODO Auto-generated method stub
452          
453        }
454    
455        public boolean isAccessibleSelected(int r, int c)
456        {
457          // TODO Auto-generated method stub
458          return false;
459        }
460    
461        public boolean isAccessibleRowSelected(int r)
462        {
463          // TODO Auto-generated method stub
464          return false;
465        }
466    
467        public boolean isAccessibleColumnSelected(int c)
468        {
469          // TODO Auto-generated method stub
470          return false;
471        }
472    
473        public int[] getSelectedAccessibleRows()
474        {
475          // TODO Auto-generated method stub
476          return null;
477        }
478    
479        public int[] getSelectedAccessibleColumns()
480        {
481          // TODO Auto-generated method stub
482          return null;
483        }
484          
485      }
486      /**
487     * Handles property changes from the <code>TableColumn</code>s of this     * Handles property changes from the <code>TableColumn</code>s of this
488     * <code>JTable</code>.     * <code>JTable</code>.
489     *     *
490     * More specifically, this triggers a {@link #revalidate} call if the     * More specifically, this triggers a {@link #revalidate()} call if the
491     * preferredWidth of one of the observed columns changes.     * preferredWidth of one of the observed columns changes.
492     */     */
493    class TableColumnPropertyChangeHandler implements PropertyChangeListener    class TableColumnPropertyChangeHandler implements PropertyChangeListener
# Line 393  public class JTable extends JComponent Line 794  public class JTable extends JComponent
794     * property when the {@link #dataModel} property is changed.     * property when the {@link #dataModel} property is changed.
795     *     *
796     * @see #setModel(TableModel)     * @see #setModel(TableModel)
797     * @see #createColumnsFromModel()     * @see #createDefaultColumnsFromModel()
798     * @see #setColumnModel(TableColumnModel)     * @see #setColumnModel(TableColumnModel)
799     * @see #setAutoCreateColumnsFromModel(boolean)     * @see #setAutoCreateColumnsFromModel(boolean)
800     * @see #getAutoCreateColumnsFromModel()     * @see #getAutoCreateColumnsFromModel()
# Line 509  public class JTable extends JComponent Line 910  public class JTable extends JComponent
910    protected ListSelectionModel selectionModel;    protected ListSelectionModel selectionModel;
911    
912    /**    /**
    * The accessibleContext property.  
    */  
   protected AccessibleContext accessibleContext;  
   
   /**  
913     * The current cell editor.     * The current cell editor.
914     */     */
915    protected TableCellEditor cellEditor;    protected TableCellEditor cellEditor;
# Line 521  public class JTable extends JComponent Line 917  public class JTable extends JComponent
917    /**    /**
918     * Whether or not drag-and-drop is enabled on this table.     * Whether or not drag-and-drop is enabled on this table.
919     *     *
920     * @see #setDragEnabled()     * @see #setDragEnabled(boolean)
921     * @see #getDragEnabled()     * @see #getDragEnabled()
922     */     */
923    private boolean dragEnabled;    private boolean dragEnabled;
# Line 678  public class JTable extends JComponent Line 1074  public class JTable extends JComponent
1074     */     */
1075    public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm)    public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm)
1076    {    {
1077      setModel(dm == null ? createDefaultDataModel() : dm);      boolean autoCreate = false;
     setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);  
   
1078      if (cm != null)      if (cm != null)
       {  
1079          setColumnModel(cm);          setColumnModel(cm);
         setAutoCreateColumnsFromModel(false);  
       }  
1080      else      else
1081        {        {
1082          setColumnModel(createDefaultColumnModel());          setColumnModel(createDefaultColumnModel());
1083          setAutoCreateColumnsFromModel(true);          autoCreate = true;
1084        }        }        
1085        setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);
1086        setModel(dm == null ? createDefaultDataModel() : dm);
1087        setAutoCreateColumnsFromModel(autoCreate);
1088      initializeLocalVars();      initializeLocalVars();
1089      // The next two lines are for compliance with the JDK which starts      // The following four lines properly set the lead selection indices.
1090      // the JLists associated with a JTable  with both lead selection      // After this, the UI will handle the lead selection indices.
1091      // indices at 0, rather than -1 as in regular JLists      // FIXME: this should probably not be necessary, if the UI is installed
1092        // before the TableModel is set then the UI will handle things on its
1093        // own, but certain variables need to be set before the UI can be installed
1094        // so we must get the correct order for all the method calls in this
1095        // constructor.
1096        selectionModel.setAnchorSelectionIndex(0);    
1097      selectionModel.setLeadSelectionIndex(0);      selectionModel.setLeadSelectionIndex(0);
1098        columnModel.getSelectionModel().setAnchorSelectionIndex(0);
1099      columnModel.getSelectionModel().setLeadSelectionIndex(0);      columnModel.getSelectionModel().setLeadSelectionIndex(0);
1100      updateUI();      updateUI();
1101    }        }    
# Line 919  public class JTable extends JComponent Line 1319  public class JTable extends JComponent
1319    
1320          createDefaultColumnsFromModel();          createDefaultColumnsFromModel();
1321    
1322        // If the structure changes, we need to revalidate, since that might
1323        // affect the size parameters of the JTable. Otherwise we only need
1324        // to perform a repaint to update the view.
1325        if (event.getType() == TableModelEvent.INSERT
1326            || event.getType() == TableModelEvent.DELETE)
1327          revalidate();
1328      repaint();      repaint();
1329    }    }
1330    
# Line 971  public class JTable extends JComponent Line 1377  public class JTable extends JComponent
1377        {        {
1378          int y0 = getLocation().y;          int y0 = getLocation().y;
1379          int nrows = getRowCount();          int nrows = getRowCount();
1380          Dimension gap = getIntercellSpacing();          int height = getRowHeight();
         int height = getRowHeight() + (gap == null ? 0 : gap.height);  
1381          int y = point.y;          int y = point.y;
1382    
1383          for (int i = 0; i < nrows; ++i)          for (int i = 0; i < nrows; ++i)
# Line 1093  public class JTable extends JComponent Line 1498  public class JTable extends JComponent
1498      // scroll direction.      // scroll direction.
1499    
1500      if (orientation == SwingConstants.VERTICAL)      if (orientation == SwingConstants.VERTICAL)
1501        return rowHeight;        return direction * rowHeight;
1502      else      else
1503        {        {
1504          int sum = 0;          int sum = 0;
1505          for (int i = 0; i < getColumnCount(); ++i)          for (int i = 0; i < getColumnCount(); ++i)
1506            sum += columnModel.getColumn(0).getWidth();            sum += columnModel.getColumn(0).getWidth();
1507          return getColumnCount() == 0 ? 10 : sum / getColumnCount();          int inc = getColumnCount() == 0 ? 10 : sum / getColumnCount();
1508            return direction * inc;
1509        }        }
1510    }    }
1511    
# Line 1680  public class JTable extends JComponent Line 2086  public class JTable extends JComponent
2086      // Don't do anything if setting the current model again.      // Don't do anything if setting the current model again.
2087      if (dataModel == m)      if (dataModel == m)
2088        return;        return;
2089        
2090        TableModel oldModel = dataModel;
2091    
2092      // Remove table as TableModelListener from old model.      // Remove table as TableModelListener from old model.
2093      if (dataModel != null)      if (dataModel != null)
2094        dataModel.removeTableModelListener(this);        dataModel.removeTableModelListener(this);
# Line 1697  public class JTable extends JComponent Line 2105  public class JTable extends JComponent
2105          if (autoCreateColumnsFromModel)          if (autoCreateColumnsFromModel)
2106            createDefaultColumnsFromModel();            createDefaultColumnsFromModel();
2107        }        }
2108        
2109        // This property is bound, so we fire a property change event.
2110        firePropertyChange("model", oldModel, dataModel);
2111    
2112      // Repaint table.      // Repaint table.
2113      revalidate();      revalidate();
2114      repaint();      repaint();
# Line 1983  public class JTable extends JComponent Line 2394  public class JTable extends JComponent
2394      int average = spill / cols.length;      int average = spill / cols.length;
2395      for (int i = 0; i < cols.length; i++)      for (int i = 0; i < cols.length; i++)
2396        {        {
2397          cols[i].setWidth(cols[i].getWidth() + average);          if (cols[i] != null)
2398              cols[i].setWidth(cols[i].getWidth() + average);
2399        }        }
2400    }    }
2401    
# Line 2402  public class JTable extends JComponent Line 2814  public class JTable extends JComponent
2814      return editor.getTableCellEditorComponent      return editor.getTableCellEditorComponent
2815        (this, getValueAt(row, column), isCellSelected(row, column), row, column);        (this, getValueAt(row, column), isCellSelected(row, column), row, column);
2816    }    }
2817    
2818      /**
2819       * This revalidates the <code>JTable</code> and queues a repaint.
2820       */
2821      protected void resizeAndRepaint()
2822      {
2823        revalidate();
2824        repaint();
2825      }
2826  }  }

Legend:
Removed from v.1.9.2.13  
changed lines
  Added in v.1.9.2.14

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26