| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2006 The Pampered Chef, Inc. and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * The Pampered Chef, Inc. - initial API and implementation |
| 10 | ******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.jface.internal.databinding.provisional.swt; |
| 13 | |
| 14 | import org.eclipse.core.databinding.observable.Realm; |
| 15 | import org.eclipse.core.databinding.observable.value.AbstractVetoableValue; |
| 16 | import org.eclipse.jface.databinding.swt.ISWTObservableValue; |
| 17 | import org.eclipse.jface.databinding.swt.SWTObservables; |
| 18 | import org.eclipse.swt.events.DisposeEvent; |
| 19 | import org.eclipse.swt.events.DisposeListener; |
| 20 | import org.eclipse.swt.widgets.Widget; |
| 21 | |
| 22 | /** |
| 23 | * NON-API - An abstract superclass for vetoable values that gurantees that the |
| 24 | * observable will be disposed when the control to which it is attached is |
| 25 | * disposed. |
| 26 | * |
| 27 | * @since 1.1 |
| 28 | */ |
| 29 | public abstract class AbstractSWTVetoableValue extends AbstractVetoableValue implements ISWTObservableValue { |
| 30 | |
| 31 | private final Widget widget; |
| 32 | |
| 33 | /** |
| 34 | * Standard constructor for an SWT VetoableValue. Makes sure that |
| 35 | * the observable gets disposed when the SWT widget is disposed. |
| 36 | * |
| 37 | * @param widget |
| 38 | */ |
| 39 | protected AbstractSWTVetoableValue(Widget widget) { |
| 40 | this(SWTObservables.getRealm(widget.getDisplay()), widget); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Constructs a new instance for the provided <code>realm</code> and <code>widget</code>. |
| 45 | * |
| 46 | * @param realm |
| 47 | * @param widget |
| 48 | * @since 1.2 |
| 49 | */ |
| 50 | protected AbstractSWTVetoableValue(Realm realm, Widget widget) { |
| 51 | super(realm); |
| 52 | this.widget = widget; |
| 53 | if (widget == null) { |
| 54 | throw new IllegalArgumentException("The widget parameter is null."); //$NON-NLS-1$ |
| 55 | } |
| 56 | widget.addDisposeListener(disposeListener); |
| 57 | } |
| 58 | |
| 59 | private DisposeListener disposeListener = new DisposeListener() { |
| 60 | public void widgetDisposed(DisposeEvent e) { |
| 61 | AbstractSWTVetoableValue.this.dispose(); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * @return Returns the widget. |
| 67 | */ |
| 68 | public Widget getWidget() { |
| 69 | return widget; |
| 70 | } |
| 71 | } |