Thursday, August 20, 2015

Making a Silky Calculator

I like Silk. This dependency injection framework is just AWESOME! It is so intuitive that I just have to give it two thumbs up. Why? Because it makes dependency injection a part of the native Java language and is definitely simple to set up and use.

So I thought I would give it a go with one of my favorite web frameworks, Vaadin. I have used Vaadin off-and-on for about a year now and I have to say it is pretty slick. Vaadin is such a quick framework. When you need to get something to the screen Vaadin can build it fairly fast. And the code is so simple, it can easily simulate procedural coding. As a result, the code you write while putting up a quick prototype usually ends up a hot mess to read through the next day.

In with the fancy patterns now!

I decided to re-create the Calculator sample application on the Vaadin.com website.

Creation of a project is easy enough. I downloaded the latest version of Eclipse (Mars) and then installed the Vaadin framework through the Eclipse Marketplace.


I had an easy time using Ant to build SilkDI from www.silkDI.com. To build the Silk library, simply import the project to your eclipse workspace and run the ant task by clicking on the build.xml file and pressing Alt+Shift+X, then press the letter Q. The library, sources and Javadoc will be created. You just need the library added to your build path.



I built the user interface up using the new Vaadin Designer tool but the thing stopped working promptly after I finished the look and feel. So I had to limp by modifying the new html format manually. Not the most fun experience...



Thankfully, the interface is simple enough, what goes into a calculator other than a few numbers and some symbols? I just made all the buttons into a grid and included a text box for the display. I had to later change the text box for a label, since the text box read-only property prevents modification from the program side as well as the user side. Wish they would make up their minds on that.

I also ran into some trouble getting the Silk to work, but that was due to my sheer ignorance when it comes to actually wiring dependencies. I did some looking at the examples on the SilkDI site and managed to make it work. I had to understand how the objects were being constructed in the background and realize which examples were worth bothering with and which were for more advanced applications.

Separating the concerns of the Presenter, View and Model were easy as cake using Java 8. Passing behavior to each button was so much easier than the Calculator example on Vaadin's own site. I went the route of creating a single method that obtained the button's text then passed a communication to the model for interpretation. I would have liked to spend more time fleshing out a better separation of the business logic out of the Presenter, but in the end, I like how it turned out.

As far as the separation goes, I added a twist to the example Calculator app on the Vaadin website and created both a BigInteger and a BigDecimal model and added a button that would switch between the two types of calculators. The calculator that depends on the Integers will truncate the decimal places for us (obviously).

Check out the sample app in the links on the right.

Update: I've put the source up on GitHub

Thanks for reading.

1 comment:

  1. The objects, whether drawn or ejected, shall be essentially equal as to size, shape, weight, and stability and as to all other characteristics which will control their choice from the receptacle. Any player in a sport canceled pursuant to this paragraph shall be permitted to play the subsequent sport free of charge. Property owned by a municipality or a county when the governing authority has, by appropriate ordinance or resolution, specifically authorized the usage of} such property for the conduct of such video games. No one under 18 years of age shall be allowed to play any bingo sport or instant bingo or be involved within the conduct of a bingo sport or instant bingo in 점보카지노 any method. Every charitable, nonprofit, or veterans’ group involved within the conduct of a bingo sport or instant bingo should be located within the county, or inside a 15-mile radius of, where the bingo sport or instant bingo is located. Except for instant bingo, which is not restricted by this subsection, the variety of days per week throughout which organizations authorized under this section may conduct bingo shall not exceed two.

    ReplyDelete