1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.editor.name.e4.container;
|
11
|
|
12
|
import java.util.Iterator;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.eclipse.core.runtime.Assert;
|
18
|
import org.eclipse.e4.ui.di.UISynchronize;
|
19
|
import org.eclipse.jface.dialogs.Dialog;
|
20
|
import org.eclipse.jface.text.IDocument;
|
21
|
import org.eclipse.jface.text.Position;
|
22
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
23
|
import org.eclipse.jface.window.DefaultToolTip;
|
24
|
import org.eclipse.swt.custom.StyledText;
|
25
|
import org.eclipse.swt.dnd.DND;
|
26
|
import org.eclipse.swt.dnd.DragSource;
|
27
|
import org.eclipse.swt.dnd.Transfer;
|
28
|
import org.eclipse.swt.events.ControlAdapter;
|
29
|
import org.eclipse.swt.events.ControlEvent;
|
30
|
import org.eclipse.swt.events.ControlListener;
|
31
|
import org.eclipse.swt.events.FocusAdapter;
|
32
|
import org.eclipse.swt.events.FocusEvent;
|
33
|
import org.eclipse.swt.events.FocusListener;
|
34
|
import org.eclipse.swt.events.ModifyEvent;
|
35
|
import org.eclipse.swt.events.ModifyListener;
|
36
|
import org.eclipse.swt.events.MouseAdapter;
|
37
|
import org.eclipse.swt.events.MouseEvent;
|
38
|
import org.eclipse.swt.graphics.Color;
|
39
|
import org.eclipse.swt.graphics.Font;
|
40
|
import org.eclipse.swt.graphics.Image;
|
41
|
import org.eclipse.swt.widgets.Composite;
|
42
|
import org.eclipse.swt.widgets.Control;
|
43
|
import org.eclipse.swt.widgets.Display;
|
44
|
import org.eclipse.swt.widgets.Label;
|
45
|
import org.eclipse.ui.forms.IFormPart;
|
46
|
import org.eclipse.ui.forms.IManagedForm;
|
47
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
48
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
49
|
|
50
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
51
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
52
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
53
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
54
|
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
|
55
|
import eu.etaxonomy.taxeditor.editor.CdmDataTransfer;
|
56
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
57
|
import eu.etaxonomy.taxeditor.editor.name.container.EditorAnnotation;
|
58
|
import eu.etaxonomy.taxeditor.editor.name.container.EditorAnnotation.EditorAnnotationType;
|
59
|
import eu.etaxonomy.taxeditor.editor.name.container.IContainerConstants;
|
60
|
import eu.etaxonomy.taxeditor.editor.name.container.LineBreakListener;
|
61
|
import eu.etaxonomy.taxeditor.editor.name.container.LineWrapSupport;
|
62
|
import eu.etaxonomy.taxeditor.editor.name.container.NameViewer;
|
63
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
64
|
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDragListenerE4;
|
65
|
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDragSourceEffect;
|
66
|
import eu.etaxonomy.taxeditor.editor.name.operation.CreateSynonymInNewGroupOperation;
|
67
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
68
|
import eu.etaxonomy.taxeditor.model.IElementHasDetails;
|
69
|
import eu.etaxonomy.taxeditor.model.NameHelper;
|
70
|
import eu.etaxonomy.taxeditor.model.TextHelper;
|
71
|
import eu.etaxonomy.taxeditor.parser.ParseHandler;
|
72
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
73
|
|
74
|
/**
|
75
|
* Formats <code>GroupedComposite</code> with cosmetic and layout properties
|
76
|
* specific to the Editor. This should be used to maintain a consistent look and
|
77
|
* feel for all Editor freetext area components, such as
|
78
|
* DescriptionElementComposite.
|
79
|
* <p>
|
80
|
* Requires an <code>IManagedForm</code>, whose <code>input</code> is set to the
|
81
|
* contents of {@link #getData()} when the <code>GroupedComposite</code> gets
|
82
|
* focus, i.e. to populate the property sheet with the data.
|
83
|
* </p>
|
84
|
* <p>
|
85
|
* The <code>IManagedForm</code> is also required to have a <code>Taxon</code>
|
86
|
* in its own <code>getData()</code>.
|
87
|
* </p>
|
88
|
* <p>
|
89
|
* The <code>IManagedForm</code> can also used for drawing borders by calling
|
90
|
* the method <code>createBorderSupport()</code>.
|
91
|
* </p>
|
92
|
* @author pplitzner
|
93
|
* @date Aug 24, 2017
|
94
|
*
|
95
|
* @param <T>
|
96
|
*/
|
97
|
abstract public class AbstractGroupedContainerE4<T extends TaxonBase> implements
|
98
|
IFormPart, IContainerConstants, IElementHasDetails {
|
99
|
|
100
|
protected ParseHandler parseHandler;
|
101
|
|
102
|
private FocusListener nameCompositeFocusListener;
|
103
|
private ModifyListener nameCompositeModifyListener;
|
104
|
|
105
|
protected NameViewer nameViewer;
|
106
|
|
107
|
private AbstractGroupE4 group;
|
108
|
|
109
|
private Label nonEditableInfoLabel;
|
110
|
private DefaultToolTip nonEditableInfoHover;
|
111
|
|
112
|
private static AbstractGroupedContainerE4 selection;
|
113
|
|
114
|
private FocusListener focusListener;
|
115
|
private LineBreakListener lineBreakListener;
|
116
|
|
117
|
private int cursorPosition;
|
118
|
|
119
|
protected Composite control;
|
120
|
|
121
|
private Color backgroundColor;
|
122
|
private boolean isDirty;
|
123
|
|
124
|
private ISelectionChangedListener selectionChangedListener;
|
125
|
|
126
|
public AbstractGroupedContainerE4(AbstractGroupE4 group, T taxonBase) {
|
127
|
setData(taxonBase);
|
128
|
this.group = group;
|
129
|
parseHandler = ParseHandler.NewInstance(taxonBase.getName());
|
130
|
}
|
131
|
|
132
|
public void createContent() {
|
133
|
createControl();
|
134
|
|
135
|
createTextViewer();
|
136
|
createLineWrapSupport();
|
137
|
createLineBreakListener();
|
138
|
|
139
|
setMenu();
|
140
|
|
141
|
setDraggableControl(new Control[] { getControl(),
|
142
|
getNameViewer().getRulerControl() });
|
143
|
|
144
|
createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
|
145
|
|
146
|
initializeComposite();
|
147
|
|
148
|
createListener();
|
149
|
|
150
|
enableFreeText();
|
151
|
}
|
152
|
|
153
|
protected void createListener() {
|
154
|
nameCompositeModifyListener = new ModifyListener() {
|
155
|
|
156
|
@Override
|
157
|
public void modifyText(ModifyEvent e) {
|
158
|
// mark the composite dirty
|
159
|
setDirty(true);
|
160
|
// parse the text
|
161
|
String text = nameViewer.getTextWidget().getText();
|
162
|
|
163
|
TaxonName name = (TaxonName)parseHandler.parse(text);
|
164
|
getTaxonBase().setName(name);
|
165
|
getTaxonBase().setTitleCache((getTaxonBase().generateTitle()));
|
166
|
|
167
|
// show errors resulting from parsing
|
168
|
calculateAnnotations();
|
169
|
// store the position of the cursor
|
170
|
storeCursor();
|
171
|
// notify selection listener
|
172
|
setDelayedSelection();
|
173
|
}
|
174
|
};
|
175
|
nameCompositeFocusListener = new FocusAdapter() {
|
176
|
|
177
|
@Override
|
178
|
public void focusLost(FocusEvent e) {
|
179
|
super.focusLost(e);
|
180
|
|
181
|
persistName();
|
182
|
}
|
183
|
};
|
184
|
|
185
|
addListener();
|
186
|
}
|
187
|
|
188
|
protected void addListener() {
|
189
|
getNameViewer().getTextWidget().addModifyListener(
|
190
|
nameCompositeModifyListener);
|
191
|
getNameViewer().getTextWidget().addFocusListener(
|
192
|
nameCompositeFocusListener);
|
193
|
}
|
194
|
|
195
|
protected void removeListener() {
|
196
|
getNameViewer().getTextWidget().removeModifyListener(
|
197
|
nameCompositeModifyListener);
|
198
|
getNameViewer().getTextWidget().removeFocusListener(
|
199
|
nameCompositeFocusListener);
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* Initialize the composite specific code
|
204
|
*/
|
205
|
protected abstract void initializeComposite();
|
206
|
|
207
|
protected String getEmptyTextPrompt() {
|
208
|
return EMPTY_NAME_PROMPT;
|
209
|
}
|
210
|
|
211
|
protected void initTextViewer() {
|
212
|
|
213
|
updateIndent();
|
214
|
|
215
|
updateIcon();
|
216
|
|
217
|
String text = NameHelper.getDisplayNameWithRef(getData());
|
218
|
|
219
|
if (text.length() == 0) {
|
220
|
initEmptyText();
|
221
|
} else {
|
222
|
getNameViewer().setText(text);
|
223
|
placeCursor();
|
224
|
}
|
225
|
calculateAnnotations();
|
226
|
}
|
227
|
|
228
|
synchronized protected void calculateAnnotations() {
|
229
|
getNameViewer().clearAnnotations();
|
230
|
showAnnotations();
|
231
|
}
|
232
|
|
233
|
public void showAnnotations() {
|
234
|
|
235
|
if (getName() != null && getName().hasProblem()) {
|
236
|
showParsingProblems();
|
237
|
}
|
238
|
|
239
|
if (!isNameParsable()) {
|
240
|
getNameViewer()
|
241
|
.addAnnotation(
|
242
|
new EditorAnnotation(EditorAnnotationType.WARNING,
|
243
|
0,
|
244
|
Messages.AbstractGroupedContainer_EDIT_IN_DETAILS_VIEW));
|
245
|
}
|
246
|
|
247
|
if (isNameUsedMultipleTimes()) {
|
248
|
getNameViewer().addAnnotation(
|
249
|
new EditorAnnotation(EditorAnnotationType.WARNING, 0,
|
250
|
Messages.AbstractGroupedContainer_MULTIPLE_USE));
|
251
|
}
|
252
|
|
253
|
}
|
254
|
|
255
|
private void showParsingProblems() {
|
256
|
TaxonName name = getName();
|
257
|
if (name == null){
|
258
|
return;
|
259
|
}
|
260
|
|
261
|
List<ParserProblem> parsingProblems = name.getParsingProblems();
|
262
|
|
263
|
for (ParserProblem problem : parsingProblems) {
|
264
|
getNameViewer().addAnnotation(new EditorAnnotation(problem),
|
265
|
getParsingProblemPosition());
|
266
|
}
|
267
|
}
|
268
|
|
269
|
private Position getParsingProblemPosition() {
|
270
|
String text = getNameViewer().getTextWidget().getText();
|
271
|
|
272
|
if (getName() != null && getName().hasProblem() && text.length() > 0) {
|
273
|
int start = getName().getProblemStarts();
|
274
|
int length = getName().getProblemEnds() - start;
|
275
|
|
276
|
if (start == -1 || getName().getProblemEnds() == -1) {
|
277
|
return null;
|
278
|
}
|
279
|
|
280
|
// Don't let squigglies try to draw beyond the end of the text
|
281
|
if (text.length() < start + length) {
|
282
|
length = text.length() - start;
|
283
|
}
|
284
|
if (length<0){
|
285
|
return null;
|
286
|
}
|
287
|
return new Position(start, length);
|
288
|
}
|
289
|
return null;
|
290
|
}
|
291
|
|
292
|
protected void handleSplitText(String text) {
|
293
|
// Create a synonym in a new homotypic group using text as name
|
294
|
TaxonName synonymName = TaxonName.castAndDeproxy(
|
295
|
ParseHandler.parseReferencedName(text, null));
|
296
|
|
297
|
AbstractUtility.executeOperation(new CreateSynonymInNewGroupOperation(
|
298
|
Messages.AbstractGroupedContainer_NEW_HETERO_SYNONYM, getEditor().getUndoContext(),
|
299
|
getEditor().getTaxon(), synonymName, getEditor()), group.getContext().get(UISynchronize.class));
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Refreshes the display with latest data from the model.
|
304
|
*
|
305
|
* Note: Will not parse the text and not calculate errors!
|
306
|
*/
|
307
|
@Override
|
308
|
public void refresh() {
|
309
|
// showNameRelations();
|
310
|
|
311
|
String text = NameHelper.getDisplayNameWithRef(getTaxonBase());
|
312
|
|
313
|
if (getNameViewer().getTextWidget() == null) {
|
314
|
// we might get here via dnd. Look slike it can be ignored
|
315
|
return;
|
316
|
}
|
317
|
|
318
|
if (text.length() == 0) {
|
319
|
initEmptyText();
|
320
|
} else if (!getNameViewer().getTextWidget().getText().equals(text)) {
|
321
|
removeListener();
|
322
|
getNameViewer().getTextWidget().setText(text);
|
323
|
addListener();
|
324
|
}
|
325
|
|
326
|
updateNonEditableInfo();
|
327
|
|
328
|
updateIcon();
|
329
|
// placeCursor();
|
330
|
updateIndent();
|
331
|
|
332
|
enableFreeText();
|
333
|
}
|
334
|
|
335
|
protected abstract void updateIcon();
|
336
|
|
337
|
protected abstract void updateIndent();
|
338
|
|
339
|
protected abstract void updateNonEditableInfo();
|
340
|
|
341
|
protected void enableFreeText() {
|
342
|
setEnabled(isFreetextEditingAllowed());
|
343
|
|
344
|
}
|
345
|
|
346
|
/**
|
347
|
* Checks whether the freetext should be editable based on specific empty
|
348
|
* fields.
|
349
|
*
|
350
|
* @return
|
351
|
*/
|
352
|
private boolean isFreetextEditingAllowed() {
|
353
|
boolean enableFreetext = true;
|
354
|
|
355
|
enableFreetext |= isNameUsedMultipleTimes();
|
356
|
enableFreetext &= isNameParsable();
|
357
|
|
358
|
return enableFreetext;
|
359
|
}
|
360
|
|
361
|
/**
|
362
|
* Checks whether there are more than one, non-orphaned taxon bases
|
363
|
* attached to the taxon name
|
364
|
*
|
365
|
* @return
|
366
|
*/
|
367
|
private boolean isNameUsedMultipleTimes() {
|
368
|
|
369
|
TaxonName name = getName();
|
370
|
if (name != null){
|
371
|
Set<TaxonBase> taxonBases = name.getTaxonBases();
|
372
|
Iterator<TaxonBase> tbItr = taxonBases.iterator();
|
373
|
int nonOrphanedTaxonBaseCount = taxonBases.size();
|
374
|
|
375
|
while(tbItr.hasNext()) {
|
376
|
TaxonBase<?> tb = tbItr.next();
|
377
|
if(tb.isOrphaned()) {
|
378
|
nonOrphanedTaxonBaseCount--;
|
379
|
}
|
380
|
}
|
381
|
if(nonOrphanedTaxonBaseCount > 1) {
|
382
|
return true;
|
383
|
}
|
384
|
}
|
385
|
return false;
|
386
|
}
|
387
|
|
388
|
private boolean isNameParsable() {
|
389
|
TaxonName name = getName();
|
390
|
if (name == null){
|
391
|
return false;
|
392
|
}
|
393
|
|
394
|
boolean isParsable = true;
|
395
|
isParsable &= StringUtils.isBlank(name.getAppendedPhrase()); // taxonFieldsEmpty();
|
396
|
|
397
|
isParsable &= !name.isProtectedAuthorshipCache();
|
398
|
isParsable &= !name.isProtectedNameCache();
|
399
|
|
400
|
return isParsable;
|
401
|
}
|
402
|
|
403
|
/**
|
404
|
* Parse the text and calculate errors
|
405
|
*/
|
406
|
public void parseAndCalculateAnnotations() {
|
407
|
removeListener();
|
408
|
String unparsedNameString = getNameViewer().getTextWidget().getText();
|
409
|
parseHandler.parse(unparsedNameString);
|
410
|
addListener();
|
411
|
calculateAnnotations();
|
412
|
}
|
413
|
|
414
|
public T getTaxonBase() {
|
415
|
return getData();
|
416
|
}
|
417
|
|
418
|
public TaxonName getName() {
|
419
|
return CdmBase.deproxy(getTaxonBase().getName());
|
420
|
}
|
421
|
|
422
|
public void persistName() {
|
423
|
if (isDirty()) {
|
424
|
getNameViewer().getTextWidget().setEnabled(false);
|
425
|
final String unparsedNameString = getNameViewer().getTextWidget()
|
426
|
.getText();
|
427
|
final TaxonName name = (TaxonName)parseHandler
|
428
|
.parseAndResolveDuplicates(unparsedNameString);
|
429
|
getTaxonBase().setName(name);
|
430
|
getTaxonBase().setTitleCache((getTaxonBase().generateTitle()));
|
431
|
setDirty(false);
|
432
|
getNameViewer().getTextWidget().setEnabled(true);
|
433
|
}
|
434
|
}
|
435
|
|
436
|
public AbstractGroupE4 getGroup() {
|
437
|
if (group == null) {
|
438
|
throw new IllegalStateException("Group shall not be null."); //$NON-NLS-1$
|
439
|
}
|
440
|
return group;
|
441
|
}
|
442
|
|
443
|
public void remove() {
|
444
|
getGroup().remove(this);
|
445
|
}
|
446
|
|
447
|
protected void createControl() {
|
448
|
control = getEditor().getToolkit().createComposite(
|
449
|
getGroup().getControl());
|
450
|
|
451
|
control.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
|
452
|
TableWrapLayout layout = new TableWrapLayout();
|
453
|
layout.leftMargin = 0;
|
454
|
layout.rightMargin = 0;
|
455
|
layout.topMargin = 5;
|
456
|
layout.bottomMargin = 5;
|
457
|
|
458
|
layout.verticalSpacing = 0;
|
459
|
layout.horizontalSpacing = 0;
|
460
|
|
461
|
control.setLayout(layout);
|
462
|
|
463
|
}
|
464
|
|
465
|
protected TaxonNameEditorE4 getEditor() {
|
466
|
return getGroup().getEditor();
|
467
|
}
|
468
|
|
469
|
public Composite getControl() {
|
470
|
return control;
|
471
|
}
|
472
|
|
473
|
protected void createLineWrapSupport() {
|
474
|
new LineWrapSupport(getNameViewer(), getEditor().getManagedForm());
|
475
|
}
|
476
|
|
477
|
protected void createTextViewer() {
|
478
|
nameViewer = new NameViewer(control);
|
479
|
|
480
|
focusListener = new FocusAdapter() {
|
481
|
@Override
|
482
|
public void focusGained(FocusEvent e) {
|
483
|
if (getEditor()
|
484
|
.getGroupedContainers().isEmpty()){
|
485
|
return;
|
486
|
}
|
487
|
|
488
|
for (AbstractGroupedContainerE4 container : getEditor()
|
489
|
.getGroupedContainers()) {
|
490
|
container.colorSelected(NOT_SELECTED);
|
491
|
}
|
492
|
getEditor().getManagedForm().setInput(
|
493
|
AbstractGroupedContainerE4.this);
|
494
|
placeCursor();
|
495
|
colorSelected(SELECTED_FOCUS);
|
496
|
}
|
497
|
};
|
498
|
nameViewer.getTextWidget().addFocusListener(focusListener);
|
499
|
|
500
|
MouseAdapter mouseListener = new MouseAdapter() {
|
501
|
@Override
|
502
|
public void mouseDown(MouseEvent e) {
|
503
|
storeCursor();
|
504
|
}
|
505
|
};
|
506
|
control.addMouseListener(mouseListener);
|
507
|
nameViewer.getRulerControl().addMouseListener(mouseListener);
|
508
|
nameViewer.getTextWidget().addMouseListener(mouseListener);
|
509
|
}
|
510
|
|
511
|
public void setIcon(Image icon) {
|
512
|
getNameViewer().setIcon(icon);
|
513
|
}
|
514
|
|
515
|
public void setIndent(int indent) {
|
516
|
if (control.getLayout() instanceof TableWrapLayout) {
|
517
|
TableWrapLayout layout = ((TableWrapLayout) control.getLayout());
|
518
|
layout.leftMargin = indent;
|
519
|
layout.rightMargin = ACCEPTED_INDENT;
|
520
|
control.setLayout(layout);
|
521
|
control.layout();
|
522
|
} else {
|
523
|
throw new RuntimeException(
|
524
|
"Couldn't indent - composite's layout must be TableWrapLayout."); //$NON-NLS-1$
|
525
|
}
|
526
|
}
|
527
|
|
528
|
public void setSelected() {
|
529
|
getNameViewer().getTextWidget().setFocus();
|
530
|
}
|
531
|
|
532
|
public boolean isSelected() {
|
533
|
return getEditor().getSelectedContainer() == this;
|
534
|
}
|
535
|
|
536
|
public void colorSelected(int mode) {
|
537
|
if (!control.isDisposed()) {
|
538
|
String colorString = null;
|
539
|
|
540
|
switch (mode) {
|
541
|
case SELECTED_FOCUS:
|
542
|
colorString = Resources.COLOR_CONTROL_SELECTED_FOCUS;
|
543
|
break;
|
544
|
case SELECTED_NO_FOCUS:
|
545
|
colorString = Resources.COLOR_CONTROL_SELECTED;
|
546
|
break;
|
547
|
default:
|
548
|
colorString = Resources.COLOR_COMPOSITE_BACKGROUND;
|
549
|
}
|
550
|
|
551
|
backgroundColor = AbstractUtility.getColor(colorString);
|
552
|
|
553
|
setBackground(backgroundColor);
|
554
|
}
|
555
|
}
|
556
|
|
557
|
private boolean isExecuting = false;
|
558
|
|
559
|
protected void setDelayedSelection() {
|
560
|
// TODO this might be done better
|
561
|
// this is the quickest solution i could come up with and it improves
|
562
|
// performance
|
563
|
// please reimplement if you know better.
|
564
|
selection = this;
|
565
|
|
566
|
// start timer
|
567
|
Display display = Display.getCurrent();
|
568
|
Runnable runnable = new Runnable() {
|
569
|
|
570
|
@Override
|
571
|
public void run() {
|
572
|
getEditor().getManagedForm().setInput(selection);
|
573
|
isExecuting = false;
|
574
|
}
|
575
|
};
|
576
|
if(!isExecuting){
|
577
|
isExecuting = true;
|
578
|
display.timerExec(1000, runnable);
|
579
|
}
|
580
|
|
581
|
}
|
582
|
|
583
|
public void setBackground(Color color) {
|
584
|
control.setBackground(color);
|
585
|
|
586
|
for (Control child : control.getChildren()) {
|
587
|
child.setBackground(color);
|
588
|
}
|
589
|
|
590
|
getNameViewer().setBackground(color);
|
591
|
}
|
592
|
|
593
|
public void setFont(Font font) {
|
594
|
getNameViewer().getTextWidget().setFont(font);
|
595
|
}
|
596
|
|
597
|
public NameViewer getNameViewer() {
|
598
|
if (nameViewer == null) {
|
599
|
throw new RuntimeException(
|
600
|
"The Name Viewer is corrupt for Name Container: " //$NON-NLS-1$
|
601
|
+ getTaxonBase().getName().getTitleCache());
|
602
|
}
|
603
|
return nameViewer;
|
604
|
}
|
605
|
|
606
|
public void createEmptyViewerPrompt(final String prompt) {
|
607
|
|
608
|
Assert.isNotNull(getNameViewer());
|
609
|
|
610
|
final StyledText textControl = getNameViewer().getTextWidget();
|
611
|
final IDocument document = getNameViewer().getDocument();
|
612
|
|
613
|
setFocusListener(new FocusListener() {
|
614
|
|
615
|
@Override
|
616
|
public void focusGained(FocusEvent e) {
|
617
|
if (document.get().equals(prompt)) {
|
618
|
textControl.setFont(getViewerFont());
|
619
|
document.set(""); //$NON-NLS-1$
|
620
|
}
|
621
|
}
|
622
|
|
623
|
@Override
|
624
|
public void focusLost(FocusEvent e) {
|
625
|
if (document.getLength() == 0) {
|
626
|
initEmptyText();
|
627
|
}
|
628
|
}
|
629
|
|
630
|
});
|
631
|
textControl.addFocusListener(getFocusListener());
|
632
|
|
633
|
if (document.getLength() == 0) {
|
634
|
textControl.setFont(AbstractUtility
|
635
|
.getFont(Resources.FONT_DEFAULT_PROMPT));
|
636
|
document.set(prompt);
|
637
|
}
|
638
|
}
|
639
|
|
640
|
abstract protected Font getViewerFont();
|
641
|
|
642
|
protected void initEmptyText() {
|
643
|
Font defaultFont = AbstractUtility.getFont(Resources.FONT_DEFAULT_PROMPT);
|
644
|
getNameViewer().getTextWidget().setFont(defaultFont);
|
645
|
|
646
|
getNameViewer().getDocument().set(getEmptyTextPrompt());
|
647
|
placeCursor();
|
648
|
}
|
649
|
|
650
|
protected void setFocusListener(FocusListener focusListener) {
|
651
|
this.focusListener = focusListener;
|
652
|
}
|
653
|
|
654
|
private FocusListener getFocusListener() {
|
655
|
return focusListener;
|
656
|
}
|
657
|
|
658
|
public void setDirty(boolean isDirty) {
|
659
|
if (isDirty) {
|
660
|
getEditor().getManagedForm().dirtyStateChanged();
|
661
|
}
|
662
|
this.isDirty = isDirty;
|
663
|
}
|
664
|
|
665
|
@Override
|
666
|
public boolean isDirty() {
|
667
|
return isDirty;
|
668
|
}
|
669
|
|
670
|
public void setMenu() {
|
671
|
getEditor().getMenuService().registerContextMenu(getNameViewer().getTextWidget(), "eu.etaxonomy.taxeditor.editor.popupmenu.nameeditor");
|
672
|
}
|
673
|
|
674
|
private Control[] draggableControls;
|
675
|
|
676
|
protected void setDraggableControl(Control[] controls) {
|
677
|
draggableControls = controls;
|
678
|
}
|
679
|
|
680
|
public void setIsDraggable(boolean draggable) {
|
681
|
|
682
|
if (draggable) {
|
683
|
|
684
|
if (draggableControls == null) {
|
685
|
throw new NullPointerException(
|
686
|
"Draggable controls must be set to add draggability"); //$NON-NLS-1$
|
687
|
}
|
688
|
|
689
|
Transfer[] types = new Transfer[] { CdmDataTransfer.getInstance() };
|
690
|
int operations = DND.DROP_MOVE;
|
691
|
|
692
|
for (Control draggableControl : draggableControls) {
|
693
|
DragSource dragSource = new DragSource(draggableControl,
|
694
|
operations);
|
695
|
dragSource.setTransfer(types);
|
696
|
|
697
|
dragSource.addDragListener(new NameEditorDragListenerE4(this));
|
698
|
dragSource.setDragSourceEffect(new NameEditorDragSourceEffect(
|
699
|
control));
|
700
|
}
|
701
|
}
|
702
|
}
|
703
|
|
704
|
private String nonEditableText;
|
705
|
|
706
|
ControlListener nonEditableResizeListener = new ControlAdapter() {
|
707
|
|
708
|
int width = 0;
|
709
|
|
710
|
@Override
|
711
|
public void controlResized(ControlEvent e) {
|
712
|
if (nonEditableInfoLabel.getBounds().width == width) {
|
713
|
return;
|
714
|
}
|
715
|
width = nonEditableInfoLabel.getBounds().width;
|
716
|
if (nonEditableInfoLabel.getBounds().width > 0) {
|
717
|
nonEditableInfoLabel.setText(Dialog.shortenText(
|
718
|
nonEditableText.toUpperCase(), nonEditableInfoLabel));
|
719
|
}
|
720
|
}
|
721
|
};
|
722
|
|
723
|
private String nonEditableHoverText;
|
724
|
|
725
|
private LabelEllipsisListener nonEditableLabelEllipsisListener;
|
726
|
|
727
|
private T data;
|
728
|
|
729
|
/**
|
730
|
* nonEditableInfo is a label displayed underneath a GroupedComposite's
|
731
|
* input field. For instance, NameComposites display things like name
|
732
|
* relations, sec. references, etc. here.
|
733
|
*
|
734
|
* @param info
|
735
|
* the text to display in the label
|
736
|
* @param append
|
737
|
* whether the string should be appended to text that is already
|
738
|
* shown in the label
|
739
|
*/
|
740
|
public void setNonEditableInfo(String info, boolean append) {
|
741
|
// TODO non editable info should only be drawn once, when everything
|
742
|
// else is drawn
|
743
|
info = info.toUpperCase();
|
744
|
|
745
|
if (append) {
|
746
|
nonEditableText += ", " + info; //$NON-NLS-1$
|
747
|
nonEditableHoverText += "\n" + info; //$NON-NLS-1$
|
748
|
} else {
|
749
|
nonEditableText = info;
|
750
|
nonEditableHoverText = info;
|
751
|
}
|
752
|
|
753
|
if (nonEditableInfoLabel == null) {
|
754
|
nonEditableInfoLabel = getEditor().getToolkit().createLabel(
|
755
|
control, ""); //$NON-NLS-1$
|
756
|
TableWrapData layoutData = new TableWrapData(
|
757
|
TableWrapData.FILL_GRAB, TableWrapData.TOP);
|
758
|
// Set indent to viewer ruler's width
|
759
|
if (getNameViewer().getRulerControl() != null) {
|
760
|
// TODO right justify
|
761
|
layoutData.indent = NameViewer.RULER_WIDTH;
|
762
|
}
|
763
|
nonEditableInfoLabel.setLayoutData(layoutData);
|
764
|
|
765
|
nonEditableLabelEllipsisListener = new LabelEllipsisListener(
|
766
|
nonEditableInfoLabel) {
|
767
|
@Override
|
768
|
public String getLabelText() {
|
769
|
return nonEditableText.toUpperCase();
|
770
|
}
|
771
|
};
|
772
|
nonEditableInfoLabel
|
773
|
.addControlListener(nonEditableLabelEllipsisListener);
|
774
|
|
775
|
nonEditableInfoHover = new DefaultToolTip(nonEditableInfoLabel);
|
776
|
nonEditableInfoHover.setRespectDisplayBounds(true);
|
777
|
|
778
|
}
|
779
|
nonEditableInfoHover.setText(nonEditableHoverText);
|
780
|
nonEditableInfoLabel.setText(nonEditableText);
|
781
|
|
782
|
calculateAnnotations();
|
783
|
}
|
784
|
|
785
|
@Override
|
786
|
public T getData() {
|
787
|
return data;
|
788
|
}
|
789
|
|
790
|
public void setData(T data) {
|
791
|
this.data = HibernateProxyHelper.deproxy(data);
|
792
|
}
|
793
|
|
794
|
/**
|
795
|
* If the user hitting carriage return should cause something to happen -
|
796
|
* i.e. the creation of a new composite - call this method and override the
|
797
|
* method handleSplitText().
|
798
|
*/
|
799
|
protected void createLineBreakListener() {
|
800
|
lineBreakListener = new LineBreakListener() {
|
801
|
@Override
|
802
|
public void handleSplitText(String text) {
|
803
|
AbstractGroupedContainerE4.this.handleSplitText(text);
|
804
|
}
|
805
|
|
806
|
};
|
807
|
|
808
|
getNameViewer().getTextWidget().addVerifyListener(lineBreakListener);
|
809
|
|
810
|
}
|
811
|
|
812
|
abstract class LabelEllipsisListener extends ControlAdapter {
|
813
|
|
814
|
private final Label label;
|
815
|
int width = 0;
|
816
|
|
817
|
LabelEllipsisListener(Label label) {
|
818
|
this.label = label;
|
819
|
}
|
820
|
|
821
|
abstract public String getLabelText();
|
822
|
|
823
|
@Override
|
824
|
public void controlResized(ControlEvent e) {
|
825
|
if (label.getBounds().width == width) {
|
826
|
return;
|
827
|
}
|
828
|
width = label.getBounds().width;
|
829
|
if (label.getBounds().width > 0) {
|
830
|
label.setText(TextHelper.shortenText(getLabelText(), label));
|
831
|
}
|
832
|
}
|
833
|
}
|
834
|
|
835
|
public void storeCursor() {
|
836
|
this.cursorPosition = getNameViewer().getCursorPosition();
|
837
|
}
|
838
|
|
839
|
/**
|
840
|
* Puts the cursor to the position it was last seen on or to the end of line
|
841
|
* if no former position is known.
|
842
|
*/
|
843
|
public void placeCursor() {
|
844
|
if (cursorPosition == 0) {
|
845
|
getNameViewer().setCursorToEOL();
|
846
|
} else {
|
847
|
getNameViewer().setCursorPosition(cursorPosition);
|
848
|
}
|
849
|
}
|
850
|
|
851
|
public void restoreColor() {
|
852
|
setBackground(backgroundColor);
|
853
|
}
|
854
|
|
855
|
@Override
|
856
|
public void initialize(IManagedForm form) {
|
857
|
// TODO Auto-generated method stub
|
858
|
|
859
|
}
|
860
|
|
861
|
@Override
|
862
|
public void dispose() {
|
863
|
if (getControl() != null) {
|
864
|
setMenu();
|
865
|
getControl().dispose();
|
866
|
}
|
867
|
}
|
868
|
|
869
|
@Override
|
870
|
public void commit(boolean onSave) {
|
871
|
if (isDirty()) {
|
872
|
persistName();
|
873
|
}
|
874
|
}
|
875
|
|
876
|
@Override
|
877
|
public boolean setFormInput(Object input) {
|
878
|
return false;
|
879
|
}
|
880
|
|
881
|
@Override
|
882
|
public void setFocus() {
|
883
|
getNameViewer().getControl().setFocus();
|
884
|
}
|
885
|
|
886
|
@Override
|
887
|
public boolean isStale() {
|
888
|
return false;
|
889
|
}
|
890
|
|
891
|
public void setDisabled(boolean disabled) {
|
892
|
setEnabled(!disabled);
|
893
|
}
|
894
|
|
895
|
public void setEnabled(boolean enabled) {
|
896
|
Color color = enabled ? control.getForeground() : AbstractUtility.getColor(Resources.COLOR_DISABLED_EDITOR);
|
897
|
|
898
|
getNameViewer().getTextWidget().setEditable(enabled);
|
899
|
getNameViewer().getTextWidget().setForeground(color);
|
900
|
}
|
901
|
|
902
|
|
903
|
}
|