View Javadoc

1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id: TopLevelWindowTemplate.java 5436 2011-04-26 18:25:22Z pether $
17   *  $HeadURL: https://cia.svn.sourceforge.net/svnroot/cia/trunk/citizen-intelligence-agency/src/main/java/com/hack23/cia/web/impl/ui/application/TopLevelWindowTemplate.java $
18  */
19  package com.hack23.cia.web.impl.ui.application;
20  
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.security.core.Authentication;
29  import org.springframework.security.core.GrantedAuthority;
30  import org.vaadin.addthis.AddThis;
31  import org.vaadin.navigator7.Navigator.NavigationEvent;
32  import org.vaadin.navigator7.WebApplication;
33  import org.vaadin.navigator7.interceptor.PageChangeListenersInterceptor.PageChangeListener;
34  import org.vaadin.navigator7.window.HeaderFooterFixedAppLevelWindow;
35  
36  import com.google.gwt.language.client.translation.Language;
37  import com.hack23.cia.web.impl.ui.common.ApplicationUserState;
38  import com.hack23.cia.web.impl.ui.common.ApplicationUserStateHolder;
39  import com.hack23.cia.web.impl.ui.page.admin.AdminPage;
40  import com.hack23.cia.web.impl.ui.page.admin.ConsolePage;
41  import com.hack23.cia.web.impl.ui.page.admin.DashBoardPage;
42  import com.hack23.cia.web.impl.ui.page.common.MenuState;
43  import com.hack23.cia.web.impl.ui.page.common.Page;
44  import com.hack23.cia.web.impl.ui.page.user.LogoutPage;
45  import com.hack23.cia.web.impl.ui.page.user.ProjectInfoPage;
46  import com.hack23.cia.web.impl.ui.page.user.WelcomePage;
47  import com.vaadin.Application;
48  import com.vaadin.data.Property;
49  import com.vaadin.data.Property.ValueChangeEvent;
50  import com.vaadin.terminal.gwt.server.WebApplicationContext;
51  import com.vaadin.terminal.gwt.server.WebBrowser;
52  import com.vaadin.ui.Alignment;
53  import com.vaadin.ui.Component;
54  import com.vaadin.ui.Label;
55  import com.vaadin.ui.MenuBar;
56  import com.vaadin.ui.MenuBar.MenuItem;
57  import com.vaadin.ui.NativeSelect;
58  import com.vaadin.ui.VerticalLayout;
59  
60  
61  /**
62   * The Class TopLevelWindowTemplate.
63   */
64  public final class TopLevelWindowTemplate extends HeaderFooterFixedAppLevelWindow
65  		implements MenuState,Property.ValueChangeListener,PageChangeListener {
66  
67  	/** The Constant serialVersionUID. */
68  	private static final long serialVersionUID = 1L;
69  
70  	/** The Constant LOGGER. */
71  	private static final Logger LOGGER = LoggerFactory
72  			.getLogger(TopLevelWindowTemplate.class);
73  
74  	/** The menu bar. */
75  	private transient final MenuBar menuBar;
76  
77  	/** The menu page map. */
78  	private transient final Map<Class<? extends com.vaadin.ui.Component>, MenuItem> menuPageMap = new HashMap<Class<? extends com.vaadin.ui.Component>, MenuItem>();
79  
80  	/** The user state. */
81  	private final ApplicationUserState userState;
82  
83  	/** The language select box. */
84  	private NativeSelect languageSelectBox;
85  
86  	/** The footer. */
87  	private VerticalLayout footer;
88  
89  	/** The add this. */
90  	private AddThis addThis;
91  
92  	/** The nav label. */
93  	private final Label navLabel;
94  
95  	/**
96  	 * Instantiates a new top level window template.
97  	 *
98  	 * @param object
99  	 *            the object
100 	 * @param userState
101 	 *            the user state
102 	 */
103 	public TopLevelWindowTemplate(final Object object,final ApplicationUserState userState) {
104 		super();
105 		this.userState = userState;
106 		menuBar = new MenuBar();
107 		navLabel = new Label();
108 		this.setCaption("Citizen Intelligence Agency");
109 		final WebBrowser browser = ((WebApplicationContext) ((Application) userState).getContext()).getBrowser();
110 		LOGGER.info("New Visitor :" + ((Application) userState).getURL() +  " :IP=" + browser.getAddress() + " :LOC=" + userState.getApplicationManager().getGeoIpCountryService().getLocation(browser.getAddress()) +":https=" + browser.isSecureConnection() + " :locale=" + browser.getLocale() + " :browser=" + browser.getBrowserApplication());
111 
112 		setupMenu();
113 	}
114 
115 	/**
116 	 * Adds the menu page.
117 	 *
118 	 * @param pageTitle
119 	 *            the page title
120 	 * @param pageClass
121 	 *            the page class
122 	 */
123 	@SuppressWarnings("serial")
124 	private void addMenuPage(final String pageTitle,
125 			final Class<? extends com.vaadin.ui.Component> pageClass) {
126 		final MenuItem menuItem = menuBar.addItem(pageTitle,
127 				new MenuBar.Command() {
128 					@Override
129 					public void menuSelected(final MenuItem selectedItem) {
130 						getNavigator().navigateTo(pageClass);
131 					}
132 				});
133 		menuPageMap.put(pageClass, menuItem);
134 	}
135 
136 	/*
137 	 * (non-Javadoc)
138 	 *
139 	 * @see
140 	 * org.vaadin.navigator7.window.HeaderFooterFixedAppLevelWindow#createFooter
141 	 * ()
142 	 */
143 	@Override
144 	protected Component createFooter() {
145 		footer = new VerticalLayout();
146 		footer.setWidth(100, Component.UNITS_PERCENTAGE);
147 		footer.setHeight("30px");
148 		footer.addStyleName("footer");
149 		footer.setMargin(false);
150 		footer.setSpacing(false);
151 
152 		setupAddThis();
153 
154 		return footer;
155 	}
156 
157 	/*
158 	 * (non-Javadoc)
159 	 *
160 	 * @see
161 	 * org.vaadin.navigator7.window.HeaderFooterFixedAppLevelWindow#createHeader
162 	 * ()
163 	 */
164 	@Override
165 	protected Component createHeader() {
166 		setTheme("cia-reindeer-modified");
167 		final VerticalLayout header = new VerticalLayout();
168 		header.addStyleName("header"); // Application specific style.
169 		header.setWidth(100, Component.UNITS_PERCENTAGE);
170 		header.setHeight("90px");
171 		header.setMargin(false);
172 		header.setSpacing(false);
173 
174 		// /// NavigationListener label
175 
176 		navLabel.setWidth(null);
177 		header.addComponent(navLabel);
178 		header.setComponentAlignment(navLabel, Alignment.TOP_CENTER);
179 		((CIAWebApplication) CIAWebApplication.getCurrent()).getPageChangeListenerInterceptor().addPageChangeListener(this);
180 
181 		// /// Menu
182 
183 		languageSelectBox = new NativeSelect(
184 				userState.translateFromEnglish("Please select Language"),Arrays.asList(Language.values()));
185 		languageSelectBox.addListener(this);
186 		languageSelectBox.setNullSelectionAllowed(false);
187 		languageSelectBox.setImmediate(true);
188 		header.addComponent(languageSelectBox);
189 		languageSelectBox.setValue(Language.ENGLISH.toString());
190 
191 		header.setComponentAlignment(languageSelectBox, Alignment.TOP_RIGHT);
192 
193 		menuBar.setWidth(100, Component.UNITS_PERCENTAGE);
194 		header.addComponent(menuBar);
195 		header.setComponentAlignment(menuBar, Alignment.BOTTOM_LEFT);
196 
197 		return header;
198 	}
199 
200 
201 	/*
202 	 * (non-Javadoc)
203 	 *
204 	 * @see com.hack23.cia.web.impl.ui.page.MenuState#initAdminMenu()
205 	 */
206 	/**
207 	 * Inits the admin menu.
208 	 */
209 	private void initAdminMenu() {
210 		final Set<Class<? extends Component>> keySet = menuPageMap.keySet();
211 		for (final Class<? extends Component> pageClass : keySet) {
212 			removeMenuPage(pageClass);
213 		}
214 
215 		addMenuPage(userState.translateFromEnglish("Welcome"), WelcomePage.class);
216 		addMenuPage(userState.translateFromEnglish("Project info"), ProjectInfoPage.class);
217 		addMenuPage(userState.translateFromEnglish("Admin"), AdminPage.class);
218 		addMenuPage(userState.translateFromEnglish("Console"), ConsolePage.class);
219 		addMenuPage(userState.translateFromEnglish("DashBoard"), DashBoardPage.class);
220 		addMenuPage(userState.translateFromEnglish("Logout"), LogoutPage.class);
221 	}
222 
223 
224 	/*
225 	 * (non-Javadoc)
226 	 *
227 	 * @see com.hack23.cia.web.impl.ui.page.MenuState#initUserMenu()
228 	 */
229 	/**
230 	 * Inits the user menu.
231 	 */
232 	private void initUserMenu() {
233 		final Set<Class<? extends Component>> keySet = menuPageMap.keySet();
234 		for (final Class<? extends Component> pageClass : keySet) {
235 			removeMenuPage(pageClass);
236 		}
237 
238 		addMenuPage(userState.translateFromEnglish("Welcome page"), WelcomePage.class);
239 		addMenuPage(userState.translateFromEnglish("Project info"), ProjectInfoPage.class);
240 		addMenuPage(userState.translateFromEnglish("Logout"), LogoutPage.class);
241 	}
242 
243 	/* (non-Javadoc)
244 	 * @see org.vaadin.navigator7.interceptor.PageChangeListenersInterceptor.PageChangeListener#pageChanged(org.vaadin.navigator7.Navigator.NavigationEvent)
245 	 */
246 	@Override
247 	public void pageChanged(final NavigationEvent event) {
248 		final Page page = (Page) getNavigator().getNavigableAppLevelWindow().getPage();
249 		if (page != null) {
250 
251 		navLabel.setValue(page.getPageTitle() +  " : language=" + ApplicationUserStateHolder.getUserState().getLanguage());
252 		addThis.setUrl(getApplication().getURL()+ WebApplication.getCurrent().getUriAnalyzer().buildFragmentFromPageAndParameters(event.getPageClass(), event.getParams(),true));
253 		addThis.setTitle(page.getPageTitle());
254 		setCaption("CIA :" + page.getPageTitle());
255 		}
256 	}
257 
258 	/*
259 	 * (non-Javadoc)
260 	 *
261 	 * @see
262 	 * org.vaadin.navigator7.window.FixedApplLevelWindow#prepareInnerBand(com
263 	 * .vaadin.ui.Component)
264 	 */
265 	@Override
266 	protected void prepareInnerBand(final Component innerComponent) {
267 		innerComponent.addStyleName("FixedPageTemplate-bandInnerLayout");
268 		innerComponent.setWidth(100, Component.UNITS_PERCENTAGE);
269 	}
270 
271 	/**
272 	 * Refresh page.
273 	 */
274 	private void refreshPage() {
275 		setupMenu();
276 		getNavigator().reloadCurrentPage();
277 	}
278 
279 	/**
280 	 * Removes the menu page.
281 	 *
282 	 * @param pageClass
283 	 *            the page class
284 	 */
285 	private void removeMenuPage(
286 			final Class<? extends com.vaadin.ui.Component> pageClass) {
287 		final MenuItem menuItem = menuPageMap.get(pageClass);
288 		menuBar.removeItem(menuItem);
289 	}
290 
291 	/**
292 	 * Setup add this.
293 	 */
294 	private void setupAddThis() {
295 		footer.removeAllComponents();
296 		addThis = new AddThis();
297 		addThis.addButton("twitter");
298 		addThis.addButton("facebook");
299 		addThis.addButton("dzone");
300 		addThis.addButton("digg");
301 		addThis.addButton("slashdot");
302 		addThis.addSeparator("|");
303 		addThis.addButton("expanded", userState.translateFromEnglish("More share link options!"));
304 		//addThis.setWidth(100, Component.UNITS_PERCENTAGE);
305 		footer.addComponent(addThis);
306 		addThis.setSizeFull();
307 		//footer.setComponentAlignment(addThis, Alignment.MIDDLE_CENTER);
308 
309 	}
310 
311 	/* (non-Javadoc)
312 	 * @see com.hack23.cia.web.impl.ui.page.common.MenuState#setupMenu()
313 	 */
314 	@Override
315 	public void setupMenu() {
316 		final Authentication authentication = (Authentication) userState.getUser();
317 
318 		if (authentication != null && hasAuthority(authentication,"ROLE_ADMIN")) {
319 			initAdminMenu();
320 		} else if (authentication == null || hasAuthority(authentication,"ROLE_VISITOR")) {
321 			initUserMenu();
322 		}
323 	}
324 
325 	/**
326 	 * Checks for authority.
327 	 *
328 	 * @param authentication
329 	 *            the authentication
330 	 * @param role
331 	 *            the role
332 	 * @return true, if successful
333 	 */
334 	private boolean hasAuthority(final Authentication authentication,final String role) {
335 		for (final GrantedAuthority auth : authentication.getAuthorities()) {
336 			if (auth.getAuthority().equals(role)) {
337 				return true;
338 			}
339 		}
340 		return false;
341 	}
342 
343 	/* (non-Javadoc)
344 	 * @see com.vaadin.data.Property.ValueChangeListener#valueChange(com.vaadin.data.Property.ValueChangeEvent)
345 	 */
346 	@Override
347 	public void valueChange(final ValueChangeEvent event) {
348 		final Language language = Language.valueOf(event.getProperty().toString());
349 		userState.setLanguage(language);
350 		languageSelectBox.setCaption(userState.translateFromEnglish("Please select Language"));
351 		setupAddThis();
352 
353 		refreshPage();
354 		LOGGER.info("Selected Language: " + language.toString());
355 	}
356 
357 
358 
359 }