had to rename the packages to make them compliant with buckminster
[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
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.VerifyEvent;
18 import org.eclipse.swt.events.VerifyListener;
19
20 /**
21 * Checks text entered into a StyledText widget for line breaks, sends
22 * split text to abstract method handleSplitText().
23 * <p>
24 * VerifyListener interface methods break up the text into lines. Each line is sent individually
25 * to the handleSplitText() method when the KeyListener interface method keyReleased() is called.
26 * This is because if the StyledText widget is disposed before the key is released, an SWT
27 * exception occurs.
28 * <p>
29 * The listener should be set on the StyledText widget as follows:
30 * <code><pre>
31 * StyledText styledText = new SourceViewer().getTextWidget();
32 * LineBreakListener lineBreakListener = new LineBreakListener() {
33 * public void handleSplitText(String text) {
34 * ... some operation ...
35 * }
36 * };
37 *
38 * styledText.addVerifyListener(lineBreakListener);
39 * styledText.addKeyListener(lineBreakListener);
40 * </pre></code>
41 *
42 * @author p.ciardelli
43 * @created 19.05.2008
44 * @version 1.0
45 */
46 public abstract class LineBreakListener implements VerifyListener{
47
48 private static String LINE_BREAK = System.getProperty("line.separator");
49
50
51 /**
52 * {@inheritDoc}
53 *
54 * Checks for 3 conditions of name viewer text:
55 * 1) no line breaks in text
56 * 2) line break at the end of the text
57 * 3) line break in the middle of the text
58 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
59 */
60 public void verifyText(VerifyEvent e) {
61
62 String verifyText = e.text;
63
64 StyledText styledText = (StyledText) e.widget;
65 String widgetText = styledText.getText();
66 int cursorPosition = styledText.getCaretOffset();
67
68 // If no line break in verifyText, Parse text normally
69 if (!verifyText.contains(LINE_BREAK)) {
70 return;
71 }
72
73 // Don't add verifyText without further processing
74 e.doit = false;
75
76 // If user has entered return at the end of the line,
77 // make a new, empty synonym
78 if (widgetText.length() == cursorPosition &&
79 verifyText.equals(LINE_BREAK)) {
80 handleSplitText("");
81 return;
82 }
83
84 // Concatenate old and new texts
85 String textForSplitting = widgetText.substring(0, cursorPosition)
86 + verifyText
87 + widgetText.substring(cursorPosition);
88
89 // Split on line breaks
90 Scanner scanner = new Scanner( textForSplitting );
91 scanner.useDelimiter(LINE_BREAK);
92
93 // Put first string into name viewer
94 styledText.setText(scanner.next());
95
96 // Start new synonyms with the rest
97 while (scanner.hasNext()) {
98 handleSplitText(scanner.next());
99 }
100 }
101
102 /**
103 * <p>handleSplitText</p>
104 *
105 * @param text a {@link java.lang.String} object.
106 */
107 abstract public void handleSplitText(String text);
108 }