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.singlesource.editor.name.container;
|
11
|
|
12
|
import java.util.Scanner;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.eclipse.swt.custom.StyledText;
|
16
|
import org.eclipse.swt.events.VerifyEvent;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
19
|
import eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer;
|
20
|
|
21
|
/**
|
22
|
* Checks text entered into a StyledText widget for line breaks, sends
|
23
|
* split text to abstract method handleSplitText().
|
24
|
* <p>
|
25
|
* VerifyListener interface methods break up the text into lines. Each line is sent individually
|
26
|
* to the handleSplitText() method when the KeyListener interface method keyReleased() is called.
|
27
|
* This is because if the StyledText widget is disposed before the key is released, an SWT
|
28
|
* exception occurs.
|
29
|
* <p>
|
30
|
* The listener should be set on the StyledText widget as follows:
|
31
|
* <code><pre>
|
32
|
* StyledText styledText = new SourceViewer().getTextWidget();
|
33
|
* LineBreakListener lineBreakListener = new LineBreakListener() {
|
34
|
* public void handleSplitText(String text) {
|
35
|
* ... some operation ...
|
36
|
* }
|
37
|
* };
|
38
|
*
|
39
|
* styledText.addVerifyListener(lineBreakListener);
|
40
|
* styledText.addKeyListener(lineBreakListener);
|
41
|
* </pre></code>
|
42
|
*
|
43
|
* @author p.ciardelli
|
44
|
* @created 19.05.2008
|
45
|
* @version 1.0
|
46
|
*/
|
47
|
public class LineBreakListenerFacadeImpl<T extends TaxonBase> extends LineBreakListenerFacade<T> {
|
48
|
private static final Logger logger = Logger.getLogger(LineBreakListenerFacadeImpl.class);
|
49
|
|
50
|
public LineBreakListenerFacadeImpl() {
|
51
|
super();
|
52
|
}
|
53
|
|
54
|
public LineBreakListenerFacadeImpl(AbstractGroupedContainer<T> container) {
|
55
|
super(container);
|
56
|
}
|
57
|
|
58
|
protected Object getInstanceInternal(AbstractGroupedContainer<T> container)
|
59
|
{
|
60
|
LineBreakListenerFacadeImpl<T> INST = new LineBreakListenerFacadeImpl<T>(container);
|
61
|
return INST;
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* {@inheritDoc}
|
66
|
*
|
67
|
* Checks for 3 conditions of name viewer text:
|
68
|
* 1) no line breaks in text
|
69
|
* 2) line break at the end of the text
|
70
|
* 3) line break in the middle of the text
|
71
|
* @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
|
72
|
*/
|
73
|
public void verifyText(VerifyEvent e) {
|
74
|
|
75
|
String verifyText = e.text;
|
76
|
|
77
|
StyledText styledText = (StyledText) e.widget;
|
78
|
String widgetText = styledText.getText();
|
79
|
int cursorPosition = styledText.getCaretOffset();
|
80
|
|
81
|
// If no line break in verifyText, Parse text normally
|
82
|
if (!verifyText.contains(LineBreakListenerFacade.LINE_BREAK)) {
|
83
|
return;
|
84
|
}
|
85
|
|
86
|
// Don't add verifyText without further processing
|
87
|
e.doit = false;
|
88
|
|
89
|
// If user has entered return at the end of the line,
|
90
|
// make a new, empty synonym
|
91
|
if (widgetText.length() == cursorPosition &&
|
92
|
verifyText.equals(LineBreakListenerFacade.LINE_BREAK)) {
|
93
|
handleSplitText("");
|
94
|
return;
|
95
|
}
|
96
|
|
97
|
// Concatenate old and new texts
|
98
|
String textForSplitting = widgetText.substring(0, cursorPosition)
|
99
|
+ verifyText
|
100
|
+ widgetText.substring(cursorPosition);
|
101
|
|
102
|
// Split on line breaks
|
103
|
Scanner scanner = new Scanner( textForSplitting );
|
104
|
scanner.useDelimiter(LineBreakListenerFacade.LINE_BREAK);
|
105
|
|
106
|
// Put first string into name viewer
|
107
|
styledText.setText(scanner.next());
|
108
|
|
109
|
// Start new synonyms with the rest
|
110
|
while (scanner.hasNext()) {
|
111
|
handleSplitText(scanner.next());
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* <p>handleSplitText</p>
|
117
|
*
|
118
|
* @param text a {@link java.lang.String} object.
|
119
|
*/
|
120
|
public void handleSplitText(String text)
|
121
|
{
|
122
|
container.handleSplitText(text);
|
123
|
}
|
124
|
}
|