Subversion Repository Public Repository

litesoft

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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright 2010 Daniel Kurka
 * 
 * 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.googlecode.mgwt.ui.client.dialog;

import java.util.List;

import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog.ConfirmCallback;
import com.googlecode.mgwt.ui.client.widget.Button;

/**
 * Utility class to use the most common dialog classes
 * 
 * @author Daniel Kurka
 */
public class Dialogs {

	/**
	 * Callback interface for the Alert Dialog
	 * 
	 * @author Daniel Kurka
	 * 
	 */
	public interface AlertCallback {
		/**
		 * called when the ok button is pressed
		 */
		public void onButtonPressed();
	}

	/**
	 * Show an alert to the user
	 * 
	 * @param title - the title of the alert
	 * @param text - the text of the alert
	 * @param callback - the callback that is called when the user clicks the ok
	 *            button (can be null)
	 */
	public static void alert(String title, String text, final AlertCallback callback) {
		AlertDialog alertDialog = new AlertDialog(MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss(), title, text);

		alertDialog.addTapHandler(new TapHandler() {

			@Override
			public void onTap(TapEvent event) {
				if (callback != null) {
					callback.onButtonPressed();
				}

			}
		});

		alertDialog.show();
	}

	/**
	 * Show a confirm dialog to the user
	 * 
	 * @param title - The title of the Dialog
	 * @param text - the text to confirm
	 * @param callback - the callback that is called when a button is taped on
	 *            the dialog
	 */
	public static void confirm(String title, String text, final ConfirmCallback callback) {
		ConfirmDialog confirmDialog = new ConfirmDialog(title, text, callback);

		confirmDialog.show();
	}

	private static class InternalTouchHandler implements TapHandler {
		private final int buttonCount;
		private final OptionCallback callback;
		private final OptionsDialog panel;

		public InternalTouchHandler(int buttonCount, OptionsDialog panel, OptionCallback callback) {
			this.buttonCount = buttonCount;
			this.panel = panel;

			this.callback = callback;
		}

		@Override
		public void onTap(TapEvent event) {
			panel.hide();
			if (callback != null) {
				callback.onOptionSelected(buttonCount);
			}

		}

	}

	/**
	 * Show an options dialog to the user
	 * 
	 * @param options - text and type of the buttons to show
	 * @param callback - the callback of the button that was selected
	 */
	public static void options(List<OptionsDialogEntry> options, OptionCallback callback) {

		options(options, callback, RootPanel.get());
	}

	/**
	 * Show an options dialog to the user
	 * 
	 * @param options - text and type of the buttons to show
	 * @param callback - the callback of the button that was selected
	 * @param widgetToCover - the widget that should be covered by the dialog
	 */
	public static void options(List<OptionsDialogEntry> options, OptionCallback callback, HasWidgets widgetToCover) {

		OptionsDialog optionsDialog = new OptionsDialog(MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss());

		int count = 0;
		for (OptionsDialogEntry optionsDialogEntry : options) {
			count++;
			Button button = new Button(optionsDialogEntry.getText());
			switch (optionsDialogEntry.getType()) {
			case NORMAL:
				break;
			case IMPORTANT:
				button.setImportant(true);
				break;
			case CONFIRM:
				button.setConfirm(true);
				break;
			default:
				throw new RuntimeException("how did we get here?");
			}
			button.addTapHandler(new InternalTouchHandler(count, optionsDialog, callback));
			optionsDialog.add(button);
		}
		optionsDialog.setPanelToOverlay(widgetToCover);
		optionsDialog.show();
	}

	/**
	 * The option Callback interface
	 * 
	 * @author Daniel Kurka
	 * 
	 */
	public interface OptionCallback {
    /**
     * called when an option gets selected
     * 
     * @param index the index of the selected button
     */
		public void onOptionSelected(int index);
	}

	/**
	 * The type of buttons to add
	 * 
	 * @author Daniel Kurka
	 * 
	 */
	public enum ButtonType {
    /**
     * normal button
     */
    NORMAL, /**
     * important button
     */
    IMPORTANT, /**
     * confirm button
     */
    CONFIRM
	};

	/**
	 * Options for Options Dialog
	 * 
	 * @author Daniel Kurka
	 * 
	 */
	public static class OptionsDialogEntry {
		private final String text;
		private final ButtonType type;

    /**
     * Construct an {@link OptionsDialogEntry}
     * 
     * @param text the text to display
     * @param type the type of button to use
     */
		public OptionsDialogEntry(String text, ButtonType type) {
			this.text = text;
			this.type = type;

		}

    /**
     * get the text of the button
     * 
     * @return the text of the button
     */
		public String getText() {
			return text;
		}

    /**
     * get the type of the button
     * 
     * @return the type of the button
     */
		public ButtonType getType() {
			return type;
		}
	}
}

Commits for litesoft/trunk/mobileGWT/zOriginal/mgwt/src_main_java_com_googlecode_mgwt_ui_client_dialog/Dialogs.java

Diff revisions: vs.
Revision Author Commited Message
751 GeorgeS picture GeorgeS Sat 07 Jul, 2012 18:21:49 +0000