@@ -0,0 +1,3367 @@
+/**%0A * Copyright 2013 Netflix, Inc.%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A * http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage rx;%0A%0Aimport static org.mockito.Mockito.*;%0A%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A%0Aimport javax.swing.AbstractButton;%0A%0Aimport org.junit.Test;%0Aimport org.mockito.Matchers;%0A%0Aimport rx.subscriptions.Subscriptions;%0Aimport rx.util.functions.Action0;%0Aimport rx.util.functions.Action1;%0Aimport rx.util.functions.Func1;%0A%0Apublic enum SwingObservable %7B ; // no instances%0A%0A    public static Observable%3CActionEvent%3E fromButtonAction(final AbstractButton button) %7B%0A        return Observable.create(new Func1%3CObserver%3CActionEvent%3E, Subscription%3E() %7B%0A            @Override%0A            public Subscription call(final Observer%3CActionEvent%3E observer) %7B%0A                final ActionListener listener = new ActionListener() %7B%0A                    @Override%0A                    public void actionPerformed(ActionEvent e) %7B%0A                        observer.onNext(e);%0A                    %7D%0A                %7D;%0A                button.addActionListener(listener);%0A                %0A                return Subscriptions.create(new Action0() %7B%0A                    @Override%0A                    public void call() %7B%0A                        button.removeActionListener(listener);%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D);%0A    %7D%0A    %0A    public static class UnitTest %7B%0A        @Test%0A        public void testObservingActionEvents() %7B%0A            @SuppressWarnings(%22unchecked%22)%0A            Action1%3CActionEvent%3E action = mock(Action1.class);%0A            @SuppressWarnings(%22unchecked%22)%0A            Action1%3CException%3E error = mock(Action1.class);%0A            Action0 complete = mock(Action0.class);%0A            %0A            final ActionEvent event = new ActionEvent(this, 1, %22command%22);%0A            %0A            class TestButton extends AbstractButton %7B%0A                void testAction() %7B%0A                    fireActionPerformed(event);%0A                %7D%0A            %7D%0A            %0A            TestButton button = new TestButton();%0A            Subscription sub = fromButtonAction(button).subscribe(action, error, complete);%0A            %0A            verify(action, never()).call(Matchers.%3CActionEvent%3Eany());%0A            verify(error, never()).call(Matchers.%3CException%3Eany());%0A            verify(complete, never()).call();%0A            %0A            button.testAction();%0A            verify(action, times(1)).call(Matchers.%3CActionEvent%3Eany());%0A            %0A            button.testAction();%0A            verify(action, times(2)).call(Matchers.%3CActionEvent%3Eany());%0A            %0A            sub.unsubscribe();%0A            button.testAction();%0A            verify(action, times(2)).call(Matchers.%3CActionEvent%3Eany());%0A            verify(error, never()).call(Matchers.%3CException%3Eany());%0A            verify(complete, never()).call();%0A        %7D%0A    %7D%0A%7D%0A

Allow creating observables from `AbstractButton`s.