package tool.model;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
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.ToolModelActivator;
import tool.model.grammar.ForteLexer;
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 static final ForteParser parser = new ForteParser(null);
protected final StringBuilder parseErrors = new StringBuilder();
public static ToolComponentComparator comparator = new ToolComponentComparator();
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;
}
public static CommonTokenStream createTokenStream(String source) {
CharStream stream =
new NoCaseStringStream(source);
ForteLexer lexer = new ForteLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
}
public static CommonTokenStream createTokenStream(File source) {
if (!source.exists())
return null;
CharStream stream;
try {
stream = new NoCaseFileStream(source);
ForteLexer lexer = new ForteLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
} catch (IOException e) {
ToolModelActivator.showError("Cannot create parser for file " + source.getName(), e);
}
return null;
}
protected TokenStream createTokenStream(IFile source) {
if (!source.exists())
return null;
CharStream stream;
try {
stream = new NoCaseFileStream(source);
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
} catch (IOException e) {
ToolModelActivator.showError("Cannot create parser for Plan " + source.getName(), e);
}
return null;
}
public String stripQuotes(String source){
return source.substring(1, source.length()-1);
}
}