├── .gitignore ├── LICENSE.txt ├── README.rst ├── pom.xml └── src ├── main └── java │ └── org │ └── codeswarm │ └── reactivegwt │ ├── AbstractHasValue.java │ ├── Functions.java │ ├── MergeFunction.java │ ├── Predicates.java │ ├── Reactive.gwt.xml │ ├── Signal.java │ ├── Signals.java │ ├── Sink.java │ ├── Sinks.java │ ├── Source.java │ └── Sources.java └── test └── java └── org └── codeswarm └── reactivegwt ├── GwtTestExample1.java ├── GwtTestExample2.java ├── Test.gwt.xml └── TestAbstract.java /.gitignore: -------------------------------------------------------------------------------- 1 | war 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Reactive GWT 2 | ============ 3 | 4 | This library supports `reactive programming`_ in a way that integrates nicely 5 | with Google Web Toolkit. 6 | 7 | AbstractHasValue 8 | ------------------- 9 | 10 | The standard GWT way to implement HasValue_ is to extend Widget_ 11 | (usually Composite_), which provides ``fireEvent(GwtEvent):void`` 12 | (required by HasHandlers_, a supertype of HasValue). 13 | If you want to design a widget that has more than one piece of observable state, 14 | you're out of luck, because your widget can only implement HasValue once. 15 | 16 | :: 17 | 18 | // Sorry, no can do. 19 | class StatefulWidget extends Composite 20 | implements HasValue, 21 | HasValue { 22 | 23 | An alternative is to let the HasValue implementations be components of your widget: 24 | 25 | :: 26 | 27 | class StatefulWidget extends Composite { 28 | HasValue getIntegerHasValue() { ... } 29 | HasValue getStringHasValue() { ... } 30 | 31 | Reactive GWT provides **AbstractHasValue** to use for these HasValue implementations. 32 | AbstractHasValue also implements Signal. 33 | 34 | Signal 35 | --------- 36 | 37 | A **Signal** is a variable observable value. Signal extends: 38 | 39 | * Source (for retrieving the signal's current value) 40 | 41 | * HasValueChangeHandlers (for observing changes to the signal's value) 42 | 43 | This is very similar to HasValue. 44 | The difference is that the Signal interface does *not* not have methods that mutate its state. 45 | This allows us to derive signals from other signals. 46 | The main purpose of Reactive GWT is to support Signal composition, 47 | because it is an incredibly useful and intuitive concept. 48 | 49 | Example 1: Boolean signal composition 50 | ------------------------------------- 51 | 52 | Suppose you have a CheckBox_, a list of TextBox_ widgets, and a Label_. 53 | The label needs to be visible only when the checkbox is checked and at least 54 | one of the text boxes is nonempty. 55 | This can be expressed naturally by declaring the label's visibility change as a reaction 56 | to the composite state of the other widgets:: 57 | 58 | // Represent the checkbox's value as a boolean signal 59 | Signal boxIsChecked = Signals.valueOf(checkBox); 60 | 61 | // Create a signal indicating whether each text box is nonempty, then 62 | // OR them to get a signal that is true when any of them are nonempty 63 | Signal anyTextBoxesAreFilled = Signals.or(Signals.transform( 64 | Signals.valueOf(textBoxes), Functions.not(Predicates.emptyString))); 65 | 66 | // Declare that the label is visible exactly when both of these signals are true 67 | Signal labelIsVisible = Signals.and(boxIsChecked, anyTextBoxesAreFilled); 68 | Signals.connect(labelIsVisible, Sinks.setVisible(label)); 69 | 70 | Example 2: String signal composition 71 | ------------------------------------ 72 | 73 | Example 1 demonstrated some of the commonly-used composition 74 | operations directly supported by the Signals class (such as and/or). 75 | You can also merge signals using arbitrary functions. 76 | Suppose you want the label to always display the contents of the 77 | nonempty text boxes, separated by commas:: 78 | 79 | MergeFunction mergeFunction = Functions.filterThenMerge( 80 | Functions.not(Predicates.emptyString), Functions.join(", ")); 81 | Signal labelText = Signals.merge(Signals.valueOf(textBoxes), mergeFunction); 82 | Signals.connect(labelText, Sinks.setText(label)); 83 | 84 | .. _`reactive programming`: http://en.wikipedia.org/wiki/Reactive_programming 85 | .. _HasValue: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/HasValue.html 86 | .. _Widget: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/Widget.html 87 | .. _Composite: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/Composite.html 88 | .. _HasHandlers: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/event/shared/HasHandlers.html 89 | .. _CheckBox: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/CheckBox.html 90 | .. _TextBox: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/TextBox.html 91 | .. _Label: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/Label.html 92 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | org.codeswarm 9 | reactive-gwt 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 14 | The Apache Software License, Version 2.0 15 | http://www.apache.org/licenses/LICENSE-2.0.txt 16 | repo 17 | 18 | 19 | 20 | 21 | https://github.com/codeswarm/reactive-gwt 22 | 23 | 24 | 25 | true 26 | 27 | 28 | 29 | 30 | 31 | com.google.gwt 32 | gwt-user 33 | [2.0,) 34 | 35 | 36 | 37 | com.google.gwt 38 | gwt-dev 39 | [2.0,) 40 | test 41 | 42 | 43 | 44 | com.google.guava 45 | guava 46 | r09 47 | 48 | 49 | com.google.guava 50 | guava-gwt 51 | r09 52 | sources 53 | 54 | 55 | 56 | com.google.code.findbugs 57 | jsr305 58 | [1.3.9,) 59 | 60 | 61 | 62 | junit 63 | junit 64 | 4.8.2 65 | test 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | org.apache.maven.plugins 76 | maven-compiler-plugin 77 | 2.0.2 78 | 79 | 1.6 80 | 1.6 81 | 82 | 83 | 84 | 85 | org.codehaus.mojo 86 | gwt-maven-plugin 87 | 2.3.0-1 88 | 89 | 90 | 91 | test 92 | 93 | 94 | 95 | 96 | 97 | 98 | org.codehaus.mojo 99 | gwt-maven-plugin 100 | 2.1.0 101 | 102 | 600 103 | htmlunit 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/AbstractHasValue.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.gwt.event.logical.shared.ValueChangeEvent; 4 | import com.google.gwt.event.logical.shared.ValueChangeHandler; 5 | import com.google.gwt.event.shared.GwtEvent; 6 | import com.google.gwt.event.shared.HandlerManager; 7 | import com.google.gwt.event.shared.HandlerRegistration; 8 | import com.google.gwt.user.client.ui.HasValue; 9 | 10 | public abstract class AbstractHasValue implements HasValue, Signal, Sink { 11 | 12 | private final HandlerManager handlerManager = new HandlerManager(this); 13 | 14 | @Override 15 | public HandlerRegistration addValueChangeHandler(ValueChangeHandler handler) { 16 | return handlerManager.addHandler(ValueChangeEvent.getType(), handler); 17 | } 18 | 19 | @Override 20 | public void fireEvent(GwtEvent event) { 21 | handlerManager.fireEvent(event); 22 | } 23 | 24 | @Override 25 | public void setValue(T value, boolean fireEvents) { 26 | setValue(value); 27 | if (fireEvents) ValueChangeEvent.fire(this, value); 28 | } 29 | 30 | public void fireValueChangeEvent() { 31 | ValueChangeEvent.fire(this, getValue()); 32 | } 33 | 34 | @Override 35 | public void destroy() { } 36 | 37 | public static AbstractHasValue create(Source source, Sink sink) { 38 | return new Impl(source, sink); 39 | } 40 | 41 | private static class Impl extends AbstractHasValue { 42 | 43 | final Source source; 44 | final Sink sink; 45 | 46 | Impl(Source source, Sink sink) { 47 | this.source = source; 48 | this.sink = sink; 49 | } 50 | 51 | @Override 52 | public T getValue() { 53 | return source.getValue(); 54 | } 55 | 56 | @Override 57 | public void setValue(T value) { 58 | sink.setValue(value); 59 | } 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Functions.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.base.Joiner; 5 | import com.google.common.base.Predicate; 6 | import com.google.common.collect.Iterables; 7 | 8 | import javax.annotation.Nullable; 9 | 10 | public final class Functions { 11 | 12 | private Functions() { } 13 | 14 | public static final Function not = 15 | new Function() { 16 | 17 | @Override 18 | public Boolean apply(@Nullable Boolean x) { 19 | return x == null ? null : !x; 20 | } 21 | }; 22 | 23 | public static MergeFunction filterThenMerge( 24 | final Predicate predicate, final MergeFunction merge) { 25 | 26 | return new MergeFunction() { 27 | @Override 28 | public T apply(Iterable iterable) { 29 | return merge.apply(Iterables.filter(iterable, predicate)); 30 | } 31 | }; 32 | } 33 | 34 | public static MergeFunction filterThenMerge( 35 | final Function function, final MergeFunction merge) { 36 | return filterThenMerge(Predicates.forFunction(function), merge); 37 | } 38 | 39 | public static Function not( 40 | final Function function) { 41 | 42 | return com.google.common.base.Functions.compose(not, function); 43 | } 44 | 45 | public static Function not(Predicate predicate) { 46 | 47 | return not(com.google.common.base.Functions.forPredicate(predicate)); 48 | } 49 | 50 | public static MergeFunction or() { 51 | return or; 52 | } 53 | 54 | private static MergeFunction or = 55 | new MergeFunction() { 56 | @Override 57 | public Boolean apply(final Iterable values) { 58 | return Iterables.any(values, identityPredicate); 59 | } 60 | }; 61 | 62 | public static MergeFunction and() { 63 | return and; 64 | } 65 | 66 | private static MergeFunction and = 67 | new MergeFunction() { 68 | @Override 69 | public Boolean apply(Iterable values) { 70 | return Iterables.all(values, identityPredicate); 71 | } 72 | }; 73 | 74 | private static final Predicate identityPredicate = 75 | new Predicate() { 76 | @Override 77 | public boolean apply(Boolean value) { 78 | return value; 79 | } 80 | }; 81 | 82 | public static MergeFunction join(Joiner joiner) { 83 | return new JoinerFunction(joiner); 84 | } 85 | 86 | public static MergeFunction join(String separator) { 87 | return new JoinerFunction(Joiner.on(separator)); 88 | } 89 | 90 | public static MergeFunction join(char separator) { 91 | return new JoinerFunction(Joiner.on(separator)); 92 | } 93 | 94 | private static final class JoinerFunction 95 | implements MergeFunction { 96 | 97 | private final Joiner joiner; 98 | 99 | public JoinerFunction(Joiner joiner) { 100 | this.joiner = joiner; 101 | } 102 | 103 | @Override 104 | public String apply(Iterable input) { 105 | return joiner.join(input); 106 | } 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/MergeFunction.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.base.Function; 4 | 5 | public interface MergeFunction extends Function, T> { } 6 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Predicates.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.base.Predicate; 5 | 6 | import javax.annotation.Nullable; 7 | import java.util.Collection; 8 | 9 | public final class Predicates { 10 | 11 | private Predicates() { } 12 | 13 | public static final Predicate emptyString = 14 | new Predicate() { 15 | @Override 16 | public boolean apply(@Nullable String value) { 17 | return value == null || value.length() == 0; 18 | } 19 | }; 20 | 21 | public static final Predicate emptyCollection = 22 | new Predicate() { 23 | @Override 24 | public boolean apply(@Nullable Collection value) { 25 | return value == null || value.size() != 0; 26 | } 27 | }; 28 | 29 | public static Predicate forFunction( 30 | final Function function) { 31 | 32 | return new Predicate() { 33 | @Override 34 | public boolean apply(@Nullable T t) { 35 | return function.apply(t); 36 | } 37 | }; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Reactive.gwt.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Signal.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.gwt.event.logical.shared.HasValueChangeHandlers; 4 | 5 | public interface Signal extends Source, HasValueChangeHandlers { 6 | 7 | /** 8 | * Releases all resources (typically, event handler registrations) 9 | * held by this signal. Call this method only if you are sure that 10 | * no one else is using the signal. 11 | */ 12 | void destroy(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Signals.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.base.Predicate; 5 | import com.google.common.collect.ImmutableList; 6 | import com.google.common.collect.Iterables; 7 | import com.google.common.collect.Lists; 8 | import com.google.gwt.event.logical.shared.HasValueChangeHandlers; 9 | import com.google.gwt.event.logical.shared.ValueChangeEvent; 10 | import com.google.gwt.event.logical.shared.ValueChangeHandler; 11 | import com.google.gwt.event.shared.GwtEvent; 12 | import com.google.gwt.event.shared.HandlerManager; 13 | import com.google.gwt.event.shared.HandlerRegistration; 14 | import com.google.gwt.user.client.ui.HasValue; 15 | 16 | import javax.annotation.Nullable; 17 | import java.util.Collection; 18 | import java.util.List; 19 | 20 | public final class Signals { 21 | 22 | private Signals() { } 23 | 24 | public static Signal create(Source source, 25 | Iterable xs) { 26 | 27 | SignalImpl signal = new SignalImpl(source); 28 | for (HasValueChangeHandlers x : xs) { 29 | signal.handlerRegistrations.add(x.addValueChangeHandler(signal.valueChangeHandler)); 30 | } 31 | return signal; 32 | } 33 | 34 | public static Signal valueOf(HasValue hasValue) { 35 | 36 | return create(Sources.valueOf(hasValue), ImmutableList.of(hasValue)); 37 | } 38 | 39 | public static Iterable> valueOf( 40 | Iterable> hasValues) { 41 | 42 | return Iterables.transform(hasValues, new Function, Signal>() { 43 | @Override 44 | public Signal apply(HasValue hasValue) { 45 | return valueOf(hasValue); 46 | } 47 | }); 48 | } 49 | 50 | public static Signal transform( 51 | Signal from, Function transformation) { 52 | 53 | return create(Sources.transform(from, transformation), ImmutableList.of(from)); 54 | } 55 | 56 | public static Iterable> transform( 57 | Iterable> from, final Function transformation) { 58 | 59 | return Iterables.transform( 60 | from, 61 | new Function, Signal>() { 62 | @Override 63 | public Signal apply(Signal signal) { 64 | return transform(signal, transformation); 65 | } 66 | } 67 | ); 68 | } 69 | 70 | public static Signal transform( 71 | Signal from, final Predicate predicate) { 72 | 73 | return transform(from, new Function() { 74 | @Override 75 | public Boolean apply(@Nullable F f) { 76 | return predicate.apply(f); 77 | } 78 | }); 79 | } 80 | 81 | public static Iterable> transform( 82 | Iterable> from, final Predicate predicate) { 83 | 84 | return Iterables.transform( 85 | from, 86 | new Function, Signal>() { 87 | @Override 88 | public Signal apply(Signal signal) { 89 | return transform(signal, predicate); 90 | } 91 | } 92 | ); 93 | } 94 | 95 | @SuppressWarnings("unchecked") 96 | public static Signal merge( 97 | Iterable> signals, 98 | final Function, T> composition) { 99 | 100 | final Iterable values = Iterables.transform(signals, (Function) getSourceValue); 101 | Source source = new Source() { 102 | @Override 103 | public T getValue() { 104 | return composition.apply(values); 105 | } 106 | }; 107 | return create(source, signals); 108 | } 109 | 110 | private static final Function, Object> getSourceValue = 111 | new Function, Object>() { 112 | @Override 113 | public Object apply(@Nullable Source input) { 114 | return input == null ? null : input.getValue(); 115 | } 116 | }; 117 | 118 | public static Signal not(Signal signal) { 119 | return transform(signal, Functions.not); 120 | } 121 | 122 | public static Signal or(Iterable> signals) { 123 | return merge(signals, Functions.or()); 124 | } 125 | 126 | public static Signal or(Signal ... signals) { 127 | return or(ImmutableList.copyOf(signals)); 128 | } 129 | 130 | public static Signal and(Iterable> signals) { 131 | return merge(signals, Functions.and()); 132 | } 133 | 134 | public static Signal and(Signal ... signals) { 135 | return and(ImmutableList.copyOf(signals)); 136 | } 137 | 138 | public static Signal emptyString(Signal stringSignal) { 139 | return transform(stringSignal, Predicates.emptyString); 140 | } 141 | 142 | public static Signal emptyCollection( 143 | Signal collectionSignal) { 144 | 145 | return transform(collectionSignal, Predicates.emptyCollection); 146 | } 147 | 148 | public static HandlerRegistration connect(Signal signal, Sink sink) { 149 | HandlerRegistration handlerRegistration = 150 | signal.addValueChangeHandler(Sinks.valueChangeHandler(sink)); 151 | sink.setValue(signal.getValue()); 152 | return handlerRegistration; 153 | } 154 | 155 | private static class SignalImpl implements Signal { 156 | 157 | final HandlerManager handlerManager = new HandlerManager(this); 158 | final Source source; 159 | List handlerRegistrations = Lists.newArrayList(); 160 | 161 | final ValueChangeHandler valueChangeHandler = new ValueChangeHandler() { 162 | @Override 163 | public void onValueChange(ValueChangeEvent event) { 164 | ValueChangeEvent.fire(SignalImpl.this, getValue()); 165 | } 166 | }; 167 | 168 | SignalImpl(Source source) { 169 | this.source = source; 170 | } 171 | 172 | @Override 173 | public HandlerRegistration addValueChangeHandler(ValueChangeHandler handler) { 174 | return handlerManager.addHandler(ValueChangeEvent.getType(), handler); 175 | } 176 | 177 | @Override 178 | public void fireEvent(GwtEvent event) { 179 | handlerManager.fireEvent(event); 180 | } 181 | 182 | @Override 183 | public T getValue() { 184 | return source.getValue(); 185 | } 186 | 187 | @Override 188 | public void destroy() { 189 | if (handlerRegistrations == null) { 190 | throw new IllegalStateException("Signal has already been destroyed"); 191 | } 192 | for (HandlerRegistration registration : handlerRegistrations) { 193 | registration.removeHandler(); 194 | } 195 | handlerRegistrations = null; 196 | } 197 | 198 | } 199 | 200 | } 201 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Sink.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | public interface Sink { 4 | 5 | void setValue(T value); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Sinks.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.gwt.event.logical.shared.ValueChangeEvent; 4 | import com.google.gwt.event.logical.shared.ValueChangeHandler; 5 | import com.google.gwt.user.client.TakesValue; 6 | import com.google.gwt.user.client.ui.HasText; 7 | import com.google.gwt.user.client.ui.UIObject; 8 | 9 | public final class Sinks { 10 | 11 | private Sinks() { } 12 | 13 | public static ValueChangeHandler valueChangeHandler(final Sink sink) { 14 | return new ValueChangeHandler() { 15 | @Override 16 | public void onValueChange(ValueChangeEvent event) { 17 | sink.setValue(event.getValue()); 18 | } 19 | }; 20 | } 21 | 22 | public static Sink setVisible(final UIObject uiObject) { 23 | return new Sink() { 24 | @Override 25 | public void setValue(Boolean value) { 26 | uiObject.setVisible(value); 27 | } 28 | }; 29 | } 30 | 31 | public static Sink setValue(final TakesValue takesValue) { 32 | return new Sink() { 33 | @Override 34 | public void setValue(T value) { 35 | takesValue.setValue(value); 36 | } 37 | }; 38 | } 39 | 40 | public static Sink setText(final HasText hasText) { 41 | return new Sink() { 42 | @Override 43 | public void setValue(String value) { 44 | hasText.setText(value); 45 | } 46 | }; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Source.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | public interface Source { 4 | 5 | T getValue(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/codeswarm/reactivegwt/Sources.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.gwt.user.client.TakesValue; 5 | 6 | public final class Sources { 7 | 8 | private Sources() { } 9 | 10 | public static Source valueOf(final TakesValue takesValue) { 11 | return new Source() { 12 | @Override 13 | public T getValue() { 14 | return takesValue.getValue(); 15 | } 16 | }; 17 | } 18 | 19 | public static Source transform(final Source source, 20 | final Function transformation) { 21 | 22 | return new Source() { 23 | @Override 24 | public T getValue() { 25 | return transformation.apply(source.getValue()); 26 | } 27 | }; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/codeswarm/reactivegwt/GwtTestExample1.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.collect.Lists; 4 | import com.google.gwt.user.client.ui.CheckBox; 5 | import com.google.gwt.user.client.ui.Label; 6 | import com.google.gwt.user.client.ui.TextBox; 7 | 8 | import java.util.List; 9 | 10 | public class GwtTestExample1 extends TestAbstract { 11 | 12 | CheckBox checkBox; 13 | List textBoxes; 14 | Label label; 15 | 16 | public void test() throws Exception { 17 | 18 | // The Players 19 | checkBox = new CheckBox(); 20 | textBoxes = Lists.newArrayList(); 21 | for (int i = 0; i < 3; ++i) textBoxes.add(new TextBox()); 22 | label = new Label(); 23 | 24 | // The Set-Up 25 | Signal boxIsChecked = Signals.valueOf(checkBox); 26 | Signal anyTextBoxesAreFilled = Signals.or(Signals.transform( 27 | Signals.valueOf(textBoxes), Functions.not(Predicates.emptyString))); 28 | Signal labelIsVisible = Signals.and(boxIsChecked, anyTextBoxesAreFilled); 29 | Signals.connect(labelIsVisible, Sinks.setVisible(label)); 30 | 31 | // The Sting 32 | assertFalse(label.isVisible()); 33 | checkBox.setValue(true, true); 34 | assertFalse(label.isVisible()); 35 | textBoxes.get(1).setValue("xyzzy", true); 36 | assertTrue(label.isVisible()); 37 | checkBox.setValue(false, true); 38 | assertFalse(label.isVisible()); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/codeswarm/reactivegwt/GwtTestExample2.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.common.collect.Lists; 4 | import com.google.gwt.user.client.ui.Label; 5 | import com.google.gwt.user.client.ui.TextBox; 6 | 7 | import java.util.List; 8 | 9 | public class GwtTestExample2 extends TestAbstract { 10 | 11 | List textBoxes; 12 | Label label; 13 | 14 | public void test() throws Exception { 15 | 16 | // The Players 17 | textBoxes = Lists.newArrayList(); 18 | for (int i = 0; i < 3; ++i) textBoxes.add(new TextBox()); 19 | label = new Label(); 20 | 21 | // The Set-Up 22 | MergeFunction mergeFunction = Functions.filterThenMerge( 23 | Functions.not(Predicates.emptyString), Functions.join(", ")); 24 | Signal labelText = Signals.merge(Signals.valueOf(textBoxes), mergeFunction); 25 | Signals.connect(labelText, Sinks.setText(label)); 26 | 27 | // The Sting 28 | assertEquals("", label.getText()); 29 | textBoxes.get(0).setValue("abc", true); 30 | assertEquals("abc", label.getText()); 31 | textBoxes.get(1).setValue("def", true); 32 | assertEquals("abc, def", label.getText()); 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/codeswarm/reactivegwt/Test.gwt.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/test/java/org/codeswarm/reactivegwt/TestAbstract.java: -------------------------------------------------------------------------------- 1 | package org.codeswarm.reactivegwt; 2 | 3 | import com.google.gwt.junit.client.GWTTestCase; 4 | 5 | public abstract class TestAbstract extends GWTTestCase { 6 | 7 | @Override 8 | public String getModuleName() { 9 | return "org.codeswarm.reactivegwt.Test"; 10 | } 11 | 12 | } 13 | --------------------------------------------------------------------------------