Project

General

Profile

« Previous | Next » 

Revision 3be6ef3e

Added by Niels Hoffmann over 13 years ago

performed javacscript:fix and worked on documentation

View differences:

taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/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;
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.KeyEvent;
18
import org.eclipse.swt.events.KeyListener;
19
import org.eclipse.swt.events.VerifyEvent;
20
import org.eclipse.swt.events.VerifyListener;
21

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

  
108
	
109
	public void keyReleased(KeyEvent e) {
110
		if (splitTexts != null) {
111
			for (String text : splitTexts) {
112
				handleSplitText(text);
113
			}
114
			splitTexts = null;
115
		}
116
	}
117
	
118
	abstract public void handleSplitText(String text);
119
}
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;
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.KeyEvent;
18
import org.eclipse.swt.events.KeyListener;
19
import org.eclipse.swt.events.VerifyEvent;
20
import org.eclipse.swt.events.VerifyListener;
21

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

  
110
	
111
	/** {@inheritDoc} */
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
	/**
122
	 * <p>handleSplitText</p>
123
	 *
124
	 * @param text a {@link java.lang.String} object.
125
	 */
126
	abstract public void handleSplitText(String text);
127
}

Also available in: Unified diff