Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ledestin/swing/CollectionTableModel.java


1   /*
2   * Copyright (C) 2000-2001 Dmitry Macsema.  All Rights Reserved.  
3   * 
4   * Redistribution and use in source and binary forms, with or without
5   * modification, are permitted under the terms of the following
6   * Open Source license:
7   *
8   * The GNU General Public License, version 2, or any later version, as
9   * published by the Free Software Foundation
10  * (http://www.fsf.org/copyleft/gpl.html);
11  */
12  
13  package ledestin.swing;
14  
15  import java.util.*;
16  import javax.swing.table.*;
17  import javax.swing.event.*;
18  
19  /**
20  Allows to show a Collection in a {@link JEditableTable}. It wraps your data source
21  Collection that allows it to be shown in a table. There is always one column and 
22  getValueAt() always returns appropriate Collection item (should have toString() 
23  implemented).<br>
24  
25  If you'd like to change this behavior you'll have to implement the following 
26  methods:
27  public int getColumnCount();
28  public Object getValueAt(int row, int column);<br />
29  */
30  public class CollectionTableModel extends AbstractTableModel {
31    protected Collection ds;
32    protected Class rowClass;
33    protected boolean dataChanged;
34    private Object[] data;
35  
36    public CollectionTableModel(Collection ds) throws IllegalArgumentException {
37      this(ds, null);
38    }
39    public CollectionTableModel(Collection ds, Class rowClass) throws IllegalArgumentException {
40      if (ds == null) {
41        throw new IllegalArgumentException("DataSource parameter can't be null");
42      }
43      this.ds = ds;
44      this.rowClass = rowClass;
45    }
46    public int getRowCount() {
47      return ds.size();
48    }
49    public Collection getDataSource() {
50      return ds;
51    }
52    public void setDataSource(Collection ds) {
53      this.ds = ds;
54    }
55    /**
56    Creates a new object/row for the table model. The faculty is wanted for
57    JEditableTable's ability to add rows to a table. When request for inserting 
58    comes to the table it shows a dialog for editing a new object.
59    */
60    public Object createNewRow() throws InstantiationException {
61      Object result = null;
62      if (rowClass != null) {
63        try {
64          result = rowClass.newInstance();        
65        } catch (IllegalAccessException e) {
66          e.printStackTrace();
67          throw new InstantiationException(e.getMessage());
68        }
69      } else {
70        throw new InstantiationException("No Class to instantiate");
71      }
72      return result;
73    }
74    protected Object[] getData() {
75      if (data == null || dataChanged) {
76        data = ds.toArray();
77        dataChanged = false;
78      }
79      return data;
80    }
81    /**
82    Returns an object representing row in a table, i.e. always return the same
83    object for any column given. Descendants will have to redefine this behaviour
84    if necessary, for example, returning ((SampleObject)o).col1() for 1st column
85    and col2() for the second.
86    */
87    public Object getValueAt(int row, int column) {
88      return getData()[row];
89    }
90    public int getColumnCount() {
91      return 1;
92    }
93    public void fireTableChanged(TableModelEvent e) {
94      dataChanged = true;
95      super.fireTableChanged(e);
96    }
97    public void fireTableDataChanged() {
98      dataChanged = true;
99      super.fireTableDataChanged();
100   }
101   public void fireTableRowsDeleted(int firstRow, int lastRow) {
102     dataChanged = true;
103     super.fireTableRowsDeleted(firstRow, lastRow);
104   }
105   public void fireTableRowsInserted(int firstRow, int lastRow) {
106     dataChanged = true;
107     super.fireTableRowsInserted(firstRow, lastRow);
108   }
109   public void fireTableStructureChanged() {
110     dataChanged = true;
111     super.fireTableStructureChanged();
112   }
113 }