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.container;
|
11
|
|
12
|
import java.util.List;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import javax.naming.OperationNotSupportedException;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.eclipse.core.runtime.Assert;
|
19
|
import org.eclipse.jface.dialogs.Dialog;
|
20
|
import org.eclipse.swt.dnd.DND;
|
21
|
import org.eclipse.swt.dnd.DragSource;
|
22
|
import org.eclipse.swt.dnd.Transfer;
|
23
|
import org.eclipse.swt.events.ControlAdapter;
|
24
|
import org.eclipse.swt.events.ControlEvent;
|
25
|
import org.eclipse.swt.events.ControlListener;
|
26
|
import org.eclipse.swt.events.FocusAdapter;
|
27
|
import org.eclipse.swt.events.FocusEvent;
|
28
|
import org.eclipse.swt.events.FocusListener;
|
29
|
import org.eclipse.swt.events.ModifyEvent;
|
30
|
import org.eclipse.swt.events.ModifyListener;
|
31
|
import org.eclipse.swt.events.MouseAdapter;
|
32
|
import org.eclipse.swt.events.MouseEvent;
|
33
|
import org.eclipse.swt.graphics.Color;
|
34
|
import org.eclipse.swt.graphics.Font;
|
35
|
import org.eclipse.swt.graphics.Image;
|
36
|
import org.eclipse.swt.widgets.Composite;
|
37
|
import org.eclipse.swt.widgets.Control;
|
38
|
import org.eclipse.swt.widgets.Display;
|
39
|
import org.eclipse.swt.widgets.Label;
|
40
|
import org.eclipse.swt.widgets.Menu;
|
41
|
import org.eclipse.ui.forms.IFormPart;
|
42
|
import org.eclipse.ui.forms.IManagedForm;
|
43
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
44
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
45
|
|
46
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
47
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
48
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
49
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
50
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
51
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
52
|
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
|
53
|
import eu.etaxonomy.taxeditor.editor.CdmDataTransfer;
|
54
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
55
|
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
|
56
|
import eu.etaxonomy.taxeditor.editor.name.dnd.NameEditorDragListener;
|
57
|
import eu.etaxonomy.taxeditor.editor.name.dnd.NameEditorDragSourceEffect;
|
58
|
import eu.etaxonomy.taxeditor.editor.name.operation.CreateSynonymInNewGroupOperation;
|
59
|
import eu.etaxonomy.taxeditor.labels.ILabelImageStrategy;
|
60
|
import eu.etaxonomy.taxeditor.labels.LabelImageProvider;
|
61
|
import eu.etaxonomy.taxeditor.model.IElementHasDetails;
|
62
|
import eu.etaxonomy.taxeditor.model.NameHelper;
|
63
|
import eu.etaxonomy.taxeditor.model.TextHelper;
|
64
|
import eu.etaxonomy.taxeditor.parser.ParseHandler;
|
65
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
66
|
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.EditorAnnotationFacade;
|
67
|
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.EditorAnnotationFacade.EditorAnnotationType;
|
68
|
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.INameViewer;
|
69
|
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.LineBreakListenerFacade;
|
70
|
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.LineWrapSupportFacade;
|
71
|
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.NameViewerFacade;
|
72
|
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.jface.text.IPosition;
|
73
|
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.jface.text.PositionFacade;
|
74
|
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.jface.window.IToolTip;
|
75
|
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.jface.window.ToolTipFacade;
|
76
|
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.swt.widgets.DisplayProxy;
|
77
|
|
78
|
/**
|
79
|
* Formats <code>GroupedComposite</code> with cosmetic and layout properties specific to the
|
80
|
* Editor. This should be used to maintain a consistent look and feel for all Editor
|
81
|
* freetext area components, such as DescriptionElementComposite.
|
82
|
* <p>
|
83
|
* Requires an <code>IManagedForm</code>, whose <code>input</code> is set to the contents
|
84
|
* of {@link #getData()} when the <code>GroupedComposite</code> gets focus, i.e. to
|
85
|
* populate the property sheet with the data.
|
86
|
* </p>
|
87
|
* <p>
|
88
|
* The <code>IManagedForm</code> is also required to have a <code>Taxon</code> in its
|
89
|
* own <code>getData()</code>.
|
90
|
* </p>
|
91
|
* <p>
|
92
|
* The <code>IManagedForm</code> can also used for drawing borders by calling the method
|
93
|
* <code>createBorderSupport()</code>.
|
94
|
* </p>
|
95
|
*
|
96
|
* @author p.ciardelli
|
97
|
* @author n.hoffmann
|
98
|
* @created 02.06.2008
|
99
|
* @version 1.0
|
100
|
*/
|
101
|
abstract public class AbstractGroupedContainer<T extends TaxonBase> implements IFormPart, IContainerConstants, IElementHasDetails {
|
102
|
private static final Logger logger = Logger.getLogger(AbstractGroupedContainer.class);
|
103
|
|
104
|
protected ParseHandler parseHandler;
|
105
|
|
106
|
private FocusListener nameCompositeFocusListener;
|
107
|
private ModifyListener nameCompositeModifyListener;
|
108
|
|
109
|
protected NameViewerFacade nameViewer;
|
110
|
|
111
|
private AbstractGroup group;
|
112
|
|
113
|
private Label nonEditableInfoLabel;
|
114
|
private IToolTip nonEditableInfoHover;
|
115
|
|
116
|
private static AbstractGroupedContainer selection;
|
117
|
|
118
|
private FocusListener focusListener;
|
119
|
private LineBreakListenerFacade lineBreakListener;
|
120
|
|
121
|
private int cursorPosition;
|
122
|
|
123
|
protected Composite control;
|
124
|
|
125
|
private Color backgroundColor;
|
126
|
private boolean isDirty;
|
127
|
|
128
|
|
129
|
/**
|
130
|
* <p>Constructor for AbstractGroupedContainer.</p>
|
131
|
*
|
132
|
* @param editor a {@link eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor} object.
|
133
|
* @param group a {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroup} object.
|
134
|
* @param taxonBase a T object.
|
135
|
* @param <T> a T object.
|
136
|
*/
|
137
|
public AbstractGroupedContainer(T taxonBase){
|
138
|
setData(taxonBase);
|
139
|
parseHandler = ParseHandler.NewInstance(taxonBase.getName());
|
140
|
}
|
141
|
|
142
|
public void createContent(){
|
143
|
createControl();
|
144
|
|
145
|
try {
|
146
|
createTextViewer();
|
147
|
createLineWrapSupport();
|
148
|
} catch (OperationNotSupportedException e) {
|
149
|
// TODO Auto-generated catch block
|
150
|
e.printStackTrace();
|
151
|
}
|
152
|
createLineBreakListener();
|
153
|
|
154
|
setMenu(getEditor().getMenu());
|
155
|
|
156
|
setDraggableControl(new Control[]{getControl(), getNameViewer().getDraggableControl()});
|
157
|
|
158
|
createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
|
159
|
|
160
|
|
161
|
initializeComposite();
|
162
|
|
163
|
createListener();
|
164
|
|
165
|
try {
|
166
|
isFreeTextEnabled();
|
167
|
} catch (OperationNotSupportedException e) {
|
168
|
logger.warn("AbstractGroupedContainer.initTextViewer(): EditorAnnotations not supported :" + e.getMessage() );
|
169
|
}
|
170
|
}
|
171
|
|
172
|
|
173
|
|
174
|
/**
|
175
|
* <p>createListener</p>
|
176
|
*/
|
177
|
protected void createListener(){
|
178
|
nameCompositeModifyListener = new ModifyListener(){
|
179
|
|
180
|
public void modifyText(ModifyEvent e) {
|
181
|
// mark the composite dirty
|
182
|
setDirty(true);
|
183
|
// parse the text
|
184
|
String text = nameViewer.getText();
|
185
|
|
186
|
NonViralName name = parseHandler.parse(text);
|
187
|
getTaxonBase().setName(name);
|
188
|
getTaxonBase().setTitleCache((getTaxonBase().generateTitle()));
|
189
|
|
190
|
// show errors resulting from parsing
|
191
|
try {
|
192
|
calculateAnnotations();
|
193
|
} catch (OperationNotSupportedException e1) {
|
194
|
logger.warn("AbstractGroupedContainer.createListener(): EditorAnnotations not supported :" + e1.getMessage() );
|
195
|
}
|
196
|
// store the position of the cursor
|
197
|
storeCursor();
|
198
|
// notify selection listener
|
199
|
setDelayedSelection();
|
200
|
}
|
201
|
};
|
202
|
nameCompositeFocusListener = new FocusAdapter(){
|
203
|
|
204
|
/* (non-Javadoc)
|
205
|
* @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)
|
206
|
*/
|
207
|
@Override
|
208
|
public void focusLost(FocusEvent e) {
|
209
|
super.focusLost(e);
|
210
|
|
211
|
persistName();
|
212
|
}
|
213
|
};
|
214
|
|
215
|
addListener();
|
216
|
}
|
217
|
|
218
|
private void addListener(){
|
219
|
getNameViewer().addModifyListener(nameCompositeModifyListener);
|
220
|
getNameViewer().addFocusListener(nameCompositeFocusListener);
|
221
|
}
|
222
|
|
223
|
private void removeListener(){
|
224
|
getNameViewer().removeModifyListener(nameCompositeModifyListener);
|
225
|
getNameViewer().removeFocusListener(nameCompositeFocusListener);
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Initialize the composite specific code
|
230
|
*/
|
231
|
protected abstract void initializeComposite();
|
232
|
|
233
|
/**
|
234
|
* <p>getEmptyTextPrompt</p>
|
235
|
*
|
236
|
* @return a {@link java.lang.String} object.
|
237
|
*/
|
238
|
protected String getEmptyTextPrompt() {
|
239
|
return EMPTY_NAME_PROMPT;
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
*
|
244
|
*/
|
245
|
private void showNameRelations() {
|
246
|
TaxonNameBase<?, ?> name = getName();
|
247
|
if (name == null) {
|
248
|
return;
|
249
|
}
|
250
|
|
251
|
ILabelImageStrategy strategy = LabelImageProvider.getLabelStrategy(name);
|
252
|
LabelImageProvider labelProvider = new LabelImageProvider(strategy);
|
253
|
|
254
|
Set<NameRelationship> nameRelations = name.getNameRelations();
|
255
|
if (nameRelations.size() == 0) {
|
256
|
return;
|
257
|
}
|
258
|
// for (NameRelationship nameRelation : nameRelations) {
|
259
|
// String typeLabel = null;
|
260
|
// TaxonNameBase<?, ?> relatedName = null;
|
261
|
//
|
262
|
// if (name.equals(nameRelation.getFromName())) {
|
263
|
// typeLabel = labelProvider.getNameRelationTypeLabel(
|
264
|
// nameRelation.getType());
|
265
|
// relatedName = nameRelation.getToName();
|
266
|
// } else {
|
267
|
// typeLabel = labelProvider.getNameRelationTypeInverseLabel(
|
268
|
// nameRelation.getType());
|
269
|
// relatedName = nameRelation.getFromName();
|
270
|
// }
|
271
|
//
|
272
|
// setNonEditableInfo(typeLabel + " " + NameHelper.getDisplayName(relatedName));
|
273
|
// }
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* <p>initTextViewer</p>
|
278
|
*/
|
279
|
protected void initTextViewer() {
|
280
|
|
281
|
// showNameRelations();
|
282
|
|
283
|
updateIndent();
|
284
|
|
285
|
updateIcon();
|
286
|
|
287
|
String text = NameHelper.getDisplayNameWithRef(getData());
|
288
|
|
289
|
if (text.length() == 0) {
|
290
|
initEmptyText();
|
291
|
} else {
|
292
|
getNameViewer().setText(text);
|
293
|
placeCursor();
|
294
|
}
|
295
|
try {
|
296
|
calculateAnnotations();
|
297
|
} catch (OperationNotSupportedException e) {
|
298
|
logger.warn("AbstractGroupedContainer.initTextViewer(): EditorAnnotations not supported :" + e.getMessage() );
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* <p>calculateErrors</p>
|
304
|
* @throws OperationNotSupportedException
|
305
|
*/
|
306
|
synchronized protected void calculateAnnotations() throws OperationNotSupportedException {
|
307
|
getNameViewer().clearAnnotations();
|
308
|
showAnnotations();
|
309
|
}
|
310
|
|
311
|
/**
|
312
|
*
|
313
|
*/
|
314
|
public void showAnnotations() throws OperationNotSupportedException {
|
315
|
|
316
|
if(getName().hasProblem()){
|
317
|
showParsingProblems();
|
318
|
}
|
319
|
|
320
|
if(!isNameParsable()){
|
321
|
getNameViewer().addAnnotation(
|
322
|
new EditorAnnotationFacade(EditorAnnotationType.WARNING, 0, "This name may only be edited in the details view."));
|
323
|
}
|
324
|
|
325
|
if(isNameUsedMultipleTimes()){
|
326
|
getNameViewer().addAnnotation(new EditorAnnotationFacade(EditorAnnotationType.WARNING, 0, "This taxons name is used multiple times."));
|
327
|
}
|
328
|
|
329
|
|
330
|
}
|
331
|
|
332
|
/**
|
333
|
* @throws OperationNotSupportedException
|
334
|
*
|
335
|
*/
|
336
|
private void showParsingProblems() throws OperationNotSupportedException {
|
337
|
String text = getNameViewer().getText();
|
338
|
|
339
|
List<ParserProblem> parsingProblems = getName().getParsingProblems();
|
340
|
|
341
|
for (ParserProblem problem : parsingProblems) {
|
342
|
getNameViewer().addAnnotation(new EditorAnnotationFacade(problem), getParsingProblemPosition());
|
343
|
}
|
344
|
}
|
345
|
|
346
|
private IPosition getParsingProblemPosition() throws OperationNotSupportedException{
|
347
|
String text = getNameViewer().getText();
|
348
|
|
349
|
if (getName().hasProblem() && text.length() > 0) {
|
350
|
int start = getName().getProblemStarts();
|
351
|
int length = getName().getProblemEnds() - start;
|
352
|
|
353
|
if (start == -1 || getName().getProblemEnds() == -1) {
|
354
|
return null;
|
355
|
}
|
356
|
|
357
|
// Don't let squigglies try to draw beyond the end of the text
|
358
|
if (text.length() < start + length) {
|
359
|
length = text.length() - start;
|
360
|
}
|
361
|
|
362
|
return new PositionFacade(start, length);
|
363
|
}
|
364
|
return null;
|
365
|
}
|
366
|
|
367
|
/**
|
368
|
* <p>handleSplitText</p>
|
369
|
*
|
370
|
* @param text a {@link java.lang.String} object.
|
371
|
*/
|
372
|
public void handleSplitText(String text) {
|
373
|
// Create a synonym in a new homotypic group using text as name
|
374
|
TaxonNameBase synonymName = ParseHandler.parseReferencedName(text, null);
|
375
|
|
376
|
EditorUtil.executeOperation(new CreateSynonymInNewGroupOperation
|
377
|
("New Heterotypic Synonym", getEditor().getUndoContext(), getEditor().getTaxon(), synonymName, getEditor()));
|
378
|
}
|
379
|
|
380
|
/**
|
381
|
* Refreshes the display with latest data from the model.
|
382
|
*
|
383
|
* Note: Will not parse the text and not calculate errors!
|
384
|
*/
|
385
|
public void refresh() {
|
386
|
// showNameRelations();
|
387
|
|
388
|
String text = NameHelper.getDisplayNameWithRef(getTaxonBase());
|
389
|
|
390
|
/* RAP SS-Exclude, obviously not needed
|
391
|
// if(getNameViewer().getTextWidget() == null){
|
392
|
// // we might get here via dnd. Look slike it can be ignored
|
393
|
// return;
|
394
|
// }
|
395
|
*/
|
396
|
|
397
|
if (text.length() == 0) {
|
398
|
initEmptyText();
|
399
|
} else if(! getNameViewer().getText().equals(text)) {
|
400
|
removeListener();
|
401
|
getNameViewer().setText(text);
|
402
|
addListener();
|
403
|
}
|
404
|
|
405
|
updateNonEditableInfo();
|
406
|
|
407
|
updateIcon();
|
408
|
// placeCursor();
|
409
|
updateIndent();
|
410
|
|
411
|
try {
|
412
|
isFreeTextEnabled();
|
413
|
} catch (OperationNotSupportedException e) {
|
414
|
logger.warn("AbstractGroupedContainer.initTextViewer(): EditorAnnotations not supported :" + e.getMessage() );
|
415
|
}
|
416
|
}
|
417
|
|
418
|
/**
|
419
|
*
|
420
|
*/
|
421
|
protected abstract void updateIcon();
|
422
|
|
423
|
|
424
|
protected abstract void updateIndent();
|
425
|
|
426
|
|
427
|
/**
|
428
|
* <p>updateNonEditableInfo</p>
|
429
|
*/
|
430
|
protected abstract void updateNonEditableInfo();
|
431
|
|
432
|
/**
|
433
|
* @throws OperationNotSupportedException
|
434
|
*
|
435
|
*/
|
436
|
private void isFreeTextEnabled() throws OperationNotSupportedException {
|
437
|
// Decide whether editing of freetext is allowed or not.
|
438
|
if (freetextEditingAllowed()) {
|
439
|
|
440
|
// set editable
|
441
|
getNameViewer().setEditable(true);
|
442
|
|
443
|
// restore foreground font color again
|
444
|
getNameViewer().setForeground(control.getForeground());
|
445
|
} else {
|
446
|
// set non-editable
|
447
|
getNameViewer().setEditable(false);
|
448
|
|
449
|
// grey out text as a non-editable indicator
|
450
|
getNameViewer().setForeground(EditorUtil.getColor(Resources.COLOR_DISABLED_EDITOR));
|
451
|
}
|
452
|
}
|
453
|
|
454
|
|
455
|
/**
|
456
|
* Checks whether the freetext should be editable based on specific empty fields.
|
457
|
* @return
|
458
|
*/
|
459
|
private boolean freetextEditingAllowed() {
|
460
|
NonViralName name = (NonViralName) HibernateProxyHelper.deproxy(getName());
|
461
|
boolean enableFreetext = true;
|
462
|
|
463
|
enableFreetext |= isNameUsedMultipleTimes();
|
464
|
enableFreetext &= isNameParsable();
|
465
|
|
466
|
return enableFreetext;
|
467
|
}
|
468
|
|
469
|
private boolean isNameUsedMultipleTimes(){
|
470
|
if(getName().getTaxonBases().size() > 1){
|
471
|
return true;
|
472
|
}
|
473
|
return false;
|
474
|
}
|
475
|
|
476
|
private boolean isNameParsable(){
|
477
|
TaxonNameBase name = getName();
|
478
|
|
479
|
boolean isParsable = true;
|
480
|
isParsable &= CdmUtils.isEmpty(name.getAppendedPhrase()); //taxonFieldsEmpty();
|
481
|
|
482
|
if(name instanceof NonViralName){
|
483
|
NonViralName nonViralName = (NonViralName) name;
|
484
|
isParsable &= ! nonViralName.isProtectedAuthorshipCache();
|
485
|
isParsable &= ! nonViralName.isProtectedNameCache();
|
486
|
}
|
487
|
|
488
|
return isParsable;
|
489
|
}
|
490
|
|
491
|
/**
|
492
|
* Parse the text and calculate errors
|
493
|
* @throws OperationNotSupportedException
|
494
|
*/
|
495
|
public void parseAndCalculateAnnotations() throws OperationNotSupportedException{
|
496
|
removeListener();
|
497
|
String unparsedNameString = getNameViewer().getText();
|
498
|
parseHandler.parse(unparsedNameString);
|
499
|
addListener();
|
500
|
calculateAnnotations();
|
501
|
}
|
502
|
|
503
|
/**
|
504
|
* <p>getTaxonBase</p>
|
505
|
*
|
506
|
* @return the taxonBase
|
507
|
*/
|
508
|
public T getTaxonBase() {
|
509
|
return getData();
|
510
|
}
|
511
|
|
512
|
/**
|
513
|
* <p>getName</p>
|
514
|
*
|
515
|
* @return a {@link eu.etaxonomy.cdm.model.name.TaxonNameBase} object.
|
516
|
*/
|
517
|
public TaxonNameBase getName(){
|
518
|
return (TaxonNameBase) HibernateProxyHelper.deproxy(getTaxonBase().getName());
|
519
|
}
|
520
|
|
521
|
/**
|
522
|
* <p>persistName</p>
|
523
|
*/
|
524
|
public void persistName(){
|
525
|
if(isDirty()){
|
526
|
try {
|
527
|
getNameViewer().setEnabled(false);
|
528
|
} catch (OperationNotSupportedException e) {
|
529
|
logger.warn("AbstractGroupedContainer.persistName(): " + e.getMessage() );
|
530
|
}
|
531
|
final String unparsedNameString = getNameViewer().getText();
|
532
|
// Job job = new Job("Persisting Name"){
|
533
|
//
|
534
|
// @Override
|
535
|
// protected IStatus run(IProgressMonitor monitor) {
|
536
|
//
|
537
|
final NonViralName name = parseHandler.parseAndResolveDuplicates(unparsedNameString);
|
538
|
//
|
539
|
// Display.getDefault().asyncExec(new Runnable(){
|
540
|
// public void run() {
|
541
|
getTaxonBase().setName(name);
|
542
|
getTaxonBase().setTitleCache((getTaxonBase().generateTitle()));
|
543
|
setDirty(false);
|
544
|
try {
|
545
|
getNameViewer().setEnabled(true);
|
546
|
} catch (OperationNotSupportedException e) {
|
547
|
logger.warn("AbstractGroupedContainer.persistName(): " + e.getMessage() );
|
548
|
}
|
549
|
// };
|
550
|
// });
|
551
|
//
|
552
|
//
|
553
|
// return Status.OK_STATUS;
|
554
|
// }
|
555
|
//
|
556
|
// };
|
557
|
//
|
558
|
// job.setPriority(Job.DECORATE);
|
559
|
// job.schedule();
|
560
|
}
|
561
|
}
|
562
|
|
563
|
/**
|
564
|
* <p>Getter for the field <code>group</code>.</p>
|
565
|
*
|
566
|
* @return a {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroup} object.
|
567
|
*/
|
568
|
public AbstractGroup getGroup(){
|
569
|
if(group == null){
|
570
|
throw new IllegalStateException("Group shall not be null.");
|
571
|
}
|
572
|
return group;
|
573
|
}
|
574
|
|
575
|
/**
|
576
|
* <p>remove</p>
|
577
|
*/
|
578
|
public void remove(){
|
579
|
getGroup().remove(this);
|
580
|
}
|
581
|
|
582
|
/**
|
583
|
* <p>createControl</p>
|
584
|
*/
|
585
|
protected void createControl() {
|
586
|
control = getEditor().getToolkit().createComposite(getGroup().getControl());
|
587
|
|
588
|
control.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
|
589
|
TableWrapLayout layout = new TableWrapLayout();
|
590
|
layout.leftMargin = 0;
|
591
|
layout.rightMargin = 0;
|
592
|
layout.topMargin = 5;
|
593
|
layout.bottomMargin = 5;
|
594
|
|
595
|
layout.verticalSpacing = 0;
|
596
|
layout.horizontalSpacing = 0;
|
597
|
|
598
|
control.setLayout(layout);
|
599
|
|
600
|
}
|
601
|
|
602
|
/**
|
603
|
* @return
|
604
|
*/
|
605
|
protected TaxonNameEditor getEditor() {
|
606
|
return getGroup().getEditor();
|
607
|
}
|
608
|
|
609
|
/**
|
610
|
* <p>Getter for the field <code>control</code>.</p>
|
611
|
*
|
612
|
* @return a {@link org.eclipse.swt.widgets.Composite} object.
|
613
|
*/
|
614
|
public Composite getControl(){
|
615
|
return control;
|
616
|
}
|
617
|
|
618
|
/**
|
619
|
* <p>createLineWrapSupport</p>
|
620
|
* @throws OperationNotSupportedException
|
621
|
*/
|
622
|
protected void createLineWrapSupport() throws OperationNotSupportedException {
|
623
|
new LineWrapSupportFacade(getNameViewer(), getEditor().getManagedForm());
|
624
|
}
|
625
|
|
626
|
|
627
|
/**
|
628
|
* <p>createTextViewer</p>
|
629
|
* @throws OperationNotSupportedException
|
630
|
*/
|
631
|
protected void createTextViewer() throws OperationNotSupportedException {
|
632
|
nameViewer = new NameViewerFacade(control);
|
633
|
|
634
|
focusListener = new FocusAdapter() {
|
635
|
@Override
|
636
|
public void focusGained(FocusEvent e) {
|
637
|
for(AbstractGroupedContainer container : getEditor().getGroupedContainers()){
|
638
|
container.colorSelected(NOT_SELECTED);
|
639
|
}
|
640
|
getEditor().getManagedForm().setInput(AbstractGroupedContainer.this);
|
641
|
placeCursor();
|
642
|
colorSelected(SELECTED_FOCUS);
|
643
|
}
|
644
|
};
|
645
|
nameViewer.addFocusListener(focusListener);
|
646
|
|
647
|
//
|
648
|
MouseAdapter mouseListener = new MouseAdapter() {
|
649
|
@Override
|
650
|
public void mouseDown(MouseEvent e) {
|
651
|
storeCursor();
|
652
|
}
|
653
|
};
|
654
|
control.addMouseListener(mouseListener);
|
655
|
nameViewer.addMouseListener(mouseListener);
|
656
|
}
|
657
|
|
658
|
/**
|
659
|
* <p>setIcon</p>
|
660
|
*
|
661
|
* @param icon a {@link org.eclipse.swt.graphics.Image} object.
|
662
|
*/
|
663
|
public void setIcon(Image icon) {
|
664
|
getNameViewer().setIcon(icon);
|
665
|
}
|
666
|
|
667
|
/**
|
668
|
* <p>setIndent</p>
|
669
|
*
|
670
|
* @param indent a int.
|
671
|
*/
|
672
|
public void setIndent(int indent) {
|
673
|
if (control.getLayout() instanceof TableWrapLayout) {
|
674
|
TableWrapLayout layout = ((TableWrapLayout) control.getLayout());
|
675
|
layout.leftMargin = indent;
|
676
|
layout.rightMargin = ACCEPTED_INDENT;
|
677
|
control.setLayout(layout);
|
678
|
control.layout();
|
679
|
} else {
|
680
|
new RuntimeException("Couldn't indent - composite's layout must be TableWrapLayout.");
|
681
|
}
|
682
|
}
|
683
|
|
684
|
/**
|
685
|
* <p>setSelected</p>
|
686
|
*/
|
687
|
public void setSelected() {
|
688
|
getNameViewer().setFocus();
|
689
|
}
|
690
|
|
691
|
/**
|
692
|
* <p>isSelected</p>
|
693
|
*
|
694
|
* @return a boolean.
|
695
|
*/
|
696
|
public boolean isSelected(){
|
697
|
return getEditor().getSelectedContainer() == this;
|
698
|
}
|
699
|
|
700
|
/**
|
701
|
* <p>colorSelected</p>
|
702
|
*
|
703
|
* @param mode a int.
|
704
|
*/
|
705
|
public void colorSelected(int mode){
|
706
|
if(!control.isDisposed()){
|
707
|
String colorString = null;
|
708
|
|
709
|
switch(mode){
|
710
|
case SELECTED_FOCUS:
|
711
|
colorString = Resources.COLOR_CONTROL_SELECTED_FOCUS;
|
712
|
break;
|
713
|
case SELECTED_NO_FOCUS:
|
714
|
colorString = Resources.COLOR_CONTROL_SELECTED;
|
715
|
break;
|
716
|
default:
|
717
|
colorString = Resources.COLOR_COMPOSITE_BACKGROUND;
|
718
|
}
|
719
|
|
720
|
backgroundColor = EditorUtil.getColor(colorString);
|
721
|
|
722
|
setBackground(backgroundColor);
|
723
|
}
|
724
|
}
|
725
|
|
726
|
|
727
|
/**
|
728
|
* <p>setDelayedSelection</p>
|
729
|
*/
|
730
|
protected void setDelayedSelection(){
|
731
|
//TODO this might be done better
|
732
|
// this is the quickest solution i could come up with and it improves performance
|
733
|
// please reimplement if you know better.
|
734
|
selection = this;
|
735
|
|
736
|
// start timer
|
737
|
Display display = DisplayProxy.getDefault();
|
738
|
Runnable runnable = new Runnable() {
|
739
|
|
740
|
public void run() {
|
741
|
getEditor().getManagedForm().setInput(selection);
|
742
|
}
|
743
|
};
|
744
|
display.timerExec(1000, runnable);
|
745
|
|
746
|
}
|
747
|
|
748
|
/**
|
749
|
* <p>setBackground</p>
|
750
|
*
|
751
|
* @param color a {@link org.eclipse.swt.graphics.Color} object.
|
752
|
*/
|
753
|
public void setBackground(Color color) {
|
754
|
control.setBackground(color);
|
755
|
|
756
|
for(Control child : control.getChildren()){
|
757
|
child.setBackground(color);
|
758
|
}
|
759
|
|
760
|
getNameViewer().setBackground(color);
|
761
|
}
|
762
|
|
763
|
/* (non-Javadoc)
|
764
|
* @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
|
765
|
*/
|
766
|
/**
|
767
|
* <p>setFont</p>
|
768
|
*
|
769
|
* @param font a {@link org.eclipse.swt.graphics.Font} object.
|
770
|
*/
|
771
|
public void setFont(Font font) {
|
772
|
getNameViewer().setFont(font);
|
773
|
}
|
774
|
|
775
|
/**
|
776
|
* <p>Getter for the field <code>nameViewer</code>.</p>
|
777
|
*
|
778
|
* @return a {@link eu.etaxonomy.taxeditor.editor.name.container.NameViewer} object.
|
779
|
*/
|
780
|
public NameViewerFacade getNameViewer() {
|
781
|
if (nameViewer == null){
|
782
|
throw new RuntimeException("The Name Viewer is corrupt for Name Container: " + getTaxonBase().getName().getTitleCache());
|
783
|
}
|
784
|
return nameViewer;
|
785
|
}
|
786
|
|
787
|
/**
|
788
|
* If <code>textViewer</code> has already been set, it will show a
|
789
|
* <code>prompt</code> along the lines of "Click here to start entering data"
|
790
|
* when empty.
|
791
|
*
|
792
|
* @param prompt a {@link java.lang.String} object.
|
793
|
*/
|
794
|
public void createEmptyViewerPrompt(final String prompt) {
|
795
|
|
796
|
Assert.isNotNull(getNameViewer());
|
797
|
|
798
|
setFocusListener(new FocusListener() {
|
799
|
|
800
|
|
801
|
public void focusGained(FocusEvent e) {
|
802
|
try {
|
803
|
if (getNameViewer().getDocumentAsString().equals(prompt)) {
|
804
|
getNameViewer().setFont(getViewerFont());
|
805
|
getNameViewer().setDocumentAsString("");
|
806
|
}
|
807
|
} catch (OperationNotSupportedException e1) {
|
808
|
}
|
809
|
}
|
810
|
|
811
|
|
812
|
public void focusLost(FocusEvent e) {
|
813
|
try {
|
814
|
if (getNameViewer().getDocumentLength() == 0) {
|
815
|
initEmptyText();
|
816
|
}
|
817
|
} catch (OperationNotSupportedException e1) {
|
818
|
}
|
819
|
}
|
820
|
|
821
|
});
|
822
|
getNameViewer().addFocusListener(getFocusListener());
|
823
|
|
824
|
try {
|
825
|
if (getNameViewer().getDocumentLength() == 0) {
|
826
|
getNameViewer().setFont(EditorUtil.getFont(Resources.FONT_DEFAULT_PROMPT));
|
827
|
getNameViewer().setDocumentAsString(prompt);
|
828
|
}
|
829
|
} catch (OperationNotSupportedException e1) {
|
830
|
}
|
831
|
}
|
832
|
|
833
|
/**
|
834
|
* <p>getViewerFont</p>
|
835
|
*
|
836
|
* @return a {@link org.eclipse.swt.graphics.Font} object.
|
837
|
*/
|
838
|
abstract protected Font getViewerFont();
|
839
|
|
840
|
/**
|
841
|
* <p>initEmptyText</p>
|
842
|
*/
|
843
|
protected void initEmptyText() {
|
844
|
Font defaultFont = EditorUtil.getFont(Resources.FONT_DEFAULT_PROMPT);
|
845
|
getNameViewer().setFont(defaultFont);
|
846
|
|
847
|
getNameViewer().setDocumentAsString(getEmptyTextPrompt());
|
848
|
placeCursor();
|
849
|
}
|
850
|
|
851
|
/**
|
852
|
* <p>Setter for the field <code>focusListener</code>.</p>
|
853
|
*
|
854
|
* @param focusListener a {@link org.eclipse.swt.events.FocusListener} object.
|
855
|
*/
|
856
|
protected void setFocusListener(FocusListener focusListener) {
|
857
|
this.focusListener = focusListener;
|
858
|
}
|
859
|
|
860
|
private FocusListener getFocusListener() {
|
861
|
return focusListener;
|
862
|
}
|
863
|
|
864
|
/**
|
865
|
* <p>setDirty</p>
|
866
|
*
|
867
|
* @param isDirty a boolean.
|
868
|
*/
|
869
|
public void setDirty(boolean isDirty) {
|
870
|
if(isDirty){
|
871
|
getEditor().getManagedForm().dirtyStateChanged();
|
872
|
}
|
873
|
this.isDirty = isDirty;
|
874
|
}
|
875
|
|
876
|
/**
|
877
|
* <p>isDirty</p>
|
878
|
*
|
879
|
* @return a boolean.
|
880
|
*/
|
881
|
public boolean isDirty(){
|
882
|
return isDirty;
|
883
|
}
|
884
|
|
885
|
/**
|
886
|
* <p>setMenu</p>
|
887
|
*
|
888
|
* @param menu a {@link org.eclipse.swt.widgets.Menu} object.
|
889
|
*/
|
890
|
public void setMenu (Menu menu) {
|
891
|
control.setMenu(menu);
|
892
|
|
893
|
getNameViewer().setMenu(menu);
|
894
|
}
|
895
|
|
896
|
private Control[] draggableControls;
|
897
|
|
898
|
/**
|
899
|
* <p>setDraggableControl</p>
|
900
|
*
|
901
|
* @param controls an array of {@link org.eclipse.swt.widgets.Control} objects.
|
902
|
*/
|
903
|
protected void setDraggableControl(Control[] controls) {
|
904
|
draggableControls = controls;
|
905
|
}
|
906
|
|
907
|
/**
|
908
|
* <p>setIsDraggable</p>
|
909
|
*
|
910
|
* @param draggable a boolean.
|
911
|
*/
|
912
|
public void setIsDraggable(boolean draggable) {
|
913
|
|
914
|
if (draggable) {
|
915
|
|
916
|
if (draggableControls == null) {
|
917
|
throw new NullPointerException(
|
918
|
"Draggable controls must be set to add draggability");
|
919
|
}
|
920
|
|
921
|
Transfer[] types = new Transfer[] { CdmDataTransfer.getInstance() };
|
922
|
int operations = DND.DROP_MOVE;
|
923
|
|
924
|
for(Control draggableControl : draggableControls){
|
925
|
DragSource dragSource = new DragSource(draggableControl, operations);
|
926
|
dragSource.setTransfer(types);
|
927
|
|
928
|
dragSource.addDragListener(new NameEditorDragListener(this));
|
929
|
dragSource.setDragSourceEffect(new NameEditorDragSourceEffect(control));
|
930
|
}
|
931
|
}
|
932
|
}
|
933
|
|
934
|
private String nonEditableText;
|
935
|
|
936
|
ControlListener nonEditableResizeListener = new ControlAdapter() {
|
937
|
|
938
|
int width = 0;
|
939
|
|
940
|
@Override
|
941
|
public void controlResized(ControlEvent e) {
|
942
|
if (nonEditableInfoLabel.getBounds().width == width) {
|
943
|
return;
|
944
|
}
|
945
|
width = nonEditableInfoLabel.getBounds().width;
|
946
|
if (nonEditableInfoLabel.getBounds().width > 0) {
|
947
|
nonEditableInfoLabel.setText(
|
948
|
Dialog.shortenText(nonEditableText.toUpperCase(),
|
949
|
nonEditableInfoLabel));
|
950
|
}
|
951
|
}
|
952
|
};
|
953
|
|
954
|
private String nonEditableHoverText;
|
955
|
|
956
|
private LabelEllipsisListener nonEditableLabelEllipsisListener;
|
957
|
|
958
|
private T data;
|
959
|
|
960
|
/**
|
961
|
* nonEditableInfo is a label displayed underneath a GroupedComposite's
|
962
|
* input field. For instance, NameComposites display things like name relations,
|
963
|
* sec. references, etc. here.
|
964
|
*
|
965
|
* @param info the text to display in the label
|
966
|
* @param append whether the string should be appended to text that is already shown in the label
|
967
|
* @throws OperationNotSupportedException
|
968
|
*/
|
969
|
public void setNonEditableInfo(String info, boolean append) throws OperationNotSupportedException {
|
970
|
// TODO non editable info should only be drawn once, when everything else is drawn
|
971
|
info = info.toUpperCase();
|
972
|
|
973
|
if(append){
|
974
|
nonEditableText += ", " + info;
|
975
|
nonEditableHoverText += "\n" + info;
|
976
|
}else{
|
977
|
nonEditableText = info;
|
978
|
nonEditableHoverText = info;
|
979
|
}
|
980
|
|
981
|
if (nonEditableInfoLabel == null) {
|
982
|
nonEditableInfoLabel = getEditor().getToolkit().createLabel(control, "");
|
983
|
TableWrapData layoutData = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP);
|
984
|
// Set indent to viewer ruler's width
|
985
|
if (getNameViewer().getVerticalRulerControl() != null) {
|
986
|
// TODO right justify
|
987
|
layoutData.indent = INameViewer.RULER_WIDTH;
|
988
|
}
|
989
|
nonEditableInfoLabel.setLayoutData(layoutData);
|
990
|
|
991
|
|
992
|
|
993
|
nonEditableLabelEllipsisListener = new LabelEllipsisListener(nonEditableInfoLabel) {
|
994
|
@Override
|
995
|
public String getLabelText() {
|
996
|
return nonEditableText.toUpperCase();
|
997
|
}
|
998
|
};
|
999
|
nonEditableInfoLabel.addControlListener(nonEditableLabelEllipsisListener);
|
1000
|
|
1001
|
nonEditableInfoHover = new ToolTipFacade(nonEditableInfoLabel);
|
1002
|
nonEditableInfoHover.setRespectDisplayBounds(true);
|
1003
|
|
1004
|
}
|
1005
|
nonEditableInfoHover.setText(nonEditableHoverText);
|
1006
|
nonEditableInfoLabel.setText(nonEditableText);
|
1007
|
|
1008
|
calculateAnnotations();
|
1009
|
}
|
1010
|
|
1011
|
/**
|
1012
|
* <p>Getter for the field <code>data</code>.</p>
|
1013
|
*
|
1014
|
* @return a T object.
|
1015
|
*/
|
1016
|
public T getData(){
|
1017
|
return data;
|
1018
|
}
|
1019
|
|
1020
|
/**
|
1021
|
* <p>Setter for the field <code>data</code>.</p>
|
1022
|
*
|
1023
|
* @param data a T object.
|
1024
|
*/
|
1025
|
public void setData(T data){
|
1026
|
this.data = (T) HibernateProxyHelper.deproxy(data);
|
1027
|
}
|
1028
|
|
1029
|
/**
|
1030
|
* If the user hitting carriage return should cause something to happen -
|
1031
|
* i.e. the creation of a new composite - call this method and override
|
1032
|
* the method handleSplitText().
|
1033
|
*/
|
1034
|
protected void createLineBreakListener() {
|
1035
|
lineBreakListener = LineBreakListenerFacade.getInstance(this);
|
1036
|
getNameViewer().addVerifyListener(lineBreakListener);
|
1037
|
}
|
1038
|
|
1039
|
abstract class LabelEllipsisListener extends ControlAdapter {
|
1040
|
|
1041
|
private final Label label;
|
1042
|
int width = 0;
|
1043
|
|
1044
|
LabelEllipsisListener(Label label) {
|
1045
|
this.label = label;
|
1046
|
}
|
1047
|
|
1048
|
abstract public String getLabelText();
|
1049
|
|
1050
|
@Override
|
1051
|
public void controlResized(ControlEvent e) {
|
1052
|
if (label.getBounds().width == width) {
|
1053
|
return;
|
1054
|
}
|
1055
|
width = label.getBounds().width;
|
1056
|
if (label.getBounds().width > 0) {
|
1057
|
label.setText(TextHelper.shortenText(getLabelText(), label));
|
1058
|
}
|
1059
|
}
|
1060
|
}
|
1061
|
|
1062
|
/**
|
1063
|
* <p>storeCursor</p>
|
1064
|
*/
|
1065
|
public void storeCursor() {
|
1066
|
try {
|
1067
|
this.cursorPosition = getNameViewer().getCursorPosition();
|
1068
|
} catch (OperationNotSupportedException e) {
|
1069
|
this.cursorPosition = 0;
|
1070
|
}
|
1071
|
}
|
1072
|
|
1073
|
/**
|
1074
|
* Puts the cursor to the position it was last seen on or to the end of line
|
1075
|
* if no former position is known.
|
1076
|
*/
|
1077
|
public void placeCursor(){
|
1078
|
if(cursorPosition == 0){
|
1079
|
getNameViewer().setCursorToEOL();
|
1080
|
}else{
|
1081
|
getNameViewer().setCursorPosition(cursorPosition);
|
1082
|
}
|
1083
|
}
|
1084
|
|
1085
|
|
1086
|
|
1087
|
/**
|
1088
|
* <p>Setter for the field <code>group</code>.</p>
|
1089
|
*
|
1090
|
* @param group a {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroup} object.
|
1091
|
*/
|
1092
|
public void setGroup(AbstractGroup group) {
|
1093
|
this.group = group;
|
1094
|
}
|
1095
|
|
1096
|
|
1097
|
|
1098
|
/**
|
1099
|
* <p>restoreColor</p>
|
1100
|
*/
|
1101
|
public void restoreColor() {
|
1102
|
setBackground(backgroundColor);
|
1103
|
}
|
1104
|
|
1105
|
|
1106
|
|
1107
|
/* (non-Javadoc)
|
1108
|
* @see org.eclipse.ui.forms.IFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
|
1109
|
*/
|
1110
|
@Override
|
1111
|
public void initialize(IManagedForm form) {
|
1112
|
// TODO Auto-generated method stub
|
1113
|
|
1114
|
}
|
1115
|
|
1116
|
|
1117
|
/* (non-Javadoc)
|
1118
|
* @see org.eclipse.ui.forms.IFormPart#dispose()
|
1119
|
*/
|
1120
|
@Override
|
1121
|
public void dispose() {
|
1122
|
if(getControl() != null){
|
1123
|
setMenu(null);
|
1124
|
getControl().dispose();
|
1125
|
}
|
1126
|
}
|
1127
|
|
1128
|
|
1129
|
/* (non-Javadoc)
|
1130
|
* @see org.eclipse.ui.forms.IFormPart#commit(boolean)
|
1131
|
*/
|
1132
|
@Override
|
1133
|
public void commit(boolean onSave) {
|
1134
|
if(isDirty()){
|
1135
|
persistName();
|
1136
|
}
|
1137
|
}
|
1138
|
|
1139
|
|
1140
|
/* (non-Javadoc)
|
1141
|
* @see org.eclipse.ui.forms.IFormPart#setFormInput(java.lang.Object)
|
1142
|
*/
|
1143
|
@Override
|
1144
|
public boolean setFormInput(Object input) {
|
1145
|
// TODO Auto-generated method stub
|
1146
|
return false;
|
1147
|
}
|
1148
|
|
1149
|
|
1150
|
/* (non-Javadoc)
|
1151
|
* @see org.eclipse.ui.forms.IFormPart#setFocus()
|
1152
|
*/
|
1153
|
@Override
|
1154
|
public void setFocus() {
|
1155
|
getNameViewer().setFocus();
|
1156
|
}
|
1157
|
|
1158
|
|
1159
|
/* (non-Javadoc)
|
1160
|
* @see org.eclipse.ui.forms.IFormPart#isStale()
|
1161
|
*/
|
1162
|
@Override
|
1163
|
public boolean isStale() {
|
1164
|
// TODO Auto-generated method stub
|
1165
|
return false;
|
1166
|
}
|
1167
|
}
|