DND-triggered operations now executed after drag end.
[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.Scanner;
13
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;
18
19 /**
20 * Checks text entered into a StyledText widget for line breaks, sends
21 * split text to abstract method handleSplitText().
22 *
23 * @author p.ciardelli
24 * @created 19.05.2008
25 * @version 1.0
26 */
27 public abstract class LineBreakListener implements VerifyListener {
28 private static final Logger logger = Logger
29 .getLogger(LineBreakListener.class);
30
31 /**
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
36 *
37 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
38 */
39 public void verifyText(VerifyEvent e) {
40
41 String verifyText = e.text;
42
43 StyledText styledText = (StyledText) e.widget;
44 String widgetText = styledText.getText();
45 int cursorPosition = styledText.getCaretOffset();
46
47 // If no line break in verifyText, Parse text normally
48 if (!verifyText.contains(System.getProperty("line.separator"))) {
49 return;
50 }
51
52 // Don't add verifyText without further processing
53 e.doit = false;
54
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"))) {
59 handleSplitText("");
60 return;
61 }
62
63 // Concatenate old and new texts
64 String textForSplitting = widgetText.substring(0, cursorPosition)
65 + verifyText
66 + widgetText.substring(cursorPosition);
67
68 // Split on line breaks
69 Scanner scanner = new Scanner( textForSplitting );
70 scanner.useDelimiter (System.getProperty("line.separator"));
71
72 // Put first string into name viewer
73 styledText.setText(scanner.next());
74
75 // Start new synonyms with the rest
76 while (scanner.hasNext()) {
77 handleSplitText(scanner.next());
78 }
79 }
80
81 abstract public void handleSplitText(String text);
82 }