View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.ui.users.editUser;
5   
6   import java.util.Arrays;
7   import java.util.HashSet;
8   import java.util.List;
9   import java.util.Set;
10  
11  import org.appfuse.webapp.client.proxies.RoleProxy;
12  
13  import com.google.gwt.cell.client.AbstractCell;
14  import com.google.gwt.cell.client.Cell;
15  import com.google.gwt.cell.client.CheckboxCell;
16  import com.google.gwt.cell.client.CompositeCell;
17  import com.google.gwt.cell.client.FieldUpdater;
18  import com.google.gwt.cell.client.HasCell;
19  import com.google.gwt.dom.client.Element;
20  import com.google.gwt.editor.client.Editor;
21  import com.google.gwt.editor.client.IsEditor;
22  import com.google.gwt.editor.client.LeafValueEditor;
23  import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
24  import com.google.gwt.safehtml.shared.SafeHtmlUtils;
25  import com.google.gwt.user.cellview.client.CellList;
26  import com.google.gwt.user.client.ui.CheckBox;
27  
28  /**
29   * @author ivangsa
30   *
31   */
32  public class RolesListBox extends CellList<RoleProxy> implements Editor<Set<RoleProxy>>, IsEditor<LeafValueEditor<Set<RoleProxy>>>, LeafValueEditor<Set<RoleProxy>> {
33  
34      private final RoleCheckboxCell roleCheckboxCell;
35  
36      public RolesListBox() {
37          this(new RoleCheckboxCell());
38          setSelectionModel(null);
39      }
40  
41      private RolesListBox(RoleCheckboxCell roleCheckboxCell) {
42          super(new RoleCell(Arrays.asList(roleCheckboxCell, new RoleNameCell())));
43          this.roleCheckboxCell = roleCheckboxCell;
44      }
45  
46      @Override
47      public LeafValueEditor<Set<RoleProxy>> asEditor() {
48          return this;
49      }
50  
51      @Override
52      public void setValue(Set<RoleProxy> value) {
53          roleCheckboxCell.setRolesSet(value);
54          this.redraw();
55      }
56  
57      @Override
58      public Set<RoleProxy> getValue() {
59          return roleCheckboxCell.getRolesSet();
60      }
61  
62      public void setReadonly(boolean readonly) {
63          roleCheckboxCell.setReadonly(readonly);
64      }
65  }
66  
67  class RoleCell extends CompositeCell<RoleProxy> {
68  
69      public RoleCell(List<HasCell<RoleProxy, ?>> hasCells) {
70          super(hasCells);
71      }
72  
73      @Override
74      public void render(Context context, RoleProxy value, SafeHtmlBuilder sb) {
75          sb.appendHtmlConstant("<table><tbody><tr>");
76          super.render(context, value, sb);
77          sb.appendHtmlConstant("</tr></tbody></table>");
78      }
79  
80      @Override
81      protected Element getContainerElement(Element parent) {
82          // Return the first TR element in the table.
83          return parent.getFirstChildElement().getFirstChildElement().getFirstChildElement();
84      }
85  
86      @Override
87      protected <X> void render(Context context, RoleProxy value, SafeHtmlBuilder sb, HasCell<RoleProxy, X> hasCell) {
88          Cell<X> cell = hasCell.getCell();
89          sb.appendHtmlConstant("<td>");
90          cell.render(context, hasCell.getValue(value), sb);
91          sb.appendHtmlConstant("</td>");
92      }
93  }
94  
95  class RoleCheckboxCell implements HasCell<RoleProxy, Boolean> {
96  
97      private boolean readonly = false;
98      private final Set<RoleProxy> rolesSet = new HashSet<RoleProxy>();
99      private final CheckboxCell cell = new CheckboxCell(true, true);
100     private final AbstractCell<Boolean> readonlyCell = new AbstractCell<Boolean>() {
101         @Override
102         public void render(com.google.gwt.cell.client.Cell.Context context, Boolean value, SafeHtmlBuilder sb) {
103             CheckBox checkBox = new CheckBox();
104             checkBox.setValue(value);
105             checkBox.setEnabled(false);
106             sb.append(SafeHtmlUtils.fromTrustedString(checkBox.toString()));
107 
108         }
109     };
110 
111     public Cell<Boolean> getCell() {
112         if (readonly) {
113             return readonlyCell;
114         } else {
115             return cell;
116         }
117     }
118 
119     public FieldUpdater<RoleProxy, Boolean> getFieldUpdater() {
120         if (readonly) {
121             return null;
122         } else {
123             return new FieldUpdater<RoleProxy, Boolean>() {
124 
125                 @Override
126                 public void update(int index, RoleProxy role, Boolean checked) {
127                     if (checked) {
128                         rolesSet.add(role);
129                     } else {
130                         remove(role);
131                     }
132                 }
133             };
134         }
135     }
136 
137     public void setReadonly(boolean readonly) {
138         this.readonly = readonly;
139     }
140 
141     public Boolean getValue(RoleProxy role) {
142         return contains(role);
143     }
144 
145     public Set<RoleProxy> getRolesSet() {
146         return rolesSet;
147     }
148 
149     public void setRolesSet(Set<RoleProxy> rolesSet) {
150         this.rolesSet.clear();
151         if (rolesSet != null) {
152             this.rolesSet.addAll(rolesSet);
153         }
154     }
155 
156     private void remove(RoleProxy role) {
157         for (RoleProxy roleProxy : rolesSet) {
158             if (roleProxy.getId().equals(role.getId())) {
159                 rolesSet.remove(roleProxy);
160                 return;
161             }
162         }
163     }
164 
165     private boolean contains(RoleProxy role) {
166         for (RoleProxy roleProxy : rolesSet) {
167             if (roleProxy.getId().equals(role.getId())) {
168                 return true;
169             }
170         }
171         return false;
172     }
173 }
174 
175 class RoleNameCell implements HasCell<RoleProxy, RoleProxy> {
176 
177     private Cell<RoleProxy> roleNameCell = new AbstractCell<RoleProxy>() {
178         @Override
179         public void render(com.google.gwt.cell.client.Cell.Context context, RoleProxy value, SafeHtmlBuilder sb) {
180             sb.append(SafeHtmlUtils.fromString(value.getName()));
181         }
182     };
183 
184     public Cell<RoleProxy> getCell() {
185         return roleNameCell;
186     }
187 
188     public FieldUpdater<RoleProxy, RoleProxy> getFieldUpdater() {
189         return null;
190     }
191 
192     public RoleProxy getValue(RoleProxy object) {
193         return object;
194     }
195 }