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