--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.diagram.participant;\r
+\r
+import java.lang.reflect.Field;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.ReferenceDefinition;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.ReferenceType;\r
+import org.simantics.g2d.canvas.impl.HintReflection.HintListener;\r
+import org.simantics.g2d.diagram.DiagramClass;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.DiagramHandler;\r
+import org.simantics.utils.datastructures.hints.IHintObservable;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * This is a base implementation for all participants that handle\r
+ * diagram. \r
+ * \r
+ * Features:\r
+ * - Overrideable onDiagramSet() method\r
+ * - diagram -field points to current diagram\r
+ * - DiagramHandlers accessible with @Dependency and @Reference annotations\r
+ * \r
+ * TODO add Diagram class handler annotations\r
+ * @author Toni Kalajainen\r
+ */\r
+public class AbstractDiagramParticipant extends AbstractCanvasParticipant {\r
+ \r
+ public IDiagram diagram;\r
+ public DiagramClass clazz;\r
+ protected ReferenceDefinition[] handlerDefs;\r
+ boolean handlerDepsSatisfied = true; \r
+ \r
+ public AbstractDiagramParticipant() {\r
+ super();\r
+ \r
+ handlerDefs = DependencyReflection.getDependencies(this, ReferenceType.DiagramHandler); \r
+ }\r
+ \r
+ @HintListener(Class=DiagramHints.class, Field="KEY_DIAGRAM")\r
+ public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
+ if (oldValue==newValue) return;\r
+ _setDiagram( (IDiagram) newValue ); \r
+ }\r
+ @HintListener(Class=DiagramHints.class, Field="KEY_DIAGRAM")\r
+ public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
+ _setDiagram(null); \r
+ }\r
+\r
+ @Override\r
+ public void addedToContext(ICanvasContext ctx) {\r
+ super.addedToContext(ctx);\r
+ _setDiagram( (IDiagram) ctx.getHintStack().getHint(DiagramHints.KEY_DIAGRAM) );\r
+ }\r
+ \r
+ @Override\r
+ public void removedFromContext(ICanvasContext ctx) {\r
+ _setDiagram(null);\r
+ super.removedFromContext(ctx);\r
+ }\r
+ \r
+ @Override\r
+ public void assertDependencies() {\r
+ super.assertDependencies();\r
+ assert(diagram!=null);\r
+ }\r
+\r
+ private void _setDiagram(IDiagram d)\r
+ {\r
+ IDiagram oldDiagram = this.diagram;\r
+ this.diagram = d;\r
+ if (d!=null) clazz = d.getDiagramClass();\r
+ else clazz=null;\r
+ \r
+ if (clazz!=null) {\r
+\r
+ try {\r
+ for (DiagramHandler h : clazz.getAll()) {\r
+ Class<?> c = h.getClass();\r
+ for (ReferenceDefinition def : handlerDefs)\r
+ {\r
+ Class<?> defClass = def.requirement;\r
+ if (!defClass.isAssignableFrom(c)) continue;\r
+ Field f = def.field;\r
+ //Object value = f.get(this);\r
+ //assert(value==null);\r
+ f.set(this, h);\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ throw new RuntimeException(e);\r
+ } \r
+ handlerDepsSatisfied = checkHandlerDependencies(); \r
+ } else {\r
+\r
+ try {\r
+ for (ReferenceDefinition def : handlerDefs)\r
+ {\r
+ Field f = def.field;\r
+ f.set(this, null);\r
+ }\r
+ } catch (Exception e) {\r
+ throw new RuntimeException(e);\r
+ } \r
+ handlerDepsSatisfied = true;\r
+ }\r
+ onDiagramSet(this.diagram, oldDiagram);\r
+ }\r
+ \r
+ /**\r
+ * OVERRIDE THIS to hook diagram modifications \r
+ * @param newDiagram new diagram or null\r
+ * @param oldDiagram ..\r
+ */\r
+ protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram)\r
+ {\r
+ }\r
+ \r
+ private boolean checkHandlerDependencies() {\r
+ try {\r
+ for (ReferenceDefinition rd : handlerDefs) {\r
+ if (!rd.dependency)\r
+ continue;\r
+ Field f = rd.field;\r
+ Object o = f.get(this);\r
+ if (o == null)\r
+ return false;\r
+ }\r
+ } catch (Exception e) {\r
+ throw new RuntimeException(e);\r
+ }\r
+ return true;\r
+ } \r
+\r
+ \r
+ \r
+}\r