Package de.innovationgate.eclipse.editors.tml

Source Code of de.innovationgate.eclipse.editors.tml.TMLHyperlinkDetector

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tml;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;

public class TMLHyperlinkDetector implements IHyperlinkDetector {

  public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    List<IHyperlink> links = new ArrayList<IHyperlink>();
    try {
      // hyperlinks should be detected in tml_start_tags only - so retrieve current partition first
      ITypedRegion partition = textViewer.getDocument().getPartition(region.getOffset());
      if (partition.getType().equals(TMLPartitionScanner.TML_TAG_START)) {
        // get partition info
        TMLRegion tmlInfo = TMLRegion.parse(partition, textViewer.getDocument());
        IHyperlink link = null;
        if (tmlInfo.getTagName().equals("include") || tmlInfo.getTagName().equals("portlet")) {
          // check if design db attribute is missing
          // we can only lookup references in local sources
          if (!tmlInfo.hasAttribute("designdb") && !tmlInfo.isDynamicAttributeValue("medium") && !tmlInfo.isDynamicAttributeValue("ref")) {
            link = createTMLReferenceLink(textViewer, region, partition, "ref", tmlInfo);
          }
        } else if (tmlInfo.getTagName().equals("url")) {
          if (tmlInfo.hasAttribute("layout") && !tmlInfo.isDynamicAttributeValue("layout")) {
            link = createTMLReferenceLink(textViewer, region, partition, "layout", tmlInfo);
          } else if (tmlInfo.hasAttribute("type") && !tmlInfo.isDynamicAttributeValue("type") && tmlInfo.hasAttribute("name") && !tmlInfo.isDynamicAttributeValue("name")) {
            String type = tmlInfo.getAttributeValue("type");
            if (type != null && type.trim().equalsIgnoreCase(WGADesignStructureHelper.SCRIPT_TYPE_CSS)) {
              link = createScriptReferenceLink(textViewer, region, partition, "name", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_CSS);
            } else if (type != null && type.trim().equalsIgnoreCase(WGADesignStructureHelper.SCRIPT_TYPE_JS)) {
              link = createScriptReferenceLink(textViewer, region, partition, "name", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_JS);
            }
          } else if (tmlInfo.hasAttribute("action") && !tmlInfo.isDynamicAttributeValue("action")) {
            // create hyperlink if action references a tml module
            String actionRef = tmlInfo.getAttributeValue("action");
            if (actionRef != null && actionRef.contains(":")) {
              link = createScriptReferenceLink(textViewer, region, partition, "action", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_TMLSCRIPT);
            }           
          }
        } else if (tmlInfo.getTagName().equals("label") && tmlInfo.hasAttribute("key") && !tmlInfo.isDynamicAttributeValue("key") && !tmlInfo.isDynamicAttributeValue("file")) {
          link = createTMLLabelLink(textViewer, region, partition, "key", tmlInfo);
        } else if (tmlInfo.getTagName().equals("button") && tmlInfo.hasAttribute("clickaction") && !tmlInfo.isDynamicAttributeValue("clickaction")) {
          // create hyperlink if action references a tml module
          String actionRef = tmlInfo.getAttributeValue("clickaction");
          if (isScriptModuleActionReference(actionRef)) {
            link = createScriptReferenceLink(textViewer, region, partition, "clickaction", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_TMLSCRIPT);
          }
        } else if (tmlInfo.getTagName().equals("input") && tmlInfo.hasAttribute("changeaction") && !tmlInfo.isDynamicAttributeValue("changeaction")) {
          // create hyperlink if action references a tml module
          String actionRef = tmlInfo.getAttributeValue("changeaction");
          if (isScriptModuleActionReference(actionRef)) {
            link = createScriptReferenceLink(textViewer, region, partition, "changeaction", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_TMLSCRIPT);
          }
        } else if (tmlInfo.getTagName().equals("action") && tmlInfo.hasAttribute("ref") && !tmlInfo.isDynamicAttributeValue("ref")) {
          // create hyperlink if action references a tml module
          String actionRef = tmlInfo.getAttributeValue("ref");
          if (isScriptModuleActionReference(actionRef)) {
            link = createScriptReferenceLink(textViewer, region, partition, "ref", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_TMLSCRIPT);
          }
        } else if (tmlInfo.getTagName().equals("form") && tmlInfo.hasAttribute("defaultaction") && !tmlInfo.isDynamicAttributeValue("defaultaction")) {
          // create hyperlink if action references a tml module
          String actionRef = tmlInfo.getAttributeValue("defaultaction");
          if (isScriptModuleActionReference(actionRef)) {
            link = createScriptReferenceLink(textViewer, region, partition, "defaultaction", tmlInfo, WGADesignStructureHelper.SCRIPT_TYPE_TMLSCRIPT);
          }
        } else if (tmlInfo.getTagName().matches("\\[.*\\]")) {
          // this is an include short cut <tml:[modulename]>
          // check if design db attribute is missing
          // we can only lookup references in local sources
          if (!tmlInfo.hasAttribute("designdb") && !tmlInfo.isDynamicAttributeValue("medium")) {
            int offset = tmlInfo.getDocumentRegion().getOffset() + "<tml:[".length();
            int length = tmlInfo.getTagName().length() - 2;
            IRegion referenceRegion = new Region(offset, length);
            link = new TMLModuleReferenceHyperlink(textViewer, referenceRegion, tmlInfo);
          }
        }

       
        if (link != null) {
          links.add(link);
        }
      }
    } catch (BadLocationException e) {
      Plugin.getDefault().logError("Unable to create hyperlink for region '" + region + "'.", e);
    } catch (ParseException e) {
      Plugin.getDefault().logError("Unable to create hyperlink for region '" + region + "'.", e);
    }
    if (links.size() > 0) {
      return links.toArray(new IHyperlink[0]);
    } else {
      return null;
    }
  }
 




  private IHyperlink createTMLLabelLink(ITextViewer viewer, IRegion region, ITypedRegion partition, String attributeName, TMLRegion tmlInfo) throws BadLocationException {
    IRegion attributeValueRegion = tmlInfo.getAttributeValueRegion(attributeName);
    if (attributeValueRegion != null) {
      return new TMLLabelHyperlink(viewer, attributeValueRegion, tmlInfo);
    } else {
      return null;
    }
  }



  private IHyperlink createTMLReferenceLink(ITextViewer viewer, IRegion region, ITypedRegion partition, String attributeName, TMLRegion tmlInfo) throws BadLocationException {
    IRegion attributeValueRegion = tmlInfo.getAttributeValueRegion(attributeName);
    if (attributeValueRegion != null) {
      return new TMLModuleReferenceHyperlink(viewer, attributeValueRegion, tmlInfo);
    } else {
      return null;
    }
  }
 
  private IHyperlink createScriptReferenceLink(ITextViewer viewer, IRegion region, ITypedRegion partition, String attributeName, TMLRegion tmlInfo, String type) throws BadLocationException {
    IRegion attributeValueRegion = tmlInfo.getAttributeValueRegion(attributeName);
    if (attributeValueRegion != null) {
      return new ScriptReferenceHyperlink(viewer, attributeValueRegion, tmlInfo, type);
    } else {
      return null;
    }
  }
 
  private boolean isScriptModuleActionReference(String ref) {
    return ref != null && ref.contains(":");
  }

}
TOP

Related Classes of de.innovationgate.eclipse.editors.tml.TMLHyperlinkDetector

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.