1 package org.simantics.document.swt.core;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
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;
34 public class SWTViews {
36 private static WidgetMappingImpl mapping = null;
38 public static WidgetMapping getMapping() {
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());
60 private static CommandMappingImpl commandMapping = null;
62 public static CommandMapping getCommandMapping() {
64 if(commandMapping == null) {
66 commandMapping = new CommandMappingImpl();
67 commandMapping.register("Button", new ButtonWidget.ButtonCommandManager());
68 commandMapping.register("Explorer", new Explorer.ExplorerCommandManager());
72 return commandMapping;
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)
83 if(trigger.equals(c.getTrigger())) {
84 WidgetData wd = document.getWidgetData().get(c.getTargetId());
86 data.add(new Pair<WidgetData, ICommand>(wd, c));
92 public static void notifyScrolledComposite(Control c) {
93 if(c instanceof ScrolledCompositeContent) {
94 ScrolledCompositeContent content = (ScrolledCompositeContent)c;
95 content.refreshSize();
98 Composite parent = c.getParent();
99 if(parent == null) return;
100 notifyScrolledComposite(parent);
103 public static Map<String, Object> encoded = new HashMap<String, Object>();
105 public static String encode(JSONObject object, String property, Object data) {
106 String key = object.getId() + "#" + property;
107 encoded.put(key, data);
111 public static Object decode(String key) {
112 return encoded.get(key);