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.UIConstants;
044: import org.netbeans.lib.profiler.ui.components.JExtendedTable;
045: import java.awt.*;
046: import java.awt.event.AdjustmentEvent;
047: import java.awt.event.AdjustmentListener;
048: import javax.swing.*;
049: import javax.swing.event.*;
050: import javax.swing.table.JTableHeader;
051:
052: /**
053: * A subclass of JPanel that provides additional fuctionality for displaying JExtendedTable.
054: * JExtendedTablePanel provides JScrollPane for displaying JExtendedTable and customized Viewport.
055: *
056: * @author Tomas Hurka
057: * @author Jiri Sedlacek
058: */
059: public class JExtendedTablePanel extends JPanel {
060: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
061:
062: //-----------------------------------------------------------------------
063: // Custom ExtendedTable Viewport
064: private class CustomExtendedTableViewport extends JViewport {
065: //~ Instance fields ------------------------------------------------------------------------------------------------------
066:
067: private JTableHeader tableHeader;
068:
069: //~ Constructors ---------------------------------------------------------------------------------------------------------
070:
071: public CustomExtendedTableViewport(JExtendedTable extendedTable) {
072: super ();
073: setView(extendedTable);
074: updateBackgroundColor();
075: this .tableHeader = extendedTable.getTableHeader();
076: }
077:
078: //~ Methods --------------------------------------------------------------------------------------------------------------
079:
080: public void paint(Graphics g) {
081: super .paint(g);
082:
083: if (UIConstants.SHOW_TABLE_VERTICAL_GRID) {
084: paintVerticalLines(g);
085: }
086: }
087:
088: private int getEmptySpaceY() {
089: if (getView() == null) {
090: return 0;
091: }
092:
093: return getView().getHeight();
094: }
095:
096: private void paintVerticalLines(Graphics g) {
097: int emptySpaceY = getEmptySpaceY();
098:
099: if (emptySpaceY > 0) {
100: g.setColor(UIConstants.TABLE_VERTICAL_GRID_COLOR);
101:
102: int cellX = 0;
103: int cellWidth;
104:
105: for (int i = 0; i < extendedTable.getColumnModel()
106: .getColumnCount(); i++) {
107: cellWidth = extendedTable.getColumnModel()
108: .getColumn(i).getWidth();
109: g.drawLine((cellX + cellWidth) - 1, emptySpaceY,
110: (cellX + cellWidth) - 1, getHeight() - 1);
111: cellX += cellWidth;
112: }
113: }
114: }
115:
116: private void updateBackgroundColor() {
117: setBackground(extendedTable.isEnabled() ? extendedTable
118: .getBackground() : UIManager
119: .getColor("TextField.inactiveBackground")); // NOI18N
120: }
121: }
122:
123: //~ Instance fields ----------------------------------------------------------------------------------------------------------
124:
125: protected CustomExtendedTableViewport extendedTableViewport;
126: protected JExtendedTable extendedTable;
127: protected JScrollPane extendedTableScrollPane;
128:
129: //~ Constructors -------------------------------------------------------------------------------------------------------------
130:
131: /** Creates a new instance of JExtendedTablePanel */
132: public JExtendedTablePanel(JExtendedTable extendedTable) {
133: super (new BorderLayout());
134: this .extendedTable = extendedTable;
135:
136: initComponents();
137: hookHeaderColumnResize();
138: }
139:
140: //~ Methods ------------------------------------------------------------------------------------------------------------------
141:
142: public void setCorner(String key, java.awt.Component corner) {
143: extendedTableScrollPane.setCorner(key, corner);
144: }
145:
146: public void setEnabled(boolean enabled) {
147: super .setEnabled(enabled);
148: updateVerticalScrollbar();
149: extendedTable.setEnabled(enabled);
150: extendedTableViewport.updateBackgroundColor();
151: }
152:
153: public JScrollPane getScrollPane() {
154: return extendedTableScrollPane;
155: }
156:
157: private void hookHeaderColumnResize() {
158: if (extendedTable.getTableHeader() != null) {
159: extendedTable.getTableHeader().getColumnModel()
160: .addColumnModelListener(
161: new TableColumnModelListener() {
162: public void columnAdded(
163: TableColumnModelEvent e) {
164: extendedTableViewport.repaint();
165: }
166:
167: public void columnMoved(
168: TableColumnModelEvent e) {
169: extendedTableViewport.repaint();
170: }
171:
172: public void columnRemoved(
173: TableColumnModelEvent e) {
174: extendedTableViewport.repaint();
175: }
176:
177: public void columnMarginChanged(
178: ChangeEvent e) {
179: extendedTableViewport.repaint();
180: }
181:
182: public void columnSelectionChanged(
183: ListSelectionEvent e) {
184: } // Ignored
185: });
186:
187: }
188: }
189:
190: private void initComponents() {
191: setBorder(BorderFactory.createLoweredBevelBorder());
192:
193: extendedTableScrollPane = new JScrollPane(
194: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
195: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
196: extendedTableScrollPane.setBorder(BorderFactory
197: .createEmptyBorder(0, 0, 0, 0));
198: extendedTableViewport = new CustomExtendedTableViewport(
199: extendedTable);
200: extendedTableScrollPane.setViewport(extendedTableViewport);
201: extendedTableScrollPane.addMouseWheelListener(extendedTable);
202: // Enable vertical scrollbar only if needed
203: extendedTableScrollPane.getVerticalScrollBar().getModel()
204: .addChangeListener(
205: new javax.swing.event.ChangeListener() {
206: public void stateChanged(ChangeEvent e) {
207: updateVerticalScrollbar();
208: }
209: });
210:
211: add(extendedTableScrollPane, BorderLayout.CENTER);
212: }
213:
214: private void updateVerticalScrollbar() {
215: if (extendedTableScrollPane.getVerticalScrollBar().getModel()
216: .getExtent() == extendedTableScrollPane
217: .getVerticalScrollBar().getModel().getMaximum()) {
218: extendedTableScrollPane.getVerticalScrollBar().setEnabled(
219: false);
220: } else {
221: extendedTableScrollPane.getVerticalScrollBar().setEnabled(
222: JExtendedTablePanel.this.isEnabled());
223: }
224: }
225: }
|