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;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.event.MouseAdapter;
046: import java.awt.event.MouseEvent;
047: import javax.swing.JSplitPane;
048: import javax.swing.SwingUtilities;
049: import javax.swing.plaf.basic.BasicSplitPaneDivider;
050: import javax.swing.plaf.basic.BasicSplitPaneUI;
051:
052: /**
053: *
054: * @author Jiri Sedlacek
055: */
056: public class JCompoundSplitPane extends JExtendedSplitPane {
057: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
058:
059: private class DividerMouseListener extends MouseAdapter {
060: //~ Instance fields ------------------------------------------------------------------------------------------------------
061:
062: private double firstResizeWeight = 0;
063: private double secondResizeWeight = 1;
064:
065: //~ Methods --------------------------------------------------------------------------------------------------------------
066:
067: public void mouseEntered(MouseEvent e) {
068: configureComponents();
069: }
070:
071: public void mousePressed(MouseEvent e) {
072: SwingUtilities.invokeLater(new Runnable() {
073: public void run() {
074: JSplitPane firstSplit = (JSplitPane) getFirstComponent();
075: JSplitPane secondSplit = (JSplitPane) getSecondComponent();
076: firstResizeWeight = firstSplit.getResizeWeight();
077: secondResizeWeight = secondSplit.getResizeWeight();
078: firstSplit.setResizeWeight(0);
079: secondSplit.setResizeWeight(1);
080: }
081: });
082: }
083:
084: public void mouseReleased(MouseEvent e) {
085: SwingUtilities.invokeLater(new Runnable() {
086: public void run() {
087: ((JSplitPane) getFirstComponent())
088: .setResizeWeight(firstResizeWeight);
089: ((JSplitPane) getSecondComponent())
090: .setResizeWeight(secondResizeWeight);
091: }
092: });
093: }
094:
095: private void configureComponents() {
096: configureFirstComponent();
097: configureSecondComponent();
098: }
099:
100: private void configureFirstComponent() {
101: JSplitPane firstSplit = (JSplitPane) getFirstComponent();
102: int newWidth;
103: int newHeight;
104:
105: newWidth = firstSplit.getMinimumSize().width;
106: newHeight = 0;
107:
108: if (getFirstComponent(firstSplit).isVisible()
109: && getSecondComponent(firstSplit).isVisible()) {
110: newHeight = getFirstComponent(firstSplit).getSize().height
111: + getSecondComponent(firstSplit)
112: .getMinimumSize().height
113: + firstSplit.getDividerSize();
114: } else if (getFirstComponent(firstSplit).isVisible()) {
115: newHeight = getFirstComponent(firstSplit)
116: .getMinimumSize().height;
117: } else {
118: newHeight = getSecondComponent(firstSplit)
119: .getMinimumSize().height;
120: }
121:
122: firstSplit
123: .setMinimumSize(new Dimension(newWidth, newHeight));
124: }
125:
126: private void configureSecondComponent() {
127: JSplitPane secondSplit = (JSplitPane) getSecondComponent();
128: int newWidth = secondSplit.getMinimumSize().width;
129: int newHeight = 0;
130:
131: if (getFirstComponent(secondSplit).isVisible()
132: && getSecondComponent(secondSplit).isVisible()) {
133: newHeight = getSecondComponent(secondSplit).getSize().height
134: + (getFirstComponent(secondSplit).isVisible() ? (getFirstComponent(
135: secondSplit).getMinimumSize().height + secondSplit
136: .getDividerSize())
137: : 0);
138: } else if (getFirstComponent(secondSplit).isVisible()) {
139: newHeight = getFirstComponent(secondSplit)
140: .getMinimumSize().height;
141: } else {
142: newHeight = getSecondComponent(secondSplit)
143: .getMinimumSize().height;
144: }
145:
146: secondSplit.setMinimumSize(new Dimension(newWidth,
147: newHeight));
148: }
149: }
150:
151: //~ Constructors -------------------------------------------------------------------------------------------------------------
152:
153: public JCompoundSplitPane() {
154: super ();
155: tweakUI();
156: }
157:
158: public JCompoundSplitPane(int newOrientation) {
159: super (newOrientation);
160: tweakUI();
161: }
162:
163: public JCompoundSplitPane(int newOrientation,
164: boolean newContinuousLayout) {
165: super (newOrientation, newContinuousLayout);
166: tweakUI();
167: }
168:
169: public JCompoundSplitPane(int newOrientation,
170: boolean newContinuousLayout, Component newLeftComponent,
171: Component newRightComponent) {
172: super (newOrientation, newContinuousLayout, newLeftComponent,
173: newRightComponent);
174: tweakUI();
175: }
176:
177: public JCompoundSplitPane(int newOrientation,
178: Component newLeftComponent, Component newRightComponent) {
179: super (newOrientation, newLeftComponent, newRightComponent);
180: tweakUI();
181: }
182:
183: //~ Methods ------------------------------------------------------------------------------------------------------------------
184:
185: private Component getFirstComponent() {
186: return getFirstComponent(this );
187: }
188:
189: private Component getFirstComponent(JSplitPane splitPane) {
190: if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
191: return splitPane.getLeftComponent();
192: } else {
193: return splitPane.getTopComponent();
194: }
195: }
196:
197: private Component getSecondComponent() {
198: return getSecondComponent(this );
199: }
200:
201: private Component getSecondComponent(JSplitPane splitPane) {
202: if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
203: return splitPane.getRightComponent();
204: } else {
205: return splitPane.getBottomComponent();
206: }
207: }
208:
209: private void tweakUI() {
210: if (!(getUI() instanceof BasicSplitPaneUI)) {
211: return;
212: }
213:
214: BasicSplitPaneDivider divider = ((BasicSplitPaneUI) getUI())
215: .getDivider();
216:
217: if (divider != null) {
218: divider.addMouseListener(new DividerMouseListener());
219: }
220: }
221: }
|