View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program 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
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.table;
21  
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import javax.help.CSH;
26  import javax.swing.JMenu;
27  import javax.swing.JTable;
28  import javax.swing.table.DefaultTableColumnModel;
29  
30  import org.xnap.gui.menu.TableColumnsMenu;
31  import org.xnap.util.TablePreferencesProvider;
32  
33  /***
34   * Provides a sortable table model with dynamic column support.
35   */
36  public abstract class AbstractColumnTableModel 
37      extends AbstractSortableTableModel implements ColumnModel
38  {
39  
40      //--- Data field(s) ---
41  
42      protected List columns = new LinkedList();
43  
44      //--- Constructor(s) ---
45  
46      public AbstractColumnTableModel()
47      {
48      }
49  
50      //--- Method(s) ---
51  
52      /***
53       * Creates a table from this model. 
54       */
55      public JTable createTable(TablePreferencesProvider tpp, String table)
56      {
57  		DefaultTableColumnModel dtcm = new DefaultTableColumnModel();
58  		JTable jta = new ColoredTable(this, dtcm);
59  		jta.setShowGrid(false);
60  		jta.setDefaultRenderer(Integer.class, new NumberCellRenderer());
61  		jta.setDefaultRenderer(String.class, new StringCellRenderer());
62  		CSH.setHelpIDString(jta, "tables");
63  	
64  		TablePreferencesHandler 
65  			tph = new TablePreferencesHandler(tpp, table, this, dtcm);
66  		TableColumnsMenu tcm 
67  			= new TableColumnsMenu(null, this, tpp, table);
68  		TableHeaderHandler thh
69  			= TableHeaderHandler.install(jta, tcm.getPopupMenu(), this);
70  		thh.setListener(tph);
71  
72  		return jta;
73      }
74  
75      public JMenu createColumnMenu(String name, TablePreferencesProvider tpp,
76  								  String table)
77      {
78  		return new TableColumnsMenu(name, this, tpp, table);
79      }
80  
81      public void addColumn(Column c)
82      {
83  		c.setModelIndex(columns.size());
84  		columns.add(c);
85      }
86  
87      public void addColumns(Column[] columns)
88      {
89  		for (int i = 0; i < columns.length; i++) {
90  			addColumn(columns[i]);
91  		}
92      }
93  
94      /***
95       * Returns the column at index <code>i</code>.
96       */
97      public Column getColumnAt(int i) 
98      {
99          return (Column)columns.get(i);
100     }
101 
102     /***
103      * Returns the class of the data displayed by the column at index 
104      * <code>i</code>.
105      */
106     public Class getColumnClass(int i) 
107     {
108         return ((Column)columns.get(i)).getDataType();
109     }
110 
111     /***
112      * Returns the number of columns.
113      */
114     public int getColumnCount() 
115     {
116         return columns.size();
117     }
118 
119     /***
120      * Returns the name of the column at index <code>i</code>.
121      */
122     public String getColumnName(int i) 
123     {
124         return ((Column)columns.get(i)).getName();
125     }
126 
127 }