]> gerrit.simantics Code Review - simantics/platform.git/blob
421315f892a6eae25d1b5a3f1a7e916298b6b934
[simantics/platform.git] /
1 package org.simantics.views.swt.client.impl;\r
2 \r
3 import org.eclipse.jface.layout.GridDataFactory;\r
4 import org.eclipse.jface.layout.GridLayoutFactory;\r
5 import org.eclipse.jface.resource.ColorDescriptor;\r
6 import org.eclipse.swt.SWT;\r
7 import org.eclipse.swt.custom.ScrolledComposite;\r
8 import org.eclipse.swt.graphics.Color;\r
9 import org.eclipse.swt.graphics.Point;\r
10 import org.eclipse.swt.graphics.RGB;\r
11 import org.eclipse.swt.widgets.Composite;\r
12 import org.eclipse.swt.widgets.Control;\r
13 import org.eclipse.swt.widgets.Display;\r
14 import org.eclipse.swt.widgets.Event;\r
15 import org.eclipse.swt.widgets.Listener;\r
16 import org.simantics.views.ViewUtils.GridLayoutBean;\r
17 import org.simantics.views.swt.client.base.SWTViewUtils;\r
18 import org.simantics.views.swt.client.base.SingleSWTViewNode;\r
19 \r
20 public class SWTScrolledComposite extends SingleSWTViewNode<ScrolledComposite> {\r
21         \r
22         private Composite explorers;\r
23 \r
24         private static final long serialVersionUID = 7932335224632082902L;\r
25         \r
26         public GridLayoutBean layout;\r
27         \r
28         private int minSize = 100;\r
29         \r
30         protected ScrollListener listener;\r
31         \r
32         public class ScrollListener implements Listener {\r
33 \r
34                 public Control[] composites;\r
35 \r
36                 final public int minSize;\r
37                 \r
38                 private boolean pending = false;\r
39 \r
40                 public ScrollListener(int minSize) {\r
41                         this.minSize = minSize;\r
42                 }\r
43 \r
44                 @Override\r
45                 public void handleEvent(Event event) {\r
46                         handleEvent();\r
47                 }\r
48 \r
49                 public void handleEvent() {\r
50                         \r
51                         if(pending) return;\r
52                         \r
53                         pending = true;\r
54                         \r
55                         Display.getCurrent().asyncExec(new Runnable() {\r
56 \r
57                                 @Override\r
58                                 public void run() {\r
59                                         doHandleEvent();\r
60                                 }\r
61                                 \r
62                         });\r
63                         \r
64                 }\r
65 \r
66                 public void doHandleEvent() {\r
67 \r
68                         if(isDisposed()) return;\r
69                         \r
70                         pending = false;\r
71 \r
72                         Point c = control.getSize();\r
73                         Point o = explorers.getSize();\r
74                         Point p = explorers.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);\r
75                         if(!o.equals(p)) {\r
76                                 explorers.setSize(c.x-25, p.y);\r
77                         }\r
78 \r
79                 }\r
80 \r
81         }\r
82         \r
83         @Override\r
84         public Control getControl() {\r
85                 return explorers;\r
86         }\r
87         \r
88         @Override\r
89         public void createControls(org.eclipse.swt.widgets.Composite parent) {\r
90                 \r
91         control = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);\r
92         \r
93         setProperties();\r
94                 \r
95                 listener = new ScrollListener(minSize);\r
96 \r
97                 control.setBackground( (Color) getResourceManager().get( ColorDescriptor.createFrom(new RGB(245, 245, 245)) ) );\r
98                 control.setLayout(GridLayoutFactory.fillDefaults()\r
99                                 .margins(0, 0).spacing(0, 0).create());\r
100                 control.setExpandVertical(false);\r
101                 control.setExpandHorizontal(false);\r
102                 GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(control);\r
103 \r
104                 explorers = new Composite(control, SWT.NONE);\r
105                 explorers.setBackground(Display.getCurrent().getSystemColor(\r
106                                 SWT.COLOR_WHITE));\r
107                 // Make sure that exploders are visible\r
108                 explorers.setSize(1300, 1300);\r
109                 explorers.setLayout(GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(1)\r
110                                 .spacing(0, 0).create());\r
111 \r
112                 control.setMinSize(100, 100);\r
113                 control.setContent(explorers);\r
114 \r
115         createChildComposites(explorers);\r
116         \r
117         listener.composites = explorers.getChildren();\r
118         \r
119         control.addListener(SWT.Resize, listener);\r
120         \r
121         for (Control composite : listener.composites) {\r
122                 composite.addListener(SWT.Resize, listener);\r
123         }\r
124         \r
125         }\r
126 \r
127         final public void synchronizeLayout(GridLayoutBean layout) {\r
128                 if(layout != null) control.setLayout(SWTViewUtils.toLayout(layout));\r
129         }\r
130         \r
131         public static void propagate(Control control) {\r
132                 \r
133                 Control parent = control.getParent();\r
134                 if(parent == null) return;\r
135                 \r
136                 if(parent instanceof ScrolledComposite) {\r
137                         \r
138                         Point c = parent.getSize();\r
139                         Point o = control.getSize();\r
140                         Point p = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);\r
141                         if(!o.equals(p)) {\r
142                                 control.setSize(c.x-25, p.y);\r
143                         }\r
144                         \r
145                 } else {\r
146                         \r
147                         propagate(parent);\r
148                         \r
149                 }\r
150                 \r
151         }\r
152         \r
153 }\r