1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.jfreechart.chart.properties;
\r
14 import org.eclipse.core.runtime.ListenerList;
\r
15 import org.eclipse.swt.events.ModifyEvent;
\r
16 import org.eclipse.swt.events.ModifyListener;
\r
17 import org.eclipse.swt.widgets.Composite;
\r
18 import org.eclipse.swt.widgets.Spinner;
\r
19 import org.simantics.browsing.ui.common.ErrorLogger;
\r
20 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;
\r
21 import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;
\r
22 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;
\r
23 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
24 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
25 import org.simantics.db.management.ISessionContext;
\r
26 import org.simantics.db.procedure.Listener;
\r
27 import org.simantics.utils.ui.SWTUtils;
\r
30 * Class for implementing Widget behavior for SWT Spinner in Simantics.
\r
32 * @author Teemu Lempinen
\r
35 public class TrackedSpinner implements Widget {
\r
37 private Spinner spinner;
\r
38 private ListenerList modifyListeners;
\r
39 private ReadFactory<?, Integer> selectionFactory;
\r
42 public TrackedSpinner(Composite parent, WidgetSupport support, int style) {
\r
43 spinner = new Spinner(parent, style);
\r
44 support.register(this);
\r
46 // Add a ModifyListener that uses all listeners in modifyListeners -list
\r
47 spinner.addModifyListener(new ModifyListener() {
\r
50 public void modifyText(ModifyEvent e) {
\r
51 if (modifyListeners != null) {
\r
52 TrackedModifyEvent event = new TrackedModifyEvent(spinner, spinner.getText());
\r
53 for (Object o : modifyListeners.getListeners()) {
\r
54 ((TextModifyListener) o).modifyText(event);
\r
62 public void setInput(ISessionContext context, Object input) {
\r
64 // Update all modifyListeners
\r
65 if (modifyListeners != null) {
\r
66 for (Object o : modifyListeners.getListeners()) {
\r
67 if(o instanceof Widget) {
\r
68 ((Widget) o).setInput(context, input);
\r
73 if (selectionFactory != null) {
\r
74 // Get a value for the spinner
\r
75 selectionFactory.listen(context, input, new Listener<Integer>() {
\r
77 public void exception(Throwable t) {
\r
78 ErrorLogger.defaultLogError(t);
\r
81 public void execute(final Integer selection) {
\r
82 SWTUtils.asyncExec(spinner, new Runnable() {
\r
85 if(isDisposed()) return;
\r
86 spinner.setSelection(selection);
\r
91 public boolean isDisposed() {
\r
92 return spinner.isDisposed();
\r
100 * Set a selection factory for the spinner
\r
102 * @param selectionFactory ReadFactory<?, Integer> SelectionFactory
\r
104 public void setSelectionFactory(ReadFactory<?, Integer> selectionFactory) {
\r
105 this.selectionFactory = selectionFactory;
\r
109 * Add a modifyListener for the spinner
\r
110 * @param listener TextModifyListener
\r
112 public synchronized void addModifyListener(TextModifyListener listener) {
\r
113 if (modifyListeners == null) {
\r
114 modifyListeners = new ListenerList(ListenerList.IDENTITY);
\r
116 modifyListeners.add(listener);
\r
120 * Remove modifyListener from the spinner
\r
122 * @param listener TextModifyListener
\r
124 public synchronized void removeModifyListener(TextModifyListener listener) {
\r
125 if (modifyListeners == null)
\r
127 modifyListeners.remove(listener);
\r
131 * Get the SWT Spinner of this TrackedSpinner widget
\r
134 public Spinner getWidget() {
\r
139 * Set minimum value
\r
140 * @param value int minimum value
\r
142 public void setMinimum(int value) {
\r
143 spinner.setMinimum(value);
\r
147 * Set maximum value
\r
148 * @param value int maximum value
\r
150 public void setMaximum(int value) {
\r
151 spinner.setMaximum(value);
\r
155 * Sets the receiver's selection, minimum value, maximum value, digits, increment and page increment all at once.
\r
157 * @param selection the new selection value
\r
158 * @param minimum the new minimum value
\r
159 * @param maximum the new maximum value
\r
160 * @param digits the new digits value
\r
161 * @param increment the new increment value
\r
162 * @param pageIncrement the new pageIncrement value
\r
164 public void setValues(int selection, int minimum, int maximum, int digits, int increment, int pageIncrement) {
\r
165 spinner.setValues(selection, minimum, maximum, digits, increment, pageIncrement);
\r
169 * Sets the selection, which is the receiver's position, to the argument.
\r
170 * If the argument is not within the range specified by minimum and maximum,
\r
171 * it will be adjusted to fall within this range.
\r
175 public void setSelection(int value) {
\r
176 spinner.setSelection(value);
\r