package com.terrynow.eclipse.stringedit.handlers;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

import com.terrynow.eclipse.stringedit.StringUtils;

public class MultiLineHyperlinkDector extends AbstractHyperlinkDetector {

	@Override
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer,
			IRegion region, boolean canShowMultipleHyperlinks) {

		ITextEditor editor = (ITextEditor) getAdapter(ITextEditor.class);
		if (!editor.getTitle().endsWith(".java")) {
			return null;
		}
		IDocumentProvider dp = editor.getDocumentProvider();
		StringUtils textUtils = new StringUtils();
		textUtils.setDoc(dp.getDocument(editor.getEditorInput()).get());
		textUtils.setCursorPosition(getCursorPosition());
		boolean val = textUtils.proccess();
		int cursorPosition = textUtils.getCursorPosition();
		if (cursorPosition < 0) {
			return null;
		}
		if (!val) {
			return null;
		}

		return new IHyperlink[] { new MultiLineHyperlink(new Region(
				textUtils.getStart(), textUtils.getLength())) };
	}

	private int getCursorPosition() {
		ISelection selection = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getSelectionService()
				.getSelection();
		if ((selection instanceof ITextSelection)) {
			return ((ITextSelection) selection).getOffset();
		}
		return -1;
	}

}

@@ -90,55 +90,8 @@
 n;%0D%0A
-import org.eclipse.jface.text.ITextSelection;%0D%0A
 impo
@@ -126,24 +126,24 @@
 extViewer;%0D%0A
+
 import org.e
@@ -294,89 +294,8 @@
 k;%0D%0A
-import org.eclipse.jface.viewers.ISelection;%0D%0Aimport org.eclipse.ui.PlatformUI;%0D%0A
 impo
@@ -996,16 +996,30 @@
 osition(
+StringHandler.
 getCurso
@@ -1339,304 +1339,11 @@
 %7D;%0D%0A
+
 %09%7D%0D%0A
-%0D%0A%09private int getCursorPosition() %7B%0D%0A%09%09ISelection selection = PlatformUI.getWorkbench()%0D%0A%09%09%09%09.getActiveWorkbenchWindow().getSelectionService()%0D%0A%09%09%09%09.getSelection();%0D%0A%09%09if ((selection instanceof ITextSelection)) %7B%0D%0A%09%09%09return ((ITextSelection) selection).getOffset();%0D%0A%09%09%7D%0D%0A%09%09return -1;%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

use string handler's StringHandler.getCursorPosition();