Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not yet merge] ChangeEventSource & fromChangeEvents #28

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/main/java/rx/observables/SwingObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,34 @@
import java.util.Set;

import javax.swing.AbstractButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.text.Document;

import rx.Observable;
import rx.Subscriber;
import rx.Observable.OnSubscribe;
import rx.functions.Func1;
import rx.schedulers.SwingScheduler;
import rx.swing.sources.*;

/**
* Allows creating observables from various sources specific to Swing.
*/
public enum SwingObservable { ; // no instances

private static <T> Observable<T> create(OnSubscribe<T> f) {
return Observable.create(f)
.subscribeOn(SwingScheduler.getInstance())
.unsubscribeOn(SwingScheduler.getInstance());
}

/**
* Creates an observable corresponding to a Swing button action.
*
Expand Down Expand Up @@ -284,6 +297,46 @@ public Boolean call(DocumentEvent event) {
});
}

/**
* Creates an observable corresponding to JSlider changes.
*
* @param component
* The slider to register the observable for.
* @return Observable of change events.
*/
public static Observable<ChangeEvent> fromChangeEvents(final JSlider component) {
return create(new ChangeEventSource() {
@Override
protected void addListenerToComponent(ChangeListener listener) {
component.addChangeListener(listener);
}
@Override
protected void removeListenerFromComponent(ChangeListener listener) {
component.removeChangeListener(listener);
}
});
}

/**
* Creates an observable corresponding to JSpinner changes.
*
* @param component
* The spinner to register the observable for.
* @return Observable of change events.
*/
public static Observable<ChangeEvent> fromChangeEvents(final JSpinner component) {
return create(new ChangeEventSource() {
@Override
protected void addListenerToComponent(ChangeListener listener) {
component.addChangeListener(listener);
}
@Override
protected void removeListenerFromComponent(ChangeListener listener) {
component.removeChangeListener(listener);
}
});
}

/**
* Check if the current thead is the event dispatch thread.
*
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/rx/swing/sources/ChangeEventSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2014 Netflix, Inc.
*
* 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 rx.swing.sources;

import rx.Subscriber;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
* Note: There's no common supertype of all components which have add/removeChangeListener
* methods, so we cannot implement add/removeListenerForComponent methods here, so this
* class has to remain abstract.
*/
public abstract class ChangeEventSource extends EventSource<ChangeEvent, ChangeListener> {

@Override
public ChangeListener createListenerFor(final Subscriber<? super ChangeEvent> subscriber) {
return new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
subscriber.onNext(e);
}
};
}
}
28 changes: 28 additions & 0 deletions src/main/java/rx/swing/sources/EventSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rx.swing.sources;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.subscriptions.Subscriptions;

public abstract class EventSource<Event, Listener> implements Observable.OnSubscribe<Event> {

protected abstract Listener createListenerFor(Subscriber<? super Event> subscriber);

protected abstract void addListenerToComponent(Listener listener);

protected abstract void removeListenerFromComponent(Listener listener);

@Override
public void call(final Subscriber<? super Event> subscriber) {
final Listener listener = createListenerFor(subscriber);
addListenerToComponent(listener);
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
removeListenerFromComponent(listener);
}
}));
}

}