]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/SWTViews.java
Playground for Antti.
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / SWTViews.java
1 package org.simantics.document.swt.core;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.swt.widgets.Control;
11 import org.simantics.document.server.JSONObject;
12 import org.simantics.document.server.client.CommandMapping;
13 import org.simantics.document.server.client.CommandMappingImpl;
14 import org.simantics.document.server.client.Document;
15 import org.simantics.document.server.client.WidgetData;
16 import org.simantics.document.server.client.WidgetMapping;
17 import org.simantics.document.server.client.WidgetMappingImpl;
18 import org.simantics.document.server.io.ICommand;
19 import org.simantics.document.swt.core.base.ScrolledCompositeContent;
20 import org.simantics.document.swt.core.widget.BrowserWidget;
21 import org.simantics.document.swt.core.widget.ButtonWidget;
22 import org.simantics.document.swt.core.widget.ComboWidget;
23 import org.simantics.document.swt.core.widget.CommandEventWidget;
24 import org.simantics.document.swt.core.widget.Explorer;
25 import org.simantics.document.swt.core.widget.FillComposite;
26 import org.simantics.document.swt.core.widget.GridCell;
27 import org.simantics.document.swt.core.widget.GridComposite;
28 import org.simantics.document.swt.core.widget.LabelWidget;
29 import org.simantics.document.swt.core.widget.SCLTextEditor;
30 import org.simantics.document.swt.core.widget.ScrolledCompositeWidget;
31 import org.simantics.document.swt.core.widget.TrackedTextWidget;
32 import org.simantics.utils.datastructures.Pair;
33
34 public class SWTViews {
35
36         private static WidgetMappingImpl mapping = null;
37         
38         public static WidgetMapping getMapping() {
39                 
40                 if(mapping == null) {
41                         mapping = new WidgetMappingImpl();
42                         mapping.register("Root", new FillComposite());
43                         mapping.register("GridComposite", new GridComposite());
44                         mapping.register("ScrolledComposite", new ScrolledCompositeWidget());
45                         mapping.register("GridCell", new GridCell());
46                         mapping.register("Label", new LabelWidget());
47                         mapping.register("Button", new ButtonWidget());
48                         mapping.register("TrackedText", new TrackedTextWidget());
49                         mapping.register("Combo", new ComboWidget());
50                         mapping.register("Explorer", new Explorer());
51                         mapping.register("CommandEvent", new CommandEventWidget());
52                         mapping.register("Browser", new BrowserWidget());
53                         mapping.register("SCLTextEditor", new SCLTextEditor());
54                 }
55                 
56                 return mapping;
57                 
58         }
59         
60     private static CommandMappingImpl commandMapping = null;
61     
62     public static CommandMapping getCommandMapping() {
63         
64         if(commandMapping == null) {
65             
66             commandMapping = new CommandMappingImpl();
67             commandMapping.register("Button", new ButtonWidget.ButtonCommandManager());
68             commandMapping.register("Explorer", new Explorer.ExplorerCommandManager());
69             
70         }
71         
72         return commandMapping;
73         
74     }
75
76     public static List<Pair<WidgetData, ICommand>> getTriggeredCommands(Document document, Collection<ICommand> commands, String trigger) {
77         // Nulls should not get this far
78         assert(commands != null);
79         List<Pair<WidgetData, ICommand>> data = new ArrayList<Pair<WidgetData, ICommand>>();
80         for(ICommand c : commands) {
81             if(c.getCommand() == null || c.getTargetId() == null || c.getTrigger() == null)
82                 continue;
83             if(trigger.equals(c.getTrigger())) {
84                 WidgetData wd = document.getWidgetData().get(c.getTargetId());
85                 if(wd != null)
86                     data.add(new Pair<WidgetData, ICommand>(wd, c));
87             }
88         }
89         return data;
90     }
91
92         public static void notifyScrolledComposite(Control c) {
93                 if(c instanceof ScrolledCompositeContent) {
94                         ScrolledCompositeContent content = (ScrolledCompositeContent)c;
95                         content.refreshSize();
96                         return;
97                 }
98                 Composite parent = c.getParent();
99                 if(parent == null) return;
100                 notifyScrolledComposite(parent);
101         }
102         
103         public static Map<String, Object> encoded = new HashMap<String, Object>();
104         
105         public static String encode(JSONObject object, String property, Object data) {
106             String key = object.getId() + "#" + property;
107             encoded.put(key, data);
108             return key;
109         }
110         
111         public static Object decode(String key) {
112             return encoded.get(key);
113         }
114         
115 }