1 package org.splat.simer;
3 import org.splat.service.dto.KnowledgeElementDTO;
4 import org.splat.wapp.PopupMenu;
6 public class KnowledgeElementFacade {
8 private transient final KnowledgeElementDTO _my;
9 private transient State _state;
10 private transient String _value;
11 private transient PopupMenu _popup;
13 * Injected application settings service.
15 private ApplicationSettings _applicationSettings;
21 // ==============================================================================================================================
23 // ==============================================================================================================================
25 public KnowledgeElementFacade(final KnowledgeElementDTO represented,
26 final ApplicationSettings app) {
28 _state = State.closed;
29 setApplicationSettings(app);
31 this.refresh(); // Initializes the presentation of my knowledge
34 // ==============================================================================================================================
35 // Public member functions
36 // ==============================================================================================================================
38 public void develop() {
42 public void reduce() {
43 _state = State.closed;
46 // ==============================================================================================================================
48 // ==============================================================================================================================
50 public String getFullValue() {
51 return _my.getValue().replaceAll("'", "‘").replaceAll("\"", """);
54 public String getIndex() {
55 return String.valueOf(_my.getIndex());
58 public PopupMenu getPopup() {
59 _popup.setContext("feedbex", _my); // Cannot be done at construction because pop-ups are shared
63 public String getPresentationState() {
64 return _state.toString();
67 public String getProgressState() {
68 return _my.getProgressState().toString();
71 public String getTitle() {
72 return _my.getTitle();
75 public String getValue() {
77 if (_state == State.closed) {
85 public boolean isFacadeOf(final KnowledgeElementDTO represented) {
86 return _my.equals(represented);
90 * Set and refresh the knoledge element value.
93 * the knowledge element to set
95 public void refresh(final KnowledgeElementDTO represented) {
96 if (represented.getTitle() != null) {
97 _my.setTitle(represented.getTitle());
99 if (represented.getValue() != null) {
100 _my.setValue(represented.getValue());
105 // ==============================================================================================================================
106 // Protected services
107 // ==============================================================================================================================
110 * Refresh knowledge element text and create a trimmed value for preview.
112 private void refresh() {
113 String[] tags = { "<b>", "<i>", "<u>", "<sup>", "<sub>" };
115 _popup = getApplicationSettings().getPopupMenu("feedbex");
116 _value = _my.getValue();
118 // One line extract of the knowledge value
119 int size = 70 + 3; // Maximum length displayable on 1 line, including the starting <p> tag
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
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
134 endex = endOfExpressionPosition(_value, size);
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 + " ...";
147 if (_value.length() > size - 3) {
148 _value = _value.substring(0, size - 3) + " ...";
154 * Find the end position of an expression taking into account specific symbols.
159 * one line length limit
160 * @return actual end of expression position
162 private int endOfExpressionPosition(final String aValue, final int size) {
164 char endchar = aValue.charAt(res);
165 if (endchar == '-' || endchar == ',' || endchar == ';'
168 } else if (endchar != ' ') {
170 endchar = aValue.charAt(res);
171 if (endchar == ' ') {
174 if (endchar == '-' || endchar == ',' || endchar == ';'
185 * Append a closing tag to the string.
191 * @return the string with closed tag
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 + ">";
202 * Find a nearest position of the given substring looking from the beginning till the given position.
207 * the substring to search
209 * the end of expression position
210 * @return the position of the first occurrence or the given end of expression position
212 private int nearestPositionOf(final String aValue, final String toFind,
214 int res = aValue.indexOf(toFind);
215 if ((res <= 0) || (res >= endex)) {
222 * Get the applicationSettings.
224 * @return the applicationSettings
226 public ApplicationSettings getApplicationSettings() {
227 return _applicationSettings;
231 * Set the applicationSettings.
233 * @param applicationSettings
234 * the applicationSettings to set
236 public void setApplicationSettings(
237 final ApplicationSettings applicationSettings) {
238 _applicationSettings = applicationSettings;