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: import org.netbeans.lib.profiler.ui.UIConstants;
044: import org.netbeans.lib.profiler.ui.components.JTreeTable;
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 JTreeTable.
054: * JTreeTablePanel provides JScrollPane for displaying JTreeTable and JScrollBar for JTree
055: * column of JTreeTable if necessary.
056: *
057: * @author Jiri Sedlacek
058: */
059: public class JTreeTablePanel extends JPanel {
060: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
061:
062: //-----------------------------------------------------------------------
063: // Custom TreeTable Viewport
064: private class CustomTreeTableViewport extends JViewport {
065: //~ Instance fields ------------------------------------------------------------------------------------------------------
066:
067: private JTableHeader tableHeader;
068:
069: //~ Constructors ---------------------------------------------------------------------------------------------------------
070:
071: public CustomTreeTableViewport(JTreeTable treeTable) {
072: super ();
073: setView(treeTable);
074: setBackground(treeTable.getBackground());
075: this .tableHeader = treeTable.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: Rectangle cellRect;
099:
100: if (emptySpaceY > 0) {
101: g.setColor(UIConstants.TABLE_VERTICAL_GRID_COLOR);
102:
103: for (int i = 0; i < tableHeader.getColumnModel()
104: .getColumnCount(); i++) {
105: cellRect = tableHeader.getHeaderRect(i);
106: g.drawLine((cellRect.x + cellRect.width) - 1,
107: emptySpaceY,
108: (cellRect.x + cellRect.width) - 1,
109: getHeight() - 1);
110: }
111: }
112: }
113: }
114:
115: //~ Instance fields ----------------------------------------------------------------------------------------------------------
116:
117: protected CustomTreeTableViewport treeTableViewport;
118: protected JPanel scrollBarPanel;
119: protected JScrollBar scrollBar;
120: protected JScrollPane treeTableScrollPane;
121: protected JTreeTable treeTable;
122: private boolean columnMarginChanged = false;
123: private boolean internalScrollBarChange = false;
124:
125: //~ Constructors -------------------------------------------------------------------------------------------------------------
126:
127: /** Creates a new instance of JTreeTablePanel */
128: public JTreeTablePanel(JTreeTable treeTable) {
129: super (new BorderLayout());
130: this .treeTable = treeTable;
131:
132: initComponents();
133: hookHeaderColumnResize();
134: hookScrollBarValueChange();
135: hookTreeCollapsedExpanded();
136: }
137:
138: //~ Methods ------------------------------------------------------------------------------------------------------------------
139:
140: public void setCorner(String key, java.awt.Component corner) {
141: treeTableScrollPane.setCorner(key, corner);
142: }
143:
144: public JScrollPane getScrollPane() {
145: return treeTableScrollPane;
146: }
147:
148: public void paint(java.awt.Graphics g) {
149: super .paint(g);
150: updateScrollBarMaximum();
151: updateScrollBarWidth();
152: }
153:
154: private void hookHeaderColumnResize() {
155: treeTable.getTableHeader().getColumnModel()
156: .addColumnModelListener(new TableColumnModelListener() {
157: public void columnAdded(TableColumnModelEvent e) {
158: treeTableViewport.repaint();
159: }
160:
161: public void columnMoved(TableColumnModelEvent e) {
162: treeTableViewport.repaint();
163: }
164:
165: public void columnRemoved(TableColumnModelEvent e) {
166: treeTableViewport.repaint();
167: }
168:
169: public void columnMarginChanged(ChangeEvent e) {
170: internalScrollBarChange = true;
171: columnMarginChanged = true;
172: updateScrollBarMaximum();
173: treeTableViewport.repaint();
174: updateScrollBarWidth();
175: internalScrollBarChange = false;
176: }
177:
178: public void columnSelectionChanged(
179: ListSelectionEvent e) {
180: } // Ignored
181: });
182: }
183:
184: private void hookScrollBarValueChange() {
185: scrollBar.addAdjustmentListener(new AdjustmentListener() {
186: public void adjustmentValueChanged(AdjustmentEvent e) {
187: if (!internalScrollBarChange) {
188: treeTable.setTreeCellOffsetX(e.getValue());
189: }
190: }
191: });
192: }
193:
194: private void hookTreeCollapsedExpanded() {
195: treeTable.getTree().addTreeExpansionListener(
196: new TreeExpansionListener() {
197: public void treeCollapsed(TreeExpansionEvent event) {
198: internalScrollBarChange = true;
199: updateScrollBarMaximum();
200: internalScrollBarChange = false;
201: };
202:
203: public void treeExpanded(TreeExpansionEvent event) {
204: internalScrollBarChange = true;
205: updateScrollBarMaximum();
206: internalScrollBarChange = false;
207: }
208: });
209: }
210:
211: private void initComponents() {
212: setBorder(BorderFactory.createLoweredBevelBorder());
213:
214: treeTableScrollPane = new JScrollPane(
215: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
216: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
217: treeTableScrollPane.setBorder(BorderFactory.createEmptyBorder(
218: 0, 0, 0, 0));
219: treeTableViewport = new CustomTreeTableViewport(treeTable);
220: treeTableScrollPane.setViewport(treeTableViewport);
221: treeTableScrollPane.addMouseWheelListener(treeTable);
222: // Enable vertical scrollbar only if needed
223: treeTableScrollPane.getVerticalScrollBar().getModel()
224: .addChangeListener(
225: new javax.swing.event.ChangeListener() {
226: public void stateChanged(ChangeEvent e) {
227: if (treeTableScrollPane
228: .getVerticalScrollBar()
229: .getModel().getExtent() == treeTableScrollPane
230: .getVerticalScrollBar()
231: .getModel().getMaximum()) {
232: treeTableScrollPane
233: .getVerticalScrollBar()
234: .setEnabled(false);
235: } else {
236: treeTableScrollPane
237: .getVerticalScrollBar()
238: .setEnabled(true);
239: }
240: }
241: });
242:
243: scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
244: scrollBar.setUnitIncrement(10);
245: scrollBarPanel = new JPanel(new BorderLayout());
246: scrollBarPanel.add(scrollBar, BorderLayout.WEST);
247: treeTable.setTreeCellOffsetX(0);
248: scrollBarPanel.setVisible(false);
249:
250: add(treeTableScrollPane, BorderLayout.CENTER);
251: add(scrollBarPanel, BorderLayout.SOUTH);
252: }
253:
254: private void updateScrollBarMaximum() {
255: int treeWidth = treeTable.getTree().getPreferredSize().width + 3; // +3 means extra right margin
256: int columnWidth = treeTable.getColumnModel().getColumn(0)
257: .getWidth();
258:
259: if (columnMarginChanged
260: && (treeTable.getTreeCellOffsetX() > 0)
261: && ((treeWidth - treeTable.getTreeCellOffsetX()) < columnWidth)) {
262: columnMarginChanged = false;
263:
264: if ((treeWidth - columnWidth) >= 0) {
265: treeTable.setTreeCellOffsetX(treeWidth - columnWidth);
266: }
267: }
268:
269: int scrollBarMaximum = treeWidth - columnWidth;
270:
271: if (!isShowing()) {
272: return;
273: }
274:
275: if (scrollBarMaximum <= 0) {
276: treeTable.setTreeCellOffsetX(0);
277: scrollBarPanel.setVisible(false);
278: } else {
279: int value = treeTable.getTreeCellOffsetX();
280: int extent = treeWidth;
281: scrollBarPanel.setVisible(true);
282: scrollBar.setValues(value, extent, 0, scrollBarMaximum
283: + extent);
284: }
285: }
286:
287: private void updateScrollBarWidth() {
288: scrollBar.setPreferredSize(new Dimension(treeTable
289: .getTableHeader().getHeaderRect(0).width, scrollBar
290: .getPreferredSize().height));
291: scrollBar.setBlockIncrement((int) (scrollBar.getModel()
292: .getExtent() * 0.95f));
293: scrollBar.invalidate();
294: JTreeTablePanel.this.revalidate();
295: }
296: }
|