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.i18n.client.LocaleInfo;
24 import com.google.gwt.sample.showcase.client.ContentWidget;
25 import com.google.gwt.sample.showcase.client.Showcase;
26 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseData;
27 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource;
28 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseStyle;
29 import com.google.gwt.user.client.rpc.AsyncCallback;
30 import com.google.gwt.user.client.ui.Button;
31 import com.google.gwt.user.client.ui.DialogBox;
32 import com.google.gwt.user.client.ui.HTML;
33 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
34 import com.google.gwt.user.client.ui.Image;
35 import com.google.gwt.user.client.ui.ListBox;
36 import com.google.gwt.user.client.ui.VerticalPanel;
37 import com.google.gwt.user.client.ui.Widget;
38 
39 /**
40  * Example file.
41  */
42 @ShowcaseStyle(/* styles */{
43     ".gwt-DialogBox", "html>body .gwt-DialogBox", "* html .gwt-DialogBox",
44     ".cw-DialogBox"})
45 public class CwDialogBox extends ContentWidget {
46   /**
47    * The constants used in this Content Widget.
48    */
49   @ShowcaseSource
50   public static interface CwConstants extends Constants {
cwDialogBoxCaption()51     String cwDialogBoxCaption();
52 
cwDialogBoxClose()53     String cwDialogBoxClose();
54 
cwDialogBoxDescription()55     String cwDialogBoxDescription();
56 
cwDialogBoxDetails()57     String cwDialogBoxDetails();
58 
cwDialogBoxItem()59     String cwDialogBoxItem();
60 
cwDialogBoxListBoxInfo()61     String cwDialogBoxListBoxInfo();
62 
cwDialogBoxMakeTransparent()63     String cwDialogBoxMakeTransparent();
64 
cwDialogBoxName()65     String cwDialogBoxName();
66 
cwDialogBoxShowButton()67     String cwDialogBoxShowButton();
68   }
69 
70   /**
71    * An instance of the constants.
72    */
73   @ShowcaseData
74   private final CwConstants constants;
75 
76   /**
77    * Constructor.
78    *
79    * @param constants the constants
80    */
CwDialogBox(CwConstants constants)81   public CwDialogBox(CwConstants constants) {
82     super(
83         constants.cwDialogBoxName(), constants.cwDialogBoxDescription(), true);
84     this.constants = constants;
85   }
86 
87   /**
88    * Initialize this example.
89    */
90   @ShowcaseSource
91   @Override
onInitialize()92   public Widget onInitialize() {
93     // Create the dialog box
94     final DialogBox dialogBox = createDialogBox();
95     dialogBox.setGlassEnabled(true);
96     dialogBox.setAnimationEnabled(true);
97 
98     // Create a button to show the dialog Box
99     Button openButton = new Button(
100         constants.cwDialogBoxShowButton(), new ClickHandler() {
101           public void onClick(ClickEvent sender) {
102             dialogBox.center();
103             dialogBox.show();
104           }
105         });
106 
107     // Create a ListBox
108     HTML listDesc = new HTML(
109         "<br><br><br>" + constants.cwDialogBoxListBoxInfo());
110 
111     ListBox list = new ListBox();
112     list.setVisibleItemCount(1);
113     for (int i = 10; i > 0; i--) {
114       list.addItem(constants.cwDialogBoxItem() + " " + i);
115     }
116 
117     // Add the button and list to a panel
118     VerticalPanel vPanel = new VerticalPanel();
119     vPanel.setSpacing(8);
120     vPanel.add(openButton);
121     vPanel.add(listDesc);
122     vPanel.add(list);
123 
124     // Return the panel
125     return vPanel;
126   }
127 
128   @Override
asyncOnInitialize(final AsyncCallback<Widget> callback)129   protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
130     GWT.runAsync(CwDialogBox.class, new RunAsyncCallback() {
131 
132       public void onFailure(Throwable caught) {
133         callback.onFailure(caught);
134       }
135 
136       public void onSuccess() {
137         callback.onSuccess(onInitialize());
138       }
139     });
140   }
141 
142   /**
143    * Create the dialog box for this example.
144    *
145    * @return the new dialog box
146    */
147   @ShowcaseSource
createDialogBox()148   private DialogBox createDialogBox() {
149     // Create a dialog box and set the caption text
150     final DialogBox dialogBox = new DialogBox();
151     dialogBox.ensureDebugId("cwDialogBox");
152     dialogBox.setText(constants.cwDialogBoxCaption());
153 
154     // Create a table to layout the content
155     VerticalPanel dialogContents = new VerticalPanel();
156     dialogContents.setSpacing(4);
157     dialogBox.setWidget(dialogContents);
158 
159     // Add some text to the top of the dialog
160     HTML details = new HTML(constants.cwDialogBoxDetails());
161     dialogContents.add(details);
162     dialogContents.setCellHorizontalAlignment(
163         details, HasHorizontalAlignment.ALIGN_CENTER);
164 
165     // Add an image to the dialog
166     Image image = new Image(Showcase.images.jimmy());
167     dialogContents.add(image);
168     dialogContents.setCellHorizontalAlignment(
169         image, HasHorizontalAlignment.ALIGN_CENTER);
170 
171     // Add a close button at the bottom of the dialog
172     Button closeButton = new Button(
173         constants.cwDialogBoxClose(), new ClickHandler() {
174           public void onClick(ClickEvent event) {
175             dialogBox.hide();
176           }
177         });
178     dialogContents.add(closeButton);
179     if (LocaleInfo.getCurrentLocale().isRTL()) {
180       dialogContents.setCellHorizontalAlignment(
181           closeButton, HasHorizontalAlignment.ALIGN_LEFT);
182 
183     } else {
184       dialogContents.setCellHorizontalAlignment(
185           closeButton, HasHorizontalAlignment.ALIGN_RIGHT);
186     }
187 
188     // Return the dialog box
189     return dialogBox;
190   }
191 }
192