1 /*******************************************************************************
2 * Copyright (c) 2019 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.swt.core;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
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;
45 public class SWTViews {
47 private static WidgetMappingImpl mapping = null;
49 public static WidgetMapping getMapping() {
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());
71 private static CommandMappingImpl commandMapping = null;
73 public static CommandMapping getCommandMapping() {
75 if(commandMapping == null) {
76 commandMapping = new CommandMappingImpl();
77 commandMapping.register("Button", new ButtonWidget.ButtonCommandManager());
78 commandMapping.register("Explorer", new Explorer.ExplorerCommandManager());
81 return commandMapping;
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)
92 if(trigger.equals(c.getTrigger())) {
93 WidgetData wd = document.getWidgetData().get(c.getTargetId());
95 data.add(new Pair<WidgetData, ICommand>(wd, c));
101 public static void notifyScrolledComposite(Control c) {
102 if(c instanceof ScrolledCompositeContent) {
103 ScrolledCompositeContent content = (ScrolledCompositeContent)c;
104 content.refreshSize();
107 Composite parent = c.getParent();
108 if(parent == null) return;
109 notifyScrolledComposite(parent);
112 public static Map<String, Object> encoded = new HashMap<String, Object>();
114 public static String encode(JSONObject object, String property, Object data) {
115 String key = object.getId() + "#" + property;
116 encoded.put(key, data);
120 public static Object decode(String key) {
121 return encoded.get(key);