package org.simantics.sysdyn.ui.editor.participant;\r
\r
-import java.awt.datatransfer.Transferable;\r
-import java.awt.datatransfer.UnsupportedFlavorException;\r
import java.awt.dnd.DropTargetDragEvent;\r
-import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.request.Read;\r
import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.g2d.dnd.ElementClassDragItem;\r
import org.simantics.g2d.dnd.IDnDContext;\r
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant;\r
import org.simantics.structural.stubs.StructuralResource2;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.ui.dnd.LocalObjectTransfer;\r
-import org.simantics.ui.dnd.LocalObjectTransferable;\r
\r
public class SysdynPopulateElementDropParticipant extends PopulateElementDropParticipant {\r
\r
\r
@Override\r
public void dragEnter(DropTargetDragEvent dtde, IDnDContext dp) {\r
- Transferable tr = dtde.getTransferable();\r
- if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {\r
- // This must be done to have SWT transfer set the source data\r
- Object obj = null;\r
- try {\r
- obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);\r
- } catch (UnsupportedFlavorException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- // Check SWT\r
- if (!(obj instanceof IStructuredSelection)) {\r
- obj = LocalObjectTransfer.getTransfer().getObject();\r
- }\r
- \r
- if (obj instanceof IStructuredSelection) {\r
- final IStructuredSelection sel = (IStructuredSelection) obj;\r
- if (!sel.isEmpty()) {\r
- \r
- boolean allowed = true;\r
- try {\r
- allowed = synchronizer.getSession().syncRequest(new Read<Boolean>(){\r
+ super.dragEnter(dtde, dp);\r
+ \r
+ // Check that user is not trying to populate a module to itself. \r
+ // This doesn't prevent infinite recursion if there is one module between two exactly the same modules.\r
+ // e.g. WorkModule -> WokrforceModule -> WorkModule creates an infinite recursion\r
+ final Collection<ElementClassDragItem> items = dp.getItemsByClass(ElementClassDragItem.class);\r
+ if(!items.isEmpty()) {\r
+ Collection<ElementClassDragItem> unvalidModules = null;\r
+ try {\r
+ unvalidModules = synchronizer.getSession().syncRequest(new Read<Collection<ElementClassDragItem>>() {\r
\r
- @Override\r
- public Boolean perform(ReadGraph graph)\r
- throws DatabaseException {\r
- for (Object elm : sel.toList()) {\r
- if (elm instanceof IAdaptable) {\r
- Object r = ((IAdaptable) elm).getAdapter(Resource.class);\r
- if(r != null && r instanceof Resource) {\r
- Resource resource = (Resource)r;\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- if(graph.isInheritedFrom(resource, sr.ModuleSymbol)) {\r
- Resource module = graph.getSingleObject(resource, ModelingResources.getInstance(graph).SymbolToComponentType);\r
- Resource configuration = graph.getSingleObject(module, StructuralResource2.getInstance(graph).IsDefinedBy);\r
- Resource dia = graph.getSingleObject(configuration, ModelingResources.getInstance(graph).CompositeToDiagram);\r
- Resource editorDia = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
- if(dia.equals(editorDia))\r
- return false;\r
- }\r
- }\r
- }\r
- }\r
- return true;\r
- }\r
- \r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
+ @Override\r
+ public Collection<ElementClassDragItem> perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ Collection<ElementClassDragItem> unvalidModules = new ArrayList<ElementClassDragItem>();\r
+ for(ElementClassDragItem item : items) {\r
+ StaticObjectAdapter soa = item.getElementClass().getSingleItem(StaticObjectAdapter.class);\r
+ Resource type = soa.adapt(Resource.class);\r
+ \r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(graph.isInheritedFrom(type, sr.ModuleSymbol)) {\r
+ Resource module = graph.getSingleObject(type, ModelingResources.getInstance(graph).SymbolToComponentType);\r
+ Resource configuration = graph.getSingleObject(module, StructuralResource2.getInstance(graph).IsDefinedBy);\r
+ Resource dia = graph.getSingleObject(configuration, ModelingResources.getInstance(graph).CompositeToDiagram);\r
+ Resource editorDia = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
+ if(dia.equals(editorDia))\r
+ unvalidModules.add(item);\r
+ }\r
+ }\r
+ return unvalidModules;\r
}\r
- \r
- // Trying to populate an instance of self to diagram -> infinite recursion\r
- // This limitation should be removed by allowing differentation (copies) from modules. i.e. detaching from class and creating a new module type\r
- if(!allowed)\r
- return;\r
- }\r
- }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ if(!unvalidModules.isEmpty()) {\r
+ for(ElementClassDragItem item : unvalidModules) {\r
+ dp.remove(item);\r
+ }\r
+ }\r
}\r
- super.dragEnter(dtde, dp);\r
}\r
}\r