2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.name
.container
;
11 import java
.util
.Scanner
;
13 import org
.eclipse
.swt
.custom
.StyledText
;
14 import org
.eclipse
.swt
.events
.VerifyEvent
;
15 import org
.eclipse
.swt
.events
.VerifyListener
;
18 * Checks text entered into a StyledText widget for line breaks, sends
19 * split text to abstract method handleSplitText().
21 * VerifyListener interface methods break up the text into lines. Each line is sent individually
22 * to the handleSplitText() method when the KeyListener interface method keyReleased() is called.
23 * This is because if the StyledText widget is disposed before the key is released, an SWT
26 * The listener should be set on the StyledText widget as follows:
28 * StyledText styledText = new SourceViewer().getTextWidget();
29 * LineBreakListener lineBreakListener = new LineBreakListener() {
30 * public void handleSplitText(String text) {
31 * ... some operation ...
35 * styledText.addVerifyListener(lineBreakListener);
36 * styledText.addKeyListener(lineBreakListener);
42 public abstract class LineBreakListener
implements VerifyListener
{
44 private static String LINE_BREAK
= System
.getProperty("line.separator"); //$NON-NLS-1$
50 * Checks for 3 conditions of name viewer text:
51 * 1) no line breaks in text
52 * 2) line break at the end of the text
53 * 3) line break in the middle of the text
54 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
56 public void verifyText(VerifyEvent e
) {
58 String verifyText
= e
.text
;
60 StyledText styledText
= (StyledText
) e
.widget
;
61 String widgetText
= styledText
.getText();
62 int cursorPosition
= styledText
.getCaretOffset();
64 // If no line break in verifyText, Parse text normally
65 if (!verifyText
.contains(LINE_BREAK
)) {
69 // Don't add verifyText without further processing
72 // If user has entered return at the end of the line,
73 // make a new, empty synonym
74 if (widgetText
.length() == cursorPosition
&&
75 verifyText
.equals(LINE_BREAK
)) {
76 handleSplitText(""); //$NON-NLS-1$
80 // Concatenate old and new texts
81 String textForSplitting
= widgetText
.substring(0, cursorPosition
)
83 + widgetText
.substring(cursorPosition
);
85 // Split on line breaks
86 Scanner scanner
= new Scanner( textForSplitting
);
87 scanner
.useDelimiter(LINE_BREAK
);
89 // Put first string into name viewer
90 styledText
.setText(scanner
.next());
92 // Start new synonyms with the rest
93 while (scanner
.hasNext()) {
94 handleSplitText(scanner
.next());
99 * <p>handleSplitText</p>
101 * @param text a {@link java.lang.String} object.
103 abstract public void handleSplitText(String text
);