1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
14 import java.util.concurrent.CopyOnWriteArrayList;
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;
26 public class Scale extends WidgetImpl {
28 private ReadFactory<?, Integer> selectionFactory;
29 private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
31 final private Display display;
32 final private org.eclipse.swt.widgets.Scale scale;
34 public Scale(Composite parent, WidgetSupport support, int style) {
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);
42 public void setSelectionFactory(ReadFactory<?, Integer> selectionFactory) {
43 this.selectionFactory = selectionFactory;
46 public org.eclipse.swt.widgets.Scale getWidget() {
51 public Control getControl() {
56 public void setInput(ISessionContext context, Object input) {
58 if (selectionListeners != null) {
59 for (SelectionListener listener : selectionListeners) {
60 if(listener instanceof Widget) {
61 ((Widget) listener).setInput(context, input);
66 if(selectionFactory != null) {
67 selectionFactory.listen(context, input, new Listener<Integer>() {
70 public void exception(Throwable t) {
75 public void execute(final Integer selection) {
76 if(isDisposed()) return;
77 display.asyncExec(new Runnable() {
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);
92 public boolean isDisposed() {
93 return scale.isDisposed();
101 public synchronized void addSelectionListener(SelectionListener listener) {
102 selectionListeners.add(listener);
103 scale.addSelectionListener(listener);