001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.value;
034:
035: import com.flexive.shared.FxArrayUtils;
036: import com.flexive.shared.FxSharedUtils;
037: import com.flexive.shared.exceptions.FxInvalidParameterException;
038: import com.flexive.shared.exceptions.FxNotFoundException;
039: import com.flexive.shared.structure.FxSelectList;
040: import com.flexive.shared.structure.FxSelectListItem;
041:
042: import java.io.Serializable;
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.List;
046:
047: /**
048: * Container for manipulating FxSelectList items used in FxSelectMany values
049: *
050: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
051: */
052: public class SelectMany implements Serializable {
053: private static final long serialVersionUID = -2077153739902083147L;
054: private FxSelectList list;
055: private List<FxSelectListItem> available, selected;
056:
057: /**
058: * Constructor
059: *
060: * @param list the handled list
061: */
062: public SelectMany(FxSelectList list) {
063: this .list = list;
064: this .available = new ArrayList<FxSelectListItem>(list
065: .getItems());
066: this .selected = new ArrayList<FxSelectListItem>(available
067: .size());
068: }
069:
070: /**
071: * Get all items available for selection (unmodifiable!)
072: *
073: * @return all items available for selection (unmodifiable!)
074: */
075: public List<FxSelectListItem> getAvailable() {
076: return Collections.unmodifiableList(available);
077: }
078:
079: /**
080: * Get all items selected (unmodifiable!)
081: *
082: * @return all items selected (unmodifiable!)
083: */
084: public List<FxSelectListItem> getSelected() {
085: return Collections.unmodifiableList(selected);
086: }
087:
088: /**
089: * Return the selected item ids.
090: *
091: * @return the selected item ids.
092: */
093: public List<Long> getSelectedIds() {
094: final List<Long> selectedIds = new ArrayList<Long>(selected
095: .size());
096: for (FxSelectListItem item : selected) {
097: selectedIds.add(item.getId());
098: }
099: return selectedIds;
100: }
101:
102: /**
103: * Return the selected item labels.
104: *
105: * @return the selected item labels.
106: */
107: public List<String> getSelectedLabels() {
108: final List<String> selectedLabels = new ArrayList<String>(
109: selected.size());
110: for (FxSelectListItem item : selected) {
111: selectedLabels.add(item.getLabel().getBestTranslation());
112: }
113: return selectedLabels;
114: }
115:
116: /**
117: * Select an item by its id, will throw an exception if item id does not belong to the managed list
118: *
119: * @param id item id to select
120: * @return this
121: */
122: public SelectMany select(long id) {
123: if (list != null)
124: selectItem(list.getItem(id));
125: return this ;
126: }
127:
128: /**
129: * Select all items that are in the given comma separated list
130: *
131: * @param list items to select
132: * @return this
133: */
134: public SelectMany selectFromList(String list) {
135: try {
136: for (long sel : FxArrayUtils.toLongArray(list, ','))
137: select(sel);
138: } catch (FxInvalidParameterException e) {
139: //ignore
140: }
141: return this ;
142: }
143:
144: /**
145: * Select an item
146: *
147: * @param item the item to select
148: * @return this
149: */
150: public SelectMany selectItem(FxSelectListItem item) {
151: if (list == null || item == null)
152: return this ;
153: if (!list.containsItem(item.getId())) {
154: throw new FxNotFoundException(
155: "ex.content.value.selectMany.select", item.getId(),
156: list.getId(), list.getItems()).asRuntimeException();
157: }
158: if (available.contains(item))
159: available.remove(item);
160: if (!selected.contains(item)) {
161: selected.add(item);
162: sortSelected();
163: }
164: return this ;
165: }
166:
167: /**
168: * Deselect an item by its id, will throw an exception if item id does not belong to the managed list
169: *
170: * @param id item id to deselect
171: * @return this
172: */
173: public SelectMany deselect(long id) {
174: if (list != null)
175: deselectItem(list.getItem(id));
176: return this ;
177: }
178:
179: /**
180: * Deselect an item
181: *
182: * @param item the item to deselect
183: * @return this
184: */
185: public SelectMany deselectItem(FxSelectListItem item) {
186: if (list == null || item == null
187: || !list.containsItem(item.getId()))
188: return this ;
189: if (selected.contains(item))
190: selected.remove(item);
191: if (!available.contains(item))
192: available.add(item);
193: return this ;
194: }
195:
196: /**
197: * Get this SelectMany's SelectList
198: *
199: * @return FxSelectList
200: */
201: public FxSelectList getSelectList() {
202: return list;
203: }
204:
205: /**
206: * Deselects all items
207: */
208: public void deselectAll() {
209: selected.clear();
210: available.clear();
211: available.addAll(list.getItems());
212: }
213:
214: /**
215: * {@inheritDoc}
216: */
217: @Override
218: public String toString() {
219: StringBuilder sb = new StringBuilder(100);
220: for (int i = 0; i < selected.size(); i++) {
221: if (i > 0)
222: sb.append(',');
223: sb.append(selected.get(i).toString());
224: }
225: return sb.toString();
226: }
227:
228: /**
229: * Get an ordered comma separated list of selected id's
230: *
231: * @return ordered comma separated list of selected id's
232: */
233: public String getSelectedIdsList() {
234: StringBuilder sb = new StringBuilder(100);
235: for (int i = 0; i < selected.size(); i++) {
236: if (i > 0)
237: sb.append(',');
238: sb.append(selected.get(i).getId());
239: }
240: return sb.toString();
241: }
242:
243: private void sortSelected() {
244: Collections.sort(selected,
245: new FxSharedUtils.SelectableObjectSorter());
246: }
247:
248: @Override
249: public boolean equals(Object o) {
250: if (this == o)
251: return true;
252: if (o == null || getClass() != o.getClass())
253: return false;
254:
255: SelectMany that = (SelectMany) o;
256:
257: if (!list.equals(that.list))
258: return false;
259: if (!selected.equals(that.selected))
260: return false;
261:
262: return true;
263: }
264:
265: @Override
266: public int hashCode() {
267: int result;
268: result = list.hashCode();
269: result = 31 * result + selected.hashCode();
270: return result;
271: }
272: }
|