001 /*
002 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package asquare.gwt.tk.client.ui.behavior.impl;
017
018 import asquare.gwt.tk.client.ui.behavior.ControllerAdaptor;
019 import asquare.gwt.tk.client.ui.behavior.GlassPanelController;
020 import asquare.gwt.tk.client.util.DomUtil;
021
022 import com.google.gwt.user.client.Window;
023 import com.google.gwt.user.client.WindowResizeListener;
024 import com.google.gwt.user.client.ui.Widget;
025
026 public class GlassPanelControllerStandard extends ControllerAdaptor implements GlassPanelController, WindowResizeListener
027 {
028 /*
029 * These track the last known scrollbar state. They are helpful in
030 * determining if the panel size needs to be updated when the window is
031 * resized.
032 */
033 protected boolean m_xScroll;
034 protected boolean m_yScroll;
035
036 private Widget m_widget;
037
038 public GlassPanelControllerStandard()
039 {
040 super(GlassPanelController.class);
041 }
042
043 public void plugIn(Widget widget)
044 {
045 m_widget = widget;
046 widget.setWidth(calculateWidth());
047 widget.setHeight(calculateHeight());
048 m_xScroll = canScrollX();
049 m_yScroll = canScrollY();
050 Window.addWindowResizeListener(this);
051 }
052
053 public void unplug(Widget widget)
054 {
055 Window.removeWindowResizeListener(this);
056 m_xScroll = m_yScroll = false;
057 m_widget = null;
058 }
059
060 public Widget getWidget()
061 {
062 return m_widget;
063 }
064
065 public boolean canScrollX()
066 {
067 return DomUtil.getDocumentScrollWidth() > DomUtil.getViewportWidth();
068 }
069
070 public boolean canScrollY()
071 {
072 return DomUtil.getDocumentScrollHeight() > DomUtil.getViewportHeight();
073 }
074
075 public String calculateWidth()
076 {
077 if (canScrollX())
078 {
079 return DomUtil.getDocumentScrollWidth() + "px";
080 }
081 else
082 {
083 return "100%";
084 }
085 }
086
087 public String calculateHeight()
088 {
089 if (canScrollY())
090 {
091 return DomUtil.getDocumentScrollHeight() + "px";
092 }
093 else
094 {
095 return "100%";
096 }
097 }
098
099 protected void updateWidth()
100 {
101 boolean canScrollX = canScrollX();
102 if (canScrollX != m_xScroll)
103 {
104 getWidget().setWidth(calculateWidth());
105 m_xScroll = canScrollX;
106 }
107 }
108
109 protected void updateHeight()
110 {
111 boolean canScrollY = canScrollY();
112 // document height can change when width is resized, so always update if vertically scrollable
113 if (canScrollY || canScrollY != m_yScroll)
114 {
115 getWidget().setHeight(calculateHeight());
116 m_yScroll = canScrollY;
117 }
118 }
119
120 // WindowResizeListener methods
121 public void onWindowResized(int width, int height)
122 {
123 updateWidth();
124 updateHeight();
125 }
126 }