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.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.awt.event.ComponentAdapter;
047: import java.awt.event.ComponentEvent;
048: import java.awt.event.ComponentListener;
049: import javax.swing.JSplitPane;
050: import javax.swing.plaf.basic.BasicSplitPaneUI;
051:
052: /**
053: *
054: * @author Jiri Sedlacek
055: */
056: public class JExtendedSplitPane extends JSplitPane {
057: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
058:
059: private class SplitPaneActionListener implements ActionListener {
060: //~ Methods --------------------------------------------------------------------------------------------------------------
061:
062: public void actionPerformed(ActionEvent e) {
063: switch (e.getID()) {
064: case JTitledPanel.STATE_CLOSED:
065:
066: //System.err.println(">>> STATE_CLOSED");
067: break;
068: case JTitledPanel.STATE_RESTORED:
069: setDividerLocation(getLastDividerLocation());
070:
071: break;
072: case JTitledPanel.STATE_MAXIMIZED:
073:
074: //System.err.println(">>> STATE_MAXIMIZED");
075: break;
076: case JTitledPanel.STATE_MINIMIZED:
077:
078: if (e.getSource() == getFirstComponent()) {
079: setDividerLocation(getFirstComponent()
080: .getPreferredSize().height);
081: } else {
082: setDividerLocation(getSize().height
083: - dividerSize
084: - getSecondComponent().getPreferredSize().height);
085: }
086:
087: break;
088: }
089: }
090: }
091:
092: private class SplitPaneComponentListener extends ComponentAdapter {
093: //~ Methods --------------------------------------------------------------------------------------------------------------
094:
095: public void componentHidden(ComponentEvent e) {
096: computeDividerLocationWhenHidden(e.getComponent());
097:
098: if ((dividerLocation == 0) || (dividerLocation == 1)) {
099: dividerLocation = 0.5;
100: }
101:
102: updateVisibility();
103: }
104:
105: public void componentShown(ComponentEvent e) {
106: updateVisibility();
107: }
108: }
109:
110: //~ Instance fields ----------------------------------------------------------------------------------------------------------
111:
112: private ActionListener splitPaneActionListener = new SplitPaneActionListener();
113: private ComponentListener splitPaneComponentListener = new SplitPaneComponentListener();
114: private double dividerLocation;
115: private int dividerSize;
116:
117: //~ Constructors -------------------------------------------------------------------------------------------------------------
118:
119: public JExtendedSplitPane() {
120: super ();
121: }
122:
123: public JExtendedSplitPane(int newOrientation) {
124: super (newOrientation);
125: }
126:
127: public JExtendedSplitPane(int newOrientation,
128: boolean newContinuousLayout) {
129: super (newOrientation, newContinuousLayout);
130: }
131:
132: public JExtendedSplitPane(int newOrientation,
133: boolean newContinuousLayout, Component newLeftComponent,
134: Component newRightComponent) {
135: super (newOrientation, newContinuousLayout, newLeftComponent,
136: newRightComponent);
137: registerListeners(newLeftComponent);
138: registerListeners(newRightComponent);
139: updateVisibility();
140:
141: if (!newLeftComponent.isVisible()) {
142: computeDividerLocationWhenInitiallyHidden(newLeftComponent);
143: }
144:
145: if (!newRightComponent.isVisible()) {
146: computeDividerLocationWhenInitiallyHidden(newRightComponent);
147: }
148: }
149:
150: public JExtendedSplitPane(int newOrientation,
151: Component newLeftComponent, Component newRightComponent) {
152: super (newOrientation, newLeftComponent, newRightComponent);
153: registerListeners(newLeftComponent);
154: registerListeners(newRightComponent);
155: updateVisibility();
156:
157: if (!newLeftComponent.isVisible()) {
158: computeDividerLocationWhenInitiallyHidden(newLeftComponent);
159: }
160:
161: if (!newRightComponent.isVisible()) {
162: computeDividerLocationWhenInitiallyHidden(newRightComponent);
163: }
164: }
165:
166: //~ Methods ------------------------------------------------------------------------------------------------------------------
167:
168: public void setBottomComponent(Component comp) {
169: setRightComponent(comp);
170: }
171:
172: public void setDividerSize(int newSize) {
173: super .setDividerSize(newSize);
174: dividerSize = newSize;
175: }
176:
177: public void setLeftComponent(Component comp) { // Actually setTopComponent is implemented as setLeftComponent
178:
179: if (getLeftComponent() != null) {
180: unregisterListeners(getLeftComponent());
181: }
182:
183: super .setLeftComponent(comp);
184:
185: if (getLeftComponent() != null) {
186: registerListeners(getLeftComponent());
187: }
188:
189: updateVisibility();
190: }
191:
192: public void setRightComponent(Component comp) { // Actually setBottomComponent is implemented as setRightComponent
193:
194: if (getRightComponent() != null) {
195: unregisterListeners(getRightComponent());
196: }
197:
198: super .setRightComponent(comp);
199:
200: if (getRightComponent() != null) {
201: registerListeners(getRightComponent());
202: }
203:
204: updateVisibility();
205: }
206:
207: public void setTopComponent(Component comp) {
208: setLeftComponent(comp);
209: }
210:
211: private Component getDivider() {
212: if (getUI() == null) {
213: return null;
214: }
215:
216: return ((BasicSplitPaneUI) getUI()).getDivider();
217: }
218:
219: private Component getFirstComponent() {
220: if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
221: return getLeftComponent();
222: } else {
223: return getTopComponent();
224: }
225: }
226:
227: private Component getSecondComponent() {
228: if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
229: return getRightComponent();
230: } else {
231: return getBottomComponent();
232: }
233: }
234:
235: private void computeDividerLocationWhenHidden(
236: Component hiddenComponent) {
237: if (getTopComponent().isVisible()
238: || getBottomComponent().isVisible()) {
239: if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
240: if (hiddenComponent == getFirstComponent()) {
241: dividerLocation = hiddenComponent.getSize().width
242: / (getSize().getWidth() - dividerSize);
243: } else {
244: dividerLocation = (getSize().getWidth()
245: - dividerSize - hiddenComponent.getSize().width)
246: / (getSize().getWidth() - dividerSize);
247: }
248: } else {
249: if (hiddenComponent == getFirstComponent()) {
250: dividerLocation = hiddenComponent.getSize().height
251: / (getSize().getHeight() - dividerSize);
252: } else {
253: dividerLocation = (getSize().getHeight()
254: - dividerSize - hiddenComponent.getSize().height)
255: / (getSize().getHeight() - dividerSize);
256: }
257: }
258: }
259: }
260:
261: private void computeDividerLocationWhenInitiallyHidden(
262: Component hiddenComponent) {
263: if (getTopComponent().isVisible()
264: || getBottomComponent().isVisible()) {
265: if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
266: if (hiddenComponent == getFirstComponent()) {
267: dividerLocation = hiddenComponent
268: .getPreferredSize().width
269: / (getPreferredSize().getWidth() - dividerSize);
270: } else {
271: dividerLocation = (getPreferredSize().getWidth()
272: - dividerSize - hiddenComponent
273: .getPreferredSize().width)
274: / (getPreferredSize().getWidth() - dividerSize);
275: }
276: } else {
277: if (hiddenComponent == getFirstComponent()) {
278: dividerLocation = hiddenComponent
279: .getPreferredSize().height
280: / (getPreferredSize().getHeight() - dividerSize);
281: } else {
282: dividerLocation = (getPreferredSize().getHeight()
283: - dividerSize - hiddenComponent
284: .getPreferredSize().height)
285: / (getPreferredSize().getHeight() - dividerSize);
286: }
287: }
288: }
289: }
290:
291: private void registerListeners(Component component) {
292: if (splitPaneComponentListener != null) {
293: component.addComponentListener(splitPaneComponentListener);
294: }
295:
296: if (splitPaneActionListener != null) {
297: if (component instanceof JTitledPanel) {
298: ((JTitledPanel) component)
299: .addActionListener(splitPaneActionListener);
300: }
301:
302: //else if (component instanceof JExtendedSplitPane) ((JTitledPanel)component).addActionListener(splitPaneActionListener);
303: }
304: }
305:
306: private void unregisterListeners(Component component) {
307: if (splitPaneComponentListener != null) {
308: component
309: .removeComponentListener(splitPaneComponentListener);
310: }
311:
312: if (splitPaneActionListener != null) {
313: if (component instanceof JTitledPanel) {
314: ((JTitledPanel) component)
315: .removeActionListener(splitPaneActionListener);
316: }
317:
318: //else if (component instanceof JExtendedSplitPane) ((JTitledPanel)component).removeActionListener(splitPaneActionListener);
319: }
320: }
321:
322: private void updateVisibility() {
323: Component firstComponent = getFirstComponent();
324: Component secondComponent = getSecondComponent();
325: Component divider = getDivider();
326:
327: if ((firstComponent == null) || (secondComponent == null)
328: || (divider == null)) {
329: return;
330: }
331:
332: if (firstComponent.isVisible() && secondComponent.isVisible()) {
333: if (!divider.isVisible()) {
334: super .setDividerSize(dividerSize);
335: divider.setVisible(true);
336: setDividerLocation(dividerLocation);
337: }
338:
339: if (!isVisible()) {
340: setVisible(true);
341: }
342: } else if (!firstComponent.isVisible()
343: && !secondComponent.isVisible()) {
344: if (isVisible()) {
345: setVisible(false);
346: }
347: } else {
348: if (divider.isVisible()) {
349: super .setDividerSize(0);
350: divider.setVisible(false);
351: setDividerLocation(0);
352: }
353:
354: if (!isVisible()) {
355: setVisible(true);
356: }
357: }
358: }
359: }
|