Package tool.model

Source Code of tool.model.ToolComponent

package tool.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;

import tool.ToolPlugin;
import tool.model.grammar.ForteLexer;
import tool.model.grammar.FortePRXLexer;
import tool.model.grammar.ForteParser;
import tool.model.grammar.NoCaseFileStream;
import tool.model.grammar.NoCaseStringStream;

public abstract class ToolComponent {
  protected Object parent;
  protected Map<String, ToolComponent> children;
  protected String toolName;
  protected IPath path;
  protected IFile file;
  protected IDocument document;
  protected Position position;
  protected List<ToolProperty> properties;
  protected boolean dirty = false;
  protected boolean readWrite = true;
  protected IRegion region;
 
  protected final ForteParser parser = new ForteParser(null);
  protected final StringBuilder parseErrors = new StringBuilder();

  public static ToolComponentComparator comparator  = new ToolComponentComparator();
  public CommonTreeAdaptor adaptor = new CommonTreeAdaptor(){
    public Object create(Token payload){
      return new CommonTree(payload);
    }
  };
  public boolean isReadWrite() {
    return readWrite;
  }

  public void printTree(CommonTree tree, int indent){
    if (tree != null){
      StringBuffer sb = new StringBuffer(indent);
      for (int i = 0; i < indent; i++)
        sb = sb.append("  ");
      for (int i = 0; i < tree.getChildCount(); i++){
        System.out.println(sb.toString() + tree.getChild(i).toString());
        printTree((CommonTree)tree.getChild(i), indent+1);
      }
    }
  }

  public void setReadWrite(boolean readOnly) {
    propertyChangeSupport.firePropertyChange("readWrite", this.readWrite,
        this.readWrite = readOnly);
  }

  protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
      this);
  protected IFile implementationFile;
 
  public void addPropertyChangeListener(String propertyName,
      PropertyChangeListener listener) {
    propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
  }

  public void removePropertyChangeListener(PropertyChangeListener listener) {
    propertyChangeSupport.removePropertyChangeListener(listener);
  }
 
  public Object getParent() {
    return parent;
  }

  public void setParent(Object parent) {
    propertyChangeSupport.firePropertyChange("parent", this.parent,
        this.parent = parent);
  }

  public Map<String, ToolComponent> getChildren() {
    return children;
  }

  public void setChildren(Map<String, ToolComponent> children) {
    this.children = children;
  }

  public String getToolName() {
    return toolName;
  }

  public void setToolName(String toolName) {
    if (toolName.equals(this.toolName))
      return;
    propertyChangeSupport.firePropertyChange("toolName", this.toolName,
        this.toolName = toolName);
    setDirty(true);
  }

  public IPath getPath() {
    return path;
  }

  public void setPath(IPath path) {
    this.path = path;
  }

  public String getHoverHelp(){
    return null;
  }
 
  @Override
  public String toString() {
    return toolName;
  }
  private void createKids(){
    if ( this.children == null){
      this.children = new HashMap<String, ToolComponent>();
    }
  }
  public void addChild(ToolComponent child){
    createKids();
    child.setParent(this);
    this.children.put(child.getToolName(), child);
  }
  public void addChild(List<ToolComponent> kids){
    createKids();
    for (ToolComponent kid : kids){
      kid.setParent(this);
      this.children.put(kid.getToolName(), kid);
    }
  }
  public void addChild(ToolComponent[] kids){
    createKids();
    for (ToolComponent kid : kids){
      kid.setParent(this);
      this.children.put(kid.getToolName(), kid);
    }
  }

  public IDocument getDocument() {
    return document;
  }


  public Position getPosition() {
    return position;
  }

  public void setPosition(Position position) {
    this.position = position;
  }
  public void load(IDocument doc) {
    this.document = doc;
   
  }

  public IFile getFile() {
    return file;
  }

  public void setFile(IFile file) {
    this.file = file;
  }

  public List<ToolProperty> getProperties() {
    return properties;
  }

  public boolean hasChildren(){
    return false;
  }
 
  public String getKey(){
    return this.toolName;
  }

  public Object[] getComponents() {
    return null;
  }

  public boolean isDirty() {
    return dirty;
  }

  public void setDirty(boolean dirty) {
    propertyChangeSupport.firePropertyChange("dirty", this.dirty,
        this.dirty = dirty);
  }

  public IRegion getRegion() {
    return region;
  }

  public void setRegion(IRegion region) {
    propertyChangeSupport.firePropertyChange("region", this.region,
        this.region = region);
  }

  public abstract String getLabelText();
  public abstract String getLabelText(int options);

  public IProject getProject() {
    if (this.file != null){
      return this.file.getProject();
    } else if (this.implementationFile != null){
      return this.implementationFile.getProject();
    }
    return null;
  }

  public IFile getImplementationFile() {
    return implementationFile;
  }
 
  protected CommonTokenStream createTokenStream(String source) {
    CharStream stream =
        new NoCaseStringStream(source);
    ForteLexer lexer = new ForteLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
      return tokenStream;
  }

  protected TokenStream createTokenStream(IFile source) {
    if (!source.exists())
      return null;

    CharStream stream;
    try {
      stream = new NoCaseFileStream(source);
      FortePRXLexer lexer = new FortePRXLexer(stream);
      TokenStream tokenStream = new CommonTokenStream(lexer);
      return tokenStream;
    } catch (IOException e) {
      ToolPlugin.showError("Cannot create parser for Plan " + source.getName(), e);
    }
    return null;
  }
 
  public String stripQuotes(String source){
    return source.substring(1, source.length()-1);
  }
}
TOP

Related Classes of tool.model.ToolComponent

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.