001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.window.swing;
020:
021: import java.awt.Component;
022: import java.awt.event.ComponentAdapter;
023: import java.awt.event.ComponentEvent;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026:
027: import javax.swing.JSplitPane;
028:
029: /**
030:
031: * @author Matthew Large
032: *
033: */
034: public class ExtendedSplitPane extends JSplitPane {
035:
036: private double m_pos = 0.5;
037:
038: private boolean reentrant = false;
039:
040: /**
041: *
042: */
043: public ExtendedSplitPane() {
044: super ();
045: setup();
046: }
047:
048: /**
049: * @param newOrientation
050: */
051: public ExtendedSplitPane(int newOrientation) {
052: super (newOrientation);
053: setup();
054: }
055:
056: /**
057: * @param newOrientation
058: * @param newContinuousLayout
059: */
060: public ExtendedSplitPane(int newOrientation,
061: boolean newContinuousLayout) {
062: super (newOrientation, newContinuousLayout);
063: setup();
064: }
065:
066: /**
067: * @param newOrientation
068: * @param newLeftComponent
069: * @param newRightComponent
070: */
071: public ExtendedSplitPane(int newOrientation,
072: Component newLeftComponent, Component newRightComponent) {
073: super (newOrientation, newLeftComponent, newRightComponent);
074: setup();
075: }
076:
077: /**
078: * @param newOrientation
079: * @param newContinuousLayout
080: * @param newLeftComponent
081: * @param newRightComponent
082: */
083: public ExtendedSplitPane(int newOrientation,
084: boolean newContinuousLayout, Component newLeftComponent,
085: Component newRightComponent) {
086: super (newOrientation, newContinuousLayout, newLeftComponent,
087: newRightComponent);
088: setup();
089: }
090:
091: private void setup() {
092: this .setOneTouchExpandable(true);
093: this .setContinuousLayout(true);
094: DividerListener l = new DividerListener();
095: this .addPropertyChangeListener(l);
096: this .addComponentListener(l);
097: }
098:
099: public void animateTo(double newLoc) {
100: if (newLoc != this .m_pos) {
101: new MoveThread(this .m_pos, newLoc).run();
102: }
103: }
104:
105: public double getProportionalDividerLocation() {
106: return this .m_pos;
107: }
108:
109: public void setDividerLocation(double loc) {
110: this .m_pos = loc;
111: super .setDividerLocation(loc);
112: }
113:
114: public void setExtDividerLocation(int loc) {
115: super .setDividerLocation(loc);
116: }
117:
118: public class DividerListener extends ComponentAdapter implements
119: PropertyChangeListener {
120: double percentage = 1.0;
121: boolean ignore = false;
122:
123: public void propertyChange(PropertyChangeEvent event) {
124: String prop = event.getPropertyName();
125: JSplitPane split = (JSplitPane) event.getSource();
126: if (prop.equals("lastDividerLocation")) {
127: if (ignore) {
128: ignore = false;
129: } else {
130: if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
131: int location = getDividerLocation();
132: double newPercentage = ((double) location)
133: / split.getWidth();
134: if (percentage - newPercentage > 0.05
135: || newPercentage - percentage > 0.05) {
136: percentage = newPercentage;
137: }
138: } else {
139: int location = getDividerLocation();
140: double newPercentage = ((double) location)
141: / split.getHeight();
142: if (percentage - newPercentage > 0.05
143: || newPercentage - percentage > 0.05) {
144: percentage = newPercentage;
145: }
146: }
147: Component[] comps = getComponents();
148: for (int i = 0; i < comps.length; i++) {
149: comps[i].validate();
150: }
151: }
152: }
153: m_pos = percentage;
154: }
155:
156: public void componentResized(ComponentEvent event) {
157: if (percentage > 0 && percentage < 1) {
158: ignore = true;
159: setDividerLocation(percentage);
160: repaint();
161: }
162: }
163: }
164:
165: class MoveThread extends Thread {
166:
167: private double m_currentLoc;
168: private double m_newLoc;
169: private int m_nCount = 5;
170: private double m_diff;
171:
172: MoveThread(double currentLoc, double newLoc) {
173: this .m_currentLoc = currentLoc;
174: this .m_newLoc = newLoc;
175: if (m_currentLoc > m_newLoc) {
176: m_diff = (m_currentLoc - m_newLoc) / m_nCount;
177: } else if (newLoc > m_currentLoc) {
178: m_diff = (m_newLoc - m_currentLoc) / m_nCount;
179: } else {
180: m_diff = 0;
181: }
182: }
183:
184: public void run() {
185: for (int n = 0; n < m_nCount; n++) {
186: if (m_currentLoc > m_newLoc) {
187: m_currentLoc = m_currentLoc + m_diff;
188: } else {
189: m_currentLoc = m_currentLoc - m_diff;
190: }
191: setDividerLocation(m_currentLoc);
192: try {
193: paintAll(getGraphics());
194: Thread.sleep(20L);
195: } catch (Exception exception) {
196: }
197: }
198: }
199: }
200:
201: /* (non-Javadoc)
202: * @see javax.swing.JSplitPane#setBottomComponent(java.awt.Component)
203: */
204: public void setBottomComponent(Component arg0) {
205: super .setBottomComponent(arg0);
206: super .setDividerLocation(this .m_pos);
207: }
208:
209: /* (non-Javadoc)
210: * @see javax.swing.JSplitPane#setLeftComponent(java.awt.Component)
211: */
212: public void setLeftComponent(Component arg0) {
213: super .setLeftComponent(arg0);
214: if (this .m_pos >= 0.0 && this .m_pos <= 1.0) {
215: super .setDividerLocation(this .m_pos);
216: }
217: }
218:
219: /* (non-Javadoc)
220: * @see javax.swing.JSplitPane#setRightComponent(java.awt.Component)
221: */
222: public void setRightComponent(Component arg0) {
223: super .setRightComponent(arg0);
224: super .setDividerLocation(this .m_pos);
225: }
226:
227: /* (non-Javadoc)
228: * @see javax.swing.JSplitPane#setTopComponent(java.awt.Component)
229: */
230: public void setTopComponent(Component arg0) {
231: super.setTopComponent(arg0);
232: super.setDividerLocation(this.m_pos);
233: }
234:
235: }
|