Palantir Logo

Cinch - annotations to make MVC easy

CircleCI Build Status Download


About

Cinch makes MVC in Swing easy. Cinch is a Java library used by developers to simplify writing certain types of GUI code.

When developing Swing applications it's very easy to fall into the trap of not separating out Models and Controllers. It's all too easy to just store the state of that boolean in the checkbox itself, or that String in the JTextField. The design goal behind Cinch was to make it easier to apply MVC than to not by reducing much of the typical Swing friction and boilerplate.

Cinch uses Java annotations to reflectively wire up Models, Views, and Controllers.

Project Resources

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:

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

License

Cinch is made available under the Apache 2.0 License.

Copyright 2011 Palantir Technologies

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.



Cinch - annotations to make MVC easy

CircleCI建立状态 下载


关于

Cinch使MVC在Swing中容易。 Cinch是开发人员用来简化编写某些类型的GUI代码的Java库。

在开发Swing应用程序时,很容易陷入不分离模型和控制器的陷阱。将这个布尔值的状态存储在复选框本身,或JTextField中的String中,这一切都太容易了。 Cinch背后的设计目标是使MVC更容易应用,而不是减少大部分典型的Swing摩擦和样板。

Cinch使用Java注释来反射连接模型,视图和控制器。

项目资源

例如:IntroCinchMVC.java

相信与否,这是一个完整的Swing程序(注意缺少匿名侦听器对象) - 完整的源代码可在 IntroCinchMVC.java

public class IntroCinchMVC {

<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">class</span> <span class="pl-en">IntroModel</span> <span class="pl-k">extends</span> <span class="pl-e">DefaultBindableModel</span> {
    <span class="pl-k">private</span> <span class="pl-smi">String</span> to <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span><span class="pl-pds">&#34;</span></span>;
    <span class="pl-k">private</span> <span class="pl-smi">String</span> subject <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span><span class="pl-pds">&#34;</span></span>;
    <span class="pl-k">private</span> <span class="pl-smi">String</span> body <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span><span class="pl-pds">&#34;</span></span>;

    <span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getBody</span>() {
        <span class="pl-k">return</span> body;
    }

    <span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">setBody</span>(<span class="pl-smi">String</span> <span class="pl-v">body</span>) {
        <span class="pl-c1">this</span><span class="pl-k">.</span>body <span class="pl-k">=</span> body;
        update();
    }

    <span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getSubject</span>() {
        <span class="pl-k">return</span> subject;
    }

    <span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">setSubject</span>(<span class="pl-smi">String</span> <span class="pl-v">subject</span>) {
        <span class="pl-c1">this</span><span class="pl-k">.</span>subject <span class="pl-k">=</span> subject;
        update();
    }

    <span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getTo</span>() {
        <span class="pl-k">return</span> to;
    }

    <span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">setTo</span>(<span class="pl-smi">String</span> <span class="pl-v">to</span>) {
        <span class="pl-c1">this</span><span class="pl-k">.</span>to <span class="pl-k">=</span> to;
        update();
    }

    <span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getCurrentMessage</span>() {
        <span class="pl-k">if</span> (<span class="pl-smi">Strings</span><span class="pl-k">.</span>isNullOrEmpty(to)) {
            <span class="pl-k">return</span> <span class="pl-s"><span class="pl-pds">&#34;</span>Fill out &#39;To&#39; field.<span class="pl-pds">&#34;</span></span>;
        } 
        <span class="pl-k">if</span> (<span class="pl-smi">Strings</span><span class="pl-k">.</span>isNullOrEmpty(subject)) {
            <span class="pl-k">return</span> <span class="pl-s"><span class="pl-pds">&#34;</span>Fill out &#39;Subject&#39; field.<span class="pl-pds">&#34;</span></span>;
        } 
        <span class="pl-k">if</span> (<span class="pl-smi">Strings</span><span class="pl-k">.</span>isNullOrEmpty(body)) {
            <span class="pl-k">return</span> <span class="pl-s"><span class="pl-pds">&#34;</span>Fill out &#39;Body&#39;.<span class="pl-pds">&#34;</span></span>;
        } 
        <span class="pl-k">return</span> <span class="pl-s"><span class="pl-pds">&#34;</span>Ready to send.<span class="pl-pds">&#34;</span></span>;
    }

    <span class="pl-k">public</span> <span class="pl-k">boolean</span> <span class="pl-en">isReady</span>() {
        <span class="pl-k">return</span> <span class="pl-k">!</span><span class="pl-smi">Strings</span><span class="pl-k">.</span>isNullOrEmpty(to) <span class="pl-k">&amp;&amp;</span> <span class="pl-k">!</span><span class="pl-smi">Strings</span><span class="pl-k">.</span>isNullOrEmpty(subject) <span class="pl-k">&amp;&amp;</span> <span class="pl-k">!</span><span class="pl-smi">Strings</span><span class="pl-k">.</span>isNullOrEmpty(body);
    }

    <span class="pl-k">@Override</span>
    <span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">toString</span>() {
        <span class="pl-k">return</span> <span class="pl-s"><span class="pl-pds">&#34;</span>IntroModel [to=<span class="pl-pds">&#34;</span></span> <span class="pl-k">+</span> to <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#34;</span>, subject=<span class="pl-pds">&#34;</span></span> <span class="pl-k">+</span> subject <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#34;</span>, body=<span class="pl-pds">&#34;</span></span> <span class="pl-k">+</span> body <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">&#34;</span>]<span class="pl-pds">&#34;</span></span>;
    }
}

<span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">class</span> <span class="pl-en">IntroController</span> {
    <span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">IntroModel</span> model;

    <span class="pl-k">public</span> <span class="pl-en">IntroController</span>(<span class="pl-smi">IntroModel</span> <span class="pl-v">model</span>) {
        <span class="pl-c1">this</span><span class="pl-k">.</span>model <span class="pl-k">=</span> model;
    }

    <span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">sendEmail</span>() {
        <span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(<span class="pl-s"><span class="pl-pds">&#34;</span>Send: <span class="pl-pds">&#34;</span></span> <span class="pl-k">+</span> model);
    }

    <span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">yell</span>() {
        model<span class="pl-k">.</span>setBody(model<span class="pl-k">.</span>getBody()<span class="pl-k">.</span>toUpperCase());
    }
}

<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JPanel</span> panel <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JPanel</span>();
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">Bindings</span> bindings <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">Bindings</span>();
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">IntroModel</span> model <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">IntroModel</span>();

<span class="pl-k">@SuppressWarnings</span>(<span class="pl-s"><span class="pl-pds">&#34;</span>unused<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">@Bindable</span>
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">IntroController</span> controller <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">IntroController</span>(model);
<span class="pl-k">@Bound</span>(<span class="pl-c1">to</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>to<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JTextField</span> toField <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JTextField</span>();
<span class="pl-k">@Bound</span>(<span class="pl-c1">to</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>subject<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JTextField</span> subjectField <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JTextField</span>();
<span class="pl-k">@Bound</span>(<span class="pl-c1">to</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>body<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JTextArea</span> bodyArea <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JTextArea</span>();
<span class="pl-k">@Action</span>(<span class="pl-c1">call</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>yell<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JButton</span> yellButton <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JButton</span>(<span class="pl-s"><span class="pl-pds">&#34;</span>YELL!<span class="pl-pds">&#34;</span></span>);
<span class="pl-k">@Action</span>(<span class="pl-c1">call</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>sendEmail<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">@EnabledIf</span>(<span class="pl-c1">to</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>ready<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JButton</span> sendButton <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JButton</span>(<span class="pl-s"><span class="pl-pds">&#34;</span>Send<span class="pl-pds">&#34;</span></span>);

<span class="pl-k">@Bound</span>(<span class="pl-c1">to</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">&#34;</span>currentMessage<span class="pl-pds">&#34;</span></span>)
<span class="pl-k">private</span> <span class="pl-k">final</span> <span class="pl-smi">JLabel</span> messageLabel <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">JLabel</span>(<span class="pl-s"><span class="pl-pds">&#34;</span><span class="pl-pds">&#34;</span></span>);

<span class="pl-k">public</span> <span class="pl-en">IntroCinchMVC</span>() {
    initializeInterface();
    bindings<span class="pl-k">.</span>bind(<span class="pl-c1">this</span>);
}

<span class="pl-k">private</span> <span class="pl-k">void</span> <span class="pl-en">initializeInterface</span>() {
    <span class="pl-c"><span class="pl-c">//</span> swing layout code cut for brevity</span>
    <span class="pl-c"><span class="pl-c">//</span> ...</span>
}

<span class="pl-k">public</span> <span class="pl-smi">JComponent</span> <span class="pl-en">getDisplayComponent</span>() {
    <span class="pl-k">return</span> panel;
}

<span class="pl-c"><span class="pl-c">//</span> main removed</span>

}

作者

Dan Cervelli

许可证

根据Apache 2.0许可证,Cinch可以使用。

Copyright 2011 Palantir Technologies

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.




相关问题推荐