Salome HOME
Fix for the bug: urls to documents in the repository are cached for all users with...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / KnowledgeElementFacade.java
1 package org.splat.simer;
2
3 import org.splat.service.dto.KnowledgeElementDTO;
4 import org.splat.wapp.PopupMenu;
5
6 public class KnowledgeElementFacade {
7
8         private transient final KnowledgeElementDTO _my;
9         private transient State _state;
10         private transient String _value;
11         private transient PopupMenu _popup;
12         /**
13          * Injected application settings service.
14          */
15         private ApplicationSettings _applicationSettings;
16
17         private enum State {
18                 closed, open
19         }
20
21         // ==============================================================================================================================
22         // Constructor
23         // ==============================================================================================================================
24
25         public KnowledgeElementFacade(final KnowledgeElementDTO represented,
26                         final ApplicationSettings app) {
27                 _my = represented;
28                 _state = State.closed;
29                 setApplicationSettings(app);
30
31                 this.refresh(); // Initializes the presentation of my knowledge
32         }
33
34         // ==============================================================================================================================
35         // Public member functions
36         // ==============================================================================================================================
37
38         public void develop() {
39                 _state = State.open;
40         }
41
42         public void reduce() {
43                 _state = State.closed;
44         }
45
46         // ==============================================================================================================================
47         // Getters
48         // ==============================================================================================================================
49
50         public String getFullValue() {
51                 return _my.getValue().replaceAll("'", "&#145").replaceAll("\"", "&#34");
52         }
53
54         public String getIndex() {
55                 return String.valueOf(_my.getIndex());
56         }
57
58         public PopupMenu getPopup() {
59                 _popup.setContext("feedbex", _my); // Cannot be done at construction because pop-ups are shared
60                 return _popup;
61         }
62
63         public String getPresentationState() {
64                 return _state.toString();
65         }
66
67         public String getProgressState() {
68                 return _my.getProgressState().toString();
69         }
70
71         public String getTitle() {
72                 return _my.getTitle();
73         }
74
75         public String getValue() {
76                 String res;
77                 if (_state == State.closed) {
78                         res = _value;
79                 } else {
80                         res = _my.getValue();
81                 }
82                 return res;
83         }
84
85         public boolean isFacadeOf(final KnowledgeElementDTO represented) {
86                 return _my.equals(represented);
87         }
88
89         /**
90          * Set and refresh the knoledge element value.
91          * 
92          * @param aValue
93          *            the knowledge element to set
94          */
95         public void refresh(final KnowledgeElementDTO represented) {
96                 if (represented.getTitle() != null) {
97                         _my.setTitle(represented.getTitle());
98                 }
99                 if (represented.getValue() != null) {
100                         _my.setValue(represented.getValue());
101                         refresh();
102                 }
103         }
104
105         // ==============================================================================================================================
106         // Protected services
107         // ==============================================================================================================================
108
109         /**
110          * Refresh knowledge element text and create a trimmed value for preview.
111          */
112         private void refresh() {
113                 String[] tags = { "<b>", "<i>", "<u>", "<sup>", "<sub>" };
114
115                 _popup = getApplicationSettings().getPopupMenu("feedbex");
116                 _value = _my.getValue();
117
118                 // One line extract of the knowledge value
119                 int size = 70 + 3; // Maximum length displayable on 1 line, including the starting <p> tag
120                 int i;
121                 for (i = 0; i < tags.length; i++) {
122                         int pos = _value.indexOf(tags[i]);
123                         if (pos > -1 && pos < size) {
124                                 size = size + 2 * tags[i].length() + 1; // Inclusion of the open and close tags
125                         }
126                 }
127                 if (_value.startsWith("<p>")) {
128                         int endex = _value.indexOf("</p>");
129                         endex = nearestPositionOf(_value, "<br", endex);
130                         endex = nearestPositionOf(_value, "<ul>", endex);
131                         endex = nearestPositionOf(_value, "<ol>", endex);
132                         int next = endex + 4; // Index of the next paragraph, if exist
133                         if (endex > size) {
134                                 endex = endOfExpressionPosition(_value, size);
135                         } else {
136                                 size = 0;
137                         }
138
139                         _value = _value.substring(3, endex).trim();
140                         _value = closeTag(_value, "b");
141                         _value = closeTag(_value, "i");
142                         _value = closeTag(_value, "u");
143                         if (_my.getValue().length() > next || size > 0) {
144                                 _value = _value + " ...";
145                         }
146                 } else {
147                         if (_value.length() > size - 3) {
148                                 _value = _value.substring(0, size - 3) + " ...";
149                         }
150                 }
151         }
152
153         /**
154          * Find the end position of an expression taking into account specific symbols.
155          * 
156          * @param aValue
157          *            the string
158          * @param size
159          *            one line length limit
160          * @return actual end of expression position
161          */
162         private int endOfExpressionPosition(final String aValue, final int size) {
163                 int res = size;
164                 char endchar = aValue.charAt(res);
165                 if (endchar == '-' || endchar == ',' || endchar == ';'
166                                 || endchar == '.') {
167                         res += 1;
168                 } else if (endchar != ' ') {
169                         while (--res > 0) {
170                                 endchar = aValue.charAt(res);
171                                 if (endchar == ' ') {
172                                         break;
173                                 }
174                                 if (endchar == '-' || endchar == ',' || endchar == ';'
175                                                 || endchar == '.') {
176                                         res += 1;
177                                         break;
178                                 }
179                         }
180                 }
181                 return res;
182         }
183
184         /**
185          * Append a closing tag to the string.
186          * 
187          * @param aValue
188          *            the string
189          * @param aTag
190          *            the tag to close
191          * @return the string with closed tag
192          */
193         private String closeTag(String aValue, final String aTag) {
194                 int i = aValue.lastIndexOf(aTag + ">");
195                 if ((i > 0) && (aValue.charAt(i - 1) == '<')) {
196                         aValue = aValue + "</" + aTag + ">";
197                 }
198                 return aValue;
199         }
200
201         /**
202          * Find a nearest position of the given substring looking from the beginning till the given position.
203          * 
204          * @param aValue
205          *            the string
206          * @param toFind
207          *            the substring to search
208          * @param endex
209          *            the end of expression position
210          * @return the position of the first occurrence or the given end of expression position
211          */
212         private int nearestPositionOf(final String aValue, final String toFind,
213                         final int endex) {
214                 int res = aValue.indexOf(toFind);
215                 if ((res <= 0) || (res >= endex)) {
216                         res = endex;
217                 }
218                 return res;
219         }
220
221         /**
222          * Get the applicationSettings.
223          * 
224          * @return the applicationSettings
225          */
226         public ApplicationSettings getApplicationSettings() {
227                 return _applicationSettings;
228         }
229
230         /**
231          * Set the applicationSettings.
232          * 
233          * @param applicationSettings
234          *            the applicationSettings to set
235          */
236         public void setApplicationSettings(
237                         final ApplicationSettings applicationSettings) {
238                 _applicationSettings = applicationSettings;
239         }
240 }