package es.iiia.sgi.providers;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import es.iiia.shapeeditor.ShapeGrammarInput;
import es.iiia.shapegrammar.enums.ShapeGrammarRuleType;
import es.iiia.shapegrammar.model.NodeModel;
import es.iiia.shapegrammar.model.ShapeGrammarModel;
import es.iiia.shapegrammar.rule.RuleModel;
public class RuleContentProvider implements IStructuredContentProvider,
PropertyChangeListener {
// private NodeModel content;
private NodeModel content;
private final Viewer viewer;
public RuleContentProvider(Viewer viewer) {
this.viewer = viewer;
// ShapeEditorInput mod = new
// ShapeEditorInput("/Users/tomi/Documents/FloorPlan.xml");
// ShapeEditorInput mod = new
// ShapeEditorInput("/Users/tomi/Documents/SimpleGrammar.xml");
content = ShapeGrammarModel.ACTIVE_SHAPE_GRAMMAR; // mod.getNode();
// content = new ShapeGrammarModel();
}
public Object[] getElements(Object inputElement) {
if (content == null) {
return new NodeModel[0];
}
return ((ShapeGrammarModel) content).getRules().toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
viewer.refresh();
}
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(NodeModel.PROPERTY_RENAME)
|| evt.getPropertyName().equals(ShapeGrammarModel.RULE_ADDED)
|| evt.getPropertyName().equals(ShapeGrammarModel.RULE_REMOVED)) {
this.viewer.refresh();
}
}
public void setContent(ShapeGrammarInput input) {
if (input != null) {
// hook on property changes
ShapeGrammarModel grammar = (ShapeGrammarModel) input.getNode();
this.content = grammar;
grammar.addPropertyChangeListener(this);
for (NodeModel model : grammar.getRules()) {
model.addPropertyChangeListener(this);
}
} else {
this.content = null;
}
// refresh view
this.viewer.refresh();
}
// operations
public RuleModel addRule(ShapeGrammarRuleType type) {
// create new shape
RuleModel model = ShapeGrammarModel.ACTIVE_SHAPE_GRAMMAR.addRule(type);
if (model != null) {
// add new listener
model.addPropertyChangeListener(this);
}
return model;
}
}