Package it.tref.eclipse.wicket.plugin.hyperlink

Source Code of it.tref.eclipse.wicket.plugin.hyperlink.WicketFunHtmlHyperlinkDetector

package it.tref.eclipse.wicket.plugin.hyperlink;

import it.tref.eclipse.wicket.plugin.editors.WicketFunEditor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
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.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;

/**
* Class that detect wicket id hyperlink on html files.
* <br>
* This class is a modification of the qwickie plugin class.
*
* @author Andrea Fantappi�
*
*/
public class WicketFunHtmlHyperlinkDetector extends AbstractHyperlinkDetector {

  private String extensionToOpen = "java";
 
  @Override
  public IHyperlink[] detectHyperlinks(final ITextViewer textViewer, final IRegion region, final boolean canShowMultipleHyperlinks)
  {
    IEditorPart editor;
    try {
      editor = PlatformUI.getWorkbench()
          .getActiveWorkbenchWindow().getActivePage()
          .getActiveEditor();
    } catch (Exception e) {
      return null;
    }
   
    if ((region == null) || (textViewer == null) || !(editor instanceof WicketFunEditor)) {
      return null;
    }
    final int offset = region.getOffset();

    final IDocument document = textViewer.getDocument();
    if (document == null) {
      return null;
    }

    String wicketId = null;
    final IRegion widRegion = findWicketId(textViewer, offset);
    if (widRegion == null) {
      return null;
    }

    try {
      wicketId = document.get(widRegion.getOffset(), widRegion.getLength());
      if (wicketId == null) {
        return null;
      }

    } catch (final Exception e) {
      return null;
    }
   
    if(extensionToOpen.equals("java"))
    {
      WicketHyperlink wicketHyperlink = new WicketHyperlink(widRegion, wicketId, extensionToOpen);
      return new IHyperlink[] { wicketHyperlink };
    }
    else
    {
      WicketFunEditor wEditor = (WicketFunEditor)editor;
      List<IHyperlink> links = new ArrayList<IHyperlink>();
      for(IEditorPart ed : wEditor.getPropertiesEditors())
      {
        links.add(new WicketHyperlink(widRegion, wicketId, extensionToOpen, ed));
      }
      return links.toArray(new IHyperlink[0]);
    }
  }
 
  private IRegion findWicketId(final ITextViewer textViewer, final int offset)
  {
    Assert.isNotNull(textViewer);
    final IDocument document = textViewer.getDocument();

    IRegion wordRegion = findStringArgument(document, offset, "wicket:id");
    if (wordRegion == null)
    {
      wordRegion = findStringArgument(document, offset, "wicket:message key");
      if (wordRegion != null)
      {
        extensionToOpen = "properties";
      }
    }
    else
    {
      extensionToOpen = "java";
    }
    return wordRegion;
  }
 
  private IRegion findStringArgument(final IDocument document, final int offset, final String argument)
  {
    try {
      final IRegion lineInfo = document.getLineInformationOfOffset(offset);
      final String line = document.get(lineInfo.getOffset(), lineInfo.getLength());
      final int lineRelativeOffset = offset - lineInfo.getOffset();
      int start = line.lastIndexOf(argument, lineRelativeOffset);
      if (start == -1) {
        return null;
      }
      start = line.lastIndexOf('"', lineRelativeOffset) + 1;
      if (start == 0) {
        return null;
      }
      final int end = line.indexOf('"', lineRelativeOffset);
      return new Region(offset - (lineRelativeOffset - start), end - start);
    } catch (final BadLocationException e) {
      return null;
    }
  }
}
TOP

Related Classes of it.tref.eclipse.wicket.plugin.hyperlink.WicketFunHtmlHyperlinkDetector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.