Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/FLYSHeader.java @ 4602:33bb8bf3899a
Merge the MainMenu into the FLYSHeader
Move all layouts and functionality from MainMenu into the FLYSHeader. Also
refactor the layout and use smaller BfG Logo.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 30 Nov 2012 09:57:56 +0100 |
parents | 047a44270348 |
children |
comparison
equal
deleted
inserted
replaced
4601:16c19d4f1833 | 4602:33bb8bf3899a |
---|---|
1 package de.intevation.flys.client.client.ui; | 1 package de.intevation.flys.client.client.ui; |
2 | 2 |
3 import com.google.gwt.core.client.GWT; | 3 import com.google.gwt.core.client.GWT; |
4 import com.google.gwt.resources.client.ImageResource; | 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; | |
5 | 7 |
6 import com.smartgwt.client.types.Alignment; | 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; | |
7 import com.smartgwt.client.widgets.Img; | 13 import com.smartgwt.client.widgets.Img; |
8 import com.smartgwt.client.widgets.Label; | 14 import com.smartgwt.client.widgets.Label; |
9 import com.smartgwt.client.widgets.layout.HLayout; | 15 import com.smartgwt.client.widgets.layout.HLayout; |
10 import com.smartgwt.client.widgets.layout.VLayout; | 16 import com.smartgwt.client.widgets.events.ClickEvent; |
11 | 17 import com.smartgwt.client.widgets.events.ClickHandler; |
18 | |
19 import de.intevation.flys.client.client.FLYS; | |
12 import de.intevation.flys.client.client.FLYSConstants; | 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; | |
13 | 24 |
14 | 25 |
15 /** | 26 /** |
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
17 */ | 28 */ |
19 | 30 |
20 /** The interface that provides the message resources. */ | 31 /** The interface that provides the message resources. */ |
21 private FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); | 32 private FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); |
22 | 33 |
23 /** The height used for this header.*/ | 34 /** The height used for this header.*/ |
24 public static final int HEIGHT = 75; | 35 public static final int HEIGHT = 56; |
25 | 36 |
26 /** The height used for the images.*/ | 37 /** The height used for the images.*/ |
27 public static final int IMG_HEIGHT = 50; | 38 public static final int IMG_HEIGHT = 50; |
28 | 39 |
29 | 40 /** The user that is currently logged in. */ |
30 public FLYSHeader() { | 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 }); | |
31 init(); | 130 init(); |
32 } | 131 } |
33 | 132 |
34 public void init() { | 133 public void init() { |
134 setStyleName("header"); | |
35 setWidth100(); | 135 setWidth100(); |
36 setHeight(HEIGHT); | 136 setHeight(HEIGHT); |
137 setBackgroundColor("#a9c9e6"); | |
37 setLayoutLeftMargin(5); | 138 setLayoutLeftMargin(5); |
38 setLayoutRightMargin(5); | 139 setLayoutRightMargin(5); |
39 | 140 |
40 String baseUrl = GWT.getHostPageBaseURL(); | 141 String baseUrl = GWT.getHostPageBaseURL(); |
41 | 142 |
42 Img flys = new Img( | 143 Img flys = new Img( |
43 baseUrl + MESSAGES.flysLogo(), | 144 baseUrl + MESSAGES.flysLogo(), |
44 50, | 145 50, |
45 IMG_HEIGHT); | 146 IMG_HEIGHT); |
46 | 147 |
47 Img bfg = new Img( | 148 Img bfg = new Img( |
48 baseUrl + MESSAGES.bfgLogo(), | 149 baseUrl + MESSAGES.bfgLogoSmall(), |
49 112, | 150 150, |
50 HEIGHT); | 151 IMG_HEIGHT); |
51 | 152 |
52 Label fullname = new Label(MESSAGES.fullname()); | 153 Label fullname = new Label(MESSAGES.fullname()); |
53 fullname.setHeight(HEIGHT - IMG_HEIGHT); | 154 fullname.setHeight(HEIGHT - IMG_HEIGHT); |
54 fullname.setStyleName ("fontNormalMid"); | 155 fullname.setStyleName("fontBlackMid"); |
55 | 156 |
56 VLayout left = new VLayout(); | 157 HLayout left = new HLayout(); |
158 left.setDefaultLayoutAlign(VerticalAlignment.CENTER); | |
159 left.setMembersMargin(3); | |
57 left.addMember(flys); | 160 left.addMember(flys); |
58 left.addMember(fullname); | 161 left.addMember(fullname); |
59 | 162 |
60 HLayout right = new HLayout(); | 163 HLayout right = new HLayout(); |
61 right.setAlign(Alignment.RIGHT); | 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); | |
62 right.addMember(bfg); | 194 right.addMember(bfg); |
63 | 195 |
64 addMember(left); | 196 addMember(left); |
65 addMember(right); | 197 addMember(right); |
66 } | 198 } |
67 | 199 |
68 | 200 /** |
69 /** | 201 * Returns the FLYS instance stored in this class. |
70 * This method calculates the wight of an image relative to the given | |
71 * height. | |
72 * | 202 * |
73 * @param res The ImageResource that points to the image. | 203 * @return the flys instance. |
74 * @param height The pre-defined height. | 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. | |
75 * | 215 * |
76 * @return the calculated width that should be used for the image. | 216 * @param currentLocale The current locale string (e.g. "en"). |
77 */ | 217 * @param newLocale The new locale string (e.g. "de"). |
78 protected int calcWidth(ImageResource res, int height) { | 218 */ |
79 int widthOrig = res.getWidth(); | 219 private void switchLanguage(String currentLocale, String newLocale) { |
80 int heightOrig = res.getHeight(); | 220 String newLocation = Window.Location.getHref(); |
81 | 221 |
82 double factor = (double)heightOrig / height; | 222 if (newLocation.endsWith("/")) { |
83 double width = (double)widthOrig / factor; | 223 newLocation = newLocation.substring(0, newLocation.length()-1); |
84 | 224 } |
85 return (int) width * 10 / 10; | 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(); | |
86 } | 289 } |
87 } | 290 } |
88 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 291 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |