Saturday, September 24, 2011

Scala stock charts, part 4 - Woking with the GUI - 1

I have been busy the last couple of weeks, or better saying, too lazy to sit home with my Scala Technical Analysis tool. Anyway, this weekend I decided to continue and this time to start from the GUI and start using the functionality done so far from a GUI.

I have also done som restructuring in the packages which now is:

package me.ironic.scabors.app
package me.ironic.scabors.service
package me.ironic.scabors.model
package me.ironic.scabors.file

even if I have not reworked the messy file-package. I will do that later. however, I finished up with loading the price list, and that code now look like this:


I also broke out the model classes from the file package, and put it in another file called PriceModel.scala, you can also see that I am now using "Price" which is a better name than "Quote" so all references to Quote is now replaced with Price. The content of that file is:

PriceModel.scala


I also made a Service object so that the GUI does not need to know or depend on anything from the file package. So far as I have gotten with the GUI it just contains two methods, one to get all price lists and one to get the data for a chosen stock. Here is the code:

PriceService.scala

GUI programming done in Scala

I decided to leave the idea of using Java FX that I had from the beginning and that I wrote in the beginning post. Instead I decided to use Scala Swing. Not so easy as it turned out, because first of all, it has been a couple of years since I last did any Java Swing programming and second of all, it was not very similar and not easy to transform Java Swing thinking to Scala Swing thinking. Well, it is less code, but surly, at this stage it looks really messy. Maybe it takes a while for my brain to melt Scala Swing programming and the fact that google got warm from all searching and few good examples does not help.

I am to tired to explain most of the code, maybe in a later post, but here is what the purpose if the GUI is so far.
  • Provide a text field to filter search for stocks "Instrument"
  • Provide a button to perform the search
  • Provide a ComboBox to present the filtered result from the search and to select stock to draw
  • Provide Panel for drawing a Bar Chart of selected stock

I have implemented and it works as I wand, except that the tedious job to provide the functionality to draw a bar chart is not done. Thea is a later exercise and a future post to describe. I let you see the messy code for this. 
ScaBorsApp.scala

Yeah, thats right, I also gave up SBT, I am now using gradle to build. here is the code for that. build.gradle
build.gradle

Until later..... ciao!

Tuesday, September 13, 2011

Web Fragment using Scala

Do you want to use Scala in a Java web environment, or write web fragment using scala. Or maybe you would ask: why do it at all? The answer is, because you can!

Well my little spike succeeded without any severe headache, actually so well that I did not got any at all and there was never any risk to get one.

I used SBT to build my Scala code, probably I could use it to make the full application, but I used Netbeans to create a web projet for my spike.

Setting up for SBT.

create folder for the project, for example:
>mkdir webfrag
enter the directory and create and edit build.sbt for servlet programming
>touch build.sbt

Put the following in the build-sbt file

Create a Servlet in Scala in this way.
create src/ folder and create and edit a file for the servlet, for example src/WebFragHello.scala

You can write what you want, but to make a form of HelloWorld-like example fill it with for example:


This code also "catches" the case that user parameter is not sent.A special feature seams to be that the scala code does not understand urlPattern as used in Java in the annotation, that is why I use {Array("/hello")} instead of {"/hello"}

Compile and package the code with SBT
>sbt compile
>sbt package

To make it work as a web-fragment: Put the compiled jar in WEB-INF/lib folder in a Servlet 3.0 server, you also need to put scala-library.jar in the server to be able to use scala objects. In netbeans you just need to add that jar-file to "libraries" folder in the test web project

Surf to .../context-root/hello or /context-root/hello?user=Ironic

Neet isn't it?