package tool.model;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import tool.ToolPlugin;
import tool.ToolProjectSupport;
import tool.model.grammar.IErrorReporter;
/**
* Two caches are implemented by this class, both contain a list of Types
* One contains a global list of Types in Forte Libraries.
* THe other contains a list of Types per project
*
* @author peter
*
*/
public abstract class ToolType extends ToolComponent implements IProjectComponent{
protected String planName;
protected static Map<String, ToolType> forteTypeCache;
static {
forteTypeCache = new HashMap<String, ToolType>();
}
/**
* Returns the global list of Forte Types
* @return
*/
public static Map<String, ToolType> getForteTypeCache() {
return forteTypeCache;
}
/**
* returns the IProject specific list of types
* @param project
* @return
*/
public static Map<String, ToolType> getTypeCache(IProject project){
Map<String, ToolType> cache = null;
try {
cache = (Map<String, ToolType>)project.getSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.TYPE_CACHE));
if (cache == null){
cache = new HashMap<String, ToolType>();
project.setSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.TYPE_CACHE), cache);
}
} catch (CoreException e) {
ToolPlugin.showError("Error getting class cache", e);
}
return cache;
}
public String getFullName() {
return ((ToolPlan)getParent()).getToolName() + "." + getToolName();
}
public static ToolComponent findType(IProject project, String plan,
String type) {
String key = (plan + "." + type).toUpperCase();
IErrorReporter toolPlan = ToolPlan.fetch(project, plan); //Warms up the plan cache
if (ToolProjectSupport.forteLibrariesSet.contains(plan)) {
return forteTypeCache.get(key);
}
ToolType result = getTypeCache(project).get(key);
if (result == null)
result = forteTypeCache.get(key);
return result;
}
public ToolPlan getPlan() {
return ToolPlan.fetch(getProject(), this.planName);
}
public String getPlanName(){
return this.planName;
}
public void setPlanName(String planName){
propertyChangeSupport.firePropertyChange("planName", this.planName,
this.planName = planName);
setDirty(true);
}
}