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 org.directdemocracyportal.democracy.model.application.OnlinePoliticalParty;
8   import org.directdemocracyportal.democracy.model.application.User;
9   import org.directdemocracyportal.democracy.model.core.GroupAgent;
10  import org.directdemocracyportal.democracy.model.world.Organisation;
11  import org.directdemocracyportal.democracy.model.world.Person;
12  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder;
13  import org.directdemocracyportal.democracy.web.BeanLocator;
14  import org.directdemocracyportal.democracy.web.UserState;
15  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder.MessageConstans;
16  import org.directdemocracyportal.democracy.web.action.JoinPoliticalPartyAction;
17  import org.directdemocracyportal.democracy.web.action.ShowOnlinePoliticalPartyAction;
18  
19  import thinwire.ui.Component;
20  import thinwire.ui.GridBox;
21  import thinwire.ui.Label;
22  import thinwire.ui.Menu;
23  import thinwire.ui.Panel;
24  import thinwire.ui.TabFolder;
25  import thinwire.ui.TabSheet;
26  import thinwire.ui.layout.TableLayout;
27  
28  /***
29   * The Class OnlinePoliticalPartyOverviewPanel.
30   */
31  public class OnlinePoliticalPartyOverviewPanel extends Panel
32  {
33  
34      /*** The party. */
35      private final OnlinePoliticalParty party;
36  
37      /***
38       * Instantiates a new online political party overview panel.
39       *
40       * @param party the party
41       */
42      public OnlinePoliticalPartyOverviewPanel(OnlinePoliticalParty party) {
43          this.party = party;
44          setLayout(new TableLayout(new double[][] { { 0 }, // Column Widths
45                  { 20, 0 } }, // Row Heights
46                  1, // Margin around edge of container
47                  5)); // Spacing between cells
48  
49          Menu menu = createMenu();
50          menu.setLimit("0,0");
51          getChildren().add(menu);
52  
53          TabFolder tabFolder = createTabFolder(party);
54          tabFolder.setLimit("0,1,1,1");
55          getChildren().add(tabFolder);
56      }
57  
58      /***
59       * Creates the tab folder.
60       *
61       * @param organisation the organisation
62       * @return the tab folder
63       */
64      private TabFolder createTabFolder(Organisation organisation) {
65          TabSheet orgStructure = createOrgStructureTabSheet(organisation);
66          TabSheet members = createMembersTabSheet(organisation);
67  
68          TabFolder tabFolder = new TabFolder();
69          tabFolder.getChildren().add(orgStructure);
70          tabFolder.getChildren().add(members);
71  
72          tabFolder.setCurrentIndex(0);
73          return tabFolder;
74      }
75  
76      /***
77       * Creates the members tab sheet.
78       *
79       * @param organisation the organisation
80       * @return the tab sheet
81       */
82      private TabSheet createMembersTabSheet(Organisation organisation) {
83          TabSheet members = new TabSheet();
84  
85          members.setText(organisation.getName() + ApplicationMessageHolder
86                  .getMessage(MessageConstans.MEMBERS));
87  
88          members.setLayout(new TableLayout(new double[][] { { 0 }, // Column
89                  // Widths
90                  { 20, 0 } }, // Row Heights
91                  1, // Margin around edge of container
92                  5)); // Spacing between cells
93  
94          Label nameLabel = new Label(ApplicationMessageHolder
95                  .getMessage(MessageConstans.MEMBERS));
96          nameLabel.setLimit("0,0");
97          members.getChildren().add(nameLabel);
98  
99          GridBox orgBox = getMemberGridBox(organisation);
100         orgBox.setLimit("0,1");
101         members.getChildren().add(orgBox);
102 
103         return members;
104     }
105 
106     /***
107      * Gets the member grid box.
108      *
109      * @param organisation the organisation
110      * @return the member grid box
111      */
112     private GridBox getMemberGridBox(Organisation organisation) {
113         GridBox gridBox = new GridBox();
114         gridBox.setVisibleHeader(true);
115 
116         GridBox.Column nameHeader = new GridBox.Column();
117         nameHeader.setName(ApplicationMessageHolder
118                 .getMessage(MessageConstans.HEADER_NAME));
119         gridBox.getColumns().add(nameHeader);
120 
121         GridBox.Column rolesHeader = new GridBox.Column();
122         rolesHeader.setName(ApplicationMessageHolder
123                 .getMessage(MessageConstans.ROLES));
124         gridBox.getColumns().add(rolesHeader);
125 
126         GridBox.Column partyHeader = new GridBox.Column();
127         partyHeader.setName(ApplicationMessageHolder
128                 .getMessage(MessageConstans.POLITICAL_PARTY));
129         gridBox.getColumns().add(partyHeader);
130 
131         for (Person person : organisation.personsActive()) {
132             gridBox.getRows().add(
133                     new GridBox.Row(person.getName(), person.getRoleInOrg(
134                             organisation).getName(), person.getPoliticalParty()
135                             .getName()));
136         }
137         return gridBox;
138     }
139 
140     /***
141      * Creates the org structure tab sheet.
142      *
143      * @param organisation the organisation
144      * @return the tab sheet
145      */
146     private TabSheet createOrgStructureTabSheet(Organisation organisation) {
147         TabSheet orgStructure = new TabSheet();
148 
149         orgStructure.setText(organisation.getName() + ApplicationMessageHolder
150                 .getMessage(MessageConstans.ORGANISATIONS));
151 
152         orgStructure.setLayout(new TableLayout(new double[][] { { 0 }, // Column
153                 // Widths
154                 { 20, 0 } }, // Row Heights
155                 1, // Margin around edge of container
156                 5)); // Spacing between cells
157 
158         Label nameLabel = new Label(ApplicationMessageHolder
159                 .getMessage(MessageConstans.ORGANISATIONS));
160         nameLabel.setLimit("0,0");
161         orgStructure.getChildren().add(nameLabel);
162 
163         GridBox orgBox = getOrgGridBox(organisation);
164         orgBox.setLimit("0,1");
165         orgStructure.getChildren().add(orgBox);
166 
167         return orgStructure;
168     }
169 
170     /***
171      * Gets the org grid box.
172      *
173      * @param organisation the organisation
174      * @return the org grid box
175      */
176     private GridBox getOrgGridBox(Organisation organisation) {
177         GridBox gridBox = new GridBox();
178         gridBox.addActionListener(ACTION_CLICK, BeanLocator
179                 .getApplicationActionListener());
180         gridBox.setVisibleHeader(true);
181 
182         GridBox.Column nameHeader = new GridBox.Column();
183         nameHeader.setName(ApplicationMessageHolder
184                 .getMessage(MessageConstans.HEADER_NAME));
185         gridBox.getColumns().add(nameHeader);
186 
187         GridBox.Column orgTypeHeader = new GridBox.Column();
188         orgTypeHeader.setName(ApplicationMessageHolder
189                 .getMessage(MessageConstans.ORGANISATION_TYPE));
190         gridBox.getColumns().add(orgTypeHeader);
191 
192         GridBox.Column numberPeopleHeader = new GridBox.Column();
193         numberPeopleHeader.setName(ApplicationMessageHolder
194                 .getMessage(MessageConstans.NUMBER_OF_MEMBERS));
195         gridBox.getColumns().add(numberPeopleHeader);
196 
197         for (GroupAgent groupAgent : organisation.getChildren()) {
198             Organisation orgUnit = (Organisation) groupAgent;
199             GridBox.Row row = new GridBox.Row(orgUnit.getName(), orgUnit
200                     .getOrganisationType(), orgUnit
201                     .getNumberOfMembers());
202             row.setUserObject(new ShowOnlinePoliticalPartyAction(orgUnit.getId()));
203 
204             gridBox.getRows().add(
205                     row);
206         }
207         return gridBox;
208     }
209 
210     /***
211      * Creates the menu.
212      *
213      * @return the menu
214      */
215     public Menu createMenu() {
216         Menu menu = new Menu();
217 
218         User user = UserState.user.get();
219         if (user != null && (!party.playsRoleInOrganisation(user.getPerson()))) {
220             Menu.Item joinPartyItem = new Menu.Item(ApplicationMessageHolder
221                     .getMessage(MessageConstans.BUTTON_JOIN_PARTY));
222             joinPartyItem.setUserObject(new JoinPoliticalPartyAction(party.getId()));
223             menu.getRootItem().getChildren().add(joinPartyItem);
224         }
225 
226         menu.addActionListener(Component.ACTION_CLICK, BeanLocator
227                 .getApplicationActionListener());
228 
229         return menu;
230     }
231 }