001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.awt.Component;
024: import java.awt.Graphics;
025:
026: import javax.accessibility.Accessible;
027: import javax.accessibility.AccessibleContext;
028: import javax.accessibility.AccessibleRole;
029: import javax.accessibility.AccessibleStateSet;
030: import javax.accessibility.AccessibleValue;
031: import javax.swing.plaf.SplitPaneUI;
032:
033: import org.apache.harmony.x.swing.internal.nls.Messages;
034:
035: public class JSplitPane extends JComponent implements Accessible {
036: public static final String BOTTOM = "bottom";
037: public static final String TOP = "top";
038: public static final String DIVIDER = "divider";
039: public static final String LEFT = "left";
040: public static final String RIGHT = "right";
041:
042: public static final String CONTINUOUS_LAYOUT_PROPERTY = "continuousLayout";
043: public static final String DIVIDER_LOCATION_PROPERTY = "dividerLocation";
044: public static final String DIVIDER_SIZE_PROPERTY = "dividerSize";
045: public static final String LAST_DIVIDER_LOCATION_PROPERTY = "lastDividerLocation";
046: public static final String ONE_TOUCH_EXPANDABLE_PROPERTY = "oneTouchExpandable";
047: public static final String ORIENTATION_PROPERTY = "orientation";
048: public static final String RESIZE_WEIGHT_PROPERTY = "resizeWeight";
049:
050: public static final int VERTICAL_SPLIT = 0;
051: public static final int HORIZONTAL_SPLIT = 1;
052:
053: protected class AccessibleJSplitPane extends AccessibleJComponent
054: implements AccessibleValue {
055: public AccessibleStateSet getAccessibleStateSet() {
056: throw new UnsupportedOperationException(Messages
057: .getString("swing.27")); //$NON-NLS-1$
058: }
059:
060: public AccessibleValue getAccessibleValue() {
061: throw new UnsupportedOperationException(Messages
062: .getString("swing.27")); //$NON-NLS-1$
063: }
064:
065: public Number getCurrentAccessibleValue() {
066: throw new UnsupportedOperationException(Messages
067: .getString("swing.27")); //$NON-NLS-1$
068: }
069:
070: public boolean setCurrentAccessibleValue(final Number n) {
071: throw new UnsupportedOperationException(Messages
072: .getString("swing.27")); //$NON-NLS-1$
073: }
074:
075: public Number getMinimumAccessibleValue() {
076: throw new UnsupportedOperationException(Messages
077: .getString("swing.27")); //$NON-NLS-1$
078: }
079:
080: public Number getMaximumAccessibleValue() {
081: throw new UnsupportedOperationException(Messages
082: .getString("swing.27")); //$NON-NLS-1$
083: }
084:
085: public AccessibleRole getAccessibleRole() {
086: return AccessibleRole.SPLIT_PANE;
087: }
088: }
089:
090: protected boolean continuousLayout;
091: protected int dividerSize;
092: protected int lastDividerLocation;
093: protected boolean oneTouchExpandable;
094: protected int orientation;
095:
096: protected Component leftComponent;
097: protected Component rightComponent;
098:
099: private static final String UI_CLASS_ID = "SplitPaneUI";
100: private double resizeWeight;
101: private Component divider;
102: private int dividerLocation = -1;
103:
104: public JSplitPane() {
105: this (HORIZONTAL_SPLIT, false, new JButton("left button"),
106: new JButton("right button"));
107: }
108:
109: public JSplitPane(final int orientation) {
110: this (orientation, false, null, null);
111: }
112:
113: public JSplitPane(final int orientation,
114: final boolean continuousLayout) {
115: this (orientation, continuousLayout, null, null);
116: }
117:
118: public JSplitPane(final int orientation,
119: final Component leftComponent,
120: final Component rightComponent) {
121:
122: this (orientation, false, leftComponent, rightComponent);
123: }
124:
125: public JSplitPane(final int orientation,
126: final boolean continuesLayout,
127: final Component leftComponent,
128: final Component rightComponent) {
129:
130: checkOrientation(orientation);
131: this .orientation = orientation;
132: this .continuousLayout = continuesLayout;
133:
134: setLayout(null);
135:
136: setLeftComponent(leftComponent);
137: setRightComponent(rightComponent);
138:
139: updateUI();
140: }
141:
142: public void setUI(final SplitPaneUI ui) {
143: super .setUI(ui);
144: }
145:
146: public SplitPaneUI getUI() {
147: return (SplitPaneUI) ui;
148: }
149:
150: public void updateUI() {
151: setUI((SplitPaneUI) UIManager.getUI(this ));
152: }
153:
154: public String getUIClassID() {
155: return UI_CLASS_ID;
156: }
157:
158: public void setDividerSize(final int size) {
159: if (dividerSize != size) {
160: int oldValue = dividerSize;
161: dividerSize = size;
162: firePropertyChange(DIVIDER_SIZE_PROPERTY, oldValue, size);
163: }
164: }
165:
166: public int getDividerSize() {
167: return dividerSize;
168: }
169:
170: public void setLeftComponent(final Component c) {
171: add(c, LEFT);
172: }
173:
174: public Component getLeftComponent() {
175: return leftComponent;
176: }
177:
178: public void setTopComponent(final Component c) {
179: add(c, TOP);
180: }
181:
182: public Component getTopComponent() {
183: return leftComponent;
184: }
185:
186: public void setRightComponent(final Component c) {
187: add(c, RIGHT);
188: }
189:
190: public Component getRightComponent() {
191: return rightComponent;
192: }
193:
194: public void setBottomComponent(final Component c) {
195: add(c, BOTTOM);
196: }
197:
198: public Component getBottomComponent() {
199: return rightComponent;
200: }
201:
202: public void setOneTouchExpandable(final boolean expandable) {
203: if (oneTouchExpandable != expandable) {
204: oneTouchExpandable = expandable;
205: firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY,
206: !expandable, expandable);
207: }
208: }
209:
210: public boolean isOneTouchExpandable() {
211: return oneTouchExpandable;
212: }
213:
214: public void setLastDividerLocation(final int lastLocation) {
215: if (lastDividerLocation != lastLocation) {
216: int oldValue = lastDividerLocation;
217: lastDividerLocation = lastLocation;
218: firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY,
219: oldValue, lastLocation);
220: }
221: }
222:
223: public int getLastDividerLocation() {
224: return lastDividerLocation;
225: }
226:
227: public void setOrientation(final int orientation) {
228: checkOrientation(orientation);
229: if (this .orientation != orientation) {
230: int oldValue = this .orientation;
231: this .orientation = orientation;
232: firePropertyChange(ORIENTATION_PROPERTY, oldValue,
233: orientation);
234: }
235: }
236:
237: public int getOrientation() {
238: return orientation;
239: }
240:
241: public void setContinuousLayout(final boolean continuousLayout) {
242: if (this .continuousLayout != continuousLayout) {
243: this .continuousLayout = continuousLayout;
244: firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY,
245: !continuousLayout, continuousLayout);
246: }
247: }
248:
249: public boolean isContinuousLayout() {
250: return continuousLayout;
251: }
252:
253: public void setResizeWeight(final double weight) {
254: if (weight < 0 || weight > 1) {
255: throw new IllegalArgumentException(Messages
256: .getString("swing.32")); //$NON-NLS-1$
257: }
258: if (resizeWeight != weight) {
259: double oldValue = resizeWeight;
260: resizeWeight = weight;
261: firePropertyChange(RESIZE_WEIGHT_PROPERTY, oldValue, weight);
262: }
263: }
264:
265: public double getResizeWeight() {
266: return resizeWeight;
267: }
268:
269: public void resetToPreferredSizes() {
270: getUI().resetToPreferredSizes(this );
271: }
272:
273: public void setDividerLocation(final double proportionalLocation) {
274: if (proportionalLocation < 0 || proportionalLocation > 1) {
275: throw new IllegalArgumentException(Messages
276: .getString("swing.33")); //$NON-NLS-1$
277: }
278:
279: int size;
280: if (orientation == HORIZONTAL_SPLIT) {
281: size = getWidth();
282: } else {
283: size = getHeight();
284: }
285: int location = (int) ((size - dividerSize) * proportionalLocation);
286: setDividerLocation(location);
287: }
288:
289: public void setDividerLocation(final int location) {
290: int oldValue = dividerLocation;
291: if (oldValue != location) {
292: dividerLocation = location;
293: getUI().setDividerLocation(this , location);
294: firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldValue,
295: location);
296: }
297: setLastDividerLocation(oldValue);
298: }
299:
300: public int getDividerLocation() {
301: return dividerLocation;
302: }
303:
304: public int getMinimumDividerLocation() {
305: return getUI().getMinimumDividerLocation(this );
306: }
307:
308: public int getMaximumDividerLocation() {
309: return getUI().getMaximumDividerLocation(this );
310: }
311:
312: public void remove(final Component component) {
313: clearComponentField(component);
314: super .remove(component);
315: }
316:
317: public void remove(final int index) {
318: Component component = getComponent(index);
319: clearComponentField(component);
320: super .remove(index);
321: }
322:
323: public void removeAll() {
324: leftComponent = null;
325: rightComponent = null;
326: divider = null;
327: super .removeAll();
328: }
329:
330: public boolean isValidateRoot() {
331: return true;
332: }
333:
334: public AccessibleContext getAccessibleContext() {
335: if (accessibleContext == null) {
336: accessibleContext = new AccessibleJSplitPane();
337: }
338:
339: return accessibleContext;
340: }
341:
342: protected void addImpl(final Component c, final Object constraints,
343: final int index) {
344: Object updatedConstraints = constraints;
345: Component oldComponent = null;
346: if (LEFT.equals(constraints) || TOP.equals(constraints)) {
347: oldComponent = leftComponent;
348: leftComponent = c;
349: } else if (RIGHT.equals(constraints)
350: || BOTTOM.equals(constraints)) {
351: oldComponent = rightComponent;
352: rightComponent = c;
353: } else if (DIVIDER.equals(constraints)) {
354: oldComponent = divider;
355: divider = c;
356: } else {
357: if (leftComponent == null) {
358: leftComponent = c;
359: updatedConstraints = LEFT;
360: } else if (rightComponent == null) {
361: rightComponent = c;
362: updatedConstraints = RIGHT;
363: } else {
364: updatedConstraints = null;
365: }
366: }
367:
368: if (oldComponent != null) {
369: super .remove(oldComponent);
370: }
371: if (c != null) {
372: super .addImpl(c, updatedConstraints, -1);
373: }
374: }
375:
376: protected void paintChildren(final Graphics g) {
377: super .paintChildren(g);
378: getUI().finishedPaintingChildren(this , g);
379: }
380:
381: private void clearComponentField(final Component component) {
382: if (leftComponent == component) {
383: leftComponent = null;
384: } else if (rightComponent == component) {
385: rightComponent = null;
386: } else if (divider == component) {
387: divider = null;
388: }
389: }
390:
391: private void checkOrientation(final int orientation) {
392: if (orientation != HORIZONTAL_SPLIT
393: && orientation != VERTICAL_SPLIT) {
394: throw new IllegalArgumentException(Messages
395: .getString("swing.1B")); //$NON-NLS-1$
396: }
397: }
398:
399: }
|