]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/client/WidgetData.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / client / WidgetData.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.server.client;
13
14 import java.util.HashSet;
15 import java.util.TreeMap;
16
17 import org.simantics.document.server.IEventCommand;
18 import org.simantics.document.server.JSONObject;
19 import org.simantics.document.server.io.CommandContext;
20 import org.simantics.document.server.io.ICommand;
21
22 public class WidgetData {
23         
24         public Object widget;
25         public JSONObject object;
26         public Document document;
27         public TreeMap<String, WidgetData> childmap = new TreeMap<String, WidgetData>(new AlphanumericComparator());
28     public HashSet<Object> listenerRegistrations = new HashSet<Object>();
29
30         public WidgetData(Document document, Object widget, JSONObject object) {
31                 this.widget = widget;
32                 this.object = object;
33                 this.document = document;
34         }
35         
36         @SuppressWarnings({ "rawtypes" })
37         public Object createElement() {
38                 if(object == null)
39                         return null;
40                 WidgetManager manager = document.getManager(object);
41                 return manager == null ? null : manager.createWidget(object);
42         }
43         
44         @SuppressWarnings({ "unchecked", "rawtypes" })
45         public void updateChildren() {
46                 if(object != null && widget != null && childmap != null) {
47                         WidgetManager manager = document.getManager(object);
48                         if(manager != null)
49                                 manager.updateChildren(document, object, widget, childmap);
50                 }
51         }
52         
53         @SuppressWarnings({ "unchecked", "rawtypes" })
54         public void updateProperties() {
55                 if(object == null)
56                         return;
57                 WidgetManager manager = document.getManager(object);
58                 if(manager != null)
59                         manager.updateProperties(document, object, widget);             
60         }
61         
62     @SuppressWarnings({ "rawtypes", "unchecked" })
63     public void updateCommands() {
64         if(object == null)
65             return;
66         
67         // Then create the updated registrations
68         CommandManager manager = document.getCommandManager(object); 
69         
70         // First remove all handler registrations
71         if(manager != null) {
72                 for(Object listener : listenerRegistrations) {
73                         manager.removeListener(widget, listener);
74                 }
75         }
76         listenerRegistrations.clear();
77         if(manager != null)
78             listenerRegistrations.addAll(manager.updateCommandListeners(document, object, widget));
79         
80     }
81     
82     @SuppressWarnings({ "unchecked", "rawtypes" })
83     public IEventCommand eventCommand(ICommand command, CommandContext c) {
84         if(object == null)
85             return null;
86         WidgetManager manager = document.getManager(object);
87         if(manager != null)
88             return manager.eventCommand(document, object, widget, command, c); 
89         else
90             return null;
91     }
92         
93 }