]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/SWTViews.java
Merge "Revert "Support enumerated property types in UC interface""
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / SWTViews.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.swt.core;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.simantics.document.server.JSONObject;
23 import org.simantics.document.server.client.CommandMapping;
24 import org.simantics.document.server.client.CommandMappingImpl;
25 import org.simantics.document.server.client.Document;
26 import org.simantics.document.server.client.WidgetData;
27 import org.simantics.document.server.client.WidgetMapping;
28 import org.simantics.document.server.client.WidgetMappingImpl;
29 import org.simantics.document.server.io.ICommand;
30 import org.simantics.document.swt.core.base.ScrolledCompositeContent;
31 import org.simantics.document.swt.core.widget.BrowserWidget;
32 import org.simantics.document.swt.core.widget.ButtonWidget;
33 import org.simantics.document.swt.core.widget.ComboWidget;
34 import org.simantics.document.swt.core.widget.CommandEventWidget;
35 import org.simantics.document.swt.core.widget.Explorer;
36 import org.simantics.document.swt.core.widget.FillComposite;
37 import org.simantics.document.swt.core.widget.GridCell;
38 import org.simantics.document.swt.core.widget.GridComposite;
39 import org.simantics.document.swt.core.widget.LabelWidget;
40 import org.simantics.document.swt.core.widget.SCLTextEditor;
41 import org.simantics.document.swt.core.widget.ScrolledCompositeWidget;
42 import org.simantics.document.swt.core.widget.TrackedTextWidget;
43 import org.simantics.utils.datastructures.Pair;
44
45 public class SWTViews {
46
47         private static WidgetMappingImpl mapping = null;
48
49         public static WidgetMapping getMapping() {
50
51                 if(mapping == null) {
52                         mapping = new WidgetMappingImpl();
53                         mapping.register("Root", new FillComposite());
54                         mapping.register("GridComposite", new GridComposite());
55                         mapping.register("ScrolledComposite", new ScrolledCompositeWidget());
56                         mapping.register("GridCell", new GridCell());
57                         mapping.register("Label", new LabelWidget());
58                         mapping.register("Button", new ButtonWidget());
59                         mapping.register("TrackedText", new TrackedTextWidget());
60                         mapping.register("Combo", new ComboWidget());
61                         mapping.register("Explorer", new Explorer());
62                         mapping.register("CommandEvent", new CommandEventWidget());
63                         mapping.register("Browser", new BrowserWidget());
64                         mapping.register("SCLTextEditor", new SCLTextEditor());
65                 }
66
67                 return mapping;
68
69         }
70
71         private static CommandMappingImpl commandMapping = null;
72
73         public static CommandMapping getCommandMapping() {
74
75                 if(commandMapping == null) {
76                         commandMapping = new CommandMappingImpl();
77                         commandMapping.register("Button", new ButtonWidget.ButtonCommandManager());
78                         commandMapping.register("Explorer", new Explorer.ExplorerCommandManager());
79                 }
80
81                 return commandMapping;
82
83         }
84
85         public static List<Pair<WidgetData, ICommand>> getTriggeredCommands(Document document, Collection<ICommand> commands, String trigger) {
86                 // Nulls should not get this far
87                 assert(commands != null);
88                 List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
89                 for(ICommand c : commands) {
90                         if(c.getCommand() == null || c.getTargetId() == null || c.getTrigger() == null)
91                                 continue;
92                         if(trigger.equals(c.getTrigger())) {
93                                 WidgetData wd = document.getWidgetData().get(c.getTargetId());
94                                 if(wd != null)
95                                         data.add(new Pair<WidgetData, ICommand>(wd, c));
96                         }
97                 }
98                 return data;
99         }
100
101         public static void notifyScrolledComposite(Control c) {
102                 if(c instanceof ScrolledCompositeContent) {
103                         ScrolledCompositeContent content = (ScrolledCompositeContent)c;
104                         content.refreshSize();
105                         return;
106                 }
107                 Composite parent = c.getParent();
108                 if(parent == null) return;
109                 notifyScrolledComposite(parent);
110         }
111
112         public static Map<String, Object> encoded = new HashMap<String, Object>();
113
114         public static String encode(JSONObject object, String property, Object data) {
115             String key = object.getId() + "#" + property;
116             encoded.put(key, data);
117             return key;
118         }
119
120         public static Object decode(String key) {
121             return encoded.get(key);
122         }
123
124 }