KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > MultiSelectSelectionModel


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.struts.util.LabelValueBean;
31
32 import com.sslexplorer.boot.PropertyList;
33 import com.sslexplorer.security.SessionInfo;
34
35
36 /**
37  * Model that maintains the selection state for a 'Multi Select' component
38  * given a 'value' containing the section items and the list of possible values
39  * from the datasource.
40  */

41 public class MultiSelectSelectionModel {
42     private List JavaDoc<LabelValueBean> availableValues;
43     private List JavaDoc<LabelValueBean> selectedValues;
44     private Map JavaDoc<String JavaDoc, LabelValueBean> availableMap;
45     private MultiSelectDataSource dataSource;
46     private PropertyList propertyList;
47
48     /**
49      * Constructor
50      *
51      * @param session session
52      * @param dataSource data source for available values
53      * @param propertyList selected values
54      */

55     public MultiSelectSelectionModel(SessionInfo session, MultiSelectDataSource dataSource, PropertyList propertyList) {
56         super();
57         this.dataSource = dataSource;
58         this.propertyList = propertyList;
59         rebuild(session);
60     }
61     
62     /**
63      * Select all values
64      *
65      * @param session values
66      */

67     public void selectAll(SessionInfo session) {
68         for(Iterator JavaDoc i = availableValues.iterator(); i.hasNext(); ) {
69             propertyList.add(((LabelValueBean)i.next()).getValue());
70         }
71         rebuild(session);
72     }
73     
74     /**
75      * Get if the model contains the value. This is true if the
76      * value is either selected or deselected.
77      *
78      * @param value
79      * @return available
80      */

81     public boolean contains(String JavaDoc value) {
82         return available(value) || selected(value);
83     }
84     
85     /**
86      * Get if the value provided is available. I.e. in the list
87      * provided by the data source.
88      *
89      * @param value value
90      * @return available
91      */

92     public boolean available(String JavaDoc value) {
93         for(Iterator JavaDoc i = availableValues.iterator(); i.hasNext(); ) {
94             if(((LabelValueBean)i.next()).getValue().equals(value)) {
95                 return true;
96             }
97         }
98         return false;
99     }
100     
101     /**
102      * Get if the value is selected.
103      *
104      * @param value value
105      * @return selected
106      */

107     public boolean selected(String JavaDoc value) {
108         for(Iterator JavaDoc i = selectedValues.iterator(); i.hasNext(); ) {
109             if(((LabelValueBean)i.next()).getValue().equals(value)) {
110                 return true;
111             }
112         }
113         return false;
114     }
115     
116     /**
117      * Rebuild the selected values.
118      *
119      * @param session session
120      */

121     public void rebuild(SessionInfo session) {
122         availableValues = new ArrayList JavaDoc<LabelValueBean>(dataSource.getValues(session));
123         selectedValues = new ArrayList JavaDoc<LabelValueBean>();
124         availableMap = new HashMap JavaDoc<String JavaDoc, LabelValueBean>();
125         for(Iterator JavaDoc i = availableValues.iterator(); i.hasNext(); ) {
126             LabelValueBean lvb = (LabelValueBean)i.next();
127             availableMap.put(lvb.getValue(), lvb);
128         }
129         for(Iterator JavaDoc i = propertyList.iterator(); i.hasNext(); ) {
130             String JavaDoc v = (String JavaDoc)i.next();
131             LabelValueBean lvb = (LabelValueBean)availableMap.get(v);
132             if(lvb != null) {
133                 selectedValues.add(lvb);
134                 availableValues.remove(lvb);
135                 availableMap.remove(lvb.getValue());
136             }
137         }
138     }
139
140     /**
141      * Get the selected values in <i>Property Text</i> format
142      * suitable for persisting.
143      *
144      * @return selected values as property text
145      */

146     public String JavaDoc getAsPropertyText() {
147         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148         for (Iterator JavaDoc i = selectedValues.iterator(); i.hasNext();) {
149             LabelValueBean lvb = (LabelValueBean)i.next();
150             if (buf.length() > 0) {
151                 buf.append("!");
152             }
153             buf.append(lvb.getValue().replaceAll("\\!", "!!"));
154         }
155         return buf.toString();
156     }
157     
158     /**
159      * Get the list of available values.
160      *
161      * @return available values
162      */

163     public List JavaDoc<LabelValueBean> getAvailableValues() {
164         return availableValues;
165     }
166
167     
168     /**
169      * Get the list of selected values.
170      *
171      * @return selected values
172      */

173     public List JavaDoc<LabelValueBean> getSelectedValues() {
174         return selectedValues;
175     }
176
177     /**
178      * Sort the selected values
179      *
180      * @param comparator
181      */

182     public void sortSelection(Comparator JavaDoc comparator) {
183         Collections.sort(selectedValues, comparator);
184     }
185 }
186
Popular Tags