package it.tref.eclipse.wicket.plugin.hyperlink;
import it.tref.eclipse.wicket.plugin.editors.WicketFunEditor;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* Is an hyperlink that open the WicketFun page editor on the selected text.
* <br>
* This class is a modification of the qwickie plugin class.
*
* @author Andrea Fantappi�
*
*/
public class WicketHyperlink implements IHyperlink
{
private final String wicketId;
private final IRegion region;
private final String extension;
private final IEditorPart propertiesEditor;
public WicketHyperlink(final IRegion region, final String wicketId, final String extension)
{
Assert.isNotNull(wicketId);
Assert.isNotNull(region);
this.region = region;
this.wicketId = wicketId;
this.extension = extension;
this.propertiesEditor = null;
}
public WicketHyperlink(final IRegion region, final String wicketId, final String extension, final IEditorPart propertiesEditor)
{
Assert.isNotNull(wicketId);
Assert.isNotNull(region);
this.region = region;
this.wicketId = wicketId;
this.extension = extension;
this.propertiesEditor = propertiesEditor;
}
public IRegion getHyperlinkRegion() {
return this.region;
}
public void open()
{
if (this.wicketId != null)
{
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
if(!(activePage.getActiveEditor() instanceof WicketFunEditor))
return;
final WicketFunEditor wEditor = (WicketFunEditor)activePage.getActiveEditor();
final IEditorPart editor = wEditor.getActiveEditor();
Assert.isNotNull(editor);
if (editor instanceof ITextEditor)
{
try {
if ("java".equals(extension))
{
final ITextEditor textEditor = (ITextEditor)wEditor.getJavaEditor();
final IDocument document = ((textEditor).getDocumentProvider()).getDocument(textEditor.getEditorInput());
final FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
final IRegion region = frda.find(0, '"' + wicketId + '"', true, true, false, false);
if (region != null)
{
textEditor.selectAndReveal(region.getOffset() + 1, wicketId.length());
wEditor.setActiveEditor(wEditor.getJavaEditor());
}
}
else if ("html".equals(extension))
{
final ITextEditor textEditor = (ITextEditor)wEditor.getHtmlEditor();
final IDocument document = ((textEditor).getDocumentProvider()).getDocument(textEditor.getEditorInput());
final FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
final IRegion region = frda.find(0, "wicket:id=\"" + wicketId, true, true, true, false);
if (region != null)
{
textEditor.selectAndReveal(region.getOffset() + 11, wicketId.length());
wEditor.setActiveEditor(wEditor.getHtmlEditor());
}
}
else if ("properties".equals(extension) && propertiesEditor != null)
{
final ITextEditor textEditor = (ITextEditor)propertiesEditor;
final IDocument document = ((textEditor).getDocumentProvider()).getDocument(textEditor.getEditorInput());
final FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
IRegion regionBegin = frda.find(0, wicketId, true, true, true, false);
if (regionBegin != null)
{
regionBegin = frda.find(regionBegin.getOffset(), "=", true, true, false, false);
if (regionBegin != null)
{
final IRegion lineRegion = document.getLineInformationOfOffset(regionBegin.getOffset());
final int selectionLength = lineRegion.getOffset() + lineRegion.getLength() - regionBegin.getOffset();
textEditor.selectAndReveal(regionBegin.getOffset() + 1, selectionLength);
wEditor.setActiveEditor(propertiesEditor);
}
}
}
} catch (final BadLocationException e) {
return;
}
}
}
}
public String getTypeLabel() {
return null;
}
public String getHyperlinkText() {
return ("Open " + extension + " file and jump to wicket:id \"" + wicketId + "\"") + (propertiesEditor != null ? " on " + propertiesEditor.getTitle() : "");
}
@Override
public String toString() {
return getClass().getSimpleName() + " for wicket:id " + wicketId + " to " + extension;
}
}