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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components.table;
042:
043: import org.netbeans.lib.profiler.ui.components.*;
044:
045: /**
046: * Table model that extends SortableTableModel and allows to hide columns
047: *
048: * @author Jiri Sedlacek
049: */
050: public class ExtendedTableModel extends SortableTableModel {
051: //~ Instance fields ----------------------------------------------------------------------------------------------------------
052:
053: private SortableTableModel realModel;
054: private int[] columnsMapping; // mapping virtual columns -> real columns
055: private boolean[] columnsVisibility; // visibility flags of real columns
056: private int realColumnsCount;
057: private int virtualColumnsCount;
058:
059: //~ Constructors -------------------------------------------------------------------------------------------------------------
060:
061: public ExtendedTableModel(SortableTableModel realModel) {
062: realColumnsCount = realModel.getColumnCount();
063: virtualColumnsCount = realColumnsCount;
064:
065: this .realModel = realModel;
066: columnsMapping = new int[realColumnsCount];
067:
068: boolean[] initialColumnsVisibility = new boolean[realColumnsCount];
069:
070: for (int i = 0; i < realColumnsCount; i++) {
071: initialColumnsVisibility[i] = true;
072: }
073:
074: setColumnsVisibility(initialColumnsVisibility);
075: }
076:
077: //~ Methods ------------------------------------------------------------------------------------------------------------------
078:
079: public Class getColumnClass(int col) {
080: return realModel.getColumnClass(getRealColumn(col));
081: }
082:
083: public int getColumnCount() {
084: return virtualColumnsCount;
085: }
086:
087: //---------------------
088: // SortableTableModel interface
089: public String getColumnName(int col) {
090: return realModel.getColumnName(getRealColumn(col));
091: }
092:
093: public String getColumnToolTipText(int columnIndex) {
094: return realModel
095: .getColumnToolTipText(getRealColumn(columnIndex));
096: }
097:
098: public void setColumnsVisibility(boolean[] columnsVisibility) {
099: this .columnsVisibility = columnsVisibility;
100: recomputeColumnsMapping();
101: }
102:
103: public boolean[] getColumnsVisibility() {
104: return columnsVisibility;
105: }
106:
107: public boolean getInitialSorting(int column) {
108: return realModel.getInitialSorting(getRealColumn(column));
109: }
110:
111: public int getRealColumn(int column) {
112: return columnsMapping[column];
113: }
114:
115: public void setRealColumnVisibility(int column, boolean visible) {
116: if (visible) {
117: showRealColumn(column);
118: } else {
119: hideRealColumn(column);
120: }
121: }
122:
123: public boolean isRealColumnVisible(int column) {
124: return columnsVisibility[column];
125: }
126:
127: public int getRowCount() {
128: return realModel.getRowCount();
129: }
130:
131: public Object getValueAt(int rowIndex, int columnIndex) {
132: return realModel.getValueAt(rowIndex,
133: getRealColumn(columnIndex));
134: }
135:
136: public int getVirtualColumn(int column) {
137: for (int i = 0; i < virtualColumnsCount; i++) {
138: if (getRealColumn(i) == column) {
139: return i;
140: }
141: }
142:
143: return -1;
144: }
145:
146: public void hideRealColumn(int column) {
147: if (isRealColumnVisible(column)) {
148: columnsVisibility[column] = false;
149: recomputeColumnsMapping();
150: }
151: }
152:
153: public void showRealColumn(int column) {
154: if (!isRealColumnVisible(column)) {
155: columnsVisibility[column] = true;
156: recomputeColumnsMapping();
157: }
158: }
159:
160: public void sortByColumn(int column, boolean order) {
161: realModel.sortByColumn(getRealColumn(column), order);
162: }
163:
164: private void recomputeColumnsMapping() {
165: virtualColumnsCount = 0;
166:
167: int virtualColumnIndex = 0;
168:
169: // set indexes virtual columns -> real columns
170: for (int i = 0; i < realColumnsCount; i++) {
171: if (columnsVisibility[i] == true) {
172: columnsMapping[virtualColumnIndex] = i;
173: virtualColumnsCount++;
174: virtualColumnIndex++;
175: }
176: }
177:
178: // clear mappings of unused real columns
179: for (int i = virtualColumnIndex; i < realColumnsCount; i++) {
180: columnsMapping[i] = -1;
181: }
182: }
183: }
|