]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Scale.java
Merge "Font property for DIA.TextElement"
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / Scale.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.widgets;
13
14 import java.util.concurrent.CopyOnWriteArrayList;
15
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Display;
20 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;
21 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
23 import org.simantics.db.management.ISessionContext;
24 import org.simantics.db.procedure.Listener;
25
26 public class Scale extends WidgetImpl {
27
28     private ReadFactory<?, Integer> selectionFactory;
29     private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
30
31     final private Display display;
32     final private org.eclipse.swt.widgets.Scale scale;
33
34     public Scale(Composite parent, WidgetSupport support, int style) {
35         super(support);
36         this.display = parent.getDisplay();
37         scale = new org.eclipse.swt.widgets.Scale(parent, style);
38         scale.setData("org.simantics.browsing.ui.widgets.Scale", this);
39         support.register(this);
40     }
41
42     public void setSelectionFactory(ReadFactory<?, Integer> selectionFactory) {
43         this.selectionFactory = selectionFactory;
44     }
45
46     public org.eclipse.swt.widgets.Scale getWidget() {
47         return scale;
48     }
49
50     @Override
51     public Control getControl() {
52         return scale;
53     }
54
55     @Override
56     public void setInput(ISessionContext context, Object input) {
57
58         if (selectionListeners != null) {
59             for (SelectionListener listener : selectionListeners) {
60                 if(listener instanceof Widget) {
61                     ((Widget) listener).setInput(context, input);
62                 }
63             }
64         }
65
66         if(selectionFactory != null) {
67             selectionFactory.listen(context, input, new Listener<Integer>() {
68
69                 @Override
70                 public void exception(Throwable t) {
71                     t.printStackTrace();
72                 }
73
74                 @Override
75                 public void execute(final Integer selection) {
76                     if(isDisposed()) return;
77                     display.asyncExec(new Runnable() {
78
79                         @Override
80                         public void run() {
81                             if(isDisposed()) return;
82                             if(selection == null) return;
83                             for(SelectionListener listener : selectionListeners) scale.removeSelectionListener(listener);
84                             scale.setSelection(selection);
85                             for(SelectionListener listener : selectionListeners) scale.addSelectionListener(listener);
86                         }
87
88                     });
89                 }
90
91                 @Override
92                 public boolean isDisposed() {
93                     return scale.isDisposed();
94                 }
95
96             });
97         }
98
99     }
100
101     public synchronized void addSelectionListener(SelectionListener listener) {
102         selectionListeners.add(listener);
103         scale.addSelectionListener(listener);
104     }
105
106 }