View Javadoc
1   /**
2    * 
3    * Copyright (c) 2014-2015, Openflexo
4    * 
5    * This file is part of Excelconnector, a component of the software infrastructure 
6    * developed at Openflexo.
7    * 
8    * 
9    * Openflexo is dual-licensed under the European Union Public License (EUPL, either 
10   * version 1.1 of the License, or any later version ), which is available at 
11   * https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
12   * and the GNU General Public License (GPL, either version 3 of the License, or any 
13   * later version), which is available at http://www.gnu.org/licenses/gpl.html .
14   * 
15   * You can redistribute it and/or modify under the terms of either of these licenses
16   * 
17   * If you choose to redistribute it and/or modify under the terms of the GNU GPL, you
18   * must include the following additional permission.
19   *
20   *          Additional permission under GNU GPL version 3 section 7
21   *
22   *          If you modify this Program, or any covered work, by linking or 
23   *          combining it with software containing parts covered by the terms 
24   *          of EPL 1.0, the licensors of this Program grant you additional permission
25   *          to convey the resulting work. * 
26   * 
27   * This software is distributed in the hope that it will be useful, but WITHOUT ANY 
28   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
29   * PARTICULAR PURPOSE. 
30   *
31   * See http://www.openflexo.org/license.html for details.
32   * 
33   * 
34   * Please contact Openflexo (openflexo-contacts@openflexo.org)
35   * or visit www.openflexo.org if you need additional information.
36   * 
37   */
38  
39  package org.openflexo.foundation.doc.fml;
40  
41  import java.util.logging.Logger;
42  
43  import org.openflexo.foundation.doc.FlexoDocElement;
44  import org.openflexo.foundation.doc.FlexoDocFragment;
45  import org.openflexo.foundation.doc.FlexoDocFragment.FragmentConsistencyException;
46  import org.openflexo.foundation.doc.FlexoDocument;
47  import org.openflexo.foundation.doc.TextSelection;
48  import org.openflexo.foundation.fml.annotations.FML;
49  import org.openflexo.foundation.fml.rt.ActorReference;
50  import org.openflexo.foundation.fml.rt.ModelSlotInstance;
51  import org.openflexo.foundation.technologyadapter.TechnologyAdapter;
52  import org.openflexo.logging.FlexoLogger;
53  import org.openflexo.pamela.annotations.Getter;
54  import org.openflexo.pamela.annotations.ImplementationClass;
55  import org.openflexo.pamela.annotations.ModelEntity;
56  import org.openflexo.pamela.annotations.PropertyIdentifier;
57  import org.openflexo.pamela.annotations.Setter;
58  import org.openflexo.pamela.annotations.XMLAttribute;
59  import org.openflexo.pamela.annotations.XMLElement;
60  
61  /**
62   * Implements {@link ActorReference} for {@link FlexoDocFragment}.<br>
63   * Represents the actual links in a given {@link FlexoDocument} connecting a template fragment to a generated fragment<br>
64   * We need to store here the bindings between elements in template and corresponding elements in referenced {@link FlexoDocument}
65   * 
66   * @author sylvain
67   * 
68   * @param <T>
69   *            type of referenced object
70   */
71  @ModelEntity
72  @ImplementationClass(TextSelectionActorReference.TextSelectionActorReferenceImpl.class)
73  @XMLElement
74  @FML("FragmentActorReference")
75  public interface TextSelectionActorReference<D extends FlexoDocument<D, TA>, TA extends TechnologyAdapter<TA>>
76  		extends ActorReference<TextSelection<D, TA>> {
77  
78  	@PropertyIdentifier(type = String.class)
79  	public static final String START_ELEMENT_IDENTIFIER_KEY = "startElementId";
80  	@PropertyIdentifier(type = String.class)
81  	public static final String END_ELEMENT_IDENTIFIER_KEY = "endElementId";
82  	@PropertyIdentifier(type = Integer.class)
83  	public static final String START_RUN_INDEX_KEY = "startRunId";
84  	@PropertyIdentifier(type = Integer.class)
85  	public static final String END_RUN_INDEX_KEY = "endRunId";
86  	@PropertyIdentifier(type = Integer.class)
87  	public static final String START_CHARACTER_INDEX_KEY = "startCharId";
88  	@PropertyIdentifier(type = Integer.class)
89  	public static final String END_CHARACTER_INDEX_KEY = "endCharId";
90  
91  	@Getter(START_ELEMENT_IDENTIFIER_KEY)
92  	@XMLAttribute
93  	public String getStartElementIdentifier();
94  
95  	@Setter(START_ELEMENT_IDENTIFIER_KEY)
96  	public void setStartElementIdentifier(String startElementIdentifier);
97  
98  	@Getter(END_ELEMENT_IDENTIFIER_KEY)
99  	@XMLAttribute
100 	public String getEndElementIdentifier();
101 
102 	@Setter(END_ELEMENT_IDENTIFIER_KEY)
103 	public void setEndElementIdentifier(String endElementIdentifier);
104 
105 	@Getter(value = START_RUN_INDEX_KEY, defaultValue = "-1")
106 	@XMLAttribute
107 	public int getStartRunIndex();
108 
109 	@Setter(START_RUN_INDEX_KEY)
110 	public void setStartRunIndex(int startRunIndex);
111 
112 	@Getter(value = END_RUN_INDEX_KEY, defaultValue = "-1")
113 	@XMLAttribute
114 	public int getEndRunIndex();
115 
116 	@Setter(END_RUN_INDEX_KEY)
117 	public void setEndRunIndex(int endRunIndex);
118 
119 	@Getter(value = START_CHARACTER_INDEX_KEY, defaultValue = "-1")
120 	@XMLAttribute
121 	public int getStartCharacterIndex();
122 
123 	@Setter(START_CHARACTER_INDEX_KEY)
124 	public void setStartCharacterIndex(int startRunIndex);
125 
126 	@Getter(value = END_CHARACTER_INDEX_KEY, defaultValue = "-1")
127 	@XMLAttribute
128 	public int getEndCharacterIndex();
129 
130 	@Setter(END_CHARACTER_INDEX_KEY)
131 	public void setEndCharacterIndex(int endRunIndex);
132 
133 	public abstract static class TextSelectionActorReferenceImpl<D extends FlexoDocument<D, TA>, TA extends TechnologyAdapter<TA>>
134 			extends ActorReferenceImpl<TextSelection<D, TA>> implements TextSelectionActorReference<D, TA> {
135 
136 		private static final Logger logger = FlexoLogger.getLogger(TextSelectionActorReference.class.getPackage().toString());
137 
138 		private TextSelection<D, TA> textSelection;
139 
140 		public FlexoDocument<D, TA> getFlexoDocument() {
141 			ModelSlotInstance<?, ?> msInstance = getModelSlotInstance();
142 			if (msInstance != null && msInstance.getAccessedResourceData() != null) {
143 				return (FlexoDocument<D, TA>) msInstance.getAccessedResourceData();
144 			}
145 			return null;
146 		}
147 
148 		@Override
149 		public TextSelection<D, TA> getModellingElement(boolean forceLoading) {
150 
151 			if (textSelection == null) {
152 				System.out.println("Decoding TextSelection");
153 				FlexoDocument<D, TA> document = getFlexoDocument();
154 				System.out.println("doc=" + document);
155 				if (document != null) {
156 					FlexoDocElement<D, TA> startElement = document.getElementWithIdentifier(getStartElementIdentifier());
157 					FlexoDocElement<D, TA> endElement = document.getElementWithIdentifier(getEndElementIdentifier());
158 					System.out.println("startElement=" + startElement + " (" + getStartElementIdentifier() + ")");
159 					System.out.println("endElement=" + endElement + " (" + getEndElementIdentifier() + ")");
160 					try {
161 						FlexoDocFragment<D, TA> fragment = document.getFactory().makeFragment(startElement, endElement);
162 						System.out.println("fragment=" + fragment);
163 						textSelection = document.getFactory().makeTextSelection(startElement, getStartRunIndex(), getStartCharacterIndex(),
164 								endElement, getEndRunIndex(), getEndCharacterIndex());
165 					} catch (FragmentConsistencyException e) {
166 						e.printStackTrace();
167 					}
168 				}
169 				else {
170 					logger.warning("Could not access to document from model slot " + getModelSlotInstance());
171 				}
172 			}
173 
174 			return textSelection;
175 		}
176 
177 		@Override
178 		public void setModellingElement(TextSelection<D, TA> aTextSelection) {
179 
180 			System.out.println("setModellingElement with " + aTextSelection);
181 			this.textSelection = aTextSelection;
182 		}
183 
184 		private String startElementIdentifier;
185 		private String endElementIdentifier;
186 		private int startRunIndex = -1;
187 		private int endRunIndex = -1;
188 		private int startCharacterIndex = -1;
189 		private int endCharacterIndex = -1;
190 
191 		@Override
192 		public String getStartElementIdentifier() {
193 			if (textSelection != null) {
194 				return textSelection.getStartElementIdentifier();
195 			}
196 			return startElementIdentifier;
197 		}
198 
199 		@Override
200 		public void setStartElementIdentifier(String startElementIdentifier) {
201 			this.startElementIdentifier = startElementIdentifier;
202 		}
203 
204 		@Override
205 		public String getEndElementIdentifier() {
206 			if (textSelection != null) {
207 				return textSelection.getEndElementIdentifier();
208 			}
209 			return endElementIdentifier;
210 		}
211 
212 		@Override
213 		public void setEndElementIdentifier(String endElementIdentifier) {
214 			this.endElementIdentifier = endElementIdentifier;
215 		}
216 
217 		@Override
218 		public int getStartRunIndex() {
219 			if (textSelection != null) {
220 				return textSelection.getStartRunIndex();
221 			}
222 			return startRunIndex;
223 		}
224 
225 		@Override
226 		public void setStartRunIndex(int startRunIndex) {
227 			this.startRunIndex = startRunIndex;
228 		}
229 
230 		@Override
231 		public int getEndRunIndex() {
232 			if (textSelection != null) {
233 				return textSelection.getEndRunIndex();
234 			}
235 			return endRunIndex;
236 		}
237 
238 		@Override
239 		public void setEndRunIndex(int endRunIndex) {
240 			this.endRunIndex = endRunIndex;
241 		}
242 
243 		@Override
244 		public int getStartCharacterIndex() {
245 			if (textSelection != null) {
246 				return textSelection.getStartCharacterIndex();
247 			}
248 			return startCharacterIndex;
249 		}
250 
251 		@Override
252 		public void setStartCharacterIndex(int startCharacterIndex) {
253 			this.startCharacterIndex = startCharacterIndex;
254 		}
255 
256 		@Override
257 		public int getEndCharacterIndex() {
258 			if (textSelection != null) {
259 				return textSelection.getEndCharacterIndex();
260 			}
261 			return endCharacterIndex;
262 		}
263 
264 		@Override
265 		public void setEndCharacterIndex(int endCharacterIndex) {
266 			this.endCharacterIndex = endCharacterIndex;
267 		}
268 	}
269 }