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;
17 // ==============================================================================================================================
19 // ==============================================================================================================================
21 public KnowledgeElementFacade(final KnowledgeElementDTO represented) {
23 _state = State.closed;
25 this.refresh(); // Initializes the presentation of my knowledge
28 // ==============================================================================================================================
29 // Public member functions
30 // ==============================================================================================================================
32 public void develop() {
36 public void reduce() {
37 _state = State.closed;
40 // ==============================================================================================================================
42 // ==============================================================================================================================
44 public String getFullValue() {
45 return _my.getValue().replaceAll("'", "‘").replaceAll("\"", """);
48 public String getIndex() {
49 return String.valueOf(_my.getIndex());
52 public PopupMenu getPopup() {
53 _popup.setContext("feedbex", _my); // Cannot be done at construction because pop-ups are shared
57 public String getPresentationState() {
58 return _state.toString();
61 public String getProgressState() {
62 return _my.getProgressState().toString();
65 public String getTitle() {
66 return _my.getTitle();
69 public String getValue() {
71 if (_state == State.closed) {
79 public boolean isFacadeOf(final KnowledgeElementDTO represented) {
80 return _my.equals(represented);
84 * Set and refresh the knoledge element value.
87 * the knowledge element to set
89 public void refresh(final KnowledgeElementDTO represented) {
90 if (represented.getTitle() != null) {
91 _my.setTitle(represented.getTitle());
93 if (represented.getValue() != null) {
94 _my.setValue(represented.getValue());
99 // ==============================================================================================================================
100 // Protected services
101 // ==============================================================================================================================
104 * Refresh knowledge element text and create a trimmed value for preview.
106 private void refresh() {
107 String[] tags = { "<b>", "<i>", "<u>", "<sup>", "<sub>" };
109 _popup = ApplicationSettings.getPopupMenu("feedbex");
110 _value = _my.getValue();
112 // One line extract of the knowledge value
113 int size = 70 + 3; // Maximum length displayable on 1 line, including the starting <p> tag
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
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
128 endex = endOfExpressionPosition(_value, size);
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 + " ...";
141 if (_value.length() > size - 3) {
142 _value = _value.substring(0, size - 3) + " ...";
148 * Find the end position of an expression taking into account specific symbols.
153 * one line length limit
154 * @return actual end of expression position
156 private int endOfExpressionPosition(final String aValue, final int size) {
158 char endchar = aValue.charAt(res);
159 if (endchar == '-' || endchar == ',' || endchar == ';'
162 } else if (endchar != ' ') {
164 endchar = aValue.charAt(res);
165 if (endchar == ' ') {
168 if (endchar == '-' || endchar == ',' || endchar == ';'
179 * Append a closing tag to the string.
185 * @return the string with closed tag
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 + ">";
196 * Find a nearest position of the given substring looking from the beginning till the given position.
201 * the substring to search
203 * the end of expression position
204 * @return the position of the first occurrence or the given end of expression position
206 private int nearestPositionOf(final String aValue, final String toFind,
208 int res = aValue.indexOf(toFind);
209 if ((res <= 0) || (res >= endex)) {