1 package net.sourceforge.phpeclipse.wiki.velocity;
3 import java.text.BreakIterator;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.IRegion;
7 import org.eclipse.jface.text.ITextSelection;
8 import org.eclipse.ui.IEditorPart;
9 import org.eclipse.ui.texteditor.IDocumentProvider;
10 import org.eclipse.ui.texteditor.ITextEditor;
12 public class EditorText {
13 private IEditorPart targetEditor = null;
15 String selection = null;
19 String wikiname = null;
21 public EditorText(IEditorPart targetEditor) {
22 this.targetEditor = targetEditor;
32 * @return Returns the selection.
34 public String getSelection() {
35 if (selection == null) {
36 selection = findSelectedText();
37 if (selection == null) {
46 * The selection to set.
48 public void setSelection(String selection) {
49 this.selection = selection;
53 * @return Returns the text.
55 public String getText() {
63 public void setText(String text) {
67 public String findSelectedText() {
68 String selectedText = null;
69 ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
71 selectedText = textSelection.getText();
72 if (selectedText == null || selectedText.trim().length() == 0) {
73 selectedText = findWord(textSelection);
78 private String findWord(ITextSelection textSelection) {
79 IDocumentProvider documentProvider = ((ITextEditor) targetEditor).getDocumentProvider();
80 IDocument document = documentProvider.getDocument(targetEditor.getEditorInput());
81 int caretPosition = textSelection.getOffset();
83 IRegion line = document.getLineInformation(document.getLineOfOffset(caretPosition));
84 String currentLine = document.get(line.getOffset(), line.getLength());
85 int positionInLine = caretPosition - line.getOffset();
86 return findWordAt(positionInLine, currentLine);
87 } catch (Exception e) {
92 private String findWordAt(int pos, String source) {
93 BreakIterator boundary = BreakIterator.getWordInstance();
94 boundary.setText(source);
95 int end = boundary.following(pos);
96 int start = boundary.previous();
97 return source.substring(start, end);
101 * @return Returns the wikiname.
103 public String getWikiname() {
109 * The wikiname to set.
111 public void setWikiname(String wikiname) {
112 this.wikiname = wikiname;