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.ArrayList;
|
13
|
import java.util.Collection;
|
14
|
import java.util.Scanner;
|
15
|
|
16
|
import org.eclipse.swt.custom.StyledText;
|
17
|
import org.eclipse.swt.events.KeyListener;
|
18
|
import org.eclipse.swt.events.VerifyEvent;
|
19
|
import org.eclipse.swt.events.VerifyListener;
|
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
|
|
48
|
public abstract class LineBreakListener implements VerifyListener{
|
49
|
|
50
|
|
51
|
private static String LINE_BREAK = System.getProperty("line.separator"); //$NON-NLS-1$
|
52
|
|
53
|
|
54
|
/**
|
55
|
* {@inheritDoc}
|
56
|
*
|
57
|
* Checks for 3 conditions of name viewer text:
|
58
|
* 1) no line breaks in text
|
59
|
* 2) line break at the end of the text
|
60
|
* 3) line break in the middle of the text
|
61
|
* @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
|
62
|
*/
|
63
|
public void verifyText(VerifyEvent e) {
|
64
|
|
65
|
String verifyText = e.text;
|
66
|
|
67
|
StyledText styledText = (StyledText) e.widget;
|
68
|
String widgetText = styledText.getText();
|
69
|
int cursorPosition = styledText.getCaretOffset();
|
70
|
|
71
|
// If no line break in verifyText, Parse text normally
|
72
|
if (!verifyText.contains(LINE_BREAK)) {
|
73
|
return;
|
74
|
}
|
75
|
|
76
|
// Don't add verifyText without further processing
|
77
|
e.doit = false;
|
78
|
|
79
|
// If user has entered return at the end of the line,
|
80
|
// make a new, empty synonym
|
81
|
if (widgetText.length() == cursorPosition &&
|
82
|
verifyText.equals(LINE_BREAK)) {
|
83
|
handleSplitText(""); //$NON-NLS-1$
|
84
|
return;
|
85
|
}
|
86
|
|
87
|
// Concatenate old and new texts
|
88
|
String textForSplitting = widgetText.substring(0, cursorPosition)
|
89
|
+ verifyText
|
90
|
+ widgetText.substring(cursorPosition);
|
91
|
|
92
|
// Split on line breaks
|
93
|
Scanner scanner = new Scanner( textForSplitting );
|
94
|
scanner.useDelimiter(LINE_BREAK);
|
95
|
|
96
|
// Put first string into name viewer
|
97
|
styledText.setText(scanner.next());
|
98
|
|
99
|
// Start new synonyms with the rest
|
100
|
while (scanner.hasNext()) {
|
101
|
handleSplitText(scanner.next());
|
102
|
}
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* <p>handleSplitText</p>
|
107
|
*
|
108
|
* @param text a {@link java.lang.String} object.
|
109
|
*/
|
110
|
abstract public void handleSplitText(String text);
|
111
|
}
|