Subversion Repository Public Repository

litesoft

Diff Revisions 282 vs 475 for /trunk/Java/GWT/Client/src/com/google/gwt/widgetideas/client/GlassPanel.java

Diff revisions: vs.
  @@ -15,25 +15,11 @@
15 15 */
16 16 package com.google.gwt.widgetideas.client;
17 17
18 - import com.google.gwt.core.client.GWT;
19 - import com.google.gwt.dom.client.Style;
20 - import com.google.gwt.user.client.Command;
21 - import com.google.gwt.user.client.DOM;
22 - import com.google.gwt.user.client.DeferredCommand;
23 - import com.google.gwt.user.client.Event;
24 - import com.google.gwt.user.client.EventPreview;
25 - import com.google.gwt.user.client.Timer;
26 - import com.google.gwt.user.client.Window;
27 - import com.google.gwt.user.client.WindowResizeListener;
28 - import com.google.gwt.user.client.ui.AbsolutePanel;
29 - import com.google.gwt.user.client.ui.Composite;
30 - import com.google.gwt.user.client.ui.FocusListener;
31 - import com.google.gwt.user.client.ui.FocusPanel;
32 - import com.google.gwt.user.client.ui.KeyboardListener;
33 - import com.google.gwt.user.client.ui.RootPanel;
34 - import com.google.gwt.user.client.ui.SimplePanel;
35 - import com.google.gwt.user.client.ui.Widget;
36 - import com.google.gwt.widgetideas.client.impl.GlassPanelImpl;
18 + import com.google.gwt.core.client.*;
19 + import com.google.gwt.dom.client.*;
20 + import com.google.gwt.user.client.*;
21 + import com.google.gwt.user.client.ui.*;
22 + import com.google.gwt.widgetideas.client.impl.*;
37 23
38 24 /**
39 25 * Semi-transparent panel which can be attached to any AbsolutePanel, although
  @@ -43,173 +29,207 @@
43 29 * href="http://code.google.com/p/google-web-toolkit/issues/detail?id=1813">GWT
44 30 * issue 1813</a> needs to be considered in order to properly use
45 31 * <code>RootPanel.get(id)</code> as the parent panel in the general case.
46 - *
32 + * <p/>
47 33 * <h3>CSS Style Rules</h3>
48 34 * <ul class='css'>
49 35 * <li>.gwt-GlassPanel { }</li>
50 36 * </ul>
51 - *
37 + * <p/>
52 38 * <p>
53 39 * <h3>Example</h3>
54 40 * {@example com.google.gwt.examples.GlassPanelExample}
55 41 * </p>
56 42 */
57 - public class GlassPanel extends Composite implements EventPreview {
58 - /**
59 - * A FocusPanel which automatically focuses itself when
60 - * attached (in order to blur any currently focused widget)
61 - * and then removes itself.
62 - */
63 - private static class FocusPanelImpl extends FocusPanel {
64 - public FocusPanelImpl() {
65 - addFocusListener(new FocusListener() {
66 -
67 - @Override
68 - public void onFocus(Widget sender) {
69 - FocusPanelImpl.this.removeFromParent();
70 - }
71 -
72 - @Override
73 - public void onLostFocus(Widget sender) {
74 - }
75 - });
43 + public class GlassPanel extends Composite implements EventPreview
44 + {
45 + /**
46 + * A FocusPanel which automatically focuses itself when
47 + * attached (in order to blur any currently focused widget)
48 + * and then removes itself.
49 + */
50 + private static class FocusPanelImpl extends FocusPanel
51 + {
52 + public FocusPanelImpl()
53 + {
54 + addFocusListener( new FocusListener()
55 + {
56 +
57 + @Override
58 + public void onFocus( Widget sender )
59 + {
60 + FocusPanelImpl.this.removeFromParent();
61 + }
62 +
63 + @Override
64 + public void onLostFocus( Widget sender )
65 + {
66 + }
67 + } );
68 + }
69 +
70 + @Override
71 + protected void onLoad()
72 + {
73 + super.onLoad();
74 + /**
75 + * Removed DeferredCommand if/when GWT issue 1849 is implemented
76 + * http://code.google.com/p/google-web-toolkit/issues/detail?id=1849
77 + */
78 + DeferredCommand.addCommand( new Command()
79 + {
80 + @Override
81 + public void execute()
82 + {
83 + setFocus( true );
84 + }
85 + } );
86 + }
87 + }
88 +
89 + private static final GlassPanelImpl impl = GWT.create( GlassPanelImpl.class );
90 +
91 + private final boolean autoHide;
92 +
93 + private final SimplePanel mySimplePanel;
94 +
95 + private WindowResizeListener resizeListener;
96 +
97 + private final Timer timer = new Timer()
98 + {
99 + @Override
100 + public void run()
101 + {
102 + impl.matchDocumentSize( GlassPanel.this, false );
103 + }
104 + };
105 +
106 + /**
107 + * Create a glass panel widget that can be attached to an AbsolutePanel via
108 + * {@link AbsolutePanel#add(com.google.gwt.user.client.ui.Widget, int, int) absolutePanel.add(glassPanel, 0, 0)}.
109 + *
110 + * @param autoHide <code>true</code> if the glass panel should be
111 + * automatically hidden when the user clicks on it or presses
112 + * <code>ESC</code>.
113 + */
114 + public GlassPanel( boolean autoHide )
115 + {
116 + this.autoHide = autoHide;
117 + mySimplePanel = new SimplePanel();
118 + initWidget( mySimplePanel );
119 + Style style = getElement().getStyle();
120 + style.setProperty( "backgroundColor", "#000" );
121 + style.setProperty( "filter", "alpha(opacity=50)" );
122 + style.setProperty( "opacity", "0.5" );
123 + setStyleName( "gwt-GlassPanel" );
76 124 }
77 125
78 126 @Override
79 - protected void onLoad() {
80 - super.onLoad();
81 - /**
82 - * Removed DeferredCommand if/when GWT issue 1849 is implemented
83 - * http://code.google.com/p/google-web-toolkit/issues/detail?id=1849
84 - */
85 - DeferredCommand.addCommand(new Command() {
86 - @Override
87 - public void execute() {
88 - setFocus(true);
127 + public boolean onEventPreview( Event event )
128 + {
129 + int type = DOM.eventGetType( event );
130 + switch ( type )
131 + {
132 + case Event.ONKEYPRESS:
133 + {
134 + if ( DOM.eventGetKeyCode( event ) == KeyboardListener.KEY_ESCAPE )
135 + {
136 + removeFromParent();
137 + return false;
138 + }
139 + }
140 + case Event.ONCLICK:
141 + {
142 + if ( DOM.isOrHasChild( getElement(), DOM.eventGetTarget( event ) ) )
143 + {
144 + removeFromParent();
145 + return false;
146 + }
147 + }
89 148 }
90 - });
149 + return true;
91 150 }
92 - }
93 151
94 - private static final GlassPanelImpl impl = GWT.create(GlassPanelImpl.class);
95 -
96 - private final boolean autoHide;
152 + @Override
153 + public void setWidget( Widget widget )
154 + {
155 + mySimplePanel.setWidget( widget );
156 + }
97 157
98 - private final SimplePanel mySimplePanel;
158 + @Override
159 + protected void onAttach()
160 + {
161 + super.onAttach();
162 + AbsolutePanel parent;
163 + try
164 + {
165 + parent = (AbsolutePanel) getParent();
166 + }
167 + catch ( RuntimeException e )
168 + {
169 + throw new IllegalStateException( "Parent widget must be an instance of AbsolutePanel" );
170 + }
171 +
172 + if ( parent == RootPanel.get() )
173 + {
174 + impl.matchDocumentSize( this, false );
175 + timer.scheduleRepeating( 100 );
176 + resizeListener = new WindowResizeListener()
177 + {
178 + @Override
179 + public void onWindowResized( int width, int height )
180 + {
181 + impl.matchDocumentSize( GlassPanel.this, true );
182 + }
183 + };
184 + Window.addWindowResizeListener( resizeListener );
185 + }
186 + else
187 + {
188 + impl.matchParentSize( this, parent );
189 + }
190 + if ( autoHide )
191 + {
192 + DOM.addEventPreview( this );
193 + }
99 194
100 - private WindowResizeListener resizeListener;
195 + RootPanel.get().add( new FocusPanelImpl(), Window.getScrollLeft(), Window.getScrollTop() );
196 + }
101 197
102 - private final Timer timer = new Timer() {
103 198 @Override
104 - public void run() {
105 - impl.matchDocumentSize(GlassPanel.this, false);
199 + protected void onDetach()
200 + {
201 + super.onDetach();
202 + timer.cancel();
203 + if ( resizeListener != null )
204 + {
205 + Window.removeWindowResizeListener( resizeListener );
206 + resizeListener = null;
207 + }
208 + if ( autoHide )
209 + {
210 + DOM.removeEventPreview( this );
211 + }
106 212 }
107 - };
108 -
109 - /**
110 - * Create a glass panel widget that can be attached to an AbsolutePanel via
111 - * {@link AbsolutePanel#add(com.google.gwt.user.client.ui.Widget, int, int) absolutePanel.add(glassPanel, 0, 0)}.
112 - *
113 - * @param autoHide <code>true</code> if the glass panel should be
114 - * automatically hidden when the user clicks on it or presses
115 - * <code>ESC</code>.
116 - */
117 - public GlassPanel(boolean autoHide) {
118 - this.autoHide = autoHide;
119 - mySimplePanel = new SimplePanel();
120 - initWidget(mySimplePanel);
121 - Style style = getElement().getStyle();
122 - style.setProperty("backgroundColor", "#000");
123 - style.setProperty("filter", "alpha(opacity=50)");
124 - style.setProperty("opacity", "0.5");
125 - setStyleName("gwt-GlassPanel");
126 - }
127 213
128 214 @Override
129 - public boolean onEventPreview(Event event) {
130 - int type = DOM.eventGetType(event);
131 - switch (type) {
132 - case Event.ONKEYPRESS: {
133 - if (DOM.eventGetKeyCode(event) == KeyboardListener.KEY_ESCAPE) {
134 - removeFromParent();
135 - return false;
136 - }
137 - }
138 - case Event.ONCLICK: {
139 - if (DOM.isOrHasChild(getElement(), DOM.eventGetTarget(event))) {
140 - removeFromParent();
141 - return false;
142 - }
143 - }
144 - }
145 - return true;
146 - }
147 -
148 - @Override
149 - public void setWidget(Widget widget) {
150 - mySimplePanel.setWidget(widget);
151 - }
152 -
153 - @Override
154 - protected void onAttach() {
155 - super.onAttach();
156 - AbsolutePanel parent;
157 - try {
158 - parent = (AbsolutePanel) getParent();
159 - } catch (RuntimeException e) {
160 - throw new IllegalStateException("Parent widget must be an instance of AbsolutePanel");
161 - }
162 -
163 - if (parent == RootPanel.get()) {
164 - impl.matchDocumentSize(this, false);
165 - timer.scheduleRepeating(100);
166 - resizeListener = new WindowResizeListener() {
167 - @Override
168 - public void onWindowResized(int width, int height) {
169 - impl.matchDocumentSize(GlassPanel.this, true);
170 - }
171 - };
172 - Window.addWindowResizeListener(resizeListener);
173 - } else {
174 - impl.matchParentSize(this, parent);
175 - }
176 - if (autoHide) {
177 - DOM.addEventPreview(this);
178 - }
179 -
180 - RootPanel.get().add(new FocusPanelImpl(), Window.getScrollLeft(), Window.getScrollTop());
181 - }
182 -
183 - @Override
184 - protected void onDetach() {
185 - super.onDetach();
186 - timer.cancel();
187 - if (resizeListener != null) {
188 - Window.removeWindowResizeListener(resizeListener);
189 - resizeListener = null;
190 - }
191 - if (autoHide) {
192 - DOM.removeEventPreview(this);
193 - }
194 - }
195 -
196 - @Override
197 - protected void onLoad() {
198 - super.onLoad();
199 - if (!GWT.isScript()) {
200 - // In Webkit quirks mode table cells refuse to take on
201 - // 'display: block', using 'display: table-cell' instead.
202 - if (!impl.isCSS1Compat()) {
203 - String nodeName = getParent().getElement().getNodeName();
204 - if (nodeName.matches("^(TD|TH)$")) {
205 - String msg = "SORRY: GlassPanel cannot properly cover a " + nodeName
206 - + " element in quirks mode due to CSS restrictions in certain browsers."
207 - + " Please consider using standards mode or try using a different type of element.";
208 - GWT.log(msg, new UnsupportedOperationException());
209 - Window.alert(msg);
210 - assert false : msg;
215 + protected void onLoad()
216 + {
217 + super.onLoad();
218 + if ( !GWT.isScript() )
219 + {
220 + // In Webkit quirks mode table cells refuse to take on
221 + // 'display: block', using 'display: table-cell' instead.
222 + if ( !impl.isCSS1Compat() )
223 + {
224 + String nodeName = getParent().getElement().getNodeName();
225 + if ( nodeName.matches( "^(TD|TH)$" ) )
226 + {
227 + String msg = "SORRY: GlassPanel cannot properly cover a " + nodeName + " element in quirks mode due to CSS restrictions in certain browsers." + " Please consider using standards mode or try using a different type of element.";
228 + GWT.log( msg, new UnsupportedOperationException() );
229 + Window.alert( msg );
230 + assert false : msg;
231 + }
232 + }
211 233 }
212 - }
213 234 }
214 - }
215 235 }