// Path: RxSwt/src/main/java/io/reactivex/swt/MainThreadDisposable.java
// public abstract class MainThreadDisposable implements Disposable {
// 
// 	private final AtomicBoolean unsubscribed = new AtomicBoolean();
// 
// 	private Display display;
// 
// 	/**
// 	 * When using this constructor the default display will be used to schedule
// 	 * the call of the onDispose method.
// 	 */
// 	public MainThreadDisposable() {
// 	}
// 
// 	/**
// 	 * When using this constructor the given display will be used to schedule
// 	 * the call of the onDispose method.
// 	 * 
// 	 * @param display
// 	 *            {@link Display} to be used to schedule the call of the
// 	 *            onDispose method
// 	 */
// 	public MainThreadDisposable(Display display) {
// 		this.display = display;
// 	}
// 
// 	@Override
// 	public final boolean isDisposed() {
// 		return unsubscribed.get();
// 	}
// 
// 	@Override
// 	public final void dispose() {
// 		if (unsubscribed.compareAndSet(false, true)) {
// 			if (Display.getCurrent() != null) {
// 				onDispose();
// 			} else {
// 				getScheduler().scheduleDirect(new Runnable() {
// 					@Override
// 					public void run() {
// 						onDispose();
// 					}
// 				});
// 			}
// 		}
// 	}
// 
// 	protected Scheduler getScheduler() {
// 		if (display != null) {
// 			return SwtSchedulers.from(display);
// 		}
// 		return SwtSchedulers.defaultDisplayThread();
// 	}
// 
// 	protected abstract void onDispose();
// 
// }
// 
// Path: RxSwt/src/main/java/io/reactivex/swt/listener/util/Preconditions.java
// public class Preconditions {
// 	public static boolean checkWidget(Observer<?> observer, Widget widget) {
// 		if (null == widget) {
// 			observer.onError(new NullPointerException("The given widget was null"));
// 			return false;
// 		} else if (widget.isDisposed()) {
// 			observer.onError(new IllegalStateException("The given widget is diposed"));
// 			return false;
// 		} else if (!(Thread.currentThread().equals(widget.getDisplay().getThread()))) {
// 			observer.onError(new IllegalStateException(
// 					"Expected to be called on the main thread but was " + Thread.currentThread().getName()));
// 			return false;
// 		}
// 		return true;
// 	}
// 
// 	public static boolean checkDisplay(Observer<?> observer, Display display) {
// 		if (null == display) {
// 			observer.onError(new NullPointerException("The given widget was null"));
// 			return false;
// 		} else if (display.isDisposed()) {
// 			observer.onError(new IllegalStateException("The given widget is diposed"));
// 			return false;
// 		} else if (!(Thread.currentThread().equals(display.getThread()))) {
// 			observer.onError(new IllegalStateException(
// 					"Expected to be called on the main thread but was " + Thread.currentThread().getName()));
// 			return false;
// 		}
// 		return true;
// 	}
// 
// 	private Preconditions() {
// 	}
// }


import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Control;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.swt.MainThreadDisposable;
import io.reactivex.swt.listener.util.Preconditions;
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.reactivex.swt.listener.focus;



public abstract class FocusObservable extends Observable<FocusEvent> {

	private Control control;

	public FocusObservable(Control control) {
		this.control = control;
	}

	@Override
	protected void subscribeActual(Observer<? super FocusEvent> observer) {
		if (!Preconditions.checkWidget(observer, control)) {
			return;
		}
		AbstractFocusListener listener = getFocusListener(control, observer);
		observer.onSubscribe(listener);
		control.addDisposeListener(e -> listener.dispose());
		control.addFocusListener(listener);

	}

// Path: RxSwt/src/main/java/io/reactivex/swt/MainThreadDisposable.java
// public abstract class MainThreadDisposable implements Disposable {
// 
// 	private final AtomicBoolean unsubscribed = new AtomicBoolean();
// 
// 	private Display display;
// 
// 	/**
// 	 * When using this constructor the default display will be used to schedule
// 	 * the call of the onDispose method.
// 	 */
// 	public MainThreadDisposable() {
// 	}
// 
// 	/**
// 	 * When using this constructor the given display will be used to schedule
// 	 * the call of the onDispose method.
// 	 * 
// 	 * @param display
// 	 *            {@link Display} to be used to schedule the call of the
// 	 *            onDispose method
// 	 */
// 	public MainThreadDisposable(Display display) {
// 		this.display = display;
// 	}
// 
// 	@Override
// 	public final boolean isDisposed() {
// 		return unsubscribed.get();
// 	}
// 
// 	@Override
// 	public final void dispose() {
// 		if (unsubscribed.compareAndSet(false, true)) {
// 			if (Display.getCurrent() != null) {
// 				onDispose();
// 			} else {
// 				getScheduler().scheduleDirect(new Runnable() {
// 					@Override
// 					public void run() {
// 						onDispose();
// 					}
// 				});
// 			}
// 		}
// 	}
// 
// 	protected Scheduler getScheduler() {
// 		if (display != null) {
// 			return SwtSchedulers.from(display);
// 		}
// 		return SwtSchedulers.defaultDisplayThread();
// 	}
// 
// 	protected abstract void onDispose();
// 
// }
// 
// Path: RxSwt/src/main/java/io/reactivex/swt/listener/util/Preconditions.java
// public class Preconditions {
// 	public static boolean checkWidget(Observer<?> observer, Widget widget) {
// 		if (null == widget) {
// 			observer.onError(new NullPointerException("The given widget was null"));
// 			return false;
// 		} else if (widget.isDisposed()) {
// 			observer.onError(new IllegalStateException("The given widget is diposed"));
// 			return false;
// 		} else if (!(Thread.currentThread().equals(widget.getDisplay().getThread()))) {
// 			observer.onError(new IllegalStateException(
// 					"Expected to be called on the main thread but was " + Thread.currentThread().getName()));
// 			return false;
// 		}
// 		return true;
// 	}
// 
// 	public static boolean checkDisplay(Observer<?> observer, Display display) {
// 		if (null == display) {
// 			observer.onError(new NullPointerException("The given widget was null"));
// 			return false;
// 		} else if (display.isDisposed()) {
// 			observer.onError(new IllegalStateException("The given widget is diposed"));
// 			return false;
// 		} else if (!(Thread.currentThread().equals(display.getThread()))) {
// 			observer.onError(new IllegalStateException(
// 					"Expected to be called on the main thread but was " + Thread.currentThread().getName()));
// 			return false;
// 		}
// 		return true;
// 	}
// 
// 	private Preconditions() {
// 	}
// }


// Path: RxSwt/src/main/java/io/reactivex/swt/listener/focus/FocusObservable.java
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Control;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.swt.MainThreadDisposable;
import io.reactivex.swt.listener.util.Preconditions;

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.reactivex.swt.listener.focus;



public abstract class FocusObservable extends Observable<FocusEvent> {

	private Control control;

	public FocusObservable(Control control) {
		this.control = control;
	}

	@Override
	protected void subscribeActual(Observer<? super FocusEvent> observer) {
		if (!Preconditions.checkWidget(observer, control)) {
			return;
		}
		AbstractFocusListener listener = getFocusListener(control, observer);
		observer.onSubscribe(listener);
		control.addDisposeListener(e -> listener.dispose());
		control.addFocusListener(listener);

	}

	protected abstract AbstractFocusListener getFocusListener(Control control,