|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.swing.sources; |
| 17 | + |
| 18 | +import java.awt.Dimension; |
| 19 | +import java.awt.event.ComponentEvent; |
| 20 | +import java.awt.event.ComponentListener; |
| 21 | + |
| 22 | +import javax.swing.JComponent; |
| 23 | + |
| 24 | +import rx.Observable; |
| 25 | +import rx.Observer; |
| 26 | +import rx.Subscription; |
| 27 | +import rx.subscriptions.Subscriptions; |
| 28 | +import rx.util.functions.Action0; |
| 29 | +import rx.util.functions.Func1; |
| 30 | + |
| 31 | +public enum ComponentEventSource { ; // no instances |
| 32 | + |
| 33 | + public static Observable<ComponentEvent> fromComponentEventsOf(final JComponent component) { |
| 34 | + return Observable.create(new Func1<Observer<ComponentEvent>, Subscription>() { |
| 35 | + @Override |
| 36 | + public Subscription call(final Observer<ComponentEvent> observer) { |
| 37 | + final ComponentListener listener = new ComponentListener() { |
| 38 | + @Override |
| 39 | + public void componentHidden(ComponentEvent event) { |
| 40 | + observer.onNext(event); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void componentMoved(ComponentEvent event) { |
| 45 | + observer.onNext(event); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void componentResized(ComponentEvent event) { |
| 50 | + observer.onNext(event); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void componentShown(ComponentEvent event) { |
| 55 | + observer.onNext(event); |
| 56 | + } |
| 57 | + }; |
| 58 | + component.addComponentListener(listener); |
| 59 | + |
| 60 | + return Subscriptions.create(new Action0() { |
| 61 | + @Override |
| 62 | + public void call() { |
| 63 | + component.removeComponentListener(listener); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public static Observable<Dimension> fromResizing(final JComponent component) { |
| 71 | + return fromComponentEventsOf(component).filter(Predicate.RESIZED).map(new Func1<ComponentEvent, Dimension>() { |
| 72 | + @Override |
| 73 | + public Dimension call(ComponentEvent event) { |
| 74 | + return event.getComponent().getSize(); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Predicates that help with filtering observables for specific component events. |
| 81 | + */ |
| 82 | + public enum Predicate implements Func1<ComponentEvent, Boolean> { |
| 83 | + RESIZED(ComponentEvent.COMPONENT_RESIZED), |
| 84 | + HIDDEN(ComponentEvent.COMPONENT_HIDDEN), |
| 85 | + MOVED(ComponentEvent.COMPONENT_MOVED), |
| 86 | + SHOWN(ComponentEvent.COMPONENT_SHOWN); |
| 87 | + |
| 88 | + private final int id; |
| 89 | + |
| 90 | + private Predicate(int id) { |
| 91 | + this.id = id; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Boolean call(ComponentEvent event) { |
| 96 | + return event.getID() == id; |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments