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.List;
8   
9   import org.directdemocracyportal.democracy.model.application.Event;
10  import org.directdemocracyportal.democracy.model.application.GovernmentPortal;
11  import org.directdemocracyportal.democracy.model.application.RoleAction;
12  import org.directdemocracyportal.democracy.model.application.User;
13  import org.directdemocracyportal.democracy.model.application.UserRole;
14  import org.directdemocracyportal.democracy.model.core.Role;
15  import org.directdemocracyportal.democracy.model.world.PoliticalParty;
16  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder;
17  import org.directdemocracyportal.democracy.web.ImageConstants;
18  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder.MessageConstans;
19  
20  import thinwire.ui.GridBox;
21  import thinwire.ui.Image;
22  import thinwire.ui.Label;
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 UserProfilePanel.
30   */
31  public class UserProfilePanel extends Panel
32  {
33  
34      /***
35       * Instantiates a new user profile panel.
36       *
37       * @param user the user
38       * @param parties the parties
39       */
40      public UserProfilePanel(User user,List<PoliticalParty> parties) {
41          super();
42          setLayout(new TableLayout(new double[][] { { 0 }, // Column Widths
43                  { 32, 0 } }, // Row Heights
44                  1, // Margin around edge of container
45                  5)); // Spacing between cells
46  
47          Image img = new Image(ImageConstants.USER_HOME_BUTTON);
48          img.setSize(ImageConstants.BUTTON_SIZE, ImageConstants.BUTTON_SIZE);
49          img.setLimit("0,0,l,c");
50          getChildren().add(img);
51  
52          TabFolder tabFolder = createTabFolder(user,parties);
53          tabFolder.setLimit("0,1,1,1");
54          getChildren().add(tabFolder);
55      }
56  
57      /***
58       * Creates the tab folder.
59       *
60       * @param user the user
61       * @param parties the parties
62       * @return the tab folder
63       */
64      private TabFolder createTabFolder(User user,List<PoliticalParty> parties) {
65          TabSheet members = createMemberTabSheet(user);
66  
67          TabFolder tabFolder = new TabFolder();
68          tabFolder.getChildren().add(members);
69  
70          GovernmentPortal nationalGovernmentPortal = user
71                  .getNationalGovernmentPortal();
72          if (nationalGovernmentPortal != null) {
73              tabFolder.getChildren().add(
74                      createGovernmentPortalTabSheet(nationalGovernmentPortal,parties));
75          }
76          GovernmentPortal regionalGovernmentPortal = user
77                  .getRegionalGovernmentPortal();
78          if (regionalGovernmentPortal != null) {
79              tabFolder.getChildren().add(
80                      createGovernmentPortalTabSheet(regionalGovernmentPortal,parties));
81          }
82          GovernmentPortal localGovernmentPortal = user
83                  .getLocalGovernmentPortal();
84          if (localGovernmentPortal != null) {
85              tabFolder.getChildren().add(
86                      createGovernmentPortalTabSheet(localGovernmentPortal,parties));
87          }
88  
89          tabFolder.setCurrentIndex(0);
90          return tabFolder;
91      }
92  
93      /***
94       * Creates the member tab sheet.
95       *
96       * @param user the user
97       * @return the tab sheet
98       */
99      private TabSheet createMemberTabSheet(User user) {
100         TabSheet members = new TabSheet();
101 
102         members.setText(user.getUsername() + ApplicationMessageHolder
103                 .getMessage(MessageConstans.PROFILE));
104         members.setLayout(new TableLayout(new double[][] { { 0 }, // Column
105                 // Widths
106                 { 20,0,20, 0, 20, 0 } }, // Row Heights
107                 1, // Margin around edge of container
108                 5)); // Spacing between cells
109 
110 
111         Label informationLabel = new Label(ApplicationMessageHolder
112                 .getMessage(MessageConstans.INFORMATION));
113         informationLabel.setLimit("0,0");
114         members.getChildren().add(informationLabel);
115 
116         GridBox informationBox = getInformationGridBox(user);
117         informationBox.setLimit("0,1");
118         members.getChildren().add(informationBox);
119 
120         Label roleLabel = new Label(ApplicationMessageHolder
121                 .getMessage(MessageConstans.ROLES));
122         roleLabel.setLimit("0,2");
123         members.getChildren().add(roleLabel);
124 
125         GridBox roleBox = getRoleGridBox(user);
126         roleBox.setLimit("0,3");
127         members.getChildren().add(roleBox);
128 
129         Label actionLabel = new Label(ApplicationMessageHolder
130                 .getMessage(MessageConstans.RECENT_ACTIONS));
131         actionLabel.setLimit("0,4");
132         members.getChildren().add(actionLabel);
133 
134         GridBox eventBox = getEventGridBox(user);
135         eventBox.setLimit("0,5");
136         members.getChildren().add(eventBox);
137 
138         return members;
139     }
140 
141     /***
142      * Gets the information grid box.
143      *
144      * @param user the user
145      * @return the information grid box
146      */
147     private GridBox getInformationGridBox(User user) {
148         GridBox gridBox = new GridBox();
149         gridBox.setVisibleHeader(true);
150 
151         GridBox.Column usernameHeader = new GridBox.Column();
152         usernameHeader.setName(ApplicationMessageHolder
153                 .getMessage(MessageConstans.USERNAME));
154         gridBox.getColumns().add(usernameHeader);
155 
156         GridBox.Column nameHeader = new GridBox.Column();
157         nameHeader.setName(ApplicationMessageHolder
158                 .getMessage(MessageConstans.NAME));
159         gridBox.getColumns().add(nameHeader);
160 
161         GridBox.Column emailHeader = new GridBox.Column();
162         emailHeader.setName(ApplicationMessageHolder
163                 .getMessage(MessageConstans.EMAIL));
164         gridBox.getColumns().add(emailHeader);
165 
166         GridBox.Column accountNonExpiredHeader = new GridBox.Column();
167         accountNonExpiredHeader.setName(ApplicationMessageHolder
168                 .getMessage(MessageConstans.AccountNonExpired));
169         gridBox.getColumns().add(accountNonExpiredHeader);
170 
171         GridBox.Column accountNonLockedHeader = new GridBox.Column();
172         accountNonLockedHeader.setName(ApplicationMessageHolder
173                 .getMessage(MessageConstans.AccountNonLocked));
174         gridBox.getColumns().add(accountNonLockedHeader);
175 
176         GridBox.Column credentialsNonExpiredHeader = new GridBox.Column();
177         credentialsNonExpiredHeader.setName(ApplicationMessageHolder
178                 .getMessage(MessageConstans.CredentialsNonExpired));
179         gridBox.getColumns().add(credentialsNonExpiredHeader);
180 
181         GridBox.Column enabledHeader = new GridBox.Column();
182         enabledHeader.setName(ApplicationMessageHolder
183                 .getMessage(MessageConstans.Enabled));
184         gridBox.getColumns().add(enabledHeader);
185 
186         gridBox.getRows().add(new GridBox.Row(user.getUsername(),user.getPerson().getName(),user.getEmail(),user.isAccountNonExpired(),user.isAccountNonLocked(), user.isCredentialsNonExpired(),user.isEnabled()));
187 
188         return gridBox;
189     }
190 
191     /***
192      * Creates the government portal tab sheet.
193      *
194      * @param governmentPortal the government portal
195      * @param parties the parties
196      * @return the tab sheet
197      */
198     private TabSheet createGovernmentPortalTabSheet(
199             GovernmentPortal governmentPortal, List<PoliticalParty> parties) {
200         TabSheet members = new TabSheet();
201 
202         members.setText(governmentPortal.getName());
203         members.setImage(ImageConstants.GOVERNMENT_ICON);
204         members.setLayout(new TableLayout(new double[][] { { 0 }, // Column
205                 // Widths
206                 { 0 } }, // Row Heights
207                 1, // Margin around edge of container
208                 5)); // Spacing between cells
209 
210         GovernmentOverviewPanel governmentOverviewPanel = new GovernmentOverviewPanel(
211                 governmentPortal.getGovernment(),parties);
212         governmentOverviewPanel.setLimit("0,0");
213         members.getChildren().add(governmentOverviewPanel);
214         return members;
215     }
216 
217     /***
218      * Gets the role grid box.
219      *
220      * @param user the user
221      * @return the role grid box
222      */
223     private GridBox getRoleGridBox(User user) {
224         GridBox gridBox = new GridBox();
225         gridBox.setVisibleHeader(true);
226 
227         GridBox.Column roleHeader = new GridBox.Column();
228         roleHeader.setName(ApplicationMessageHolder
229                 .getMessage(MessageConstans.HEADER_ROLE));
230         gridBox.getColumns().add(roleHeader);
231 
232         GridBox.Column orgHeader = new GridBox.Column();
233         orgHeader.setName(ApplicationMessageHolder
234                 .getMessage(MessageConstans.ORGANISATION));
235         gridBox.getColumns().add(orgHeader);
236 
237         GridBox.Column actionHeader = new GridBox.Column();
238         actionHeader.setName(ApplicationMessageHolder
239                 .getMessage(MessageConstans.ALLOWED_ACTIONS));
240         gridBox.getColumns().add(actionHeader);
241 
242         for (Role role : user.getPerson().getRoles()) {
243 
244             StringBuilder allowedActions = new StringBuilder();
245             if (role instanceof UserRole) {
246                 UserRole userRole = (UserRole) role;
247 
248                 int actioNr = 0;
249                 for (RoleAction roleAction : userRole.getRoleActions()) {
250                     if (actioNr++ != 0) {
251                         allowedActions.append(",");
252                     }
253                     allowedActions.append(roleAction.getAction());
254                 }
255             }
256 
257             gridBox.getRows().add(
258                     new GridBox.Row(role.getName(), role.getGroupAgent()
259                             .getName(), allowedActions.toString()));
260         }
261         return gridBox;
262     }
263 
264     /***
265      * Gets the event grid box.
266      *
267      * @param user the user
268      * @return the event grid box
269      */
270     private GridBox getEventGridBox(User user) {
271         GridBox gridBox = new GridBox();
272         gridBox.setVisibleHeader(true);
273 
274         GridBox.Column timeHeader = new GridBox.Column();
275         timeHeader.setName(ApplicationMessageHolder
276                 .getMessage(MessageConstans.HEADER_TIME));
277         gridBox.getColumns().add(timeHeader);
278 
279         GridBox.Column actionHeader = new GridBox.Column();
280         actionHeader.setName(ApplicationMessageHolder
281                 .getMessage(MessageConstans.HEADER_ACTION));
282         gridBox.getColumns().add(actionHeader);
283 
284         for (Event event : user.getEvents()) {
285 
286             gridBox.getRows().add(
287                     new GridBox.Row(event.getTime(), event.getAction()));
288         }
289         return gridBox;
290     }
291 }