1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.ui.element;
|
5
|
|
6
|
import java.awt.Point;
|
7
|
import java.io.IOException;
|
8
|
import java.util.Set;
|
9
|
import java.util.regex.Matcher;
|
10
|
import java.util.regex.Pattern;
|
11
|
|
12
|
import javax.xml.parsers.ParserConfigurationException;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.eclipse.jface.dialogs.MessageDialog;
|
16
|
import org.eclipse.jface.layout.GridDataFactory;
|
17
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
18
|
import org.eclipse.jface.viewers.CellEditor.LayoutData;
|
19
|
import org.eclipse.nebula.widgets.richtext.RichTextEditor;
|
20
|
import org.eclipse.nebula.widgets.richtext.RichTextEditorConfiguration;
|
21
|
import org.eclipse.nebula.widgets.richtext.toolbar.ToolbarButton;
|
22
|
import org.eclipse.nebula.widgets.richtext.toolbar.ToolbarConfiguration;
|
23
|
import org.eclipse.swt.SWT;
|
24
|
import org.eclipse.swt.events.KeyAdapter;
|
25
|
import org.eclipse.swt.events.KeyEvent;
|
26
|
import org.eclipse.swt.events.ModifyEvent;
|
27
|
import org.eclipse.swt.events.ModifyListener;
|
28
|
import org.eclipse.swt.events.SelectionAdapter;
|
29
|
import org.eclipse.swt.events.SelectionEvent;
|
30
|
import org.eclipse.swt.graphics.Color;
|
31
|
import org.eclipse.swt.layout.GridLayout;
|
32
|
import org.eclipse.swt.layout.RowLayout;
|
33
|
import org.eclipse.swt.widgets.Button;
|
34
|
import org.eclipse.swt.widgets.Composite;
|
35
|
import org.eclipse.swt.widgets.Control;
|
36
|
import org.eclipse.swt.widgets.Label;
|
37
|
import org.eclipse.swt.widgets.Listener;
|
38
|
import org.eclipse.swt.widgets.Text;
|
39
|
import org.eclipse.ui.dialogs.SelectionDialog;
|
40
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
41
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
42
|
|
43
|
|
44
|
|
45
|
|
46
|
|
47
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
48
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
49
|
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
|
50
|
import eu.etaxonomy.cdm.model.common.IntextReference;
|
51
|
import eu.etaxonomy.cdm.model.common.Language;
|
52
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
53
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
54
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
55
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
56
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
57
|
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
|
58
|
import eu.etaxonomy.taxeditor.util.RichtextHelper;
|
59
|
|
60
|
/**
|
61
|
* @author k. luther
|
62
|
* @version $Id: $
|
63
|
*/
|
64
|
public class RichTextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
|
65
|
ISelectable {
|
66
|
|
67
|
|
68
|
protected RichTextEditor text;
|
69
|
|
70
|
public void setLanguageString(LanguageString languageString) {
|
71
|
if (languageString == null){
|
72
|
this.languageString = LanguageString.NewInstance("", Language.DEFAULT());
|
73
|
}
|
74
|
this.languageString = languageString;
|
75
|
|
76
|
|
77
|
}
|
78
|
|
79
|
private LanguageString languageString ;
|
80
|
|
81
|
EntitySelectionElement<Reference> selection_reference;
|
82
|
|
83
|
private Label label;
|
84
|
|
85
|
private final boolean isMultiLine;
|
86
|
|
87
|
public static final int MAX_HEIGHT = 0;
|
88
|
public static final int SINGLE = -1;
|
89
|
|
90
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
|
91
|
super(formFactory, parentElement);
|
92
|
this.isMultiLine = isMultiLine;
|
93
|
}
|
94
|
|
95
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
96
|
LanguageString initialLanguageString, Integer textHeight, int style) {
|
97
|
this(formFactory, parentElement, labelString, initialLanguageString, textHeight, null, true, style);
|
98
|
}
|
99
|
|
100
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
101
|
LanguageString initialText, Integer textHeight, boolean isMultiLine, int style) {
|
102
|
this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
|
103
|
}
|
104
|
|
105
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
106
|
LanguageString initialText, Integer textHeight, Integer textLimit, int style) {
|
107
|
this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
|
108
|
}
|
109
|
|
110
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
111
|
LanguageString initialLanguageString, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
|
112
|
super(formFactory, parentElement);
|
113
|
|
114
|
this.isMultiLine = isMultiLine;
|
115
|
this.languageString = initialLanguageString;
|
116
|
|
117
|
initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
|
118
|
if (initialLanguageString != null){
|
119
|
initText( initialLanguageString.getText(), textHeight, textLimit, isMultiLine, style, getLayoutComposite());
|
120
|
}else{
|
121
|
initText( "", textHeight, textLimit, isMultiLine, style, getLayoutComposite());
|
122
|
|
123
|
}
|
124
|
formFactory.addPropertyChangeListener(this);
|
125
|
}
|
126
|
|
127
|
protected void initText(String initialText, Integer textHeight, Integer textLimit,
|
128
|
boolean isMultiLine, int style, Composite layoutComposite) {
|
129
|
int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
|
130
|
|
131
|
int combinedStyle = style | SWT.BORDER | scrollStyle;
|
132
|
|
133
|
// SWT.PASSWORD does not work when SWT.WRAP is set.
|
134
|
if (style != SWT.PASSWORD) {
|
135
|
combinedStyle = combinedStyle | SWT.WRAP;
|
136
|
}
|
137
|
|
138
|
text = formFactory.createRichTextEditor(layoutComposite, "", combinedStyle);
|
139
|
|
140
|
|
141
|
|
142
|
String browsertype = text.getEditorConfiguration().getBrowser().getBrowserType();
|
143
|
|
144
|
|
145
|
addControl(text);
|
146
|
|
147
|
// text.setWO
|
148
|
|
149
|
// if (textHeight == null) {
|
150
|
// text.addKeyListener(new KeyAdapter() {
|
151
|
// @Override
|
152
|
// public void keyPressed(KeyEvent e) {
|
153
|
// if (e.character == SWT.CR) {
|
154
|
// // Don't accept carriage returns as input when in single
|
155
|
// // line mode
|
156
|
// e.doit = false;
|
157
|
// } else if (e.character == SWT.TAB) {
|
158
|
// // traverse is not working for wrapped text widgets so
|
159
|
// // we reintroduce it here
|
160
|
// e.doit = false;
|
161
|
// text.traverse(SWT.TRAVERSE_TAB_NEXT);
|
162
|
// }
|
163
|
// }
|
164
|
// });
|
165
|
// }
|
166
|
|
167
|
TableWrapData layoutData;
|
168
|
if(isMultiLine){
|
169
|
layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
|
170
|
}
|
171
|
else{
|
172
|
layoutData = LayoutConstants.FILL();
|
173
|
}
|
174
|
if (textHeight != null && textHeight > 0) {
|
175
|
(layoutData).heightHint = textHeight;
|
176
|
}
|
177
|
|
178
|
text.setLayoutData(layoutData);
|
179
|
|
180
|
text.addModifyListener(this);
|
181
|
|
182
|
|
183
|
setText();
|
184
|
}
|
185
|
|
186
|
//
|
187
|
|
188
|
protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
|
189
|
if (labelString != null) {
|
190
|
label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
|
191
|
addControl(label);
|
192
|
if(isMultiLine){
|
193
|
label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
194
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
195
|
}
|
196
|
else{
|
197
|
label.setLayoutData(LayoutConstants.LEFT());
|
198
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
199
|
}
|
200
|
}
|
201
|
}
|
202
|
|
203
|
|
204
|
|
205
|
/** {@inheritDoc} */
|
206
|
@Override
|
207
|
public void setBackground(Color color) {
|
208
|
if (label != null) {
|
209
|
label.setBackground(color);
|
210
|
}
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public void setSelected(boolean selected) {
|
215
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
216
|
}
|
217
|
|
218
|
@Override
|
219
|
public void setIrrelevant(boolean irrelevant) {
|
220
|
// TODO Auto-generated method stub
|
221
|
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public void setEnabled(boolean enabled) {
|
226
|
// TODO Auto-generated method stub
|
227
|
|
228
|
}
|
229
|
|
230
|
@Override
|
231
|
public boolean isEnabled() {
|
232
|
// TODO Auto-generated method stub
|
233
|
return false;
|
234
|
}
|
235
|
|
236
|
@Override
|
237
|
public void modifyText(ModifyEvent e) {
|
238
|
if(languageString == null){
|
239
|
languageString = LanguageString.NewInstance("", CdmStore.getDefaultLanguage());
|
240
|
}
|
241
|
|
242
|
languageString.setText(getText());
|
243
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
244
|
}
|
245
|
|
246
|
public LanguageString getLanguageString(){
|
247
|
return languageString;
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* Set the text of this composites text composite
|
252
|
*
|
253
|
* @param string
|
254
|
* a {@link java.lang.String} object.
|
255
|
*/
|
256
|
|
257
|
public void setText() {
|
258
|
|
259
|
|
260
|
String convertedString = "";
|
261
|
|
262
|
if (languageString != null){
|
263
|
Set<IntextReference> inrefs = this.languageString.getIntextReferences();
|
264
|
convertedString = languageString.getText();
|
265
|
String inLineString = null;
|
266
|
String innerText = null;
|
267
|
String htmlString = null;
|
268
|
boolean converted = true;
|
269
|
if (!languageString.getText().contains("<cdm")){
|
270
|
text.setText(languageString.getText());
|
271
|
return;
|
272
|
}
|
273
|
if (inrefs == null || inrefs.size() == 0){
|
274
|
text.setText(CdmUtils.Nz(languageString.getText()));
|
275
|
}else{
|
276
|
for (IntextReference inref: inrefs){
|
277
|
|
278
|
if (inref.getTarget() == null){
|
279
|
continue;
|
280
|
}
|
281
|
|
282
|
inLineString = RichtextHelper.findInlineString(convertedString, inref.toInlineString("(.+?)"));
|
283
|
innerText = RichtextHelper.findInText(inLineString, "(<cdm:.+>)(.+?)(</cdm:.+>)");
|
284
|
if (innerText != null){
|
285
|
htmlString = RichtextHelper.createHtml(inref, innerText);
|
286
|
}else{
|
287
|
inLineString =RichtextHelper.findInlineString(convertedString, "(<cdm:.+/>)");
|
288
|
htmlString = RichtextHelper.createHtml(inref, innerText);
|
289
|
}
|
290
|
if (inLineString != null){
|
291
|
convertedString = convertedString.replace(inLineString, htmlString);
|
292
|
System.out.println("new text after converting "+convertedString + " - " + languageString.getText());
|
293
|
}else{
|
294
|
converted = false;
|
295
|
}
|
296
|
|
297
|
}
|
298
|
|
299
|
if(!converted){
|
300
|
System.err.println("There is a problem with the language string and an intextrefernce.");
|
301
|
}else{
|
302
|
text.setText(convertedString);
|
303
|
}
|
304
|
|
305
|
}
|
306
|
}
|
307
|
|
308
|
// Listener[] listeners = text.getListeners(SWT.Modify);
|
309
|
//
|
310
|
// text.removeModifyListener(this);
|
311
|
// text.addModifyListener(this);
|
312
|
// for (Listener listener : listeners) {
|
313
|
// text.removeListener(SWT.Modify, listener);
|
314
|
// }
|
315
|
//
|
316
|
//
|
317
|
//
|
318
|
// for (Listener listener : listeners) {
|
319
|
// text.addListener(SWT.Modify, listener);
|
320
|
// }
|
321
|
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* Set the text of this composites text composite
|
326
|
*
|
327
|
* @param string
|
328
|
* a {@link java.lang.String} object.
|
329
|
*/
|
330
|
public String getText() {
|
331
|
|
332
|
|
333
|
String convertedString = "";
|
334
|
|
335
|
if (this.languageString != null){
|
336
|
Set<IntextReference> inrefs = this.languageString.getIntextReferences();
|
337
|
String inLineString = null;
|
338
|
String innerText = null;
|
339
|
String htmlString = null;
|
340
|
convertedString = text.getText();
|
341
|
System.out.println("before converting html to markup: "+convertedString);
|
342
|
for (IntextReference inref: inrefs){
|
343
|
if (inref.getTarget() == null){
|
344
|
break;
|
345
|
}
|
346
|
|
347
|
htmlString = findHtmlString(text.getText(), inref);
|
348
|
System.out.println("html"+htmlString);
|
349
|
if (htmlString != null){
|
350
|
innerText = RichtextHelper.findInText(htmlString, "(.+>)(.*?)(</span>)");
|
351
|
inLineString = RichtextHelper.createCdmMarkUp(inref, innerText);
|
352
|
|
353
|
convertedString = convertedString.replace(htmlString, inLineString);
|
354
|
|
355
|
}else{
|
356
|
convertedString = text.getText();
|
357
|
}
|
358
|
}
|
359
|
}
|
360
|
return convertedString;
|
361
|
|
362
|
|
363
|
|
364
|
|
365
|
}
|
366
|
|
367
|
|
368
|
private String findHtmlString(String htmlText, IntextReference inRef) {
|
369
|
String htmlString = RichtextHelper.createHtml(inRef, ".*?");
|
370
|
System.out.println(htmlString);
|
371
|
System.out.println(htmlText);
|
372
|
Pattern pattern = Pattern.compile(htmlString);
|
373
|
Matcher matcher = pattern.matcher(htmlText);
|
374
|
if (matcher.find())
|
375
|
{
|
376
|
String result = matcher.group();
|
377
|
return result;
|
378
|
}
|
379
|
return null;
|
380
|
|
381
|
|
382
|
}
|
383
|
|
384
|
|
385
|
|
386
|
|
387
|
|
388
|
|
389
|
|
390
|
|
391
|
}
|
392
|
|
393
|
|
394
|
|
395
|
|
396
|
|
397
|
|
398
|
|
399
|
|