Moved Google and Koders Search to the wiki plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / velocity / EditorText.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/velocity/EditorText.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/velocity/EditorText.java
new file mode 100644 (file)
index 0000000..c1d58a2
--- /dev/null
@@ -0,0 +1,96 @@
+package net.sourceforge.phpeclipse.wiki.velocity;
+
+import java.text.BreakIterator;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
+
+public class EditorText {
+  private IEditorPart targetEditor = null;
+
+  String selection = null;
+
+  String text = null;
+
+  public EditorText(IEditorPart targetEditor) {
+    this.targetEditor = targetEditor;
+  }
+
+  public void clear() {
+    selection = null;
+    text = null;
+  }
+
+  /**
+   * @return Returns the selection.
+   */
+  public String getSelection() {
+    if (selection == null) {
+      selection = findSelectedText();
+      if (selection == null) {
+        selection = "";
+      }
+    }
+    return selection;
+  }
+
+  /**
+   * @param selection
+   *          The selection to set.
+   */
+  public void setSelection(String selection) {
+    this.selection = selection;
+  }
+
+  /**
+   * @return Returns the text.
+   */
+  public String getText() {
+    return text;
+  }
+
+  /**
+   * @param text
+   *          The text to set.
+   */
+  public void setText(String text) {
+    this.text = text;
+  }
+
+  public String findSelectedText() {
+    String selectedText = null;
+    ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
+
+    selectedText = textSelection.getText();
+    if (selectedText == null || selectedText.trim().length() == 0) {
+      selectedText = findWord(textSelection);
+    }
+    return selectedText;
+  }
+
+  private String findWord(ITextSelection textSelection) {
+    IDocumentProvider documentProvider = ((ITextEditor) targetEditor).getDocumentProvider();
+    IDocument document = documentProvider.getDocument(targetEditor.getEditorInput());
+    int caretPosition = textSelection.getOffset();
+    try {
+      IRegion line = document.getLineInformation(document.getLineOfOffset(caretPosition));
+      String currentLine = document.get(line.getOffset(), line.getLength());
+      int positionInLine = caretPosition - line.getOffset();
+      return findWordAt(positionInLine, currentLine);
+    } catch (Exception e) {
+    }
+    return null;
+  }
+
+  private String findWordAt(int pos, String source) {
+    BreakIterator boundary = BreakIterator.getWordInstance();
+    boundary.setText(source);
+    int end = boundary.following(pos);
+    int start = boundary.previous();
+    return source.substring(start, end);
+  }
+}
\ No newline at end of file