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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.Scanner
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.swt
.custom
.StyledText
;
16 import org
.eclipse
.swt
.events
.VerifyEvent
;
17 import org
.eclipse
.swt
.events
.VerifyListener
;
20 * Checks text entered into a StyledText widget for line breaks, sends
21 * split text to abstract method handleSplitText().
27 public abstract class LineBreakListener
implements VerifyListener
{
28 private static final Logger logger
= Logger
29 .getLogger(LineBreakListener
.class);
32 * Checks for 3 conditions of name viewer text:
33 * 1) no line breaks in text
34 * 2) line break at the end of the text
35 * 3) line break in the middle of the text
37 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
39 public void verifyText(VerifyEvent e
) {
41 String verifyText
= e
.text
;
43 StyledText styledText
= (StyledText
) e
.widget
;
44 String widgetText
= styledText
.getText();
45 int cursorPosition
= styledText
.getCaretOffset();
47 // If no line break in verifyText, Parse text normally
48 if (!verifyText
.contains(System
.getProperty("line.separator"))) {
52 // Don't add verifyText without further processing
55 // If user has entered return at the end of the line,
56 // make a new, empty synonym
57 if (widgetText
.length() == cursorPosition
&&
58 verifyText
.equals(System
.getProperty("line.separator"))) {
63 // Concatenate old and new texts
64 String textForSplitting
= widgetText
.substring(0, cursorPosition
)
66 + widgetText
.substring(cursorPosition
);
68 // Split on line breaks
69 Scanner scanner
= new Scanner( textForSplitting
);
70 scanner
.useDelimiter (System
.getProperty("line.separator"));
72 // Put first string into name viewer
73 styledText
.setText(scanner
.next());
75 // Start new synonyms with the rest
76 while (scanner
.hasNext()) {
77 handleSplitText(scanner
.next());
81 abstract public void handleSplitText(String text
);