1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.diagram.participant;
14 import java.lang.reflect.Field;
16 import org.simantics.g2d.canvas.ICanvasContext;
17 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
18 import org.simantics.g2d.canvas.impl.DependencyReflection;
19 import org.simantics.g2d.canvas.impl.DependencyReflection.ReferenceDefinition;
20 import org.simantics.g2d.canvas.impl.DependencyReflection.ReferenceType;
21 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;
22 import org.simantics.g2d.diagram.DiagramClass;
23 import org.simantics.g2d.diagram.DiagramHints;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.diagram.handler.DiagramHandler;
26 import org.simantics.utils.datastructures.hints.IHintObservable;
27 import org.simantics.utils.datastructures.hints.IHintContext.Key;
30 * This is a base implementation for all participants that handle
34 * - Overrideable onDiagramSet() method
35 * - diagram -field points to current diagram
36 * - DiagramHandlers accessible with @Dependency and @Reference annotations
38 * TODO add Diagram class handler annotations
39 * @author Toni Kalajainen
41 public class AbstractDiagramParticipant extends AbstractCanvasParticipant {
43 public IDiagram diagram;
44 public DiagramClass clazz;
45 protected ReferenceDefinition[] handlerDefs;
46 boolean handlerDepsSatisfied = true;
48 public AbstractDiagramParticipant() {
51 handlerDefs = DependencyReflection.getDependencies(this, ReferenceType.DiagramHandler);
54 @HintListener(Class=DiagramHints.class, Field="KEY_DIAGRAM")
55 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
56 if (oldValue==newValue) return;
57 _setDiagram( (IDiagram) newValue );
59 @HintListener(Class=DiagramHints.class, Field="KEY_DIAGRAM")
60 public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
65 public void addedToContext(ICanvasContext ctx) {
66 super.addedToContext(ctx);
67 _setDiagram( (IDiagram) ctx.getHintStack().getHint(DiagramHints.KEY_DIAGRAM) );
71 public void removedFromContext(ICanvasContext ctx) {
73 super.removedFromContext(ctx);
77 public void assertDependencies() {
78 super.assertDependencies();
79 assert(diagram!=null);
82 private void _setDiagram(IDiagram d)
84 IDiagram oldDiagram = this.diagram;
86 if (d!=null) clazz = d.getDiagramClass();
92 for (DiagramHandler h : clazz.getAll()) {
93 Class<?> c = h.getClass();
94 for (ReferenceDefinition def : handlerDefs)
96 Class<?> defClass = def.requirement;
97 if (!defClass.isAssignableFrom(c)) continue;
99 //Object value = f.get(this);
100 //assert(value==null);
104 } catch (Exception e) {
105 throw new RuntimeException(e);
107 handlerDepsSatisfied = checkHandlerDependencies();
111 for (ReferenceDefinition def : handlerDefs)
116 } catch (Exception e) {
117 throw new RuntimeException(e);
119 handlerDepsSatisfied = true;
121 onDiagramSet(this.diagram, oldDiagram);
125 * OVERRIDE THIS to hook diagram modifications
126 * @param newDiagram new diagram or null
127 * @param oldDiagram ..
129 protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram)
133 private boolean checkHandlerDependencies() {
135 for (ReferenceDefinition rd : handlerDefs) {
139 Object o = f.get(this);
143 } catch (Exception e) {
144 throw new RuntimeException(e);