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
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.Scanner
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.swt
.custom
.StyledText
;
18 import org
.eclipse
.swt
.events
.KeyEvent
;
19 import org
.eclipse
.swt
.events
.KeyListener
;
20 import org
.eclipse
.swt
.events
.VerifyEvent
;
21 import org
.eclipse
.swt
.events
.VerifyListener
;
24 * Checks text entered into a StyledText widget for line breaks, sends
25 * split text to abstract method handleSplitText().
27 * VerifyListener interface methods break up the text into lines. Each line is sent individually
28 * to the handleSplitText() method when the KeyListener interface method keyReleased() is called.
29 * This is because if the StyledText widget is disposed before the key is released, an SWT
32 * The listener should be set on the StyledText widget as follows:
34 * StyledText styledText = new SourceViewer().getTextWidget();
35 * LineBreakListener lineBreakListener = new LineBreakListener() {
36 * public void handleSplitText(String text) {
37 * ... some operation ...
41 * styledText.addVerifyListener(lineBreakListener);
42 * styledText.addKeyListener(lineBreakListener);
49 public abstract class LineBreakListener
implements VerifyListener
, KeyListener
{
50 private static final Logger logger
= Logger
51 .getLogger(LineBreakListener
.class);
53 Collection
<String
> splitTexts
;
56 * Checks for 3 conditions of name viewer text:
57 * 1) no line breaks in text
58 * 2) line break at the end of the text
59 * 3) line break in the middle of the text
61 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
63 public void verifyText(VerifyEvent e
) {
65 String verifyText
= e
.text
;
67 StyledText styledText
= (StyledText
) e
.widget
;
68 String widgetText
= styledText
.getText();
69 int cursorPosition
= styledText
.getCaretOffset();
71 // If no line break in verifyText, Parse text normally
72 if (!verifyText
.contains(System
.getProperty("line.separator"))) {
76 // Don't add verifyText without further processing
79 // Initialize split text collection
80 splitTexts
= new ArrayList
<String
>();
82 // If user has entered return at the end of the line,
83 // make a new, empty synonym
84 if (widgetText
.length() == cursorPosition
&&
85 verifyText
.equals(System
.getProperty("line.separator"))) {
90 // Concatenate old and new texts
91 String textForSplitting
= widgetText
.substring(0, cursorPosition
)
93 + widgetText
.substring(cursorPosition
);
95 // Split on line breaks
96 Scanner scanner
= new Scanner( textForSplitting
);
97 scanner
.useDelimiter (System
.getProperty("line.separator"));
99 // Put first string into name viewer
100 styledText
.setText(scanner
.next());
102 // Start new synonyms with the rest
103 while (scanner
.hasNext()) {
104 splitTexts
.add(scanner
.next());
109 public void keyPressed(KeyEvent e
) {}
112 public void keyReleased(KeyEvent e
) {
113 if (splitTexts
!= null) {
114 for (String text
: splitTexts
) {
115 handleSplitText(text
);
121 abstract public void handleSplitText(String text
);