litesoft
@ 948
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
/* * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.google.gwt.widgetideas.client; import com.google.gwt.core.client.*; import com.google.gwt.dom.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import com.google.gwt.widgetideas.client.impl.*; /** * Semi-transparent panel which can be attached to any AbsolutePanel, although * typically a RootPanel via * {@link AbsolutePanel#add(com.google.gwt.user.client.ui.Widget, int, int) parent.add(glassPanel, 0, 0)}. * Note that <a * href="http://code.google.com/p/google-web-toolkit/issues/detail?id=1813">GWT * issue 1813</a> needs to be considered in order to properly use * <code>RootPanel.get(id)</code> as the parent panel in the general case. * <p/> * <h3>CSS Style Rules</h3> * <ul class='css'> * <li>.gwt-GlassPanel { }</li> * </ul> * <p/> * <p> * <h3>Example</h3> * {@example com.google.gwt.examples.GlassPanelExample} * </p> */ public class GlassPanel extends Composite implements EventPreview { /** * A FocusPanel which automatically focuses itself when * attached (in order to blur any currently focused widget) * and then removes itself. */ private static class FocusPanelImpl extends FocusPanel { public FocusPanelImpl() { addFocusListener( new FocusListener() { @Override public void onFocus( Widget sender ) { FocusPanelImpl.this.removeFromParent(); } @Override public void onLostFocus( Widget sender ) { } } ); } @Override protected void onLoad() { super.onLoad(); /** * Removed DeferredCommand if/when GWT issue 1849 is implemented * http://code.google.com/p/google-web-toolkit/issues/detail?id=1849 */ DeferredCommand.addCommand( new Command() { @Override public void execute() { setFocus( true ); } } ); } } private static final GlassPanelImpl impl = GWT.create( GlassPanelImpl.class ); private final boolean autoHide; private final SimplePanel mySimplePanel; private WindowResizeListener resizeListener; private final Timer timer = new Timer() { @Override public void run() { impl.matchDocumentSize( GlassPanel.this, false ); } }; /** * Create a glass panel widget that can be attached to an AbsolutePanel via * {@link AbsolutePanel#add(com.google.gwt.user.client.ui.Widget, int, int) absolutePanel.add(glassPanel, 0, 0)}. * * @param autoHide <code>true</code> if the glass panel should be * automatically hidden when the user clicks on it or presses * <code>ESC</code>. */ public GlassPanel( boolean autoHide ) { this.autoHide = autoHide; mySimplePanel = new SimplePanel(); initWidget( mySimplePanel ); Style style = getElement().getStyle(); style.setProperty( "backgroundColor", "#000" ); style.setProperty( "filter", "alpha(opacity=50)" ); style.setProperty( "opacity", "0.5" ); setStyleName( "gwt-GlassPanel" ); } @Override public boolean onEventPreview( Event event ) { int type = DOM.eventGetType( event ); switch ( type ) { case Event.ONKEYPRESS: { if ( DOM.eventGetKeyCode( event ) == KeyboardListener.KEY_ESCAPE ) { removeFromParent(); return false; } } case Event.ONCLICK: { if ( DOM.isOrHasChild( getElement(), DOM.eventGetTarget( event ) ) ) { removeFromParent(); return false; } } } return true; } @Override public void setWidget( Widget widget ) { mySimplePanel.setWidget( widget ); } @Override protected void onAttach() { super.onAttach(); AbsolutePanel parent; try { parent = (AbsolutePanel) getParent(); } catch ( RuntimeException e ) { throw new IllegalStateException( "Parent widget must be an instance of AbsolutePanel" ); } if ( parent == RootPanel.get() ) { impl.matchDocumentSize( this, false ); timer.scheduleRepeating( 100 ); resizeListener = new WindowResizeListener() { @Override public void onWindowResized( int width, int height ) { impl.matchDocumentSize( GlassPanel.this, true ); } }; Window.addWindowResizeListener( resizeListener ); } else { impl.matchParentSize( this, parent ); } if ( autoHide ) { DOM.addEventPreview( this ); } RootPanel.get().add( new FocusPanelImpl(), Window.getScrollLeft(), Window.getScrollTop() ); } @Override protected void onDetach() { super.onDetach(); timer.cancel(); if ( resizeListener != null ) { Window.removeWindowResizeListener( resizeListener ); resizeListener = null; } if ( autoHide ) { DOM.removeEventPreview( this ); } } @Override protected void onLoad() { super.onLoad(); if ( !GWT.isScript() ) { // In Webkit quirks mode table cells refuse to take on // 'display: block', using 'display: table-cell' instead. if ( !impl.isCSS1Compat() ) { String nodeName = getParent().getElement().getNodeName(); if ( nodeName.matches( "^(TD|TH)$" ) ) { 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."; GWT.log( msg, new UnsupportedOperationException() ); Window.alert( msg ); assert false : msg; } } } } } |