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 42 public final class HorizontalSplitPanel extends SplitPanel { 43 44 48 private static class Impl { 49 protected int initialThumbPos = 0; 51 52 protected int initialLeftWidth = 0; 54 protected int initialLeftContentWidth = 0; 55 protected int initialRightContentWidth = 0; 56 57 62 protected void init(HorizontalSplitPanel panel) { 63 setWidth(panel.rightTD, "100%"); 65 setWidth(panel.rightDiv, "100%"); 66 } 67 68 74 protected void onSplitResize(final HorizontalSplitPanel panel, int pos) { 75 int offset = pos - initialThumbPos; 77 78 82 int newLeftContentWidth = initialLeftContentWidth + offset; 83 int newRightContentWidth = initialRightContentWidth - offset; 84 85 if (newLeftContentWidth < 0) { 86 offset -= newLeftContentWidth; 87 } 88 89 if (newRightContentWidth < 0) { 90 offset += newRightContentWidth; 91 } 92 93 setSplitPosition(panel, (initialLeftWidth + offset) + "px"); 95 } 96 97 103 protected void onSplitResizeStarted(final HorizontalSplitPanel panel, 104 int pos) { 105 initialThumbPos = pos; 106 initialLeftWidth = getOffsetWidth(panel.leftDiv); 107 initialLeftContentWidth = getClientWidth(panel.getElement(LEFT)); 108 initialRightContentWidth = getClientWidth(panel.getElement(RIGHT)); 109 } 110 111 117 protected void setSplitPosition(final HorizontalSplitPanel panel, 118 final String pos) { 119 panel.lastSplitPosition = pos; 121 122 126 setWidth(panel.leftDiv, pos); 127 } 128 } 129 130 133 private static class ImplIE6 extends Impl { 134 private static native void addWidthExpression(Element elem) ; 137 138 protected void init(final HorizontalSplitPanel panel) { 139 143 DOM.setStyleAttribute(panel.table, "tableLayout", "fixed"); 144 145 addWidthExpression(panel.leftDiv); 146 addWidthExpression(panel.rightDiv); 147 addWidthExpression(panel.getElement(LEFT)); 148 addWidthExpression(panel.getElement(RIGHT)); 149 } 150 151 protected void onSplitResizeStarted(final HorizontalSplitPanel panel, 152 final int x) { 153 initialThumbPos = x; 154 initialLeftWidth = getOffsetWidth(panel.leftTD); 155 initialLeftContentWidth = getClientWidth(panel.getElement(LEFT)); 156 initialRightContentWidth = getClientWidth(panel.getElement(RIGHT)); 157 } 158 159 protected void setSplitPosition(final HorizontalSplitPanel panel, 160 final String pos) { 161 panel.lastSplitPosition = pos; 163 final Element leftTD = panel.leftTD; 164 setWidth(leftTD, pos); 166 } 167 } 168 169 172 private static class ImplSafari extends Impl { 173 174 protected void init(final HorizontalSplitPanel panel) { 175 179 DOM.setStyleAttribute(panel.table, "tableLayout", "fixed"); 180 181 final String autoProp = "auto"; 182 setWidth(panel.leftDiv, autoProp); 183 setWidth(panel.rightDiv, autoProp); 184 setWidth(panel.getElement(LEFT), autoProp); 185 setWidth(panel.getElement(RIGHT), autoProp); 186 187 191 panel.setWidth("100%"); 192 } 193 194 protected void setSplitPosition(final HorizontalSplitPanel panel, String pos) { 195 panel.lastSplitPosition = pos; 197 setWidth(panel.leftTD, pos); 199 } 200 } 201 202 206 private static final int LEFT = 0; 207 private static final int RIGHT = 1; 208 209 private static final int DEFAULT_SPLITTER_WIDTH = 10; 210 211 private static final int getClientWidth(final Element elem) { 212 return DOM.getElementPropertyInt(elem, "clientWidth"); 213 } 214 215 private static final int getOffsetWidth(final Element elem) { 216 return DOM.getElementPropertyInt(elem, "offsetWidth"); 217 } 218 219 private static final native int parseInt(String number) ; 222 223 private static final void setWidth(Element elem, String size) { 224 DOM.setStyleAttribute(elem, "width", size); 225 } 226 227 242 private final Element table; 243 private final Element leftTD, rightTD; 244 private final Element leftDiv, rightDiv; 245 246 private final Impl impl = (Impl) GWT.create(Impl.class); 247 248 252 private String lastSplitPosition = "50%"; 253 254 257 public HorizontalSplitPanel() { 258 super(DOM.createTable(), DOM.createTD(), DOM.createDiv(), DOM.createDiv()); 259 260 table = getElement(); 261 leftDiv = preventElementBoxStyles(DOM.createDiv()); 262 rightDiv = preventElementBoxStyles(DOM.createDiv()); 263 leftTD = preventElementBoxStyles(DOM.createTD()); 264 rightTD = preventElementBoxStyles(DOM.createTD()); 265 266 buildDOM(); 267 268 setStyleName("gwt-HorizontalSplitPanel"); 269 270 impl.init(this); 271 } 272 273 278 public final Widget getLeftWidget() { 279 return getWidget(LEFT); 280 } 281 282 287 public final Widget getRightWidget() { 288 return getWidget(RIGHT); 289 } 290 291 public final void setHeight(String height) { 292 DOM.setStyleAttribute(getElement(LEFT), "height", height); 293 DOM.setStyleAttribute(getElement(RIGHT), "height", height); 294 } 295 296 301 public final void setLeftWidget(Widget w) { 302 setWidget(LEFT, w); 303 } 304 305 310 public final void setRightWidget(Widget w) { 311 setWidget(RIGHT, w); 312 } 313 314 public final void setSplitPosition(String pos) { 315 lastSplitPosition = pos.trim(); 316 if (!lastSplitPosition.endsWith("%")) { 317 impl.setSplitPosition(this, pos); 318 } else if (isAttached()) { 319 DeferredCommand.addCommand(new Command() { 323 public void execute() { 324 int percentage = parseInt(lastSplitPosition); 325 impl.setSplitPosition(HorizontalSplitPanel.this, 326 (getOffsetWidth() * (percentage / 100.0)) + "px"); 327 } 328 }); 329 } 330 } 331 332 protected void onLoad() { 333 setSplitPosition(lastSplitPosition); 335 } 336 337 final void onSplitterResize(int x, int y) { 338 impl.onSplitResize(this, x); 339 } 340 341 final void onSplitterResizeStarted(int x, int y) { 342 impl.onSplitResizeStarted(this, x); 343 } 344 345 private void buildDOM() { 346 final Element leftContentDiv = getElement(LEFT); 347 final Element rightContentDiv = getElement(RIGHT); 348 349 final Element tbody = DOM.createTBody(); 350 final Element tr = DOM.createTR(); 351 final Element splitTD = getSplitElement(); 352 353 DOM.appendChild(table, tbody); 354 DOM.appendChild(tbody, tr); 355 DOM.appendChild(tr, leftTD); 356 DOM.appendChild(tr, splitTD); 357 DOM.appendChild(tr, rightTD); 358 DOM.appendChild(leftTD, leftDiv); 359 DOM.appendChild(rightTD, rightDiv); 360 DOM.appendChild(leftDiv, leftContentDiv); 361 DOM.appendChild(rightDiv, rightContentDiv); 362 363 DOM.setInnerHTML(splitTD, " "); 364 365 DOM.setElementProperty(table, "cellSpacing", "0"); 366 DOM.setElementProperty(table, "cellPadding", "0"); 367 368 addElementScrolling(leftContentDiv); 369 addElementScrolling(rightContentDiv); 370 371 setElementClassname(leftContentDiv, "left"); 372 setElementClassname(splitTD, "splitter"); 373 setElementClassname(rightContentDiv, "right"); 374 375 DOM.setStyleAttribute(leftTD, "verticalAlign", "top"); 376 DOM.setStyleAttribute(rightTD, "verticalAlign", "top"); 377 378 382 DOM.setElementPropertyInt(splitTD, "width", DEFAULT_SPLITTER_WIDTH); 383 } 384 } 385 | Popular Tags |