KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > kitchensink > client > Popups


1 /*
2  * Copyright 2007 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.kitchensink.client;
17
18 import com.google.gwt.user.client.ui.Button;
19 import com.google.gwt.user.client.ui.ClickListener;
20 import com.google.gwt.user.client.ui.DialogBox;
21 import com.google.gwt.user.client.ui.DockPanel;
22 import com.google.gwt.user.client.ui.HTML;
23 import com.google.gwt.user.client.ui.Image;
24 import com.google.gwt.user.client.ui.ListBox;
25 import com.google.gwt.user.client.ui.PopupPanel;
26 import com.google.gwt.user.client.ui.VerticalPanel;
27 import com.google.gwt.user.client.ui.Widget;
28
29 /**
30  * Demonstrates {@link com.google.gwt.user.client.ui.PopupPanel} and
31  * {@link com.google.gwt.user.client.ui.DialogBox}.
32  */

33 public class Popups extends Sink implements ClickListener {
34
35   /**
36    * A simple dialog box that displays a message, a Frame, and a close button.
37    */

38   private static class MyDialog extends DialogBox implements ClickListener {
39     public MyDialog() {
40       setText("Sample DialogBox");
41
42       Button closeButton = new Button("Close", this);
43       HTML msg = new HTML(
44           "<center>This is an example of a standard dialog box component.</center>",
45           true);
46
47       DockPanel dock = new DockPanel();
48       dock.setSpacing(4);
49
50       dock.add(closeButton, DockPanel.SOUTH);
51       dock.add(msg, DockPanel.NORTH);
52       dock.add(new Image("images/jimmy.jpg"), DockPanel.CENTER);
53
54       dock.setCellHorizontalAlignment(closeButton, DockPanel.ALIGN_RIGHT);
55       dock.setWidth("100%");
56       setWidget(dock);
57     }
58
59     public void onClick(Widget sender) {
60       hide();
61     }
62   }
63
64   /**
65    * A very simple popup that closes automatically when you click off of it.
66    */

67   private static class MyPopup extends PopupPanel {
68     public MyPopup() {
69       super(true);
70
71       HTML contents = new HTML(
72           "Click anywhere outside this popup to make it disappear.");
73       contents.setWidth("128px");
74       setWidget(contents);
75
76       setStyleName("ks-popups-Popup");
77     }
78   }
79
80   public static SinkInfo init() {
81     return new SinkInfo(
82         "Popups",
83         "<h2>Popups and Dialog Boxes</h2>"
84             + "<p>This page demonstrates GWT's built-in support for in-page "
85             + "popups. The first is a very simple informational popup that closes "
86             + "itself automatically when you click off of it. The second is a more "
87             + "complex draggable dialog box. If you're wondering why there's "
88             + "a list box at the bottom, it's to demonstrate that you can drag the "
89             + "dialog box over it (this obscure corner case often renders incorrectly "
90             + "on some browsers).</p>") {
91
92       public Sink createInstance() {
93         return new Popups();
94       }
95
96       public String JavaDoc getColor() {
97         return "#bf2a2a";
98       }
99     };
100   }
101
102   private Button dialogButton = new Button("Show Dialog", this);
103   private Button popupButton = new Button("Show Popup", this);
104
105   public Popups() {
106     VerticalPanel panel = new VerticalPanel();
107     panel.add(popupButton);
108     panel.add(dialogButton);
109
110     ListBox list = new ListBox();
111     list.setVisibleItemCount(1);
112     for (int i = 0; i < 10; ++i) {
113       list.addItem("list item " + i);
114     }
115     panel.add(list);
116
117     panel.setSpacing(8);
118     initWidget(panel);
119   }
120
121   public void onClick(Widget sender) {
122     if (sender == popupButton) {
123       MyPopup p = new MyPopup();
124       int left = sender.getAbsoluteLeft() + 10;
125       int top = sender.getAbsoluteTop() + 10;
126       p.setPopupPosition(left, top);
127       p.show();
128     } else if (sender == dialogButton) {
129       DialogBox dlg = new MyDialog();
130       dlg.show();
131       dlg.setVisible(false);
132       dlg.center();
133       dlg.setVisible(true);
134     }
135   }
136
137   public void onShow() {
138   }
139 }
140
Popular Tags