-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from bdmstyle/0.x
Add FocusEvent support and fix build
- Loading branch information
Showing
4 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* 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.Observable; | ||
import rx.Observable.OnSubscribe; | ||
import rx.Subscriber; | ||
import rx.functions.Action0; | ||
import rx.functions.Func1; | ||
import rx.observables.SwingObservable; | ||
import rx.subscriptions.SwingSubscriptions; | ||
|
||
import java.awt.*; | ||
import java.awt.event.FocusEvent; | ||
import java.awt.event.FocusListener; | ||
|
||
public enum FocusEventSource { ; // no instances | ||
|
||
/** | ||
* @see rx.observables.SwingObservable#fromFocusEvents | ||
*/ | ||
public static Observable<FocusEvent> fromFocusEventsOf(final Component component) { | ||
return Observable.create(new OnSubscribe<FocusEvent>() { | ||
@Override | ||
public void call(final Subscriber<? super FocusEvent> subscriber) { | ||
SwingObservable.assertEventDispatchThread(); | ||
final FocusListener listener = new FocusListener() { | ||
|
||
@Override | ||
public void focusGained(FocusEvent event) { | ||
subscriber.onNext(event); | ||
} | ||
|
||
@Override | ||
public void focusLost(FocusEvent event) { | ||
subscriber.onNext(event); | ||
} | ||
}; | ||
component.addFocusListener(listener); | ||
subscriber.add(SwingSubscriptions.unsubscribeInEventDispatchThread(new Action0() { | ||
@Override | ||
public void call() { | ||
component.removeFocusListener(listener); | ||
} | ||
})); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Predicates that help with filtering observables for specific focus events. | ||
*/ | ||
public enum Predicate implements Func1<FocusEvent, Boolean> { | ||
FOCUS_GAINED(FocusEvent.FOCUS_GAINED), | ||
FOCUS_LOST(FocusEvent.FOCUS_LOST); | ||
|
||
private final int id; | ||
|
||
private Predicate(int id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public Boolean call(FocusEvent event) { | ||
return event.getID() == id; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* 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 org.junit.Test; | ||
import org.mockito.Matchers; | ||
import rx.Subscription; | ||
import rx.functions.Action0; | ||
import rx.functions.Action1; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.FocusEvent; | ||
import java.awt.event.FocusListener; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class FocusEventSourceTest { | ||
private Component comp = new JPanel(); | ||
|
||
@Test | ||
public void testObservingFocusEvents() throws Throwable { | ||
SwingTestHelper.create().runInEventDispatchThread(new Action0() { | ||
|
||
@Override | ||
public void call() { | ||
@SuppressWarnings("unchecked") | ||
Action1<FocusEvent> action = mock(Action1.class); | ||
@SuppressWarnings("unchecked") | ||
Action1<Throwable> error = mock(Action1.class); | ||
Action0 complete = mock(Action0.class); | ||
|
||
final FocusEvent event = mock(FocusEvent.class); | ||
|
||
Subscription sub = FocusEventSource.fromFocusEventsOf(comp) | ||
.subscribe(action, error, complete); | ||
|
||
verify(action, never()).call(Matchers.<FocusEvent> any()); | ||
verify(error, never()).call(Matchers.<Throwable> any()); | ||
verify(complete, never()).call(); | ||
|
||
fireFocusEvent(event); | ||
verify(action, times(1)).call(Matchers.<FocusEvent> any()); | ||
|
||
fireFocusEvent(event); | ||
verify(action, times(2)).call(Matchers.<FocusEvent> any()); | ||
|
||
sub.unsubscribe(); | ||
fireFocusEvent(event); | ||
verify(action, times(2)).call(Matchers.<FocusEvent> any()); | ||
verify(error, never()).call(Matchers.<Throwable> any()); | ||
verify(complete, never()).call(); | ||
} | ||
|
||
}).awaitTerminal(); | ||
} | ||
|
||
private void fireFocusEvent(FocusEvent event) { | ||
for (FocusListener listener : comp.getFocusListeners()) { | ||
listener.focusGained(event); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters