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.HashSet;
18 import java.util.List;
21 import org.eclipse.jface.layout.GridDataFactory;
22 import org.eclipse.jface.layout.GridLayoutFactory;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Device;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.ui.IWorkbenchSite;
30 import org.simantics.db.common.utils.Logger;
31 import org.simantics.document.server.IEventCommand;
32 import org.simantics.document.server.JSONObject;
33 import org.simantics.document.server.client.DocumentClient;
34 import org.simantics.document.server.client.WidgetData;
35 import org.simantics.document.server.client.WidgetMapping;
36 import org.simantics.document.server.io.AbstractEventHandler;
37 import org.simantics.document.server.io.CommandContext;
38 import org.simantics.document.server.io.CommandContextMutable;
39 import org.simantics.document.server.io.ICommand;
40 import org.simantics.document.swt.core.base.WidgetContainer;
41 import org.simantics.document.swt.core.widget.FillComposite;
42 import org.simantics.ui.colors.Colors;
43 import org.simantics.ui.fonts.Fonts;
44 import org.simantics.utils.datastructures.Pair;
45 import org.simantics.utils.threads.IThreadWorkQueue;
46 import org.simantics.utils.threads.SWTThread;
48 public class SWTDocumentClient extends DocumentClient implements SWTDocument {
50 private Composite container;
51 private IWorkbenchSite site;
52 private ISelectionProvider selectionProvider;
53 private Device device;
54 private IThreadWorkQueue thread;
55 private Map<org.simantics.datatypes.literal.RGB.Integer, Color> colors = new HashMap<org.simantics.datatypes.literal.RGB.Integer, Color>();
56 private Map<org.simantics.datatypes.literal.Font, Font> fonts = new HashMap<org.simantics.datatypes.literal.Font, Font>();
58 private boolean requireLayout=false;
60 public SWTDocumentClient(WidgetMapping mapping, ISelectionProvider selectionProvider, IWorkbenchSite site, Composite container) {
62 super(mapping, SWTViews.getCommandMapping());
64 this.container = container;
66 this.selectionProvider = selectionProvider;
67 this.device = container.getDisplay();
68 this.thread = SWTThread.getThreadAccess(container);
70 JSONObject rootObject = new JSONObject("root");
71 rootObject.addJSONField("type", "Root");
72 FillComposite mgr = new FillComposite();
73 WidgetContainer<?> wc = mgr.createWidget(rootObject);
74 wc.createControl(this, container, rootObject);
76 GridDataFactory.fillDefaults().grab(true, true).minSize(1, 1).applyTo((Control)wc.getControl());
77 GridLayoutFactory.fillDefaults().applyTo((Composite)wc.getControl());
79 widgetData.put("root", new WidgetData(this, wc, rootObject));
84 public IThreadWorkQueue thread() {
89 public Color getColor(org.simantics.datatypes.literal.RGB.Integer descriptor) {
90 Color color = colors.get(descriptor);
92 color = Colors.swt(device, descriptor);
93 colors.put(descriptor, color);
99 public Font getFont(org.simantics.datatypes.literal.Font descriptor) {
100 Font font = fonts.get(descriptor);
102 font = Fonts.swt(device, descriptor);
103 fonts.put(descriptor, font);
109 public ISelectionProvider getSelectionProvider() {
110 return selectionProvider;
114 public IWorkbenchSite getSite() {
119 public void post(AbstractEventHandler handler, CommandContextMutable parameters) {
120 handler.handle(parameters);
125 protected void updateDocument(Collection<JSONObject> objects) {
126 assert thread.currentThreadAccess();
127 super.updateDocument(objects);
129 container.layout(true, true);
130 requireLayout = false;
135 protected void updateTree(HashSet<WidgetData> updates) {
137 if(updates.isEmpty()) return;
139 for(WidgetData data : updates) {
140 WidgetContainer<?> container = (WidgetContainer<?>)data.widget;
141 Control ctrl = container.getControl();
142 if (ctrl == null || ctrl.isDisposed())
144 if(ctrl instanceof Composite) {
145 Composite composite = (Composite)ctrl;
146 for(Control c : composite.getChildren()) {
152 super.updateTree(updates);
154 for(WidgetData data : widgetData.values()) {
155 WidgetContainer<?> container = (WidgetContainer<?>)data.widget;
156 if(container != null)
157 container.getOrCreateControl(this, data.object);
163 public CommandContext handleCommands(List<Pair<WidgetData, ICommand>> data, CommandContextMutable context, Object component) {
165 // Build a linked list of commands
167 ArrayList<IEventCommand> commands = new ArrayList<>();
168 for(Pair<WidgetData, ICommand> pair : data) {
169 WidgetData d = pair.first;
171 ICommand c = pair.second;
172 IEventCommand p = d.eventCommand(c, null);
173 // if(component != null && p != null)
174 // p.setTrigger(component);
176 if(!commands.isEmpty())
177 commands.get(commands.size()-1).setNext(p);
182 // Execute the first command, the linked list handles the rest of them
183 if(!commands.isEmpty()) {
185 commands.get(0).handleCommand(context);
193 public void layout() {
194 requireLayout = true;
198 public HashMap<String, WidgetData> getWidgetData() {
202 public void displayError(String error) {
203 Logger.defaultLogError(error);