View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package org.directdemocracyportal.democracy.web.views;
6   
7   import java.util.Comparator;
8   import java.util.List;
9   
10  import org.directdemocracyportal.democracy.model.core.GroupAgent;
11  import org.directdemocracyportal.democracy.model.world.Election;
12  import org.directdemocracyportal.democracy.model.world.Government;
13  import org.directdemocracyportal.democracy.model.world.Organisation;
14  import org.directdemocracyportal.democracy.model.world.PoliticalParty;
15  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder;
16  import org.directdemocracyportal.democracy.web.BeanLocator;
17  import org.directdemocracyportal.democracy.web.ImageConstants;
18  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder.MessageConstans;
19  import org.directdemocracyportal.democracy.web.action.ShowOrganisationAction;
20  import org.directdemocracyportal.democracy.web.views.comparator.LongComparator;
21  
22  import thinwire.ui.GridBox;
23  import thinwire.ui.Image;
24  import thinwire.ui.Label;
25  import thinwire.ui.Panel;
26  import thinwire.ui.TabFolder;
27  import thinwire.ui.TabSheet;
28  import thinwire.ui.layout.TableLayout;
29  
30  /***
31   * The Class GovernmentOverviewPanel.
32   */
33  public class GovernmentOverviewPanel extends Panel
34  {
35  
36      /***
37       * Instantiates a new government overview panel.
38       *
39       * @param government the government
40       * @param parties the parties
41       */
42      public GovernmentOverviewPanel(Government government, List<PoliticalParty> parties) {
43          setLayout(new TableLayout(new double[][] { { 0 }, // Column Widths
44                  { ImageConstants.BUTTON_SIZE, 0 } }, // Row Heights
45                  1, // Margin around edge of container
46                  5)); // Spacing between cells
47  
48          Image img = new Image(ImageConstants.GOVERNMENT_BUTTON);
49          img.setSize(ImageConstants.BUTTON_SIZE, ImageConstants.BUTTON_SIZE);
50          img.setLimit("0,0,l,c");
51          getChildren().add(img);
52  
53          TabFolder tabFolder = createTabFolder(government,parties);
54          tabFolder.setLimit("0,1,1,1");
55          getChildren().add(tabFolder);
56      }
57  
58      /***
59       * Creates the tab folder.
60       *
61       * @param government the government
62       * @param parties the parties
63       * @return the tab folder
64       */
65      private TabFolder createTabFolder(Government government, List<PoliticalParty> parties) {
66          TabSheet orgStructure = createOrgStructureTabSheet(government);
67          TabSheet partiesInPower = createPartiesInPowerTabSheet(government,parties);
68          TabSheet elections = createElectionsTabSheet(government);
69  
70          TabFolder tabFolder = new TabFolder();
71          tabFolder.getChildren().add(orgStructure);
72          tabFolder.getChildren().add(partiesInPower);
73          tabFolder.getChildren().add(elections);
74  
75          tabFolder.setCurrentIndex(0);
76          return tabFolder;
77      }
78  
79      /***
80       * Creates the parties in power tab sheet.
81       *
82       * @param government the government
83       * @param parties the parties
84       * @return the tab sheet
85       */
86      private TabSheet createPartiesInPowerTabSheet(Government government, List<PoliticalParty> parties) {
87          TabSheet members = new TabSheet();
88  
89          members.setText(ApplicationMessageHolder
90                  .getMessage(MessageConstans.PARTIES_IN_POWER));
91          members.setImage(ImageConstants.POLITICAL_PARTY_ICON);
92  
93          members.setLayout(new TableLayout(new double[][] { { 0 }, // Column
94                  // Widths
95                  { 20, 0 } }, // Row Heights
96                  1, // Margin around edge of container
97                  5)); // Spacing between cells
98  
99          Label nameLabel = new Label(ApplicationMessageHolder
100                 .getMessage(MessageConstans.PARTIES_IN_POWER)
101                 + government.getName());
102         nameLabel.setLimit("0,0");
103         members.getChildren().add(nameLabel);
104 
105         GridBox orgBox = getPartyGridBox(government,parties);
106         orgBox.setLimit("0,1");
107         members.getChildren().add(orgBox);
108 
109         return members;
110     }
111 
112     /***
113      * Creates the elections tab sheet.
114      *
115      * @param government the government
116      * @return the tab sheet
117      */
118     private TabSheet createElectionsTabSheet(Government government) {
119         TabSheet members = new TabSheet();
120 
121         members.setText("Elections");
122 
123         members.setLayout(new TableLayout(new double[][] { { 0 }, // Column
124                 // Widths
125                 { 20, 0 } }, // Row Heights
126                 1, // Margin around edge of container
127                 5)); // Spacing between cells
128 
129         Label nameLabel = new Label(ApplicationMessageHolder
130                 .getMessage(MessageConstans.ELECTIONS) + government.getName());
131         nameLabel.setLimit("0,0");
132         members.getChildren().add(nameLabel);
133 
134         GridBox orgBox = getElectionsGridBox(government);
135         orgBox.setLimit("0,1");
136         members.getChildren().add(orgBox);
137 
138         return members;
139     }
140 
141     /***
142      * Gets the elections grid box.
143      *
144      * @param government the government
145      * @return the elections grid box
146      */
147     private GridBox getElectionsGridBox(Government government) {
148         GridBox gridBox = new GridBox();
149         gridBox.setVisibleHeader(true);
150 
151         GridBox.Column nameHeader = new GridBox.Column();
152         nameHeader.setName(ApplicationMessageHolder
153                 .getMessage(MessageConstans.NAME));
154         gridBox.getColumns().add(nameHeader);
155 
156         GridBox.Column positionerHeader = new GridBox.Column();
157         positionerHeader.setName(ApplicationMessageHolder
158                 .getMessage(MessageConstans.TYPE));
159         gridBox.getColumns().add(positionerHeader);
160 
161         for (Election election : government.getElections()) {
162             gridBox.getRows().add(
163                     new GridBox.Row(election.getName(), election.getType()));
164         }
165         return gridBox;
166     }
167 
168     /***
169      * Creates the org structure tab sheet.
170      *
171      * @param government the government
172      * @return the tab sheet
173      */
174     private TabSheet createOrgStructureTabSheet(Government government) {
175         TabSheet members = new TabSheet();
176 
177         members.setText(government.getName() + ApplicationMessageHolder
178                 .getMessage(MessageConstans.OVERVIEW));
179 
180         members.setLayout(new TableLayout(new double[][] { { 0 }, // Column
181                 // Widths
182                 { 20, 0 } }, // Row Heights
183                 1, // Margin around edge of container
184                 5)); // Spacing between cells
185 
186         Label nameLabel = new Label(ApplicationMessageHolder
187                 .getMessage(MessageConstans.ORGANISATIONS));
188         nameLabel.setLimit("0,0");
189         members.getChildren().add(nameLabel);
190 
191         GridBox orgBox = getOrgGridBox(government);
192         orgBox.setLimit("0,1");
193         members.getChildren().add(orgBox);
194 
195         return members;
196     }
197 
198     /***
199      * Gets the org grid box.
200      *
201      * @param government the government
202      * @return the org grid box
203      */
204     private GridBox getOrgGridBox(Government government) {
205         GridBox gridBox = new GridBox();
206         gridBox.addActionListener(ACTION_CLICK, BeanLocator
207                 .getApplicationActionListener());
208         gridBox.setVisibleHeader(true);
209 
210         GridBox.Column nameHeader = new GridBox.Column();
211         nameHeader.setName("Name");
212         gridBox.getColumns().add(nameHeader);
213 
214         GridBox.Column orgTypeHeader = new GridBox.Column();
215         orgTypeHeader.setName("Organisation Type");
216         gridBox.getColumns().add(orgTypeHeader);
217 
218         GridBox.Column numberPeopleHeader = new GridBox.Column();
219         numberPeopleHeader.setName("Antal Medlemmar");
220         gridBox.getColumns().add(numberPeopleHeader);
221 
222         for (GroupAgent groupAgent : government.getChildren()) {
223             Organisation organisation = (Organisation) groupAgent;
224             GridBox.Row row = new GridBox.Row(organisation
225                     .getName(), organisation.getOrganisationType(),
226                     organisation.getNumberOfMembers());
227             row.setUserObject(new ShowOrganisationAction(organisation.getId()));
228             gridBox.getRows().add(row);
229         }
230         return gridBox;
231     }
232 
233     /***
234      * Gets the party grid box.
235      *
236      * @param government the government
237      * @param parties the parties
238      * @return the party grid box
239      */
240     @SuppressWarnings("unchecked")
241 	private GridBox getPartyGridBox(Government government, List<PoliticalParty> parties) {
242         GridBox gridBox = new GridBox();
243         gridBox.setVisibleHeader(true);
244 
245         GridBox.Column nameHeader = new GridBox.Column();
246         nameHeader.setName("Name");
247         gridBox.getColumns().add(nameHeader);
248 
249         GridBox.Column positionerHeader = new GridBox.Column();
250         positionerHeader.setName("Antal positioner");
251         gridBox.getColumns().add(positionerHeader);
252 
253         // TODO fix hack below
254         for (PoliticalParty party : parties) {
255             gridBox.getRows()
256                     .add(
257                             new GridBox.Row(party.getName(), party
258                                     .getNumberOfMembers()));
259         }
260         positionerHeader.setSortComparator((Comparator) new LongComparator());
261         positionerHeader
262                 .setSortOrder(thinwire.ui.GridBox.Column.SortOrder.DESC);
263         return gridBox;
264     }
265 }