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.structure;
034:
035: import com.flexive.shared.exceptions.FxInvalidParameterException;
036: import com.flexive.shared.security.ACL;
037: import com.flexive.shared.value.BinaryDescriptor;
038: import com.flexive.shared.value.FxString;
039:
040: import java.io.Serializable;
041:
042: /**
043: * Editable select list item
044: *
045: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
046: */
047: public class FxSelectListItemEdit extends FxSelectListItem implements
048: Serializable {
049:
050: private static final long serialVersionUID = -4068691002679713796L;
051:
052: /**
053: * is this a new list item?
054: */
055: private boolean isNew;
056: /**
057: * The original item to compare for changes
058: */
059: private FxSelectListItem original;
060:
061: /**
062: * Constructor to make an existing list item editable
063: *
064: * @param item the item to make editable
065: */
066: public FxSelectListItemEdit(FxSelectListItem item) {
067: super (item.id, item.acl, item.list, item.parentItemId,
068: item.label, item.data, item.color, item.iconId,
069: item.iconVer, item.iconQuality, item.lifeCycleInfo);
070: this .isNew = false;
071: this .original = item;
072: this .parentItem = item.parentItem;
073: }
074:
075: /**
076: * Internal constructor to make an existing list item editable and add it to an editable select list.
077: *
078: * @param item the item to make editable
079: * @param list the new list to add the item to
080: */
081: FxSelectListItemEdit(FxSelectListItem item, FxSelectListEdit list) {
082: super (item.id, item.acl, list, item.parentItemId, item.label,
083: item.data, item.color, item.iconId, item.iconVer,
084: item.iconQuality, item.lifeCycleInfo);
085: this .isNew = false;
086: this .original = item;
087: this .parentItem = item.parentItem;
088: }
089:
090: /**
091: * Constructor for creating a new list item
092: *
093: * @param acl the items ACL
094: * @param list the list this item belongs to
095: * @param label this items label
096: * @param data optional data
097: * @param color color for display
098: */
099: public FxSelectListItemEdit(ACL acl, FxSelectList list,
100: FxString label, String data, String color) {
101: super (calcId(list), acl, list, -1, label, data, color,
102: BinaryDescriptor.SYS_SELECTLIST_DEFAULT, 1, 1, null);
103: this .isNew = true;
104: this .original = null;
105: }
106:
107: /**
108: * Calculate an unused id for the parent list to use for this item (for new items only)
109: *
110: * @param list parent list to check
111: * @return calculated id
112: */
113: private static synchronized long calcId(FxSelectList list) {
114: int curr = -1;
115: while (list.containsItem(curr))
116: curr--;
117: return curr;
118: }
119:
120: /**
121: * Is this a new list item?
122: *
123: * @return is anew list item?
124: */
125: public boolean isNew() {
126: return isNew;
127: }
128:
129: /**
130: * Have any changes been made?
131: *
132: * @return changes made?
133: */
134: public boolean changes() {
135: return isNew || original == null
136: || !this .data.equals(original.data)
137: || !this .color.equals(original.color)
138: || this .iconId != original.iconId
139: || this .iconVer != original.iconVer
140: || this .iconQuality != original.iconQuality
141: || !this .label.equals(original.label)
142: || this .parentItemId != original.parentItemId
143: || this .acl.getId() != original.acl.getId();
144: }
145:
146: /**
147: * Set this items label
148: *
149: * @param label label of this item
150: */
151: public void setLabel(FxString label) {
152: this .label = label;
153: if (this .label == null)
154: this .label = new FxString("");
155: }
156:
157: /**
158: * Set this items acl
159: *
160: * @param acl acl of this item
161: */
162: public void setAcl(ACL acl) {
163: this .acl = acl;
164: }
165:
166: /**
167: * Set optional item data
168: *
169: * @param data optional item data
170: */
171: public void setData(String data) {
172: this .data = data;
173: if (this .data == null)
174: this .data = "";
175: }
176:
177: /**
178: * Set the items display color
179: *
180: * @param color display color
181: */
182: public void setColor(String color) {
183: this .color = color;
184: if (this .color == null)
185: this .color = "";
186: }
187:
188: /**
189: * Set the id of icon used for user interfaces (reference to binaries, internal field!)
190: *
191: * @param iconId id of icon used for user interfaces (reference to binaries, internal field!)
192: */
193: public void setIconId(long iconId) {
194: this .iconId = iconId;
195: }
196:
197: /**
198: * Set the version of icon used for user interfaces (reference to binaries, internal field!)
199: *
200: * @param iconVer version of icon used for user interfaces (reference to binaries, internal field!)
201: */
202: public void setIconVer(int iconVer) {
203: this .iconVer = iconVer;
204: }
205:
206: /**
207: * Set the quality of icon used for user interfaces (reference to binaries, internal field!)
208: *
209: * @param iconQuality quality of icon used for user interfaces (reference to binaries, internal field!)
210: */
211: public void setIconQuality(int iconQuality) {
212: this .iconQuality = iconQuality;
213: }
214:
215: public FxSelectListItemEdit setDefaultItem() {
216: this .getList()._setDefaultItem(this );
217: return this ;
218: }
219:
220: /**
221: * Helper method to create a new FxSelectListItemEdit instance
222: *
223: * @param acl the items ACL
224: * @param list the list this item belongs to
225: * @param label this items label
226: * @param data optional data
227: * @param color color for display
228: * @return FxSelectListItemEdit
229: */
230: public static FxSelectListItemEdit createNew(ACL acl,
231: FxSelectList list, FxString label, String data, String color) {
232: return new FxSelectListItemEdit(acl, list, label, data, color);
233: }
234:
235: /**
236: * Set a parent item
237: *
238: * @param item item from the parent list to assign as parent item
239: * @return FxSelectListItemEdit
240: * @throws FxInvalidParameterException if the parent item is not valid for this item
241: */
242: public FxSelectListItemEdit setParentItem(FxSelectListItem item)
243: throws FxInvalidParameterException {
244: if (item == null)
245: return this ;
246: if (!this .getList().hasParentList())
247: throw new FxInvalidParameterException("item",
248: "ex.structure.list.item.noParentList", this
249: .getLabel().getBestTranslation());
250: if (item.getList().getId() != this .getList().getId())
251: throw new FxInvalidParameterException("item",
252: "ex.structure.list.item.invalidParent", this
253: .getList().getId(), item.getList().getId());
254: this.parentItem = item;
255: return this;
256: }
257: }
|