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