Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / container / LineBreakListener.java
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 package eu.etaxonomy.taxeditor.editor.name.container;
10
11 import java.util.Scanner;
12
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.events.VerifyEvent;
15 import org.eclipse.swt.events.VerifyListener;
16
17 /**
18 * Checks text entered into a StyledText widget for line breaks, sends
19 * split text to abstract method handleSplitText().
20 * <p>
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
24 * exception occurs.
25 * <p>
26 * The listener should be set on the StyledText widget as follows:
27 * <code><pre>
28 * StyledText styledText = new SourceViewer().getTextWidget();
29 * LineBreakListener lineBreakListener = new LineBreakListener() {
30 * public void handleSplitText(String text) {
31 * ... some operation ...
32 * }
33 * };
34 *
35 * styledText.addVerifyListener(lineBreakListener);
36 * styledText.addKeyListener(lineBreakListener);
37 * </pre></code>
38 *
39 * @author p.ciardelli
40 * @created 19.05.2008
41 */
42 public abstract class LineBreakListener implements VerifyListener{
43
44 private static String LINE_BREAK = System.getProperty("line.separator"); //$NON-NLS-1$
45
46
47 /**
48 * {@inheritDoc}
49 *
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)
55 */
56 public void verifyText(VerifyEvent e) {
57
58 String verifyText = e.text;
59
60 StyledText styledText = (StyledText) e.widget;
61 String widgetText = styledText.getText();
62 int cursorPosition = styledText.getCaretOffset();
63
64 // If no line break in verifyText, Parse text normally
65 if (!verifyText.contains(LINE_BREAK)) {
66 return;
67 }
68
69 // Don't add verifyText without further processing
70 e.doit = false;
71
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$
77 return;
78 }
79
80 // Concatenate old and new texts
81 String textForSplitting = widgetText.substring(0, cursorPosition)
82 + verifyText
83 + widgetText.substring(cursorPosition);
84
85 // Split on line breaks
86 Scanner scanner = new Scanner( textForSplitting );
87 scanner.useDelimiter(LINE_BREAK);
88
89 // Put first string into name viewer
90 styledText.setText(scanner.next());
91
92 // Start new synonyms with the rest
93 while (scanner.hasNext()) {
94 handleSplitText(scanner.next());
95 }
96 }
97
98 /**
99 * <p>handleSplitText</p>
100 *
101 * @param text a {@link java.lang.String} object.
102 */
103 abstract public void handleSplitText(String text);
104 }