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.value.FxString;
037: import org.apache.commons.lang.StringUtils;
038:
039: import java.util.List;
040:
041: /**
042: * FxGroup used for structure editing
043: *
044: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
045: */
046: public class FxGroupEdit extends FxGroup {
047:
048: private static final long serialVersionUID = -889437501601209068L;
049: private boolean isNew;
050: private GroupMode mode = GroupMode.AnyOf;
051: private int defaultMultiplicity = 1;
052:
053: /**
054: * Make an editable instance of an existing group
055: *
056: * @param group existing group
057: */
058: public FxGroupEdit(FxGroup group) {
059: super (group.getId(), group.getName(), group.getLabel().copy(),
060: group.getHint().copy(), group
061: .mayOverrideBaseMultiplicity(),
062: new FxMultiplicity(group.getMultiplicity()),
063: FxStructureOption.cloneOptions(group.options));
064: this .isNew = false;
065: }
066:
067: /**
068: * Create a clone of an existing group with a new name
069: *
070: * @param base group to clone
071: * @param newName new name to assign
072: * @return FxGroupEdit
073: */
074: public static FxGroupEdit createNew(FxGroup base, String newName) {
075: FxGroupEdit ret = new FxGroupEdit(base).setName(newName);
076: ret.isNew = true;
077: return ret;
078: }
079:
080: /**
081: * Constructor
082: *
083: * @param name group name
084: * @param description description
085: * @param hint hint message
086: * @param overrideBaseMultiplicity allow base multiplicity override?
087: * @param multiplicity multiplicity
088: */
089: private FxGroupEdit(String name, FxString description,
090: FxString hint, boolean overrideBaseMultiplicity,
091: FxMultiplicity multiplicity) {
092: super (-1, name, description, hint, overrideBaseMultiplicity,
093: multiplicity, FxStructureOption.getEmptyOptionList(2));
094: setName(name);
095: isNew = true;
096: }
097:
098: /**
099: * Create a new FxGroupEdit instance
100: *
101: * @param name group name
102: * @param description description
103: * @param hint hint message
104: * @param overrideBaseMultiplicity allow base multiplicity override?
105: * @param multiplicity multiplicity
106: * @return FxGroupEdit
107: */
108: public static FxGroupEdit createNew(String name,
109: FxString description, FxString hint,
110: boolean overrideBaseMultiplicity,
111: FxMultiplicity multiplicity) {
112: return new FxGroupEdit(name, description, hint,
113: overrideBaseMultiplicity, multiplicity).setName(name);
114: }
115:
116: /**
117: * Is this a new group
118: *
119: * @return if this is a new group
120: */
121: public boolean isNew() {
122: return isNew;
123: }
124:
125: /**
126: * Set the name for the group
127: *
128: * @param name group name
129: * @return this
130: * @throws com.flexive.shared.exceptions.FxRuntimeException
131: * on errors
132: */
133: public FxGroupEdit setName(String name) {
134: if (StringUtils.isEmpty(name))
135: throw new FxInvalidParameterException("NAME",
136: "ex.general.parameter.empty", "name")
137: .asRuntimeException();
138: name = name.trim().toUpperCase();
139: this .name = name;
140: return this ;
141: }
142:
143: /**
144: * Set the group mode. Only possible for new groups to reflect it to the new assignment
145: *
146: * @param mode group mode
147: * @return this
148: * @throws FxInvalidParameterException if group is not new
149: */
150: public FxGroupEdit setAssignmentGroupMode(GroupMode mode)
151: throws FxInvalidParameterException {
152: if (!isNew())
153: throw new FxInvalidParameterException("mode",
154: "ex.structure.group.notAllowed.mode");
155: this .mode = mode;
156: return this ;
157: }
158:
159: /**
160: * Get the group mode if this GroupEdit is for a new group and assignment is to be created
161: *
162: * @return group mode
163: */
164: public GroupMode getAssignmentGroupMode() {
165: return mode;
166: }
167:
168: /**
169: * Set the label
170: *
171: * @param label the label
172: * @return this
173: */
174: public FxGroupEdit setLabel(FxString label) {
175: this .label = label;
176: return this ;
177: }
178:
179: /**
180: * Set the hint message
181: *
182: * @param hint hint message
183: * @return this
184: */
185: public FxGroupEdit setHint(FxString hint) {
186: this .hint = hint;
187: return this ;
188: }
189:
190: /**
191: * Set the multiplicity
192: *
193: * @param multiplicity multiplicity
194: * @return this
195: */
196: public FxGroupEdit setMultiplicity(FxMultiplicity multiplicity) {
197: this .multiplicity = multiplicity;
198: return this ;
199: }
200:
201: /**
202: * May the base multiplicity be overridden?
203: *
204: * @param overrideMultiplicity may the base multiplicity be overridden?
205: * @return this
206: */
207: public FxGroupEdit setOverrideMultiplicity(
208: boolean overrideMultiplicity) {
209: this .overrideMultiplicity = overrideMultiplicity;
210: return this ;
211: }
212:
213: /**
214: * Set an option
215: *
216: * @param key option key
217: * @param overrideable is the option overrideable from assignments?
218: * @param value value of the option
219: * @return the group itself, useful for chained calls
220: */
221: public FxGroupEdit setOption(String key, boolean overrideable,
222: String value) {
223: FxStructureOption.setOption(options, key, overrideable, value);
224: return this ;
225: }
226:
227: /**
228: * Set a boolean option
229: *
230: * @param key option key
231: * @param overrideable is the option overrideable from assignments?
232: * @param value value of the option
233: * @return the group itself, useful for chained calls
234: */
235: public FxGroupEdit setOption(String key, boolean overrideable,
236: boolean value) {
237: FxStructureOption.setOption(options, key, overrideable, value);
238: return this ;
239: }
240:
241: /**
242: * Get a (unmodifiable) list of all options set for this group
243: *
244: * @return (unmodifiable) list of all options set for this group
245: */
246: public List<FxStructureOption> getOptions() {
247: return FxStructureOption.getUnmodifieableOptions(options);
248: }
249:
250: /**
251: * Clear an option entry
252: *
253: * @param key option name
254: */
255: public void clearOption(String key) {
256: FxStructureOption.clearOption(options, key);
257: }
258:
259: /**
260: * <b>This value set will only be used when creating a new property and will be set for the created assignment!</b>
261: * Set the default multiplicity (used i.e. in user interfaces editors and determines the amount of values that will
262: * be initialized when creating an empty element).
263: * <p/>
264: * If the set value is < min or > max multiplicity of this assignment it will
265: * be auto adjusted to the next valid value without throwing an exception
266: *
267: * @param defaultMultiplicity the default multiplicity
268: * @return this
269: */
270: public FxGroupEdit setAssignmentDefaultMultiplicity(
271: int defaultMultiplicity) {
272: this .defaultMultiplicity = defaultMultiplicity;
273: return this ;
274: }
275:
276: /**
277: * Get the created assignments default multiplicity
278: *
279: * @return default multiplicity
280: */
281: public int getAssignmentDefaultMultiplicity() {
282: return defaultMultiplicity;
283: }
284: }
|