1 16 package com.google.gwt.user.client.ui; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.user.client.Command; 20 import com.google.gwt.user.client.DOM; 21 import com.google.gwt.user.client.DeferredCommand; 22 import com.google.gwt.user.client.Element; 23 24 43 public final class VerticalSplitPanel extends SplitPanel { 44 45 private static final int TOP = 0; 46 private static final int BOTTOM = 1; 47 private static final Impl impl = (Impl) GWT.create(Impl.class); 48 49 53 private static class Impl { 54 60 protected int getElementHeight(Element elem) { 61 return DOM.getElementPropertyInt(elem, "clientHeight"); 62 } 63 } 64 65 68 private static class ImplIE6 extends Impl { 69 protected native int getElementHeight(Element elem) ; 73 } 74 75 private static int getOffsetTop(Element elem) { 76 return DOM.getElementPropertyInt(elem, "offsetTop"); 77 } 78 79 private static Element lockStyles(final Element elem) { 80 DOM.setIntStyleAttribute(elem, "height", 0); 81 return preventElementBoxStyles(elem); 82 } 83 84 private static void setHeight(Element elem, int px) { 85 DOM.setStyleAttribute(elem, "height", Math.max(0, px) + "px"); 86 } 87 88 private final Element probeElem; 91 92 private int initialTopHeight = 0; 94 95 private int initialThumbPos = 0; 97 98 101 public VerticalSplitPanel() { 102 super(DOM.createDiv(), DOM.createDiv(), DOM.createDiv(), DOM.createDiv()); 103 104 final Element thisElem = getElement(); 105 final Element splitElem = getSplitElement(); 106 final Element topElem = getElement(TOP); 107 final Element bottomElem = getElement(BOTTOM); 108 probeElem = lockStyles(DOM.createDiv()); 109 110 DOM.appendChild(thisElem, topElem); 111 DOM.appendChild(thisElem, splitElem); 112 DOM.appendChild(thisElem, bottomElem); 113 DOM.appendChild(thisElem, probeElem); 114 115 addElementClipping(thisElem); 116 addElementScrolling(topElem); 117 addElementScrolling(bottomElem); 118 119 preventElementPadding(thisElem); 121 preventElementPadding(topElem); 122 preventElementPadding(bottomElem); 123 124 setElementClassname(topElem, "top"); 125 setElementClassname(splitElem, "splitter"); 126 setElementClassname(bottomElem, "bottom"); 127 128 setStyleName("gwt-VerticalSplitPanel"); 129 130 DeferredCommand.addCommand(new Command() { 132 public void execute() { 133 updateBottomHeight(); 134 } 135 }); 136 } 137 138 143 public final Widget getBottomWidget() { 144 return getWidget(BOTTOM); 145 } 146 147 152 public final Widget getTopWidget() { 153 return getWidget(TOP); 154 } 155 156 161 public final void setBottomWidget(Widget w) { 162 setWidget(BOTTOM, w); 163 } 164 165 public final void setSplitPosition(String size) { 166 DOM.setStyleAttribute(getElement(TOP), "height", size); 167 updateBottomHeight(); 168 } 169 170 175 public final void setTopWidget(Widget w) { 176 setWidget(TOP, w); 177 } 178 179 final void onSplitterResize(int x, int y) { 180 185 final Element topElem = getElement(TOP); 186 final Element botElem = getElement(BOTTOM); 187 188 final int newTopHeight = initialTopHeight + (y - initialThumbPos); 190 final int newBotHeight = impl.getElementHeight(botElem) 191 + impl.getElementHeight(topElem) - newTopHeight; 192 193 198 if (newBotHeight < 0) { 199 setHeight(botElem, 0); 200 setHeight(topElem, newTopHeight + newBotHeight); 201 } else { 202 setHeight(botElem, newBotHeight); 203 setHeight(topElem, newTopHeight); 204 } 205 206 updateBottomHeight(); 207 } 208 209 final void onSplitterResizeStarted(int x, int y) { 210 initialThumbPos = y; 211 initialTopHeight = impl.getElementHeight(getElement(TOP)); 212 } 213 214 218 private void updateBottomHeight() { 219 final Element thisElem = getElement(); 220 final Element bottomElem = getElement(BOTTOM); 221 222 227 final int adjust = impl.getElementHeight(thisElem) 228 - (getOffsetTop(probeElem) - getOffsetTop(thisElem)); 229 230 236 if (adjust == 0) { 237 return; 238 } 239 240 248 final int curHeight = impl.getElementHeight(bottomElem); 249 final int newHeight = curHeight + adjust; 250 setHeight(bottomElem, newHeight); 251 final int error = impl.getElementHeight(bottomElem) - newHeight; 252 253 if (error == 0) { 254 return; 255 } 256 257 setHeight(bottomElem, newHeight - error); 258 } 259 } 260 | Popular Tags |