Package archmapper.main.ui

Source Code of archmapper.main.ui.ContentAssistProcessorBase

package archmapper.main.ui;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* Base class for content assist processors that can add attribute
* values as content assist in an XML editor.
*
* @author mg
*
*/
@SuppressWarnings("restriction")
public class ContentAssistProcessorBase extends XMLContentAssistProcessor {

  /**
   * Returns the modification stamp of a file. If the file does
   * not exist, returns 0.
   *
   * @param file
   * @return
   */
  protected long getModificationStamp(IFile file) {
    if (file != null) {
      return file.getModificationStamp();
    } else {
      return 0;
    }
  }
 
 
  /**
   * Get the underlying resource from an contentAssistRequest
   *
   * Taken from Spring IDE (http://springide.org) from the class:
   *   org.springframework.ide.eclipse.beans.ui.editor.util.BeansEditorUtils
   *
   * @param request
   * @return
   */
  public IFile getResource(ContentAssistRequest request) {
    IResource resource = null;
    String baselocation = null;

    if (request != null) {
      IStructuredDocumentRegion region = request.getDocumentRegion();
      if (region != null) {
        IDocument document = region.getParentDocument();
        IStructuredModel model = null;
        try {
          model = org.eclipse.wst.sse.core.StructuredModelManager
              .getModelManager()
              .getExistingModelForRead(document);
          if (model != null) {
            baselocation = model.getBaseLocation();
          }
        }
        finally {
          if (model != null) {
            model.releaseFromRead();
          }
        }
      }
    }

    if (baselocation != null) {
      // copied from JSPTranslationAdapter#getJavaProject
      IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
      IPath filePath = new Path(baselocation);
      if (filePath.segmentCount() > 0) {
        resource = root.getFile(filePath);
      }
    }
    return (IFile) resource;
  }
 
  /**
   * Adds a content assist proposal for an attribute value with the given proposal text to
   * the given request.
   *
   * @param text
   * @param request
   */
  protected void addAttributeProposal(String text, ContentAssistRequest request) {
    text = "\""+ text + "\"";
   
    request.addProposal(new CompletionProposal(text,
        request.getReplacementBeginPosition(), request.getReplacementLength(),
        text.length()));
  }
 
  /**
   * Adds a content assist proposal for an element value (PCDATA)
   * with the given proposal text to the given request.
   *
   * @param text
   * @param request
   */
  protected void addElementValueProposal(String text, ContentAssistRequest request) {
    int endOffset = request.getText().length();
    int startPos = request.getStartOffset();
   
    // If the cursor is on the rightmost position in the element,
    // then the current node is not the text node, but the
    // Element surrounding the text.
    if (request.getNode() instanceof Element) {
      Node textNode = ((Element) request.getNode()).getFirstChild();
     
      if (textNode != null) {
        startPos -= textNode.getNodeValue().length();
        endOffset = textNode.getNodeValue().length();
      } else {
        endOffset = 0;
      }
    }
   
    request.addProposal(new CompletionProposal(text, startPos,
        endOffset, text.length()));
  }
 
  /**
   * Returns the attribute for which content assist is requested, or an
   * empty string, if no attribute could be found.
   *
   *
   * The code is taken from from Spring IDE (http://springide.org)
   *    from the class:
   *   org.springframework.ide.eclipse.beans.ui.editor.contentassist.AbstractContentAssistProcessor
   *
   * @param node1
   * @param request
   * @return
   */
  public String getAttributeName(Node node1, ContentAssistRequest request) {
    IDOMNode node = (IDOMNode) node1;
   
    IStructuredDocumentRegion open = node
        .getFirstStructuredDocumentRegion();
    ITextRegionList openRegions = open.getRegions();
    int i = openRegions.indexOf(request.getRegion());
    if (i < 0) {
      return "";
    }
    ITextRegion nameRegion = null;
    while (i >= 0) {
      nameRegion = openRegions.get(i--);
      if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
        break;
      }
    }
   
    String matchString = request.getMatchString();
    if (matchString == null) {
      matchString = "";
    }
    if (matchString.length() > 0
        && (matchString.startsWith("\"") || matchString.startsWith("'"))) {
      matchString = matchString.substring(1);
    }
   
    // the name region is REQUIRED to do anything useful
    if (nameRegion != null) {
      String attributeName = open.getText(nameRegion);
      Node attribute = node.getAttributes().getNamedItem(attributeName);
      if (attribute != null) {
        String prefix = attribute.getPrefix();
        if (prefix != null) {
          attributeName = attributeName
              .substring(prefix.length() + 1);
        }
        return attributeName;
      }
    }
 
    return "";
  }
}
TOP

Related Classes of archmapper.main.ui.ContentAssistProcessorBase

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.