integrated velocity engine for URL templates
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / velocity / EditorText.java
1 package net.sourceforge.phpeclipse.wiki.velocity;
2
3 import java.text.BreakIterator;
4
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;
11
12 public class EditorText {
13   private IEditorPart targetEditor = null;
14
15   String selection = null;
16
17   String text = null;
18
19   String wikiname = null;
20
21   public EditorText(IEditorPart targetEditor) {
22     this.targetEditor = targetEditor;
23   }
24
25   public void clear() {
26     selection = null;
27     text = null;
28     wikiname = null;
29   }
30
31   /**
32    * @return Returns the selection.
33    */
34   public String getSelection() {
35     if (selection == null) {
36       selection = findSelectedText();
37       if (selection == null) {
38         selection = "";
39       }
40     }
41     return selection;
42   }
43
44   /**
45    * @param selection
46    *          The selection to set.
47    */
48   public void setSelection(String selection) {
49     this.selection = selection;
50   }
51
52   /**
53    * @return Returns the text.
54    */
55   public String getText() {
56     return text;
57   }
58
59   /**
60    * @param text
61    *          The text to set.
62    */
63   public void setText(String text) {
64     this.text = text;
65   }
66
67   public String findSelectedText() {
68     String selectedText = null;
69     ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
70
71     selectedText = textSelection.getText();
72     if (selectedText == null || selectedText.trim().length() == 0) {
73       selectedText = findWord(textSelection);
74     }
75     return selectedText;
76   }
77
78   private String findWord(ITextSelection textSelection) {
79     IDocumentProvider documentProvider = ((ITextEditor) targetEditor).getDocumentProvider();
80     IDocument document = documentProvider.getDocument(targetEditor.getEditorInput());
81     int caretPosition = textSelection.getOffset();
82     try {
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) {
88     }
89     return null;
90   }
91
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);
98   }
99
100   /**
101    * @return Returns the wikiname.
102    */
103   public String getWikiname() {
104     return wikiname;
105   }
106
107   /**
108    * @param wikiname
109    *          The wikiname to set.
110    */
111   public void setWikiname(String wikiname) {
112     this.wikiname = wikiname;
113   }
114 }