Package archmapper.main.ui

Source Code of archmapper.main.ui.ArchitectureContentAssistProcessor

package archmapper.main.ui;

import java.util.Calendar;
import java.util.List;

import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import archmapper.main.model.ArchitectureMappingModel;
import archmapper.main.model.stylemapping.StyleElementMappingBase;
import archmapper.main.model.stylemapping.StyleMapping;

@SuppressWarnings("restriction")
public class ArchitectureContentAssistProcessor extends
    ContentAssistProcessorBase {
  private static StyleMapping lastLoadedStyleMapping = null;
  private static long lastStyleMappingLoadTime = 0;
 
 
  @Override
  protected void addAttributeValueProposals(ContentAssistRequest request) {
    Node node = request.getNode();
    String attribute = getAttributeName(node, request);
    String nodename = node.getLocalName();
   
    if (nodename == null) {
      return;
    }
   
    if (nodename.equals("architecture") &&
        (attribute.equals("styleType") || attribute.equals("architecturalStyle"))) {
      addStylenameProposals(request);
    }
   
    if (attribute.equals("styleType")) {
      if (nodename.equals("component")) {
        addComponentTypeProposals(request);
      } else if (nodename.equals("connector")) {
        addConnectorTypeProposals(request);
      } else if (nodename.equals("port")) {
        addPortTypeProposals(request);
      } else if (nodename.equals("role")) {
        addRoleTypeProposals(request);
      }
    }
   
    if (nodename.equals("role") && attribute.equals("connector")) {
      addConnectorNameProposals(request);
    }
  }
 
  protected void addStylenameProposals(ContentAssistRequest request) {
    for (String styleName : ArchitectureMappingModel.getAllAvailableStylesFromExtensionPoint()) {
      addAttributeProposal(styleName, request);
    }
  }
 
  protected void addComponentTypeProposals(ContentAssistRequest request) {
    StyleMapping mapping = getStyleMapping(request);
    if (mapping == null) {
      return;
    }
   
    addStyleTypeProposalFor(mapping.getComponentTypeMapping(), request);
  }
 
  protected void addConnectorTypeProposals(ContentAssistRequest request) {
    StyleMapping mapping = getStyleMapping(request);
    if (mapping == null) {
      return;
    }
   
    addStyleTypeProposalFor(mapping.getConnectorTypeMapping(), request);
  }
 
  protected void addRoleTypeProposals(ContentAssistRequest request) {
    StyleMapping mapping = getStyleMapping(request);
    if (mapping == null) {
      return;
    }
   
    addStyleTypeProposalFor(mapping.getRoleTypeMapping(), request);
  }
 
  protected void addPortTypeProposals(ContentAssistRequest request) {
    StyleMapping mapping = getStyleMapping(request);
    if (mapping == null) {
      return;
    }
   
    addStyleTypeProposalFor(mapping.getPortTypeMapping(), request);
  }
 
  protected void addStyleTypeProposalFor(List<? extends StyleElementMappingBase> mappings, ContentAssistRequest request) {
    for (StyleElementMappingBase elem : mappings) {
      addAttributeProposal(elem.getTypeName(), request);
    }
  }
 
  protected void addConnectorNameProposals(ContentAssistRequest request) {
    Element rootElem = request.getNode().getOwnerDocument().getDocumentElement();
   
    NodeList conns = rootElem.getElementsByTagName("connector");
    for (int i=0; i < conns.getLength(); i++) {
      Element conn = (Element) conns.item(i);
      addAttributeProposal(conn.getAttribute("name"), request);
    }
  }
 
 
  /**
   * Returns the style mapping that belongs to the currently edited
   * Architecture file, or null, if no style mapping could
   * be found. The style mapping is cached internally: it is reloaded
   * every 20 seconds, since it is difficult to track if it really
   * changed.
   *
   * @param request
   * @return
   */
  public StyleMapping getStyleMapping(ContentAssistRequest request) {
    Element rootElem = request.getNode().getOwnerDocument().getDocumentElement();
   
    long currentTime = Calendar.getInstance().getTimeInMillis();
    long secondsSinceLastLoad = (currentTime - lastStyleMappingLoadTime) / 1000;
   
   
   
    if (lastLoadedStyleMapping != null && secondsSinceLastLoad < 20) {
      return lastLoadedStyleMapping;
    } else {
      String styleName = rootElem.getAttribute("architecturalStyle");
      if (styleName == null) {
        styleName = rootElem.getAttribute("styleType");
      }
      if (styleName == null) {
        // We don't know the style
        return null;
      }
     
      StyleMapping styleMapping = ArchitectureMappingModel.loadStyleMapping(null,
          null, styleName, null);
     
      lastLoadedStyleMapping = styleMapping;
      lastStyleMappingLoadTime = Calendar.getInstance().getTimeInMillis();
     
      return styleMapping;
    }
  } 
 
}
TOP

Related Classes of archmapper.main.ui.ArchitectureContentAssistProcessor

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.