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 org.openflexo.foundation.doc.FlexoDocParagraph;
24  import org.openflexo.foundation.doc.FlexoDocument;
25  import org.openflexo.foundation.fml.FlexoRole;
26  import org.openflexo.foundation.fml.rt.AbstractVirtualModelInstanceModelFactory;
27  import org.openflexo.foundation.fml.rt.FlexoConceptInstance;
28  import org.openflexo.foundation.technologyadapter.TechnologyAdapter;
29  import org.openflexo.pamela.annotations.Getter;
30  import org.openflexo.pamela.annotations.ImplementationClass;
31  import org.openflexo.pamela.annotations.ModelEntity;
32  import org.openflexo.pamela.annotations.PropertyIdentifier;
33  import org.openflexo.pamela.annotations.Setter;
34  import org.openflexo.pamela.annotations.XMLAttribute;
35  import org.openflexo.toolbox.StringUtils;
36  
37  @ModelEntity(isAbstract = true)
38  @ImplementationClass(FlexoParagraphRole.FlexoParagraphRoleImpl.class)
39  public interface FlexoParagraphRole<P extends FlexoDocParagraph<D, TA>, D extends FlexoDocument<D, TA>, TA extends TechnologyAdapter<TA>>
40  		extends FlexoRole<P> {
41  
42  	@PropertyIdentifier(type = FlexoDocParagraph.class)
43  	public static final String PARAGRAPH_KEY = "paragraph";
44  	@PropertyIdentifier(type = String.class)
45  	public static final String PARAGRAPH_ID_KEY = "paragraphId";
46  
47  	/**
48  	 * Return the template document
49  	 * 
50  	 * @return
51  	 */
52  	public FlexoDocument<D, TA> getDocument();
53  
54  	/**
55  	 * Return the represented paragraph containing image in the template document resource<br>
56  	 * Note that is not the paragraph that is to be managed at run-time
57  	 * 
58  	 * @return
59  	 */
60  	@Getter(value = PARAGRAPH_KEY)
61  	public P getParagraph();
62  
63  	/**
64  	 * Sets the represented paragraph containing image in the template document resource<br>
65  	 * 
66  	 * @param fragment
67  	 */
68  	@Setter(PARAGRAPH_KEY)
69  	public void setParagraph(P table);
70  
71  	/**
72  	 * Return the represented paragraph id in the template document resource<br>
73  	 */
74  	@Getter(PARAGRAPH_ID_KEY)
75  	@XMLAttribute
76  	public String getParagraphId();
77  
78  	@Setter(PARAGRAPH_ID_KEY)
79  	public void setParagraphId(String paragraphId);
80  
81  	public static abstract class FlexoParagraphRoleImpl<P extends FlexoDocParagraph<D, TA>, D extends FlexoDocument<D, TA>, TA extends TechnologyAdapter<TA>>
82  			extends FlexoRoleImpl<P> implements FlexoParagraphRole<P, D, TA> {
83  
84  		private P paragraph;
85  		private String paragraphId;
86  
87  		@Override
88  		public FlexoDocument<D, TA> getDocument() {
89  			if (getModelSlot() instanceof FlexoDocumentModelSlot) {
90  				return ((FlexoDocumentModelSlot<D>) getModelSlot()).getTemplateResource().getDocument();
91  			}
92  			return null;
93  		}
94  
95  		@Override
96  		public String getParagraphId() {
97  			if (getParagraph() != null) {
98  				return getParagraph().getIdentifier();
99  			}
100 			return paragraphId;
101 		}
102 
103 		@Override
104 		public void setParagraphId(String tableId) {
105 			if ((tableId == null && this.paragraphId != null) || (tableId != null && !tableId.equals(this.paragraphId))) {
106 				String oldValue = getParagraphId();
107 				this.paragraphId = tableId;
108 				this.paragraph = null;
109 				getPropertyChangeSupport().firePropertyChange(PARAGRAPH_ID_KEY, oldValue, tableId);
110 				getPropertyChangeSupport().firePropertyChange(PARAGRAPH_KEY, null, paragraph);
111 			}
112 		}
113 
114 		@Override
115 		public P getParagraph() {
116 			if (paragraph == null && StringUtils.isNotEmpty(paragraphId) && getDocument() != null) {
117 				paragraph = (P) getDocument().getElementWithIdentifier(paragraphId);
118 			}
119 			return paragraph;
120 		}
121 
122 		@Override
123 		public void setParagraph(P paragraph) {
124 			P oldValue = this.paragraph;
125 			if (paragraph != oldValue) {
126 				this.paragraph = paragraph;
127 				getPropertyChangeSupport().firePropertyChange(PARAGRAPH_KEY, oldValue, paragraph);
128 				getPropertyChangeSupport().firePropertyChange(PARAGRAPH_ID_KEY, null, getParagraphId());
129 			}
130 		}
131 
132 		@Override
133 		public ParagraphActorReference<P> makeActorReference(P paragraph, FlexoConceptInstance fci) {
134 			AbstractVirtualModelInstanceModelFactory<?> factory = fci.getFactory();
135 			ParagraphActorReference<P> returned = factory.newInstance(ParagraphActorReference.class);
136 			returned.setFlexoRole(this);
137 			returned.setFlexoConceptInstance(fci);
138 			returned.setModellingElement(paragraph);
139 			return returned;
140 		}
141 
142 	}
143 }