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.treetable;
042:
043: /**
044: * TreeTable model that extends AbstractTableModel and allows to hide columns
045: *
046: * @author Jiri Sedlacek
047: */
048: public class ExtendedTreeTableModel extends AbstractTreeTableModel {
049: //~ Instance fields ----------------------------------------------------------------------------------------------------------
050:
051: private AbstractTreeTableModel realModel;
052: private int[] columnsMapping; // mapping virtual columns -> real columns
053: private boolean[] columnsVisibility; // visibility flags of real columns
054: private int realColumnsCount;
055: private int virtualColumnsCount;
056:
057: //~ Constructors -------------------------------------------------------------------------------------------------------------
058:
059: public ExtendedTreeTableModel(AbstractTreeTableModel realModel) {
060: super (realModel.root, realModel.supportsSorting,
061: realModel.initialSortingColumn,
062: realModel.initialSortingOrder);
063:
064: realColumnsCount = realModel.getColumnCount();
065: virtualColumnsCount = realColumnsCount;
066:
067: this .realModel = realModel;
068: columnsMapping = new int[realColumnsCount];
069:
070: boolean[] initialColumnsVisibility = new boolean[realColumnsCount];
071:
072: for (int i = 0; i < realColumnsCount; i++) {
073: initialColumnsVisibility[i] = true;
074: }
075:
076: setColumnsVisibility(initialColumnsVisibility);
077: }
078:
079: //~ Methods ------------------------------------------------------------------------------------------------------------------
080:
081: public boolean isCellEditable(Object node, int column) {
082: return realModel.isCellEditable(node, getRealColumn(column));
083: }
084:
085: public Class getColumnClass(int col) {
086: return realModel.getColumnClass(getRealColumn(col));
087: }
088:
089: public int getColumnCount() {
090: return virtualColumnsCount;
091: }
092:
093: //---------------------
094: // AbstractTreeTableModel interface
095: public String getColumnName(int col) {
096: return realModel.getColumnName(getRealColumn(col));
097: }
098:
099: public String getColumnToolTipText(int columnIndex) {
100: int realColumn = getRealColumn(columnIndex);
101:
102: if (realColumn == -1) {
103: return null;
104: }
105:
106: return realModel.getColumnToolTipText(realColumn);
107: }
108:
109: public void setColumnsVisibility(boolean[] columnsVisibility) {
110: this .columnsVisibility = columnsVisibility;
111: recomputeColumnsMapping();
112: }
113:
114: public boolean[] getColumnsVisibility() {
115: return columnsVisibility;
116: }
117:
118: public boolean getInitialSorting(int column) {
119: return realModel.getInitialSorting(getRealColumn(column));
120: }
121:
122: public int getInitialSortingColumn() {
123: return realModel.getInitialSortingColumn();
124: }
125:
126: public boolean getInitialSortingOrder() {
127: return realModel.getInitialSortingOrder();
128: }
129:
130: public boolean isLeaf(Object node) {
131: return realModel.isLeaf(node);
132: }
133:
134: public int getRealColumn(int column) {
135: if ((column > -1) && (column < columnsMapping.length)) {
136: return columnsMapping[column];
137: }
138:
139: return -1;
140: }
141:
142: public void setRealColumnVisibility(int column, boolean visible) {
143: if (visible) {
144: showRealColumn(column);
145: } else {
146: hideRealColumn(column);
147: }
148: }
149:
150: public boolean isRealColumnVisible(int column) {
151: if ((column > -1) && (column < columnsMapping.length)) {
152: return columnsVisibility[column];
153: }
154:
155: return false;
156: }
157:
158: public void setRoot(Object root) {
159: realModel.setRoot(root);
160: }
161:
162: public Object getRoot() {
163: return realModel.getRoot();
164: }
165:
166: public void setValueAt(Object aValue, Object node, int column) {
167: realModel.setValueAt(aValue, node, getRealColumn(column));
168: }
169:
170: /*public Object getValueAt (int rowIndex, int columnIndex) {
171: return realModel.getValueAt(rowIndex, getRealColumn(columnIndex));
172: }*/
173: public Object getValueAt(Object node, int column) {
174: return realModel.getValueAt(node, getRealColumn(column));
175: }
176:
177: public int getVirtualColumn(int column) {
178: for (int i = 0; i < virtualColumnsCount; i++) {
179: if (getRealColumn(i) == column) {
180: return i;
181: }
182: }
183:
184: return -1;
185: }
186:
187: public void hideRealColumn(int column) {
188: if (isRealColumnVisible(column)) {
189: columnsVisibility[column] = false;
190: recomputeColumnsMapping();
191: }
192: }
193:
194: public void showRealColumn(int column) {
195: if (!isRealColumnVisible(column)) {
196: columnsVisibility[column] = true;
197: recomputeColumnsMapping();
198: }
199: }
200:
201: public void sortByColumn(int column, boolean order) {
202: realModel.sortByColumn(getRealColumn(column), order);
203: }
204:
205: private void recomputeColumnsMapping() {
206: virtualColumnsCount = 0;
207:
208: int virtualColumnIndex = 0;
209:
210: // set indexes virtual columns -> real columns
211: for (int i = 0; i < realColumnsCount; i++) {
212: if (columnsVisibility[i] == true) {
213: columnsMapping[virtualColumnIndex] = i;
214: virtualColumnsCount++;
215: virtualColumnIndex++;
216: }
217: }
218:
219: // clear mappings of unused real columns
220: for (int i = virtualColumnIndex; i < realColumnsCount; i++) {
221: columnsMapping[i] = -1;
222: }
223: }
224: }
|