KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > popupswitcher > SwitcherTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.popupswitcher;
21
22 import javax.swing.event.TableModelEvent JavaDoc;
23 import javax.swing.table.AbstractTableModel JavaDoc;
24 import org.openide.util.Utilities;
25
26 /**
27  * <code>TableModel</code> for <code>SwitcherTable</class>.
28  *
29  * @see SwitcherTable
30  *
31  * @author mkrauskopf
32  */

33 class SwitcherTableModel extends AbstractTableModel JavaDoc {
34
35     /**
36      * Used to estimate number of cells fitting to given space Event object for
37      * this TableModel.
38      */

39     private TableModelEvent JavaDoc event;
40
41     /** Number of rows */
42     private int rows;
43     
44     /** Number of columns */
45     private int cols;
46     
47     /** Items */
48     private SwitcherTableItem[] items;
49     
50     /**
51      * Use whole screen for table height during number of columns/row
52      * computing.
53      */

54     SwitcherTableModel(SwitcherTableItem[] items, int rowHeight) {
55         this(items, rowHeight, Utilities.getUsableScreenBounds().height);
56     }
57     
58     /** Use specified table height during number of columns/row computing. */
59     SwitcherTableModel(SwitcherTableItem[] items, int rowHeight, int tableHeight) {
60         super();
61         this.items = items;
62         computeRowsAndCols(rowHeight, tableHeight);
63     }
64     
65     private void computeRowsAndCols(int rowHeight, int tableHeight) {
66         // Default algorithm - use whole screen for SwitcherTable
67
int nOfItems = items.length;
68         if (nOfItems > 0) { // avoid div by 0
69
// Compute number of rows in one column
70
int maxRowsPerCol = tableHeight / rowHeight;
71             int nOfColumns = (nOfItems / maxRowsPerCol);
72             if (nOfItems % maxRowsPerCol > 0) {
73                 nOfColumns++;
74             }
75             int nOfRows = nOfItems / nOfColumns;
76             if (nOfItems % nOfColumns > 0) {
77                 nOfRows++;
78             }
79             setRowsAndColumns(nOfRows, nOfColumns);
80         } else {
81             setRowsAndColumns(0, 0);
82         }
83     }
84     
85     private void setRowsAndColumns(int rows, int cols) {
86         if ((this.rows != rows) || (this.cols != cols)) {
87             this.rows = rows;
88             this.cols = cols;
89             if (event == null) {
90                 event = new TableModelEvent JavaDoc(this);
91             }
92             fireTableChanged(event);
93         }
94     }
95     
96     public Class JavaDoc getColumnClass(int columnIndex) {
97         return SwitcherTableItem.class;
98     }
99     
100     public String JavaDoc getColumnName(int columnIndex) {
101         return "";
102     }
103     
104     public boolean isCellEditable(int rowIndex, int columnIndex) {
105         return true;
106     }
107     
108     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
109         if ((rowIndex == -1) || (columnIndex == -1)) {
110             return null;
111         }
112         int docIdx = (columnIndex * getRowCount()) + rowIndex;
113         return (docIdx < items.length ? items[docIdx] : null);
114     }
115     
116     public int getRowCount() {
117         return rows;
118     }
119     
120     public int getColumnCount() {
121         return cols;
122     }
123 }
124
Popular Tags