1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.google.gwt.sample.showcase.client.content.popups;
17 
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.core.client.RunAsyncCallback;
20 import com.google.gwt.event.dom.client.ClickEvent;
21 import com.google.gwt.event.dom.client.ClickHandler;
22 import com.google.gwt.i18n.client.Constants;
23 import com.google.gwt.sample.showcase.client.ContentWidget;
24 import com.google.gwt.sample.showcase.client.Showcase;
25 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseData;
26 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource;
27 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseStyle;
28 import com.google.gwt.user.client.rpc.AsyncCallback;
29 import com.google.gwt.user.client.ui.Button;
30 import com.google.gwt.user.client.ui.DecoratedPopupPanel;
31 import com.google.gwt.user.client.ui.HTML;
32 import com.google.gwt.user.client.ui.Image;
33 import com.google.gwt.user.client.ui.PopupPanel;
34 import com.google.gwt.user.client.ui.VerticalPanel;
35 import com.google.gwt.user.client.ui.Widget;
36 
37 /**
38  * Example file.
39  */
40 @ShowcaseStyle({
41     ".gwt-PopupPanel", "html>body .gwt-PopupPanel", "* html .gwt-PopupPanel",
42     ".gwt-DecoratedPopupPanel", "html>body .gwt-DecoratedPopupPanel",
43     "* html .gwt-DecoratedPopupPanel"})
44 public class CwBasicPopup extends ContentWidget {
45   /**
46    * The constants used in this Content Widget.
47    */
48   @ShowcaseSource
49   public static interface CwConstants extends Constants {
cwBasicPopupClickOutsideInstructions()50     String cwBasicPopupClickOutsideInstructions();
51 
cwBasicPopupDescription()52     String cwBasicPopupDescription();
53 
cwBasicPopupInstructions()54     String cwBasicPopupInstructions();
55 
cwBasicPopupName()56     String cwBasicPopupName();
57 
cwBasicPopupShowButton()58     String cwBasicPopupShowButton();
59   }
60 
61   /**
62    * An instance of the constants.
63    */
64   @ShowcaseData
65   private final CwConstants constants;
66 
67   /**
68    * Constructor.
69    *
70    * @param constants the constants
71    */
CwBasicPopup(CwConstants constants)72   public CwBasicPopup(CwConstants constants) {
73     super(constants.cwBasicPopupName(), constants.cwBasicPopupDescription(),
74         true);
75     this.constants = constants;
76   }
77 
78   /**
79    * Initialize this example.
80    */
81   @ShowcaseSource
82   @Override
onInitialize()83   public Widget onInitialize() {
84     // Create a basic popup widget
85     final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
86     simplePopup.ensureDebugId("cwBasicPopup-simplePopup");
87     simplePopup.setWidth("150px");
88     simplePopup.setWidget(
89         new HTML(constants.cwBasicPopupClickOutsideInstructions()));
90 
91     // Create a button to show the popup
92     Button openButton = new Button(
93         constants.cwBasicPopupShowButton(), new ClickHandler() {
94           public void onClick(ClickEvent event) {
95             // Reposition the popup relative to the button
96             Widget source = (Widget) event.getSource();
97             int left = source.getAbsoluteLeft() + 10;
98             int top = source.getAbsoluteTop() + 10;
99             simplePopup.setPopupPosition(left, top);
100 
101             // Show the popup
102             simplePopup.show();
103           }
104         });
105 
106     // Create a popup to show the full size image
107     Image jimmyFull = new Image(Showcase.images.jimmy());
108     final PopupPanel imagePopup = new PopupPanel(true);
109     imagePopup.setAnimationEnabled(true);
110     imagePopup.ensureDebugId("cwBasicPopup-imagePopup");
111     imagePopup.setWidget(jimmyFull);
112     jimmyFull.addClickHandler(new ClickHandler() {
113       public void onClick(ClickEvent event) {
114         imagePopup.hide();
115       }
116     });
117 
118     // Add an image thumbnail
119     Image jimmyThumb = new Image(Showcase.images.jimmyThumb());
120     jimmyThumb.ensureDebugId("cwBasicPopup-thumb");
121     jimmyThumb.addStyleName("cw-BasicPopup-thumb");
122     jimmyThumb.addClickHandler(new ClickHandler() {
123       public void onClick(ClickEvent event) {
124         imagePopup.center();
125       }
126     });
127 
128     // Add the widgets to a panel
129     VerticalPanel vPanel = new VerticalPanel();
130     vPanel.setSpacing(5);
131     vPanel.add(openButton);
132     vPanel.add(new HTML("<br><br><br>" + constants.cwBasicPopupInstructions()));
133     vPanel.add(jimmyThumb);
134 
135     // Return the panel
136     return vPanel;
137   }
138 
139   @Override
asyncOnInitialize(final AsyncCallback<Widget> callback)140   protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
141     GWT.runAsync(CwBasicPopup.class, new RunAsyncCallback() {
142 
143       public void onFailure(Throwable caught) {
144         callback.onFailure(caught);
145       }
146 
147       public void onSuccess() {
148         callback.onSuccess(onInitialize());
149       }
150     });
151   }
152 }
153