Skip to content
Browse files

Added example into intro.

  • Loading branch information...
1 parent 0a6c30c commit 51cd9b2eb7a1b9ee1d61149cc888bfa514b26643 @dcervelli dcervelli committed Dec 14, 2011
Showing with 120 additions and 0 deletions.
  1. +120 −0 README.md
View
120 README.md
@@ -24,6 +24,126 @@ Cinch uses Java annotations to reflectively wire up Models, Views, and Controlle
* Please file issues on the [Github Issue Tracker](/palantir/Cinch/issues)
* Email project admin: [Ari Gesher](mailto:agesher@palantir.com)
+## Example: [IntroCinchMVC.java](http://github.com/palantir/Cinch/blob/master/example/com/palantir/ptoss/cinch/example/IntroCinchMVC.java)
+
+Believe it or not, this is a complete Swing program (note the lack of anonymous listener objects) - full source is available in [IntroCinchMVC.java](https://github.com/palantir/Cinch/blob/master/example/com/palantir/ptoss/cinch/example/IntroCinchMVC.java):
+
+```java
+
+public class IntroCinchMVC {
+
+ public static class IntroModel extends DefaultBindableModel {
+ private String to = "";
+ private String subject = "";
+ private String body = "";
+
+ public String getBody() {
+ return body;
+ }
+
+ public void setBody(String body) {
+ this.body = body;
+ update();
+ }
+
+ public String getSubject() {
+ return subject;
+ }
+
+ public void setSubject(String subject) {
+ this.subject = subject;
+ update();
+ }
+
+ public String getTo() {
+ return to;
+ }
+
+ public void setTo(String to) {
+ this.to = to;
+ update();
+ }
+
+ public String getCurrentMessage() {
+ if (Strings.isNullOrEmpty(to)) {
+ return "Fill out 'To' field.";
+ }
+ if (Strings.isNullOrEmpty(subject)) {
+ return "Fill out 'Subject' field.";
+ }
+ if (Strings.isNullOrEmpty(body)) {
+ return "Fill out 'Body'.";
+ }
+ return "Ready to send.";
+ }
+
+ public boolean isReady() {
+ return !Strings.isNullOrEmpty(to) && !Strings.isNullOrEmpty(subject) && !Strings.isNullOrEmpty(body);
+ }
+
+ @Override
+ public String toString() {
+ return "IntroModel [to=" + to + ", subject=" + subject + ", body=" + body + "]";
+ }
+ }
+
+ public static class IntroController {
+ private final IntroModel model;
+
+ public IntroController(IntroModel model) {
+ this.model = model;
+ }
+
+ public void sendEmail() {
+ System.out.println("Send: " + model);
+ }
+
+ public void yell() {
+ model.setBody(model.getBody().toUpperCase());
+ }
+ }
+
+ private final JPanel panel = new JPanel();
+ private final Bindings bindings = new Bindings();
+ private final IntroModel model = new IntroModel();
+
+ @SuppressWarnings("unused")
+ @Bindable
+ private final IntroController controller = new IntroController(model);
+ @Bound(to = "to")
+ private final JTextField toField = new JTextField();
+ @Bound(to = "subject")
+ private final JTextField subjectField = new JTextField();
+ @Bound(to = "body")
+ private final JTextArea bodyArea = new JTextArea();
+ @Action(call = "yell")
+ private final JButton yellButton = new JButton("YELL!");
+ @Action(call = "sendEmail")
+ @EnabledIf(to = "ready")
+ private final JButton sendButton = new JButton("Send");
+
+ @Bound(to = "currentMessage")
+ private final JLabel messageLabel = new JLabel("");
+
+ public IntroCinchMVC() {
+ initializeInterface();
+ bindings.bind(this);
+ }
+
+ private void initializeInterface() {
+ // swing layout code cut for brevity
+ // ...
+ }
+
+ public JComponent getDisplayComponent() {
+ return panel;
+ }
+
+ // main removed
+}
+
+```
+
## Authors #
[Dan Cervelli](https://github.com/dcervelli)

0 comments on commit 51cd9b2

Please sign in to comment.
Something went wrong with that request. Please try again.