p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / 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;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Scanner;
15
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;
22
23 /**
24 * Checks text entered into a StyledText widget for line breaks, sends
25 * split text to abstract method handleSplitText().
26 * <p>
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
30 * exception occurs.
31 * <p>
32 * The listener should be set on the StyledText widget as follows:
33 * <code><pre>
34 * StyledText styledText = new SourceViewer().getTextWidget();
35 * LineBreakListener lineBreakListener = new LineBreakListener() {
36 * public void handleSplitText(String text) {
37 * ... some operation ...
38 * }
39 * };
40 *
41 * styledText.addVerifyListener(lineBreakListener);
42 * styledText.addKeyListener(lineBreakListener);
43 * </pre></code>
44 *
45 * @author p.ciardelli
46 * @created 19.05.2008
47 * @version 1.0
48 */
49 public abstract class LineBreakListener implements VerifyListener, KeyListener {
50 private static final Logger logger = Logger
51 .getLogger(LineBreakListener.class);
52
53 Collection<String> splitTexts;
54
55 /**
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
60 *
61 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
62 */
63 public void verifyText(VerifyEvent e) {
64
65 String verifyText = e.text;
66
67 StyledText styledText = (StyledText) e.widget;
68 String widgetText = styledText.getText();
69 int cursorPosition = styledText.getCaretOffset();
70
71 // If no line break in verifyText, Parse text normally
72 if (!verifyText.contains(System.getProperty("line.separator"))) {
73 return;
74 }
75
76 // Don't add verifyText without further processing
77 e.doit = false;
78
79 // Initialize split text collection
80 splitTexts = new ArrayList<String>();
81
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"))) {
86 splitTexts.add("");
87 return;
88 }
89
90 // Concatenate old and new texts
91 String textForSplitting = widgetText.substring(0, cursorPosition)
92 + verifyText
93 + widgetText.substring(cursorPosition);
94
95 // Split on line breaks
96 Scanner scanner = new Scanner( textForSplitting );
97 scanner.useDelimiter (System.getProperty("line.separator"));
98
99 // Put first string into name viewer
100 styledText.setText(scanner.next());
101
102 // Start new synonyms with the rest
103 while (scanner.hasNext()) {
104 splitTexts.add(scanner.next());
105 }
106 }
107
108
109 public void keyPressed(KeyEvent e) {}
110
111
112 public void keyReleased(KeyEvent e) {
113 if (splitTexts != null) {
114 for (String text : splitTexts) {
115 handleSplitText(text);
116 }
117 splitTexts = null;
118 }
119 }
120
121 abstract public void handleSplitText(String text);
122 }