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: GlobalSiteViewPanel.java 5582 2012-01-04 20:03:13Z pether $
17   *  $HeadURL: https://cia.svn.sourceforge.net/svnroot/cia/trunk/citizen-intelligence-agency/src/main/java/com/hack23/cia/web/impl/ui/page/user/GlobalSiteViewPanel.java $
18   */
19  package com.hack23.cia.web.impl.ui.page.user;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.vaadin.navigator7.uri.ParamPageResource;
25  import org.vaadin.vaadinvisualizations.GeoMap;
26  import org.vaadin.vaadinvisualizations.VisualizationComponent.SelectionListener;
27  
28  import com.hack23.cia.model.internal.application.data.impl.AggregatedCountryData;
29  import com.hack23.cia.model.internal.application.data.impl.CountryMetaDataType;
30  import com.hack23.cia.service.api.DataContainer;
31  import com.hack23.cia.web.impl.ui.page.common.panel.AbstractPanel;
32  import com.vaadin.data.Property;
33  import com.vaadin.data.Property.ValueChangeEvent;
34  import com.vaadin.ui.ListSelect;
35  import com.vaadin.ui.VerticalSplitPanel;
36  
37  /**
38   * The Class GlobalSiteViewPanel.
39   */
40  public final class GlobalSiteViewPanel extends AbstractPanel implements
41  		SelectionListener, Property.ValueChangeListener {
42  
43  	/** The Constant serialVersionUID. */
44  	private static final long serialVersionUID = 1L;
45  
46  	/** The vert. */
47  	private VerticalSplitPanel vert = null;
48  
49  	/** The gm. */
50  	private GeoMap gm;
51  
52  	/** The all countries. */
53  	private List<AggregatedCountryData> allCountries;
54  
55  	/**
56  	 * Instantiates a new global site view panel.
57  	 */
58  	public GlobalSiteViewPanel() {
59  		super();
60  		update();
61  	}
62  
63  	/*
64  	 * (non-Javadoc)
65  	 * 
66  	 * @see
67  	 * org.vaadin.vaadinvisualizations.VisualizationComponent.SelectionListener
68  	 * #selectionChanged(java.util.List)
69  	 */
70  	@Override
71  	public void selectionChanged(final List<String> selectedItems) {
72  		for (final AggregatedCountryData countryData : allCountries) {
73  			final String countryName = countryData.getCountry().getIso2Code();
74  
75  			if (selectedItems.contains(countryName)) {
76  				final ParamPageResource paramPageLink = new ParamPageResource(
77  						CountryPage.class, countryData.getCountry().getName(),
78  						countryData.getCountry().getIso2Code());
79  				navigateTo(CountryPage.class, paramPageLink.getParams());
80  			}
81  		}
82  	}
83  
84  	/**
85  	 * Update.
86  	 */
87  	public void update() {
88  		vert = new VerticalSplitPanel();
89  		setCaption(translateFromEnglish("Site Coverage"));
90  		vert.setSplitPosition(30, VerticalSplitPanel.UNITS_PIXELS);
91  
92  		gm = new GeoMap();
93  		gm.setSizeFull();
94  
95  		final DataContainer<AggregatedCountryData, String> dataContainer = getApplicationManager()
96  				.getDataContainer(AggregatedCountryData.class);
97  
98  		final List<String> countryNames = new ArrayList<String>();
99  		allCountries = dataContainer.getAll();
100 
101 		for (final AggregatedCountryData countryData : allCountries) {
102 			final String countryName = countryData.getCountry().getName();
103 			countryNames.add(countryName);
104 
105 			if (countryName.equals("Sweden")) {
106 				gm.add(countryData.getCountry().getIso2Code(), 3,
107 						translateFromEnglish(countryName));
108 			} else if (countryData.getType().equals(
109 					CountryMetaDataType.ITSYOUR_PARLIAMENT)) {
110 				gm.add(countryData.getCountry().getIso2Code(), 2,
111 						translateFromEnglish(countryName));
112 			} else {
113 				gm.add(countryData.getCountry().getIso2Code(), 1,
114 						translateFromEnglish(countryName));
115 			}
116 		}
117 
118 		final ListSelect targetSelect = new ListSelect(
119 				translateFromEnglish("Please select target Country"),
120 				countryNames);
121 		targetSelect.setImmediate(true);
122 		targetSelect.addListener(this);
123 		vert.addComponent(targetSelect);
124 
125 		gm.setOption("dataMode", "regions");
126 		gm.setOption("width", "100%");
127 		gm.setOption("height", "100%");
128 		gm.addListener(this);
129 		gm.setImmediate(true);
130 		vert.addComponent(gm);
131 		gm.setSizeFull();
132 		setContent(vert);
133 		vert.setSizeFull();
134 	}
135 
136 	/*
137 	 * (non-Javadoc)
138 	 * 
139 	 * @see
140 	 * com.vaadin.data.Property.ValueChangeListener#valueChange(com.vaadin.data
141 	 * .Property.ValueChangeEvent)
142 	 */
143 	@Override
144 	public void valueChange(final ValueChangeEvent event) {
145 		for (final AggregatedCountryData countryData : allCountries) {
146 			final String countryName = countryData.getCountry().getIso2Code();
147 
148 			if (event.getProperty().toString().equals(countryName)) {
149 				final ParamPageResource paramPageLink = new ParamPageResource(
150 						CountryPage.class, countryData.getCountry().getName(),
151 						countryData.getCountry().getIso2Code());
152 				navigateTo(CountryPage.class, paramPageLink.getParams());
153 			}
154 		}
155 	}
156 
157 }