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.FlexoDocParagraph;
44  import org.openflexo.foundation.doc.FlexoDocParagraph.FlexoDocParagraphImpl;
45  import org.openflexo.foundation.doc.FlexoDocument;
46  import org.openflexo.foundation.fml.annotations.FML;
47  import org.openflexo.foundation.fml.rt.ActorReference;
48  import org.openflexo.foundation.fml.rt.ModelSlotInstance;
49  import org.openflexo.logging.FlexoLogger;
50  import org.openflexo.pamela.annotations.Getter;
51  import org.openflexo.pamela.annotations.ImplementationClass;
52  import org.openflexo.pamela.annotations.ModelEntity;
53  import org.openflexo.pamela.annotations.PropertyIdentifier;
54  import org.openflexo.pamela.annotations.Setter;
55  import org.openflexo.pamela.annotations.XMLAttribute;
56  import org.openflexo.pamela.annotations.XMLElement;
57  import org.openflexo.toolbox.StringUtils;
58  
59  /**
60   * Implements {@link ActorReference} for {@link FlexoDocParagraph}.<br>
61   * 
62   * @author sylvain
63   * 
64   * @param <T>
65   *            type of referenced object
66   */
67  @ModelEntity
68  @ImplementationClass(ParagraphActorReference.ParagraphActorReferenceImpl.class)
69  @XMLElement
70  @FML("ParagraphActorReference")
71  public interface ParagraphActorReference<P extends FlexoDocParagraph<?, ?>> extends ActorReference<P> {
72  
73  	@PropertyIdentifier(type = String.class)
74  	public static final String PARAGRAPH_ID_KEY = "paragraphIdentifier";
75  
76  	@Getter(value = PARAGRAPH_ID_KEY)
77  	@XMLAttribute
78  	public String getParagraphId();
79  
80  	@Setter(PARAGRAPH_ID_KEY)
81  	public void setParagraphId(String paragraphIdentifier);
82  
83  	public abstract static class ParagraphActorReferenceImpl<P extends FlexoDocParagraphImpl<?, ?>> extends ActorReferenceImpl<P>
84  			implements ParagraphActorReference<P> {
85  
86  		private static final Logger logger = FlexoLogger.getLogger(ParagraphActorReference.class.getPackage().toString());
87  
88  		private String paragraphIdentifier;
89  		private P paragraph;
90  
91  		public FlexoDocument<?, ?> getFlexoDocument() {
92  			ModelSlotInstance<?, ?> msInstance = getModelSlotInstance();
93  			if (msInstance != null && msInstance.getAccessedResourceData() != null) {
94  				return (FlexoDocument<?, ?>) msInstance.getAccessedResourceData();
95  			}
96  			return null;
97  		}
98  
99  		@Override
100 		public String getParagraphId() {
101 			if (paragraph != null) {
102 				return paragraph.getIdentifier();
103 			}
104 			return paragraphIdentifier;
105 		}
106 
107 		@Override
108 		public void setParagraphId(String paragraphIdentifier) {
109 			this.paragraphIdentifier = paragraphIdentifier;
110 		}
111 
112 		@Override
113 		public P getModellingElement(boolean forceLoading) {
114 
115 			if (paragraph == null) {
116 				FlexoDocument<?, ?> document = getFlexoDocument();
117 				if (document != null) {
118 					if (StringUtils.isNotEmpty(paragraphIdentifier)) {
119 						paragraph = (P) document.getElementWithIdentifier(paragraphIdentifier);
120 					}
121 				}
122 			}
123 
124 			return paragraph;
125 		}
126 
127 		@Override
128 		public void setModellingElement(P aParagraph) {
129 
130 			if (aParagraph != paragraph) {
131 				P oldValue = paragraph;
132 				paragraph = aParagraph;
133 				getPropertyChangeSupport().firePropertyChange(MODELLING_ELEMENT_KEY, oldValue, paragraph);
134 			}
135 		}
136 	}
137 
138 }