001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.gwt.widget;
022:
023: import com.google.gwt.user.client.DOM;
024: import com.google.gwt.user.client.ui.*;
025:
026: public class SplitPane extends AbsolutePanel implements MouseListener,
027: IContainer, HasOrientation {
028:
029: private class Divider extends MousePanel {
030:
031: public Divider() {
032: setStyleName("splitpane-divider");
033: }
034: }
035:
036: public static final int HORIZONTAL_SPLIT = 0, VERTICAL_SPLIT = 1;
037:
038: private OrientationConstant _orientation;
039:
040: private ComponentLayout _layout = new ComponentLayout();
041:
042: private CellPanel _dividerHalf;
043:
044: private Widget _firstComponent, _secondComponent;
045:
046: private int _dividerSize = 10, _dividerLoc = 100;
047:
048: private boolean capture = false;
049:
050: private int startX, startY;
051:
052: private Divider _divider = new Divider();
053:
054: private int _width;
055:
056: private int _height;
057:
058: public SplitPane() {
059: this (VERTICAL);
060: }
061:
062: public SplitPane(OrientationConstant orientation) {
063: _orientation = orientation;
064: _divider.addMouseListener(this );
065: initPanels();
066: }
067:
068: private void initPanels() {
069: clear();
070: if (_orientation == HORIZONTAL) {
071: if (_firstComponent != null) {
072: add(_firstComponent, 0, 0);
073: }
074: if (_secondComponent != null) {
075: _dividerHalf = new HorizontalPanel();
076: DOM.setStyleAttribute(_dividerHalf.getElement(),
077: "zOrder", "-1");
078: _dividerHalf.setHeight("100%");
079: _dividerHalf.add(_divider);
080: _dividerHalf.add(_secondComponent);
081: add(_dividerHalf, 0, 0);
082: }
083: } else {//assume vertical
084: if (_firstComponent != null) {
085: add(_firstComponent, 0, 0);
086: }
087: if (_secondComponent != null) {
088: _dividerHalf = new VerticalPanel();
089: DOM.setStyleAttribute(_dividerHalf.getElement(),
090: "zOrder", "-1");
091: _dividerHalf.setWidth("100%");
092: _dividerHalf.add(_divider);
093: _dividerHalf.add(_secondComponent);
094: add(_dividerHalf, 0, 0);
095: }
096: }
097: setDividerLoc(_dividerLoc);
098: }
099:
100: public void setFirstComponent(Widget w) {
101: _firstComponent = w;
102: initPanels();
103: }
104:
105: public void setSecondComponent(Widget w) {
106: _secondComponent = w;
107: initPanels();
108: }
109:
110: public void setDividerLoc(int loc) {
111: _dividerLoc = loc;
112: if (_orientation == HORIZONTAL) {
113: if (_secondComponent != null) {
114: _divider.setHeight("100%");
115: _divider.setWidth(String.valueOf(_dividerSize));
116: setWidgetPosition(_dividerHalf, loc, 0);
117: }
118: resizeComponent(_firstComponent, loc);
119: resizeComponent(_secondComponent, _width - _dividerSize
120: - loc);
121: } else {//assume vertical
122: if (_secondComponent != null) {
123: _divider.setHeight(String.valueOf(_dividerSize));
124: _divider.setWidth("100%");
125: setWidgetPosition(_dividerHalf, 0, loc);
126: }
127: resizeComponent(_firstComponent, loc);
128: resizeComponent(_secondComponent, _height - _dividerSize
129: - loc);
130: }
131: }
132:
133: public void setWidth(String width) {
134: _width = Integer.parseInt(width);
135: super .setWidth(width);
136: }
137:
138: public void setHeight(String height) {
139: _height = Integer.parseInt(height);
140: super .setHeight(height);
141: }
142:
143: private void resizeComponent(Widget com, int size) {
144: if (com != null && size > 0) {
145: if (_orientation == HORIZONTAL) {
146: com.setWidth(String.valueOf(size));
147: System.out.println("Set width to " + size);
148: } else {
149: com.setHeight(String.valueOf(size));
150: System.out.println("Set height to " + size);
151: }
152: }
153: }
154:
155: public void setDividerSize(int size) {
156: _dividerSize = size;
157: setDividerLoc(_dividerLoc);
158: }
159:
160: public void onMouseDown(Widget sender, int x, int y) {
161: capture = true;
162: DOM.setCapture(sender.getElement());
163: startX = x;
164: startY = y;
165: }
166:
167: public void onMouseEnter(Widget sender) {
168: // TODO Auto-generated method stub
169: }
170:
171: public void onMouseLeave(Widget sender) {
172: }
173:
174: public void onMouseMove(Widget sender, int x, int y) {
175: if (capture) {
176: if (_orientation == HORIZONTAL) {
177: int absX = getWidgetLeft(_dividerHalf);
178: int newX = absX + (x - startX);
179: setDividerLoc(newX);
180: } else {
181: int absY = getWidgetTop(_dividerHalf);
182: int newY = absY + (y - startY);
183: setDividerLoc(newY);
184: }
185: }
186: }
187:
188: public void onMouseUp(Widget sender, int x, int y) {
189: capture = false;
190: DOM.releaseCapture(sender.getElement());
191: }
192:
193: public void addChild(Widget widget, CellInfo info) {
194: throw new IllegalArgumentException("Shouldn't be called");
195: }
196:
197: public ComponentLayout getLayout() {
198: return _layout;
199: }
200:
201: public void removeChild(Widget child) {
202: throw new IllegalArgumentException("Shouldn't be called");
203: }
204:
205: public void setOrientation(OrientationConstant orientation) {
206: if (_orientation != orientation) {
207: _orientation = orientation;
208: initPanels();
209: }
210: }
211:
212: public OrientationConstant getOrientation() {
213: return _orientation;
214: }
215: }
|