]> gerrit.simantics Code Review - simantics/platform.git/blob
4fb21b2bf8ff2e4ab2ef24977913568400eef577
[simantics/platform.git] /
1 package org.simantics.document.swt.core.base;
2
3 import org.eclipse.swt.widgets.Composite;
4 import org.eclipse.swt.widgets.Control;
5 import org.simantics.document.server.JSONObject;
6 import org.simantics.document.server.client.WidgetData;
7 import org.simantics.document.swt.core.SWTDocument;
8
9 public abstract class WidgetContainer<C extends Control> {
10         
11         C control;
12         
13         abstract protected Control doCreateControl(SWTDocument document, Composite parent, JSONObject object);
14         abstract protected void doUpdateProperties(SWTDocument document, Control control, JSONObject object);
15         
16         @SuppressWarnings("unchecked")
17         public void createControl(SWTDocument document, Composite parent, final JSONObject object) {
18                 control = (C)doCreateControl(document, parent, object);
19                 if(control == null) {
20                         new Exception().printStackTrace();
21                         return;
22                 }
23                 doUpdateProperties(document, control, object);
24         }
25         
26         public C getControl() {
27                 return (C)control;
28         }
29         
30         @SuppressWarnings("unchecked")
31         public <T> T getOrCreateControl(SWTDocument document, JSONObject object) {
32                 if(control == null || control.isDisposed()) {
33                         String parentId = object.getJSONField("parent");
34                         WidgetData parent = document.getWidget(parentId);
35                         if(parent != null) {
36                                 WidgetContainer<?> parentContainer = (WidgetContainer<?>)parent.widget;
37                                 Composite pc = (Composite)parentContainer.getOrCreateControl(document, parent.object);
38                                 // TODO: pc may be disposed, how to handle this and why is it happening?
39                                 if(pc != null && !pc.isDisposed())
40                                         createControl(document, pc, object);
41                         }
42                 }
43                 return (T)control;
44         }
45         
46         public void updateProperties(SWTDocument document, JSONObject object) {
47                 if(control != null) {
48                         doUpdateProperties(document, control, object);
49                 }
50         }
51         
52 }