]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTTabFolder.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTTabFolder.java
1 package org.simantics.views.swt.client.impl;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.eclipse.jface.viewers.ISelectionChangedListener;
8 import org.eclipse.jface.viewers.ISelectionProvider;
9 import org.eclipse.jface.viewers.SelectionChangedEvent;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.custom.CTabFolder;
12 import org.eclipse.swt.custom.CTabItem;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
16 import org.simantics.utils.ui.jface.BasePostSelectionProvider;
17 import org.simantics.views.ViewUtils.LayoutBean;
18 import org.simantics.views.swt.client.base.ISWTViewNode;
19 import org.simantics.views.swt.client.base.SingleSWTViewNode;
20
21 public class SWTTabFolder extends SingleSWTViewNode<CTabFolder> {
22         
23         private static final long serialVersionUID = -8003676031533344133L;
24
25         public List<String> childNames;
26
27         public LayoutBean layout;
28         
29         final BasePostSelectionProvider selectionProvider = new BasePostSelectionProvider();
30
31         ISelectionChangedListener selectionListener = new ISelectionChangedListener() {
32                 
33                 @Override
34                 public void selectionChanged(SelectionChangedEvent event) {
35                         selectionProvider.firePostSelection(event.getSelection());
36                 }
37                 
38         };
39         
40         private CTabItem createItem(int index, CTabFolder folder, Control control) {
41                 
42                 CTabItem item = new CTabItem(folder, SWT.NONE, index);
43                 item.setControl(control);
44                 return item;
45                 
46         }
47         
48         @Override
49         public void createControls(Composite parent) {
50                 control = new CTabFolder(parent, SWT.BOTTOM | SWT.FLAT);
51                 setProperties();
52                 createChildComposites();
53                 updateFolder();
54         }
55         
56         protected void childrenChanged() {
57             // FIXME: this is probably not what is intended, just modified it like this to get rid of the NPE it was producing.
58             if (control != null && !control.isDisposed())
59                 updateFolder();
60         }
61
62         protected void updateFolder() {
63                 
64                 assert(childNames != null);
65                 
66             HashSet<Control> existing = new HashSet<Control>();
67
68             for(CTabItem c : control.getItems()) {
69                 existing.add(c.getControl());
70             }
71
72             int index = existing.size();
73             boolean hasSomeSelectionProviders = false;
74
75             for(ISWTViewNode node : getChildComposites()) {
76
77                 Control c = node.getControl();
78                 if(c != null && !existing.contains(c)) {
79
80                     CTabItem item = createItem(index, control, c);
81                     item.setText(childNames.get(index++));
82
83                     for(Control c2 : getControls(item)) {
84                         if(c2 instanceof GraphExplorerComposite) {
85                             GraphExplorerComposite gec = (GraphExplorerComposite)c2;
86                             ISelectionProvider sp = (ISelectionProvider)gec.getAdapter(ISelectionProvider.class);
87                             sp.addSelectionChangedListener(selectionListener);
88                             hasSomeSelectionProviders = true;
89                         }
90                     }
91
92                 }
93             }
94
95             if(hasSomeSelectionProviders) {
96                 // We use our combined selection provider
97                 getSite().setSelectionProvider(selectionProvider);
98             }
99
100             control.setSelection(0);
101             control.update();
102         }
103
104     Set<Control> getControls(CTabItem item) {
105                 HashSet<Control> result = new HashSet<Control>();
106                 forControl(item.getControl(), result);
107                 return result;
108         }
109         
110         void forControl(Control control, HashSet<Control> controls) {
111                 controls.add(control);
112                 if(control instanceof Composite) {
113                         Composite composite = (Composite)control;
114                         for(Control child : composite.getChildren()) forControl(child, controls);
115                 }
116         }
117         
118         public void synchronizeChildNames(List<String> childNames) {
119                 
120         }
121         
122         final public void synchronizeLayout(LayoutBean layout) {
123         }
124         
125 }