1 package eu
.etaxonomy
.taxeditor
.prototype2
.view
.nameviewersupport
;
3 import java
.util
.Scanner
;
5 import org
.eclipse
.swt
.custom
.StyledText
;
6 import org
.eclipse
.swt
.events
.VerifyEvent
;
7 import org
.eclipse
.swt
.events
.VerifyListener
;
9 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
10 import eu
.etaxonomy
.taxeditor
.prototype2
.controller
.ActionAddSynonymToTaxon
;
13 * Checks text entered into a name viewer for line breaks, creates
14 * new synonyms accordingly
18 public class NameViewerVerifyListener
implements VerifyListener
{
22 NameViewerVerifyListener(Taxon taxon
) {
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
33 * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
35 public void verifyText(VerifyEvent e
) {
37 String verifyText
= e
.text
;
39 StyledText styledText
= (StyledText
)e
.widget
;
40 String widgetText
= styledText
.getText();
41 int cursorPosition
= styledText
.getCaretOffset();
43 // Is there a line break in verifyText?
44 if (!verifyText
.contains(System
.getProperty("line.separator")))
45 // Parse text normally
48 // Don't add verifyText without further processing
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();
59 // Concatenate old and new texts
60 String textForSplitting
= widgetText
.substring(0, cursorPosition
)
62 + widgetText
.substring(cursorPosition
);
64 // Split on line breaks
65 Scanner scanner
= new Scanner( textForSplitting
);
66 scanner
.useDelimiter (System
.getProperty("line.separator"));
68 // Put first string into name viewer
69 styledText
.setText(scanner
.next());
71 // Start new synonyms with the rest
72 while (scanner
.hasNext())
73 new ActionAddSynonymToTaxon(taxon
, scanner
.next()).run();