Professional Documents
Culture Documents
Koen Aers
JBoss jBPM (http://www.jboss.com/products/jbpm)
JBoss IDE (http://www.jboss.com/products/jbosside)
=> Graphical Process Designer
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Agenda
GEF in 3 Slides
An Empty Editor for Directed Graphs
Adding Nodes to the Editor
Moving and Resizing Nodes
Creating Nodes with the Palette
Next Steps and Directions
Conclusion
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The Graphical Editing Framework
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Structure of a Typical GEF Editor
GraphEditor
1 PaletteViewer
1
EditDomain PaletteRoot
1
observes
1..n
Graph
GraphicalViewer RootEditPart
1 1..n 1
1..n
1 GraphEditPart
EditPartFactory 1..n Node
1
NodeEditPart
creates observes
1..n 1..n
1
ConnectionEditPart Connection
observes
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Typical GEF MVC Interactions
NodeComponentEditPolicy
1
observes
1..n
1
NodeFigure NodeEditPart Node
1
refreshes
1
modifies
NodeGraphicalNodeEditPolicy
1..n
creates
ConnectionCreateCommand creates Connection
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
An Empty Editor for Directed Graphs
GraphEditor
Graph
GraphicalViewer RootEditPart
1 1..n 1
1 GraphEditPart
EditPartFactory
creates
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
A Minimal GEF Editor in 6 Steps
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
GraphEditPart and GraphEditPartFactory
public class GraphEditPart extends AbstractGraphicalEditPart {
public GraphEditPart(Graph graph) {
setModel(graph);
}
protected IFigure createFigure() {
FreeformLayer layer = new FreeformLayer();
layer.setLayoutManager(new FreeformLayout());
layer.setBorder(new LineBorder(1));
return layer;
}
protected void createEditPolicies() {}
}
public class GraphicalEditPartFactory implements EditPartFactory {
public EditPart createEditPart(EditPart context, Object model) {
if (model instanceof Graph)
return new GraphEditPart((Graph)model);
else
return null;
}
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The Initial GraphEditor in Detail
public class GraphEditor extends EditorPart {
…
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
setSite(site);
setInput(input);
}
…
public void createPartControl(Composite parent) {
ScrollingGraphicalViewer viewer =
new ScrollingGraphicalViewer();
viewer.createControl(parent);
viewer.setRootEditPart(new ScalableFreeformRootEditPart());
viewer.getControl().setBackground(ColorConstants.white);
viewer.setEditPartFactory(new GraphEditPartFactory());
viewer.setContents(new Graph());
}
…
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The Empty Editor in Action
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Adding Nodes to the Editor
GraphEditor
observes
Graph
GraphicalViewer RootEditPart
1 1..n 1
1..n
1 GraphEditPart
EditPartFactory 1..n Node
1
NodeEditPart
creates observes
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Showing the Nodes
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The Node and Graph Model Implementation
public class Node {
Rectangle constraint;
String name;
public Rectangle getConstraint() { return constraint;}
public void setConstraint(Rectangle constraint) {
this.constraint = constraint;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
GraphEditPart and GraphEditPartFactory Revisited
public class GraphEditPart extends AbstractGraphicalEditPart {
…
protected List getModelChildren() {
return ((Graph)getModel()).getNodes();
}
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The Node-enabled Graph Editor
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Moving and Resizing Nodes
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Adding the EditDomain to the Editor
public class GraphEditor extends EditorPart {
private EditDomain editDomain;
…
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
…
editDomain = new DefaultEditDomain(this);
}
…
public void createPartControl(Composite parent) {
…
editDomain.addViewer(viewer);
}
…
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The Node Move/Resize Scenario
GraphEditPart
creates
observes
1
NodeFigure NodeEditPart Node
1
refreshes
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Revisit GraphEditPart
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The GraphXYLayoutEditPolicy Implementation
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The NodeChangeConstraintCommand
Implementation
public class NodeChangeConstraintCommand extends Command {
private Rectangle newConstraint;
private Rectangle oldConstraint;
private Node node;
public void execute() {
if (oldConstraint == null) {
oldConstraint = new Rectangle(node.getConstraint());
}
node.setConstraint(newConstraint);
}
public void undo() {
node.setConstraint(oldConstraint);
}
public void setNewConstraint(Rectangle newConstraint) {
this.newConstraint = newConstraint;
}
public void setNode(Node node) { this.node = node; }
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The NodeEditPart/Node Combination
public class NodeEditPart extends AbstractGraphicalEditPart implements Observer
{
…
public void activate() {
if (!isActive()) ((Node)getModel()).addObserver(this);
super.activate();
}
public void deactivate() {
if (isActive()) ((Node)getModel()).deleteObserver(this);
super.deactivate();
}
public void update(Observable arg0, Object arg1) { refreshVisuals(); }
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Creating Nodes with the Palette
Creating the Palette
PaletteRoot and its Tools
Splitting the Canvas: SashForm and PaletteViewer
Creating Nodes
NodeCreateCommand
Make the GraphEditPart/Graph combo Observer/Observable
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The PaletteRoot Implementation
public class GraphPalette extends PaletteRoot {
public GraphPalette() {
PaletteGroup group = new PaletteGroup("Graph Controls");
SelectionToolEntry entry = new SelectionToolEntry();
group.add(entry);
setDefaultEntry(entry);
group.add(new PaletteSeparator());
group.add(new CreationToolEntry(
“Node”,
“Creates a new node.”,
new NodeFactory(), null, null));
}
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Adding the Palette to the Editor
public class GraphEditor extends EditorPart {
…
public void createPartControl(Composite parent) {
SashForm form = new SashForm(parent, SWT.HORIZONTAL);
createPaletteViewer(form);
createGraphViewer(form);
form.setWeights(new int[] { 15, 85 });
}
private void createPaletteViewer(Composite parent) {
PaletteViewer viewer = new PaletteViewer();
viewer.createControl(parent);
editDomain.setPaletteViewer(viewer);
editDomain.setPaletteRoot(new GraphPalette());
}
private void createGraphViewer(Composite parent) {
ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer();
viewer.setRootEditPart(new ScalableFreeformRootEditPart());
viewer.createControl(parent);
viewer.getControl().setBackground(ColorConstants.white);
viewer.setEditPartFactory(new GraphicalEditPartFactory());
viewer.setContents(ContentProvider.INSTANCE.newSampleGraph());
editDomain.addViewer(viewer);
}
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Graph Editor with Palette
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The NodeCreateCommand Implementation
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Revisit GraphXYLayoutEditPolicy
public class GraphXYLayoutEditPolicy
extends XYLayoutEditPolicy {
…
protected Command getCreateCommand(CreateRequest request) {
if (request.getNewObjectType().equals(Node.class)) {
NodeCreateCommand result = new NodeCreateCommand();
result.constraint = new Rectangle(
request.getLocation(),
new Dimension(65, 35));
result.node = (Node)request.getNewObject();
result.parent = (Graph)getHost().getModel();
result.node.setName(result.graph.getNextNodeName());
return result;
}
return null;
}
…
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
The GraphEditPart/Graph Combination
public class Graph extends Observable {
…
public void addNode(Node node) {
getNodes().add(node);
setChanged();
notifyObservers();
}
public void removeNode(Node node) {
getNodes().remove(node);
setChanged();
notifyObservers();
}
…
}
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Editor with Some Created Nodes
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Next Steps and Directions
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0
Conclusion
A Gentle Introduction to GEF | © 2006 by Koen Aers; made available under the EPL v1.0