View Javadoc
1   /*
2    * (c) Copyright 2013- Openflexo
3    *
4    * This file is part of OpenFlexo.
5    *
6    * OpenFlexo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * OpenFlexo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
18   *
19   */
20  
21  package org.openflexo.foundation.doc.fml;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.openflexo.connie.DataBinding;
27  import org.openflexo.connie.DataBinding.BindingDefinitionType;
28  import org.openflexo.foundation.FlexoObject;
29  import org.openflexo.foundation.doc.FlexoDocElement;
30  import org.openflexo.foundation.doc.FlexoDocParagraph;
31  import org.openflexo.foundation.doc.FlexoDocument;
32  import org.openflexo.foundation.doc.FlexoDrawingRun;
33  import org.openflexo.foundation.fml.FlexoRole;
34  import org.openflexo.foundation.fml.rt.AbstractVirtualModelInstanceModelFactory;
35  import org.openflexo.foundation.fml.rt.FlexoConceptInstance;
36  import org.openflexo.foundation.fml.rt.VirtualModelInstanceNature;
37  import org.openflexo.foundation.nature.ScreenshotableNature;
38  import org.openflexo.foundation.technologyadapter.TechnologyAdapter;
39  import org.openflexo.pamela.annotations.Getter;
40  import org.openflexo.pamela.annotations.ImplementationClass;
41  import org.openflexo.pamela.annotations.ModelEntity;
42  import org.openflexo.pamela.annotations.PropertyIdentifier;
43  import org.openflexo.pamela.annotations.Setter;
44  import org.openflexo.pamela.annotations.XMLAttribute;
45  import org.openflexo.toolbox.StringUtils;
46  
47  @ModelEntity(isAbstract = true)
48  @ImplementationClass(FlexoImageRole.FlexoImageRoleImpl.class)
49  public interface FlexoImageRole<R extends FlexoDrawingRun<D, TA>, D extends FlexoDocument<D, TA>, TA extends TechnologyAdapter<TA>>
50  		extends FlexoRole<R> {
51  
52  	@PropertyIdentifier(type = FlexoDrawingRun.class)
53  	public static final String DRAWING_RUN_KEY = "drawingRun";
54  	@PropertyIdentifier(type = String.class)
55  	public static final String PARAGRAPH_ID_KEY = "paragraphId";
56  	@PropertyIdentifier(type = Integer.class)
57  	public static final String RUN_INDEX_KEY = "runIndex";
58  	@PropertyIdentifier(type = Object.class)
59  	public static final String REPRESENTED_OBJECT_KEY = "representedObject";
60  	@PropertyIdentifier(type = Class.class)
61  	public static final String NATURE_KEY = "nature";
62  
63  	/**
64  	 * Return the template document
65  	 * 
66  	 * @return
67  	 */
68  	public FlexoDocument<D, TA> getDocument();
69  
70  	/**
71  	 * Return the represented drawing run in the template document resource<br>
72  	 * Note that is not the run that is to be managed at run-time
73  	 * 
74  	 * @return
75  	 */
76  	@Getter(value = DRAWING_RUN_KEY)
77  	public R getDrawingRun();
78  
79  	/**
80  	 * Sets the represented drawing run in the template document resource<br>
81  	 * 
82  	 * @param fragment
83  	 */
84  	@Setter(DRAWING_RUN_KEY)
85  	public void setDrawingRun(R run);
86  
87  	/**
88  	 * Return the drawingRun id where the drawing run is contained, in the template document resource<br>
89  	 */
90  	@Getter(PARAGRAPH_ID_KEY)
91  	@XMLAttribute
92  	public String getParagraphId();
93  
94  	/**
95  	 * Sets the drawingRun id where the drawing run is contained, in the template document resource<br>
96  	 * 
97  	 * @param paragraphId
98  	 */
99  	@Setter(PARAGRAPH_ID_KEY)
100 	public void setParagraphId(String paragraphId);
101 
102 	/**
103 	 * Return the run index of drawingRun where the drawing run is contained, in the template document resource<br>
104 	 */
105 	@Getter(value = RUN_INDEX_KEY, defaultValue = "-1")
106 	@XMLAttribute
107 	public int getRunIndex();
108 
109 	/**
110 	 * Sets the run index of drawingRun where the drawing run is contained, in the template document resource<br>
111 	 * 
112 	 * @param runIndex
113 	 */
114 	@Setter(RUN_INDEX_KEY)
115 	public void setRunIndex(int runIndex);
116 
117 	/**
118 	 * Return a {@link DataBinding} representing the object to represent
119 	 * 
120 	 * @return
121 	 */
122 	@Getter(REPRESENTED_OBJECT_KEY)
123 	@XMLAttribute
124 	public DataBinding<FlexoObject> getRepresentedObject();
125 
126 	/**
127 	 * Sets {@link DataBinding} representing the object to represent
128 	 * 
129 	 * @param value
130 	 */
131 	@Setter(REPRESENTED_OBJECT_KEY)
132 	public void setRepresentedObject(DataBinding<FlexoObject> value);
133 
134 	/**
135 	 * Return the nature used to extract screenshot<br>
136 	 */
137 	@Getter(NATURE_KEY)
138 	@XMLAttribute
139 	public Class<? extends ScreenshotableNature<?>> getNature();
140 
141 	/**
142 	 * Sets the nature used to extract screenshot<br>
143 	 * 
144 	 * @param nature
145 	 */
146 	@Setter(NATURE_KEY)
147 	public void setNature(Class<? extends ScreenshotableNature<?>> nature);
148 
149 	public List<Class<? extends ScreenshotableNature<?>>> getAvailableNatures();
150 
151 	public static abstract class FlexoImageRoleImpl<R extends FlexoDrawingRun<D, TA>, D extends FlexoDocument<D, TA>, TA extends TechnologyAdapter<TA>>
152 			extends FlexoRoleImpl<R> implements FlexoImageRole<R, D, TA> {
153 
154 		private R drawingRun;
155 		private String paragraphId;
156 		private int runIndex = -1;
157 		private Class<? extends ScreenshotableNature<?>> natureClass;
158 
159 		@Override
160 		public FlexoDocument<D, TA> getDocument() {
161 			if (getModelSlot() instanceof FlexoDocumentModelSlot) {
162 				return ((FlexoDocumentModelSlot<D>) getModelSlot()).getTemplateResource().getDocument();
163 			}
164 			return null;
165 		}
166 
167 		@Override
168 		public String getParagraphId() {
169 			if (getDrawingRun() != null && getDrawingRun().getParagraph() != null) {
170 				return getDrawingRun().getParagraph().getIdentifier();
171 			}
172 			return paragraphId;
173 		}
174 
175 		@Override
176 		public void setParagraphId(String paragraphId) {
177 			if ((paragraphId == null && this.paragraphId != null) || (paragraphId != null && !paragraphId.equals(this.paragraphId))) {
178 				String oldValue = getParagraphId();
179 				this.paragraphId = paragraphId;
180 				this.drawingRun = null;
181 				getPropertyChangeSupport().firePropertyChange(PARAGRAPH_ID_KEY, oldValue, paragraphId);
182 				getPropertyChangeSupport().firePropertyChange(DRAWING_RUN_KEY, null, drawingRun);
183 			}
184 		}
185 
186 		@Override
187 		public int getRunIndex() {
188 			if (getDrawingRun() != null && getDrawingRun().getParagraph() != null) {
189 				return getDrawingRun().getIndex();
190 			}
191 			return runIndex;
192 		}
193 
194 		@Override
195 		public void setRunIndex(int index) {
196 			if (index != this.runIndex) {
197 				int oldValue = getRunIndex();
198 				this.runIndex = index;
199 				this.drawingRun = null;
200 				getPropertyChangeSupport().firePropertyChange(RUN_INDEX_KEY, oldValue, index);
201 				getPropertyChangeSupport().firePropertyChange(DRAWING_RUN_KEY, null, drawingRun);
202 			}
203 		}
204 
205 		@Override
206 		public R getDrawingRun() {
207 			if (drawingRun == null && StringUtils.isNotEmpty(paragraphId) && runIndex > -1 && getDocument() != null) {
208 				FlexoDocElement<D, TA> element = getDocument().getElementWithIdentifier(paragraphId);
209 				if (element instanceof FlexoDocParagraph) {
210 					// TODO: implement here a scheme to retrieve the closest DrawingRun if runIndex does not match
211 					drawingRun = (R) ((FlexoDocParagraph) element).getRuns().get(runIndex);
212 				}
213 			}
214 			return drawingRun;
215 		}
216 
217 		@Override
218 		public void setDrawingRun(R run) {
219 			R oldValue = this.drawingRun;
220 			if (run != oldValue) {
221 				this.drawingRun = run;
222 				getPropertyChangeSupport().firePropertyChange(DRAWING_RUN_KEY, oldValue, run);
223 				getPropertyChangeSupport().firePropertyChange(PARAGRAPH_ID_KEY, null, getParagraphId());
224 				getPropertyChangeSupport().firePropertyChange(RUN_INDEX_KEY, null, getRunIndex());
225 			}
226 		}
227 
228 		@Override
229 		public ImageActorReference<R> makeActorReference(R drawingRun, FlexoConceptInstance fci) {
230 			AbstractVirtualModelInstanceModelFactory<?> factory = fci.getFactory();
231 			ImageActorReference<R> returned = factory.newInstance(ImageActorReference.class);
232 			returned.setFlexoRole(this);
233 			returned.setFlexoConceptInstance(fci);
234 			returned.setModellingElement(drawingRun);
235 			return returned;
236 		}
237 
238 		private DataBinding<FlexoObject> representedObject;
239 
240 		@Override
241 		public DataBinding<FlexoObject> getRepresentedObject() {
242 			if (representedObject == null) {
243 				representedObject = new DataBinding<>(this, FlexoObject.class, DataBinding.BindingDefinitionType.GET);
244 				representedObject.setBindingName("representedObject");
245 				representedObject.setMandatory(false);
246 			}
247 			return representedObject;
248 		}
249 
250 		@Override
251 		public void setRepresentedObject(DataBinding<FlexoObject> representedObject) {
252 			if (representedObject != null) {
253 				representedObject.setOwner(this);
254 				representedObject.setDeclaredType(FlexoObject.class);
255 				representedObject.setBindingName("representedObject");
256 				representedObject.setMandatory(true);
257 				representedObject.setBindingDefinitionType(BindingDefinitionType.GET);
258 			}
259 			this.representedObject = representedObject;
260 			notifiedBindingChanged(getRepresentedObject());
261 		}
262 
263 		@Override
264 		public Class<? extends ScreenshotableNature<?>> getNature() {
265 			return natureClass;
266 		}
267 
268 		@Override
269 		public void setNature(Class<? extends ScreenshotableNature<?>> nature) {
270 			if (this.natureClass != nature) {
271 				Class<? extends ScreenshotableNature<?>> oldValue = this.natureClass;
272 				this.natureClass = nature;
273 				getPropertyChangeSupport().firePropertyChange(FlexoImageRole.NATURE_KEY, oldValue, nature);
274 			}
275 		}
276 
277 		private List<Class<? extends ScreenshotableNature<?>>> availableNatures = null;
278 
279 		@Override
280 		public List<Class<? extends ScreenshotableNature<?>>> getAvailableNatures() {
281 			if (availableNatures == null && getServiceManager() != null) {
282 				availableNatures = new ArrayList<>();
283 				for (TechnologyAdapter<?> ta : getServiceManager().getTechnologyAdapterService().getTechnologyAdapters()) {
284 					for (Class<? extends VirtualModelInstanceNature> natureClass : ta.getAvailableVirtualModelInstanceNatures()) {
285 						if (ScreenshotableNature.class.isAssignableFrom(natureClass)) {
286 							availableNatures.add((Class<? extends ScreenshotableNature<?>>) natureClass);
287 						}
288 					}
289 				}
290 			}
291 			return availableNatures;
292 		}
293 
294 	}
295 }