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