001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ElementModel.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.model;
009:
010: import com.uwyn.rife.gui.model.exceptions.GuiModelException;
011: import com.uwyn.rife.gui.model.exceptions.ParticlePropertyInvalidRemovalException;
012: import java.util.Collection;
013: import java.util.Iterator;
014:
015: public class ElementModel extends ParticleModel {
016: private ElementIdModel mElementIdModel = null;
017:
018: public ElementModel(String id) throws GuiModelException {
019: super ();
020:
021: if (null == id)
022: throw new IllegalArgumentException("id can't be null.");
023: if (0 == id.length())
024: throw new IllegalArgumentException("id can't be empty.");
025:
026: try {
027: mElementIdModel = new ElementIdModel(this , id);
028: } catch (GuiModelException e) {
029: e.fillInStackTrace();
030: throw e;
031: }
032: }
033:
034: protected ParticleModel findConflictingParticle(
035: ParticleModel parentParticle) {
036: assert parentParticle != null;
037:
038: for (ElementModel sibling : parentParticle
039: .getChildren(ElementModel.class)) {
040: if (sibling.getId().getName().equals(getId().getName())) {
041: return sibling;
042: }
043: }
044:
045: return null;
046: }
047:
048: public boolean removeProperty(ParticlePropertyModel property)
049: throws GuiModelException {
050: if (null == property)
051: throw new IllegalArgumentException(
052: "property can't be null.");
053:
054: if (property instanceof ElementIdModel) {
055: throw new ParticlePropertyInvalidRemovalException(this ,
056: property);
057: } else {
058: return super .removeProperty(property);
059: }
060: }
061:
062: public ElementImplementationModel setImplementation(String name)
063: throws GuiModelException {
064: if (null == name)
065: throw new IllegalArgumentException("name can't be null.");
066: if (0 == name.length())
067: throw new IllegalArgumentException("name can't be empty.");
068:
069: synchronized (mPropertiesMonitor) {
070: ElementImplementationModel current_classname = getImplementation();
071:
072: if (current_classname != null) {
073: removeProperty(current_classname);
074: }
075:
076: return new ElementImplementationModel(this , name);
077: }
078: }
079:
080: public ElementInputModel addInput(String name)
081: throws GuiModelException {
082: if (null == name)
083: throw new IllegalArgumentException("name can't be null.");
084: if (0 == name.length())
085: throw new IllegalArgumentException("name can't be empty.");
086:
087: return new ElementInputModel(this , name);
088: }
089:
090: public ElementOutputModel addOutput(String name)
091: throws GuiModelException {
092: if (null == name)
093: throw new IllegalArgumentException("name can't be null.");
094: if (0 == name.length())
095: throw new IllegalArgumentException("name can't be empty.");
096:
097: return new ElementOutputModel(this , name);
098: }
099:
100: public ElementExitModel addExit(String name)
101: throws GuiModelException {
102: if (null == name)
103: throw new IllegalArgumentException("name can't be null.");
104: if (0 == name.length())
105: throw new IllegalArgumentException("name can't be empty.");
106:
107: return new ElementExitModel(this , name);
108: }
109:
110: public SubmissionModel addSubmission(String name)
111: throws GuiModelException {
112: if (null == name)
113: throw new IllegalArgumentException("name can't be null.");
114: if (0 == name.length())
115: throw new IllegalArgumentException("name can't be empty.");
116:
117: return new SubmissionModel(this , name);
118: }
119:
120: public ElementImplementationModel getImplementation() {
121: Iterator<ElementImplementationModel> implementation_it = getProperties(
122: ElementImplementationModel.class).iterator();
123:
124: if (implementation_it.hasNext()) {
125: return implementation_it.next();
126: } else {
127: return null;
128: }
129: }
130:
131: public ElementIdModel getId() {
132: return mElementIdModel;
133: }
134:
135: public Collection<ElementInputModel> getInputs() {
136: return getProperties(ElementInputModel.class);
137: }
138:
139: public Collection<ElementOutputModel> getOutputs() {
140: return getProperties(ElementOutputModel.class);
141: }
142:
143: public Collection<ElementExitModel> getExits() {
144: return getProperties(ElementExitModel.class);
145: }
146:
147: public Collection<SubmissionModel> getSubmissions() {
148: Collection<SubmissionModel> result = getChildren(SubmissionModel.class);
149:
150: assert result != null;
151:
152: return result;
153: }
154:
155: public int countInputs() {
156: return countProperties(ElementInputModel.class);
157: }
158:
159: public int countOutputs() {
160: return countProperties(ElementOutputModel.class);
161: }
162:
163: public int countExits() {
164: return countProperties(ElementExitModel.class);
165: }
166:
167: public int countSubmissions() {
168: int result = countChildren(SubmissionModel.class);
169:
170: assert result >= 0;
171:
172: return result;
173: }
174: }
|