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