Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ViewpointSelector.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.util.Collection;
15
16 import org.eclipse.jface.layout.GridDataFactory;
17 import org.eclipse.jface.viewers.IPostSelectionProvider;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.graphics.FontData;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.simantics.browsing.ui.BuiltinKeys;
29 import org.simantics.browsing.ui.GraphExplorer;
30 import org.simantics.browsing.ui.NodeContext;
31 import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;
32 import org.simantics.browsing.ui.content.ViewpointFactory;
33 import org.simantics.db.layer0.SelectionHints;
34 import org.simantics.utils.datastructures.Pair;
35 import org.simantics.utils.ui.ISelectionUtils;
36 import org.simantics.utils.ui.LayoutUtils;
37
38 /**
39  * TODO: support multiselection within the IGraphExplorer
40  */
41 public class ViewpointSelector extends Composite {
42
43     Font smallFont;
44
45     public ViewpointSelector(final GraphExplorer explorer, final UserSelectedViewpointFactoryQueryProcessor queryProcessor, Composite parent, int style) {
46
47         super(parent, style);
48
49         setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
50
51         setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
52
53         final Combo combo = new Combo(this, SWT.READ_ONLY);
54
55         Font initialFont = combo.getFont();
56         FontData[] fontData = initialFont.getFontData();
57         for (int i = 0; i < fontData.length; i++) {
58             fontData[i].setHeight(fontData[i].getHeight() - 1);
59         }
60         smallFont = new Font(getDisplay(), fontData);
61         combo.setFont(smallFont);
62
63         combo.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
64         combo.add("-- No viewpoints --");
65         combo.select(0);
66
67         combo.addSelectionListener(new SelectionListener() {
68
69             @Override
70             public void widgetDefaultSelected(SelectionEvent e) {
71                 widgetSelected(e);
72             }
73
74             @SuppressWarnings("unchecked")
75             @Override
76             public void widgetSelected(SelectionEvent e) {
77
78                 int index = combo.getSelectionIndex();
79
80                 Pair<ViewpointFactory, NodeContext> pair =
81                     (Pair<ViewpointFactory, NodeContext>)combo.getData(String.valueOf(index));
82                 if (pair == null)
83                     return;
84
85                 queryProcessor.select(pair.second, pair.first);
86
87             }
88
89         });
90
91         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
92
93         selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
94
95             @Override
96             public void selectionChanged(SelectionChangedEvent event) {
97
98                 ISelection selection = event.getSelection();
99                 NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
100
101                 if(context == null) {
102                     context = explorer.getRoot();
103                 }
104
105                 assert(context != null);
106
107                 Collection<ViewpointFactory> factories = explorer.query(context, BuiltinKeys.VIEWPOINT_FACTORIES);
108
109                 ViewpointFactory oldSelection = queryProcessor.getSelection(context);
110
111                 combo.removeAll();
112                 if(factories != null) {
113                     for(ViewpointFactory f : factories) {
114                         int index = combo.getItemCount();
115                         combo.add(f.toString());
116                         combo.setData(String.valueOf(index), new Pair<ViewpointFactory, NodeContext>(f, context));
117                         if(f == oldSelection) {
118                             combo.select(index);
119                         }
120                     }
121                 }
122                 if(combo.getItemCount() == 0) {
123                     combo.add("-- No viewpoints --");
124                 }
125                 if(combo.getSelectionIndex() == -1) {
126                     combo.select(0);
127                 }
128
129             }
130
131         });
132
133     }
134
135     @Override
136     public void dispose() {
137         if (isDisposed())
138             return;
139         smallFont.dispose();
140         super.dispose();
141     }
142
143 }