p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor.prototype2 / src / eu / etaxonomy / taxeditor / prototype2 / view / nameviewersupport / NameViewerVerifyListener.java
1 package eu.etaxonomy.taxeditor.prototype2.view.nameviewersupport;
2
3 import java.util.Scanner;
4
5 import org.eclipse.swt.custom.StyledText;
6 import org.eclipse.swt.events.VerifyEvent;
7 import org.eclipse.swt.events.VerifyListener;
8
9 import eu.etaxonomy.cdm.model.taxon.Taxon;
10 import eu.etaxonomy.taxeditor.prototype2.controller.ActionAddSynonymToTaxon;
11
12 /**
13 * Checks text entered into a name viewer for line breaks, creates
14 * new synonyms accordingly
15 *
16 * @author p.ciardelli
17 */
18 public class NameViewerVerifyListener implements VerifyListener {
19
20 private Taxon taxon;
21
22 NameViewerVerifyListener(Taxon taxon) {
23 this.taxon = taxon;
24 }
25
26 /*
27 * Checks for 3 conditions of name viewer text:
28 * 1) no line breaks in text
29 * 2) line break at the end of the text
30 * 3) line break in the middle of the text
31 *
32 * (non-Javadoc)
33 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
34 */
35 public void verifyText(VerifyEvent e) {
36
37 String verifyText = e.text;
38
39 StyledText styledText = (StyledText)e.widget;
40 String widgetText = styledText.getText();
41 int cursorPosition = styledText.getCaretOffset();
42
43 // Is there a line break in verifyText?
44 if (!verifyText.contains(System.getProperty("line.separator")))
45 // Parse text normally
46 return;
47
48 // Don't add verifyText without further processing
49 e.doit = false;
50
51 // If user has entered return at the end of the line,
52 // make a new, empty synonym
53 if (widgetText.length() == cursorPosition &&
54 verifyText.equals(System.getProperty("line.separator"))) {
55 new ActionAddSynonymToTaxon(taxon, "").run();
56 return;
57 }
58
59 // Concatenate old and new texts
60 String textForSplitting = widgetText.substring(0, cursorPosition)
61 + verifyText
62 + widgetText.substring(cursorPosition);
63
64 // Split on line breaks
65 Scanner scanner = new Scanner( textForSplitting );
66 scanner.useDelimiter (System.getProperty("line.separator"));
67
68 // Put first string into name viewer
69 styledText.setText(scanner.next());
70
71 // Start new synonyms with the rest
72 while (scanner.hasNext())
73 new ActionAddSynonymToTaxon(taxon, scanner.next()).run();
74 }
75 }