001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/mailtool/tags/sakai_2-4-1/mailtool/src/java/org/sakaiproject/tool/mailtool/SideBySideModel.java $
003: * $Id: SideBySideModel.java 27662 2007-03-22 19:44:57Z kimsooil@bu.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.mailtool;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.faces.model.SelectItem;
027:
028: /**
029: * SideBySideModel, just slightly modified for group-awareness by kimsooil@bu.edu
030: *
031: * @author sgithens
032: *
033: */
034: public class SideBySideModel {
035:
036: /** This will be the 'object' we hook up to in
037: *
038: * <h:selectManyListbox value="ListBoxEntry"
039: */
040: public class ListboxEntry {
041: /* This could be a EmailGroup, or EmailUser, or
042: * String for a dummy divider
043: */
044: int m_position;
045:
046: Object m_data = null;
047: String m_label = "";
048:
049: boolean m_sourceSelected = false;
050: boolean m_sinkSelected = false;
051: boolean m_divider = false;
052: boolean m_isgroup = false;
053: boolean m_isuser = false;
054:
055: public int getPosition() {
056: return m_position;
057: }
058:
059: public ListboxEntry(int position) {
060: m_position = position;
061: }
062:
063: public String getLabel() {
064: return m_label;
065: }
066:
067: public void setData(EmailGroup group) {
068: m_data = group;
069: m_label = "All " + group.getEmailrole().getRoleplural();
070:
071: String rtype = group.getEmailrole().roletype;
072:
073: if (rtype.equals("group")) {
074: m_label += "(G)";
075: } else if (rtype.equals("section")) {
076: m_label += "(S)";
077: }
078:
079: // String rolename=group.getEmailrole().getRoleplural();
080: // m_label +=(rolename.equals("maintain") || rolename.equals("access")) ? " Users" : "s";
081: m_isgroup = true;
082: }
083:
084: public void setData(EmailUser user) {
085: m_data = user;
086: m_label = user.getDisplayname();
087: m_isuser = true;
088: }
089:
090: public void setDataAsDivider() {
091: m_data = "divider";
092: m_label = "-------------";
093: m_divider = true;
094: }
095:
096: public void setDataAsDivider(String more) {
097: m_data = "divider";
098: m_label = "-------------" + more;
099: m_divider = true;
100: }
101:
102: public boolean isDivider() {
103: return m_divider;
104: }
105:
106: public boolean isGroup() {
107: return m_isgroup;
108: }
109:
110: public boolean isUser() {
111: return m_isuser;
112: }
113:
114: public Object getData() {
115: return m_data;
116: }
117:
118: public void setSourceSelected(boolean selected) {
119: m_sourceSelected = selected;
120: }
121:
122: public boolean isSourceSelected() {
123: return m_sourceSelected;
124: }
125:
126: public void setSinkSelected(boolean selected) {
127: m_sinkSelected = selected;
128: }
129:
130: public boolean isSinkSelected() {
131: return m_sinkSelected;
132: }
133:
134: }
135:
136: protected List /* ListboxEntry */m_listboxdata = null;
137:
138: public List /* EmailUser */getSelectedUsers() {
139: List users = new ArrayList();
140:
141: for (Iterator i = m_listboxdata.iterator(); i.hasNext();) {
142: ListboxEntry entry = (ListboxEntry) i.next();
143: if ((entry.isGroup() == true)
144: && (entry.isSinkSelected() == true)) {
145: EmailGroup group = (EmailGroup) entry.getData();
146: users.addAll(group.getEmailusers());
147: } else if ((entry.isUser() == true)
148: && (entry.isSinkSelected() == true)) {
149: users.add(entry.getData());
150: }
151: }
152:
153: return users;
154: }
155:
156: public SideBySideModel(List /* EmailGroup */groups) {
157: m_listboxdata = new ArrayList();
158:
159: int count = 0;
160: for (Iterator i = groups.iterator(); i.hasNext();) {
161: EmailGroup group = (EmailGroup) i.next();
162: ListboxEntry entry = new ListboxEntry(count);
163: entry.setData(group);
164: entry.setSourceSelected(true);
165: m_listboxdata.add(entry);
166: count++;
167: }
168: /*
169: ListboxEntry diventry = new ListboxEntry(count);
170: diventry.setDataAsDivider();
171: diventry.setSourceSelected(true);
172: m_listboxdata.add(diventry);
173: count++;
174: */
175: for (Iterator i = groups.iterator(); i.hasNext();) {
176: EmailGroup group = (EmailGroup) i.next();
177:
178: String rtype = group.getEmailrole().roletype;
179: String rname = group.getEmailrole().getRolesingular();
180: ListboxEntry diventry = new ListboxEntry(count);
181: diventry.setDataAsDivider("[" + rname + "]");
182: diventry.setSourceSelected(true);
183: m_listboxdata.add(diventry);
184: count++;
185:
186: List users = group.getEmailusers();
187: for (Iterator j = users.iterator(); j.hasNext();) {
188: EmailUser user = (EmailUser) j.next();
189: ListboxEntry entry = new ListboxEntry(count);
190: entry.setData(user);
191: entry.setSourceSelected(true);
192: m_listboxdata.add(entry);
193: count++;
194:
195: }
196: /*
197: if (i.hasNext() == true)
198: {
199: ListboxEntry entry = new ListboxEntry(count);
200: entry.setDataAsDivider();
201: entry.setSourceSelected(true);
202: m_listboxdata.add(entry);
203: count++;
204: }
205: */
206: }
207: }
208:
209: public List /* SelectItem */getSourceSelectItems() {
210: List items = new ArrayList();
211:
212: for (Iterator i = m_listboxdata.iterator(); i.hasNext();) {
213: ListboxEntry entry = (ListboxEntry) i.next();
214: if (entry.isSourceSelected() == true) {
215: SelectItem item = new SelectItem();
216: item.setLabel(entry.getLabel());
217: item.setValue(Integer.toString(entry.getPosition()));
218:
219: items.add(item);
220: }
221: }
222:
223: return items;
224: }
225:
226: public List /* SelectItem */getSinkSelectItems() {
227: List items = new ArrayList();
228:
229: for (Iterator i = m_listboxdata.iterator(); i.hasNext();) {
230: ListboxEntry entry = (ListboxEntry) i.next();
231: if (entry.isSinkSelected() == true) {
232: SelectItem item = new SelectItem();
233: item.setLabel(entry.getLabel());
234: item.setValue(Integer.toString(entry.getPosition()));
235:
236: items.add(item);
237: }
238: }
239:
240: return items;
241: }
242:
243: protected List m_sinkSelectedObjects = new ArrayList();
244:
245: public void setSinkSelectedObjects(List /* String */items) {
246: m_sinkSelectedObjects = items;
247: }
248:
249: protected List m_sourceSelectedObjects = new ArrayList();
250:
251: public void setSourceSelectedObjects(List /* String */items) {
252: m_sourceSelectedObjects = items;
253: }
254:
255: public void actionSource() {
256: for (Iterator i = m_sourceSelectedObjects.iterator(); i
257: .hasNext();) {
258: Integer integer = new Integer((String) i.next());
259: ListboxEntry entry = (ListboxEntry) this .m_listboxdata
260: .get(integer.intValue());
261: if (entry.isDivider() == false) {
262: entry.setSourceSelected(false);
263: entry.setSinkSelected(true);
264: }
265: }
266: }
267:
268: public void actionSink() {
269: for (Iterator i = m_sinkSelectedObjects.iterator(); i.hasNext();) {
270: Integer integer = new Integer((String) i.next());
271: ListboxEntry entry = (ListboxEntry) this .m_listboxdata
272: .get(integer.intValue());
273: entry.setSourceSelected(true);
274: entry.setSinkSelected(false);
275: }
276: }
277: }
|