Getting Resource from ISymbolItem
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / scl / SymbolLibrary.java
1 package org.simantics.diagram.scl;
2
3 import java.awt.Color;
4
5 import org.simantics.db.Resource;
6 import org.simantics.diagram.symbollibrary.ISymbolItem;
7 import org.simantics.g2d.canvas.Hints;
8 import org.simantics.g2d.canvas.ICanvasContext;
9 import org.simantics.g2d.canvas.impl.CanvasContext;
10 import org.simantics.g2d.diagram.DiagramClass;
11 import org.simantics.g2d.diagram.DiagramHints;
12 import org.simantics.g2d.diagram.IDiagram;
13 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
14 import org.simantics.g2d.diagram.handler.layout.FlowLayout;
15 import org.simantics.g2d.diagram.impl.Diagram;
16 import org.simantics.g2d.diagram.participant.DiagramParticipant;
17 import org.simantics.g2d.diagram.participant.ElementInteractor;
18 import org.simantics.g2d.diagram.participant.ElementPainter;
19 import org.simantics.g2d.diagram.participant.Selection;
20 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
21 import org.simantics.g2d.element.ElementClass;
22 import org.simantics.g2d.element.ElementHints;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.g2d.element.handler.StaticSymbol;
25 import org.simantics.g2d.element.impl.Element;
26 import org.simantics.g2d.gallery.GalleryItemSGNode;
27 import org.simantics.g2d.image.DefaultImages;
28 import org.simantics.g2d.image.Image;
29 import org.simantics.g2d.participant.BackgroundPainter;
30 import org.simantics.g2d.participant.KeyToCommand;
31 import org.simantics.g2d.participant.KeyUtil;
32 import org.simantics.g2d.participant.MouseUtil;
33 import org.simantics.g2d.participant.SymbolUtil;
34 import org.simantics.g2d.participant.TransformUtil;
35 import org.simantics.g2d.tooltip.TooltipParticipant;
36 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;
37 import org.simantics.utils.datastructures.hints.IHintContext;
38 import org.simantics.utils.threads.AWTThread;
39 import org.simantics.utils.threads.IThreadWorkQueue;
40
41 public class SymbolLibrary {
42         
43         public static ICanvasContext create(ISymbolItem item) {
44                 IDiagram diagram =  Diagram.spawnNew(DiagramClass.DEFAULT);
45         diagram.setHint(FlowLayout.HGAP, 5.0);
46         diagram.setHint(FlowLayout.VGAP, 5.0);
47         IThreadWorkQueue thread = AWTThread.getThreadAccess();
48         CanvasContext ctx = new CanvasContext(thread);
49         initializeCanvasContext(ctx);
50         IHintContext hintCtx = ctx.getDefaultHintContext();
51         hintCtx.setHint(DiagramHints.KEY_DIAGRAM, diagram);
52         
53         ElementClass ec = item.getElementClass(diagram);
54         StaticSymbol ss = ec.getSingleItem(StaticSymbol.class);
55         Image source = ss == null ? DefaultImages.UNKNOWN2.get() : ss.getImage();
56         //ImageProxy proxy = new ImageProxy(source);
57         
58         IElement element = Element.spawnNew(ec);
59         element.setHint(ElementHints.KEY_OBJECT, item);
60         element.setHint(GalleryItemSGNode.KEY_IMAGE, source);
61         diagram.addElement(element);
62         
63         //element.getElementClass().getSingleItem(GalleryItemSGNode.class).update(element);
64         
65         return ctx;
66         }
67         
68         public static Resource getItemResource(ISymbolItem item) {
69                 return item.getAdapter(Resource.class);
70         }
71         
72          private static void initializeCanvasContext(final CanvasContext canvasContext) {
73                 // Create canvas context and a layer of interactors
74                 final IHintContext h = canvasContext.getDefaultHintContext();
75
76                 // Support & Util Participants
77                 canvasContext.add( new TransformUtil() );
78
79                 canvasContext.add( new MouseUtil() );
80                 canvasContext.add( new KeyUtil() );
81                 canvasContext.add( new SymbolUtil() );
82
83                 // Grid & Ruler & Background
84                 h.setHint(Hints.KEY_BACKGROUND_COLOR, Color.WHITE);
85                 canvasContext.add( new BackgroundPainter() );
86
87                 // Key bindings
88                 canvasContext.add( new KeyToCommand( CommandKeyBinding.DEFAULT_BINDINGS ) );
89
90                 ////// Diagram Participants //////
91                 PointerInteractor pi = new PointerInteractor(true, true, false, true, false, null);
92                 pi.setBoxSelectMode(PickPolicy.PICK_INTERSECTING_OBJECTS);
93                 canvasContext.add( pi );
94                 canvasContext.add( new Selection() );
95                 canvasContext.add( new DiagramParticipant() );
96                 canvasContext.add( new ElementPainter() );
97                 canvasContext.add( new ElementInteractor() );
98                 canvasContext.add( new TooltipParticipant());
99
100                 h.setHint(ElementPainter.KEY_SELECTION_FRAME_COLOR, Color.WHITE);
101                 h.setHint(ElementPainter.KEY_SELECTION_CONTENT_COLOR, new Color(0.7f, 0.7f, 1.f, 0.5f));
102                 h.setHint(Hints.KEY_TOOL, Hints.POINTERTOOL);
103
104                 
105                 canvasContext.assertParticipantDependencies();
106             }
107
108 }