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.model.NodeModel;
import es.iiia.shapegrammar.model.ShapeGrammarModel;
import es.iiia.shapegrammar.shape.ShapeModel;
public class ShapeContentProvider implements IStructuredContentProvider,
PropertyChangeListener {
// private NodeModel content;
private NodeModel content;
private final Viewer viewer;
public ShapeContentProvider(Viewer viewer) {
this.viewer = viewer;
content = ShapeGrammarModel.ACTIVE_SHAPE_GRAMMAR;
// content = new ShapeGrammarModel();
// content = this.createGraphics();
// content.addChild(this.CreateEntreprise());
// content.addChild(this.createGraphics());
}
public Object[] getElements(Object inputElement) {
if (content == null) {
return new NodeModel[0];
}
return ((ShapeGrammarModel) content).getShapes().toArray();
}
public void dispose() {
}
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.getShapes()) {
model.addPropertyChangeListener(this);
}
} else {
this.content = null;
}
// refresh view
this.viewer.refresh();
}
// operations
public ShapeModel addShape() {
// create new shape
ShapeModel model = ShapeGrammarModel.ACTIVE_SHAPE_GRAMMAR.addShape();
// add new listener
model.addPropertyChangeListener(this);
return model;
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.viewer.refresh();
}
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(NodeModel.PROPERTY_RENAME)
|| evt.getPropertyName().equals(ShapeGrammarModel.SHAPE_ADDED)
|| evt.getPropertyName()
.equals(ShapeGrammarModel.SHAPE_REMOVED)) {
this.viewer.refresh();
}
}
}