comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/FLYSHeader.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/FLYSHeader.java@33bb8bf3899a
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.LocaleInfo;
5 import com.google.gwt.user.client.Window;
6 import com.google.gwt.user.client.rpc.AsyncCallback;
7
8 import com.smartgwt.client.types.Alignment;
9 import com.smartgwt.client.types.VerticalAlignment;
10 import com.smartgwt.client.util.BooleanCallback;
11 import com.smartgwt.client.util.SC;
12 import com.smartgwt.client.widgets.Button;
13 import com.smartgwt.client.widgets.Img;
14 import com.smartgwt.client.widgets.Label;
15 import com.smartgwt.client.widgets.layout.HLayout;
16 import com.smartgwt.client.widgets.events.ClickEvent;
17 import com.smartgwt.client.widgets.events.ClickHandler;
18
19 import de.intevation.flys.client.client.FLYS;
20 import de.intevation.flys.client.client.FLYSConstants;
21 import de.intevation.flys.client.client.services.UserService;
22 import de.intevation.flys.client.client.services.UserServiceAsync;
23 import de.intevation.flys.client.shared.model.User;
24
25
26 /**
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
28 */
29 public class FLYSHeader extends HLayout {
30
31 /** The interface that provides the message resources. */
32 private FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
33
34 /** The height used for this header.*/
35 public static final int HEIGHT = 56;
36
37 /** The height used for the images.*/
38 public static final int IMG_HEIGHT = 50;
39
40 /** The user that is currently logged in. */
41 private User currentUser;
42
43 /** The label that displays the current logged in user. */
44 private Label userText;
45
46 /** The button to log the current user out.*/
47 private Button logout;
48
49 /** The button to open the project list.*/
50 private Button projectList;
51
52 /** The button to switch between the english and german version.*/
53 private Button language;
54
55 /** The button to open an info panel.*/
56 private Button info;
57
58 private UserServiceAsync userService =
59 GWT.create(UserService.class);
60
61 /** An instance to FLYS.*/
62 private FLYS flys;
63
64
65 public FLYSHeader(FLYS flys) {
66 this.flys = flys;
67
68 String guest = MESSAGES.user() + " " + MESSAGES.guest();
69
70 userText = new Label(guest);
71 projectList = new Button(MESSAGES.manage_projects());
72 logout = new Button(MESSAGES.logout());
73 language = new Button(MESSAGES.switch_language());
74 info = new Button(MESSAGES.info());
75
76 projectList.addClickHandler(new ClickHandler() {
77 @Override
78 public void onClick(ClickEvent event) {
79 GWT.log("Clicked 'Open ProjectList' button.");
80 getFlys().openProjectList();
81 }
82 });
83
84 logout.addClickHandler(new ClickHandler() {
85 @Override
86 public void onClick(ClickEvent event) {
87 GWT.log("Clicked 'logout' button.");
88 userService.logoutCurrentUser(new AsyncCallback<Void>() {
89 public void onFailure(Throwable caught) {
90 }
91
92 public void onSuccess(Void result) {
93 /* Just reload the page. GGInAFilter is goint to redirect
94 * to the correct login page */
95 Window.Location.reload();
96 }
97 });
98
99 }
100 });
101
102 language.addClickHandler(new ClickHandler() {
103 @Override
104 public void onClick(ClickEvent event) {
105 LocaleInfo info = LocaleInfo.getCurrentLocale();
106 final String currentLocale = info.getLocaleName();
107 final String newLocale = currentLocale.equals("de")
108 ? "en"
109 : "de";
110
111 SC.confirm(MESSAGES.warning(), MESSAGES.warning_language(),
112 new BooleanCallback() {
113 @Override
114 public void execute(Boolean value) {
115 if (value) {
116 switchLanguage(currentLocale, newLocale);
117 }
118 }
119 });
120 }
121 });
122
123 info.addClickHandler(new ClickHandler() {
124 @Override
125 public void onClick(ClickEvent event) {
126 GWT.log("Clicked 'info' button.");
127 GWT.log("IMPLEMENT the 'open info panel' function.");
128 }
129 });
130 init();
131 }
132
133 public void init() {
134 setStyleName("header");
135 setWidth100();
136 setHeight(HEIGHT);
137 setBackgroundColor("#a9c9e6");
138 setLayoutLeftMargin(5);
139 setLayoutRightMargin(5);
140
141 String baseUrl = GWT.getHostPageBaseURL();
142
143 Img flys = new Img(
144 baseUrl + MESSAGES.flysLogo(),
145 50,
146 IMG_HEIGHT);
147
148 Img bfg = new Img(
149 baseUrl + MESSAGES.bfgLogoSmall(),
150 150,
151 IMG_HEIGHT);
152
153 Label fullname = new Label(MESSAGES.fullname());
154 fullname.setHeight(HEIGHT - IMG_HEIGHT);
155 fullname.setStyleName("fontBlackMid");
156
157 HLayout left = new HLayout();
158 left.setDefaultLayoutAlign(VerticalAlignment.CENTER);
159 left.setMembersMargin(3);
160 left.addMember(flys);
161 left.addMember(fullname);
162
163 HLayout right = new HLayout();
164 right.setAlign(Alignment.RIGHT);
165 right.setDefaultLayoutAlign(Alignment.RIGHT);
166 right.setDefaultLayoutAlign(VerticalAlignment.CENTER);
167 right.setMembersMargin(3);
168 right.setLayoutRightMargin(5);
169
170 projectList.setStyleName("manageProjects");
171 userText.setStyleName("fontBlackSmall");
172 logout.setStyleName("fontLightSmall");
173 language.setStyleName("fontLightSmall");
174 info.setStyleName("fontLightSmall");
175
176 userText.setAlign(Alignment.RIGHT);
177 userText.setWidth(200);
178 logout.setWidth(70);
179 info.setWidth(40);
180 language.setWidth(70);
181
182 left.addMember(projectList);
183 if (this.flys.isProjectListVisible()) {
184 hideProjectButton();
185 }
186 else {
187 showProjectButton();
188 }
189
190 right.addMember(userText);
191 right.addMember(logout);
192 right.addMember(language);
193 right.addMember(info);
194 right.addMember(bfg);
195
196 addMember(left);
197 addMember(right);
198 }
199
200 /**
201 * Returns the FLYS instance stored in this class.
202 *
203 * @return the flys instance.
204 */
205 private FLYS getFlys() {
206 return flys;
207 }
208
209 /**
210 * This method triggers the language switch between the <i>currentLocale</i>
211 * and the <i>newLocale</i>. The switch is done by replacing a "locale="
212 * parameter in the url of the application. We could use the GWT UrlBuilder
213 * class to create a new URL, but - in my eyes - this class is a bit
214 * inconsistens in its implementation.
215 *
216 * @param currentLocale The current locale string (e.g. "en").
217 * @param newLocale The new locale string (e.g. "de").
218 */
219 private void switchLanguage(String currentLocale, String newLocale) {
220 String newLocation = Window.Location.getHref();
221
222 if (newLocation.endsWith("/")) {
223 newLocation = newLocation.substring(0, newLocation.length()-1);
224 }
225
226 String replace = null;
227 String replaceWith = null;
228
229 if (newLocation.indexOf("&locale=") >= 0) {
230 replace = currentLocale.equals("de")
231 ? "&locale=de"
232 : "&locale=en";
233
234 replaceWith = "&locale=" + newLocale;
235 }
236 else if (newLocation.indexOf("?locale=") >= 0) {
237 replace = currentLocale.equals("de")
238 ? "?locale=de"
239 : "?locale=en";
240
241 replaceWith = "?locale=" + newLocale;
242 }
243 else {
244 newLocation += newLocation.indexOf("?") >= 0
245 ? "&locale=" + newLocale
246 : "?locale=" + newLocale;
247 }
248
249 if (replace != null && replaceWith != null) {
250 newLocation = newLocation.replace(replace, replaceWith);
251 }
252
253 Window.open(newLocation, "_self", "");
254 }
255
256 /**
257 * Update the text field that shows the current user. If no user is
258 * currently logged in, the text will display {@link FLYSConstants.guest()}.
259 */
260 private void updateCurrentUser() {
261 String name = currentUser != null
262 ? currentUser.getName()
263 : MESSAGES.guest();
264
265 GWT.log("Update the current user: " + name);
266
267 String username = MESSAGES.user() + " " + name;
268 userText.setContents(username);
269 }
270
271 /**
272 * Set the current {@link User} and call {@link updateCurrentUser()}
273 * afterwards.
274 *
275 * @param user the new user.
276 */
277 public void setCurrentUser(User currentUser) {
278 this.currentUser = currentUser;
279
280 updateCurrentUser();
281 }
282
283 public void hideProjectButton() {
284 this.projectList.hide();
285 }
286
287 public void showProjectButton() {
288 this.projectList.show();
289 }
290 }
291 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org