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: SwedishBudgetResultPanel.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/SwedishBudgetResultPanel.java $
18   */
19  package com.hack23.cia.web.impl.ui.page.user;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.GregorianCalendar;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.vaadin.vaadinvisualizations.AnnotatedTimeLine;
30  import org.vaadin.vaadinvisualizations.AnnotatedTimeLineEntry;
31  
32  import com.google.api.translate.Language;
33  import com.hack23.cia.model.internal.application.data.impl.OperationalStatementData;
34  import com.hack23.cia.service.api.Translator;
35  import com.hack23.cia.web.impl.ui.common.ApplicationUserStateHolder;
36  import com.hack23.cia.web.impl.ui.page.common.panel.AbstractPanel;
37  import com.vaadin.data.Property;
38  import com.vaadin.data.Property.ValueChangeEvent;
39  import com.vaadin.ui.NativeSelect;
40  import com.vaadin.ui.VerticalSplitPanel;
41  
42  /**
43   * The Class SwedishBudgetResultPanel.
44   */
45  public final class SwedishBudgetResultPanel extends AbstractPanel implements
46  		Property.ValueChangeListener {
47  
48  	/** The Constant serialVersionUID. */
49  	private static final long serialVersionUID = 1L;
50  
51  	/** The vert. */
52  	private final VerticalSplitPanel vert = new VerticalSplitPanel();
53  
54  	/** The timeline. */
55  	private AnnotatedTimeLine timeline;
56  
57  	/** The operation statements collection. */
58  	private final Collection<List<OperationalStatementData>> operationStatementsCollection;
59  
60  	/** The language to swedish map. */
61  	private final Map<String, String> languageToSwedishMap = new HashMap<String, String>();
62  
63  	/** The translator. */
64  	private final Translator translator;
65  
66  	/** The language. */
67  	private final Language language;
68  
69  	/**
70  	 * Instantiates a new swedish budget result panel.
71  	 */
72  	public SwedishBudgetResultPanel() {
73  		super();
74  		translator = getApplicationManager().getTranslator();
75  		language = getUserState().getGoogleApiLanguage();
76  		setCaption(translateFromEnglish("Budget/Cost & Operational Statements"));
77  
78  		vert.setSplitPosition(30, VerticalSplitPanel.UNITS_PIXELS);
79  		operationStatementsCollection = ApplicationUserStateHolder
80  				.getUserState().getApplicationManager()
81  				.getOperationalStatementDAO().getList();
82  
83  		final NativeSelect governmentAuthoritySelectBox = new NativeSelect(
84  				translateFromEnglish("Please select a Government Authority"));
85  
86  		final List<String> names = new ArrayList<String>();
87  		for (final List<OperationalStatementData> opList : operationStatementsCollection) {
88  			final String swedishName = opList.get(0).getName();
89  			final String englishName = translator.translateFromSwedish(
90  					swedishName, language);
91  
92  			languageToSwedishMap.put(englishName, swedishName);
93  
94  			names.add(englishName);
95  
96  		}
97  
98  		Collections.sort(names);
99  		for (final String name : names) {
100 			governmentAuthoritySelectBox.addItem(name);
101 		}
102 
103 		final List<OperationalStatementData> selectedOpStatements = operationStatementsCollection
104 				.iterator().next();
105 
106 		governmentAuthoritySelectBox.setNullSelectionAllowed(false);
107 		governmentAuthoritySelectBox.setValue(selectedOpStatements.get(0)
108 				.getName());
109 		governmentAuthoritySelectBox.setImmediate(true);
110 		governmentAuthoritySelectBox.addListener(this);
111 		vert.addComponent(governmentAuthoritySelectBox);
112 
113 		timeline = createAnnotatedTimeLine(selectedOpStatements);
114 		vert.addComponent(timeline);
115 		timeline.setSizeFull();
116 		setContent(vert);
117 		vert.setSizeFull();
118 	}
119 
120 	/**
121 	 * Creates the annotated time line.
122 	 * 
123 	 * @param selectedOpStatements
124 	 *            the selected op statements
125 	 * @return the annotated time line
126 	 */
127 	private AnnotatedTimeLine createAnnotatedTimeLine(
128 			final List<OperationalStatementData> selectedOpStatements) {
129 		final AnnotatedTimeLine atl = new AnnotatedTimeLine();
130 		atl.setOption("displayAnnotations", true);
131 		atl.setOption("wmode", "window");
132 		atl.setOption("width", "100%");
133 		atl.setOption("height", "100%");
134 
135 		atl.addLineLabel(translator.translateFromSwedish(selectedOpStatements
136 				.get(0).getName(), language)
137 				+ ", 1997-jan -- 2009-dec");
138 
139 		for (final OperationalStatementData operationalStatement : selectedOpStatements) {
140 			try {
141 				final ArrayList<AnnotatedTimeLineEntry> timeLineEntries = new ArrayList<AnnotatedTimeLineEntry>();
142 				timeLineEntries.add(new AnnotatedTimeLineEntry(
143 						operationalStatement.getStateSubsidyResult()
144 								.doubleValue(), "", "")); // Sold Pencils
145 				atl.add(new GregorianCalendar(operationalStatement.getYear(),
146 						operationalStatement.getMonth().intValue(), 1),
147 						timeLineEntries);
148 
149 			} catch (final Exception e) {
150 				LOGGER.warn("Add msg", e);
151 			}
152 		}
153 
154 		return atl;
155 	}
156 
157 	/**
158 	 * Find by name.
159 	 * 
160 	 * @param operationStatementsCollection2
161 	 *            the operation statements collection2
162 	 * @param property
163 	 *            the property
164 	 * @return the list
165 	 */
166 	private List<OperationalStatementData> findByName(
167 			final Collection<List<OperationalStatementData>> operationStatementsCollection2,
168 			final String property) {
169 
170 		for (final List<OperationalStatementData> stmt : operationStatementsCollection2) {
171 			if (stmt.iterator().next().getName().equals(property)) {
172 				return stmt;
173 			}
174 		}
175 		return null;
176 	}
177 
178 	/*
179 	 * (non-Javadoc)
180 	 * 
181 	 * @see
182 	 * com.vaadin.data.Property.ValueChangeListener#valueChange(com.vaadin.data
183 	 * .Property.ValueChangeEvent)
184 	 */
185 	@Override
186 	public void valueChange(final ValueChangeEvent event) {
187 		if (event != null) {
188 			vert.removeComponent(timeline);
189 			timeline = createAnnotatedTimeLine(findByName(
190 					operationStatementsCollection,
191 					languageToSwedishMap.get(event.getProperty().toString())));
192 			vert.addComponent(timeline);
193 			timeline.setSizeFull();
194 		}
195 	}
196 
197 }