001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.popupswitcher;
043:
044: import javax.swing.event.TableModelEvent;
045: import javax.swing.table.AbstractTableModel;
046: import org.openide.util.Utilities;
047:
048: /**
049: * <code>TableModel</code> for <code>SwitcherTable</class>.
050: *
051: * @see SwitcherTable
052: *
053: * @author mkrauskopf
054: */
055: class SwitcherTableModel extends AbstractTableModel {
056:
057: /**
058: * Used to estimate number of cells fitting to given space Event object for
059: * this TableModel.
060: */
061: private TableModelEvent event;
062:
063: /** Number of rows */
064: private int rows;
065:
066: /** Number of columns */
067: private int cols;
068:
069: /** Items */
070: private SwitcherTableItem[] items;
071:
072: /**
073: * Use whole screen for table height during number of columns/row
074: * computing.
075: */
076: SwitcherTableModel(SwitcherTableItem[] items, int rowHeight) {
077: this (items, rowHeight, Utilities.getUsableScreenBounds().height);
078: }
079:
080: /** Use specified table height during number of columns/row computing. */
081: SwitcherTableModel(SwitcherTableItem[] items, int rowHeight,
082: int tableHeight) {
083: super ();
084: this .items = items;
085: computeRowsAndCols(rowHeight, tableHeight);
086: }
087:
088: private void computeRowsAndCols(int rowHeight, int tableHeight) {
089: // Default algorithm - use whole screen for SwitcherTable
090: int nOfItems = items.length;
091: if (nOfItems > 0) { // avoid div by 0
092: // Compute number of rows in one column
093: int maxRowsPerCol = tableHeight / rowHeight;
094: int nOfColumns = (nOfItems / maxRowsPerCol);
095: if (nOfItems % maxRowsPerCol > 0) {
096: nOfColumns++;
097: }
098: int nOfRows = nOfItems / nOfColumns;
099: if (nOfItems % nOfColumns > 0) {
100: nOfRows++;
101: }
102: setRowsAndColumns(nOfRows, nOfColumns);
103: } else {
104: setRowsAndColumns(0, 0);
105: }
106: }
107:
108: private void setRowsAndColumns(int rows, int cols) {
109: if ((this .rows != rows) || (this .cols != cols)) {
110: this .rows = rows;
111: this .cols = cols;
112: if (event == null) {
113: event = new TableModelEvent(this );
114: }
115: fireTableChanged(event);
116: }
117: }
118:
119: public Class getColumnClass(int columnIndex) {
120: return SwitcherTableItem.class;
121: }
122:
123: public String getColumnName(int columnIndex) {
124: return "";
125: }
126:
127: public boolean isCellEditable(int rowIndex, int columnIndex) {
128: return true;
129: }
130:
131: public Object getValueAt(int rowIndex, int columnIndex) {
132: if ((rowIndex == -1) || (columnIndex == -1)) {
133: return null;
134: }
135: int docIdx = (columnIndex * getRowCount()) + rowIndex;
136: return (docIdx < items.length ? items[docIdx] : null);
137: }
138:
139: public int getRowCount() {
140: return rows;
141: }
142:
143: public int getColumnCount() {
144: return cols;
145: }
146: }
|