]> gerrit.simantics Code Review - simantics/district.git/blob
690860620059dc9c7802b2bf7bfa0468c840692b
[simantics/district.git] /
1 package org.simantics.district.network.ui.participants;
2
3 import java.awt.geom.AffineTransform;
4 import java.util.Map;
5
6 import org.simantics.district.network.ui.DistrictDiagramViewer;
7 import org.simantics.district.network.ui.nodes.DynamicVisualisationContributionsNode;
8 import org.simantics.district.network.visualisations.model.ColorBarOptions;
9 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
10 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
11 import org.simantics.district.network.visualisations.model.SizeBarOptions;
12 import org.simantics.g2d.canvas.ICanvasContext;
13 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
14 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
15 import org.simantics.scenegraph.g2d.G2DParentNode;
16 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
17 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
18 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
19 import org.simantics.utils.datastructures.hints.IHintContext.Key;
20 import org.simantics.utils.datastructures.hints.IHintListener;
21 import org.simantics.utils.datastructures.hints.IHintObservable;
22
23 public class DynamicVisualisationContributionsParticipant extends AbstractCanvasParticipant {
24
25     IHintListener hintListener = new HintListenerAdapter() {
26         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
27             ICanvasContext cc = getContext();
28             if (cc != null) {
29                 updateNode();
30                 cc.getContentContext().setDirty();
31             }
32         }
33     };
34     
35     private DynamicVisualisationContributionsNode node;
36     private AffineTransform transform;
37     
38     public DynamicVisualisationContributionsParticipant(AffineTransform tr) {
39         this.transform = tr;
40     }
41
42     @Override
43     public void addedToContext(ICanvasContext ctx) {
44         super.addedToContext(ctx);
45         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS, hintListener);
46         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS, hintListener);
47         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS, hintListener);
48         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS, hintListener);
49     }
50     
51     @Override
52     public void removedFromContext(ICanvasContext ctx) {
53         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS, hintListener);
54         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS, hintListener);
55         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS, hintListener);
56         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS, hintListener);
57         super.removedFromContext(ctx);
58     }
59
60     @SGInit
61     public void initSG(G2DParentNode parent) {
62         node = parent.addNode(DynamicVisualisationContributionsNode.ID, DynamicVisualisationContributionsNode.class);
63         node.setTransform(transform);
64         node.setEnabled(true);
65         node.setZIndex(1000);
66     }
67     
68     @EventHandler(priority = 0)
69     protected boolean handleKeyEvent(CommandEvent e) {
70         if (e.command.equals(DistrictDiagramViewer.MAP_COLOR_BAR_OPTIONS_CHANGE)) {
71             System.out.println(e);
72             return true;
73         }
74         return false;
75     }
76     
77 //    @Override
78 //    protected boolean handleCommand(CommandEvent e) {
79 //        if (e.command.equals(DistrictDiagramViewer.MAP_COLOR_BAR_OPTIONS_CHANGE)) {
80 //            ICanvasContext context = (ICanvasContext) e.getContext();
81 //            ColorBarOptions options = context.getHintStack().getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
82 //            this.colorBarsOptions = options;
83 //            repaint();
84 //            return true;
85 //        } else {
86 //            return super.handleCommand(e);
87 //        }
88 //    }
89     
90     protected void updateNode() {
91         node.setDynamicColoringObjects(getDynamicColoringObjects());
92         node.setColorBarOptions(getColorBarOptions());
93         node.setDynamicSizingObjects(getDynamicSizingObjects());
94         node.setSizeBarOptions(getSizeBarOptions());
95     }
96
97     private Map<String,DynamicColorContribution> getDynamicColoringObjects() {
98         Map<String,DynamicColorContribution> objects = getHint(DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS);
99         return objects;
100     }
101
102     private ColorBarOptions getColorBarOptions() {
103         ColorBarOptions options = getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
104         return options;
105     }
106
107     private Map<String, DynamicSizeContribution> getDynamicSizingObjects() {
108         Map<String, DynamicSizeContribution> objects = getHint(DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS);
109         return objects;
110     }
111
112     private SizeBarOptions getSizeBarOptions() {
113         SizeBarOptions options = getHint(DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS);
114         return options;
115     }
116 }