Posts Tagged ‘eclipse’

Developing Translatable Rich Bidirectional Swing Applications: Part 1 – Dos and Don’ts

All the problems in software development starts with at least one of the following:

  • Low budget.
  • Limited time.
  • Limited resources.
  • Skipping research and design phase.

Now one of the above will be more than enough to blow up your project for instance lets take a look at the research and design phase if you skipped the research phase you might end up re inventing the wheel and developing a workflow engine while you can use one of the already existing Open Source frame work such as OS workflow which has a dedicated team for developing it and test it, and also it is wildly used and have a good community that you can get help from and last but not least saving much time which will be more useful if used it to focus on your project requirements…etc.

In SWING application the user experience could define the success or the faulier in of your project, is this true? well not necessarily as it depends on the type of your application, the type of users , and as far as I know people don’t pay more for good look(in applications of course)! but a good design does not include only nice looks it also has re usability, extensibility and maintainability  for your application, finally having a good look for your application will promote it…

having sayed this lets look at the dos and don’ts in GUI development:

  • Avoid heavily saturated colours, as it will make it even harder to look at the screen!
  • Always use native fonts of your OS like Serif and do not manipulate fonts decoration as it could look nice on your machine but it will be very ugly on others machines!
  • When using icons try to have them all from the same Icon set or at least match them in a proper way!
  • Avoid nested borders and favour using separators.
  • Align your components horizontally and vertically.
  • Give information when needed.
  • Make validation when needed.

For bidirectional applications you have more to care about like:

  • Planning to have a mechanism for loading and using multiple translations.
  • Providing a mechanism to have your layout flipped according to the language direction.
  • using the right UI components that will help you accomplish your task.

Next we will be focusing on bidirectional development and providing some tricks to do this.

Developing Translatable Rich Bidirectional Swing Applications

Today’s applications are getting more and more complicated and user experience is the main focus to have a successful product, And we all know the huge boost in user experience that web 2.0 has made with it is splashy interfaces and AJAX functionality, but what if the application is not web-enabled what if it is SWING application? is it possible to develop such an application with a good user experience? hopefully this is what I am going to highlight in this series of posts…

My plan is to make the posts in this series on a regular basis but lets leave this to my quality time…

The main aspects am going to talk about are:

  • Main features that will enhance a user experience.
  • Design Challenges Architects encounter while developing such applications.
  • Best practices in making a successful UI interfaces.
  • Making flexible bidirectional SWING application.
  • Making translatable SWING application.
  • Data validation on spot or on submit!
  • Data binding between UI Components and application backend beans.
  • Theming SWING applications.

This and maybe other subjects will be covered in this series Insha’ Allah.

Till then :)

Creating first service

In the previous post we saw how to inject resources to bundles, and today we will make our first service it is going to be a Date formatting service.

you can download the source for this service from dateformattertar and now we will start creating our service…

go to in the same workspace we started before to get advantage of the target platform and the log4j configuration

File –> New –> Other…

And select to create Plug-in project

screenshot-new

Click next and name the project DateFormatter and don’t forget to change the target platform to “an OSGi Target”

screenshot-new-plug-in-project

Click Next and Finish.

In thee newly created project Open the MAINFEST.MF file in the META-INF folder and add the “org.apache.log4j” to the imported packages as we are going to use it.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: DateFormatter Plug-in
Bundle-SymbolicName: DateFormatter
Bundle-Version: 1.0.0
Bundle-Activator: dateformatter.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version=”1.3.0″,
org.apache.log4j

Now create a sub-package “dateformatter.app” And create a new Interface in it with the name DateFormatter

package dateformatter.app;

import java.util.Date;

public interface DateFormatter {

String formatDate(Date date);

}

Now create a sub-package “dateformatter.app.impl” And create a new Interface in it with the name DateFormatterImpl

package dateformatter.app.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import dateformatter.app.DateFormatter;

public class DateFormatterImpl implements DateFormatter {

private String format;

private Logger logger = Logger.getLogger(DateFormatterImpl.class);

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

@Override
public String formatDate(Date date) {
logger.info(“formatting a new date”);
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}

}

Now all we need to do is to add a spring definition for our classes, spring looks for application context in the META-INF/spring/ so create your application context file there…

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>

<bean id=”formatterImpl” class=”dateformatter.app.impl.DateFormatterImpl”>
<property name=”format” value=”dd-MM-yyyy”></property>
</bean>
</beans>

Creating a Registration listener and Importing the service

Now it is very useful to use a registration listener for keeping track of your bundle, Spring DM offers a very good way for doing this, it is by making a new class containing at least two methods which there names are irrelevant -as the class name- but the only constraint is to make the arguments in the both methods take the first argument as reference to the service INTERFACE and the other as a Map to hold the service properties…

package dateformatter.listener;

import java.util.Map;

import dateformatter.app.DateFormatter;

public class RegListener {

public void reg(DateFormatter formatter, Map map) {
System.out.println(“am here”);
}

public void unReg(DateFormatter formatter, Map map) {
System.out.println(“am going away”);
}
}

Now a wrote another spring context file and put it in the spring folder too, this one to hold the OSGi beans:

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:osgi=”http://www.springframework.org/schema/osgi”
xmlns:osgi-compendium=”http://www.springframework.org/schema/osgi-compendium”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd”>

<osgi:service id=”formatter” interface=”dateformatter.app.DateFormatter” ref=”formatterImpl”>
<osgi:registration-listener  registration-method=”reg” unregistration-method=”unReg”>
<bean class=”dateformatter.listener.RegListener”>
</bean>
</osgi:registration-listener>
</osgi:service>
</beans>

this way I publish a service with a registration listener, to run it you need to run the target platform.

if every thing goes will you will see something like the following:

screenshot-java-dateformatter-meta-inf-spring-dateformattercontext-osgixml-springsource-tool-suite

you can test the registration listener by stopping and running the service.

Next post we will write a client that uses this service…

Logging with OSGi and knopflerfish

In the previous post Getting started We had prepared our Target platform to run on Equinox and tested it, also we noticed that the logging is not working so today we will configure it on both Equinox and Knopflerfish which is my favourite as it has a very easy to use GUI, which enables developers to easily work with it :)

Now log4j.properties is a resources file like any other resources, the thing is in an OSGi environment you can not inject resources unless you used a fragment to do so. a Fragment is an empty project that has it is own META-INF directory that contains a MANIFEST.MF file to configure the fragment. now we have two options to make a fragment either we do it without a src folder just resources directly or we do make an src folder and put resources in it, now some would say that we don’t need the src folder as we are not going to write any code in the fragment, while this is true you need to know the fact that adding a fragment without src folder will work as expected on Equinox but it wont on knopflerfish, on the other hand making the fragment with src folder that contains the resources will be picked in the expected way by both Equinox and knopflerfish(please note that am not working on felix not yet, but I’ll update this info when I test it).

to sum it up we are going to make a fragment that will deliver resources data for a bundle(in our case it is a log4j.properties to org.apache.log4j),  and we are going to make sure that this bundle will be working correctly on both Equinox and Knopflerfish. So lets get started!

  • We will make a Fragment project, go to:

File –> New –> Other –> Plug-in Development –> Fragment Project

fragment

I will call it Log4jConfig , keep the create a java project checked as deiscussed erlier and change the Target Platform to an OSGi framework and choose Equinox!

log4jconfig

now we will configure our fragment as shown in the following pic! and note that we can only add one host for each fragment, I have chosen “org.springframework.osgi.log4j.osgi” now click finish.

screenshot-new-fragment-project

Now in the src folder make a new file and call it log4j.properties, I have configured one log4j. your workspace should looks like this:

log4jproject

If you take a look at our target folder you will see that we have two bundles provides log4j “org.springframework.osgi.log4j.osgi” and “com.springsource.org.apache.log4j” and both of them exports the “org.apache.log4j” which will make a conflict when trying to run the project! to solve this we need to deactivate the second one as our fragment is already hosted by “org.springframework.osgi.log4j.osgi”, to do that go to run configurations and un select the the “com.springsource.org.apache.log4j” from the target platform.

run

click run and you should see logging on your console!

logging

now we need to test on knopflerfish:

  • To deploy the log4jConfig fragment right click on it and click Export:

plug-in Development –> Deployable plug-ins and fragments

and click next and in the directory choose the OSGi-Target project root. and click finish.

screenshot-export-1

Refresh you OSGi-Target and you will find a new folder called plugins containing a JAR file called log4jConfig_1.0.0.jar

exported

now open Knopflerfish and follow the instructions:

  • add the log4jConfig_1.0.0.jar to it by

File –> Open bundle from file

  • make sure that it is status is installed not resolved. (if it is resolved click on the refresh button).
  • as in first step add the following bundles from our target folder:
  1. aopalliance-1.0.jar
  2. com.springsource.org.aopalliance-1.0.0.jar
  3. com.springsource.org.apache.commons.logging-1.1.1.jar
  4. commons-logging-1.1.1.jar
  5. log4j.osgi-1.2.15-SNAPSHOT.jar
  6. spring-aop-2.5.5.jar
  7. spring-beans-2.5.5.jar
  8. spring-context-2.5.5.jar
  9. spring-context-support-2.5.5.jar
  10. spring-core-2.5.5.jar
  11. spring-osgi-annotation-1.1.0.jar
  12. spring-osgi-core-1.1.0.jar
  13. spring-osgi-extender-1.1.0.jar
  14. spring-osgi-io-1.1.0.jar
  15. spring-osgi-mock-1.1.0.jar
  • start them all and make sure to leave the “spring-osgi-extender-1.1.0.jar” to the end (last one to start). you should see the logging on the Knopflerfish console:
  • If you want to turn off Knopflerfish make sure to stop “spring-osgi-extender-1.1.0.jar” as if you don’t when you start Knopflerfish next time logging wont work! because the status of the log4jConfig_1.0.0.jar will be resolved and not installed.

screenshot-knopflerfish-osgi-desktop-knopflerfish

Spring DM-OSGi Getting started

Am using STS as an IDE which is basically eclipse but preloaded with plugins to fit this kinda job! So I’ll be talking about it (every thing should work just fine on any other eclipse). To day I’ll talk on how to prepare the environment to start an Spring DM-OSGi project. we will start with creating a target-platform for the project, which means the set of jars that will be the backbone to run my project.

  • Open your IDE and make a new project by going to

File –> New –> Project…

new-project

we will use a plain project for the target-platform name it OSGi-Target and click finish.

osgi-target

  • Now we will extract the OSGi-Target JARs using Maven (a quick maven tutorial can be found here) I have prepared my pom file and you have to make sure to change the following tag with your own value:

<taget-platform.root>
/home/fadi/Programs/STS/workSpaces/OSGi-Series/OSGi-Target/target
</taget-platform.root>

create a new folder on the project and call it target

target-hir

Am using m2eclipse plugin for maven integration. to run the file Right click on the file:

Run as –> maven build…

in the goals field make sure to right package

screenshot-external-tools-configurations

Click run and maven will download and prepare your target folder you should see something like the following if every thing goes fine!

maven-build

Now refresh your target folder and you should see the newly installed JARs!

Now we need to make sure that STS/eclipse will run this platform when we make our first bundle!

  • Go to :

Window –> Preferences –> Plug-in Development –> Target Platform

change the location to match your target folder that you made earlier…

screenshot-preferences

click reload and you will see the 22 JARs in your target folder being displayed.

  • We can test our platform now by going to Run-configuration and creating new OSGi Framework, lets call it Target!

screenshot-run-configurations

click run and you will see that you have a warning on the console says that you don’t have a logging appenders declared(this is what we are going to fix in the next post), we can check our work by typing on the console “ss” which will give us the status of our Target:

screenshot-java-osgi-target-pomxml-springsource-tool-suite-1

That’s it for now and next post we will configure log4j using a fragment.

This is an on going Series and I hope to find some interested people who can help me in it :)

Tag Cloud
Categories
Archives