Saturday, March 19, 2005

ImageDescriptor problem on JFace actions?

I found what seems to be an issue with setting an jface.resource.ImageDescriptor for an jface.Action class.  I have a few standard button bar images that I was going to use in my application.  When I first set the image for the action, I thought I was doing something wrong.  I set the image originally, like this:

public ExitAction() extends Action {
 
    public ExitAction() {
        super( “E&xit@Ctrl+X“, ImageDescriptor.createFromFile( “images\\close.gif“ ) );
        setToolTipText( “Exit Application“ );
    }
}
Unfortunately, this produced a small red square on the button and on the menu item that the ExitAction was assigned to.  I checked the image thinking it was corrupt, but all was well.  After doing a little snooping, I found that you can also create an ImageDescriptor from a swt.graphics.Image.  I changed the code to look like this:

public ExitAction() extends Action {
 
    public ExitAction() {
        super( “E&xit@Ctrl+X“ );
        setToolTipText( “Exit Application“ );
        Image image = new Image( Display.getCurrent(), “images\\close.gif“ );
        setImageDescriptor( ImageDescriptor.createFromImage( image ) );
    }
}
This produces the desired results.  I am not sure why the first way of setting the image for the action did not work, but every tutorial that I have gone through and even the code in Apress' The Definitive Guide to SWT and JFace do it like I did it in the first code fragment above.

Technorati Tags: , ,

Using Ant to build an executable Jar file

Boy... I am tired.  It is 3:09 pm in the afternoon and I have only been up for just over three hours today.  I dove deep into SWT and JFace last night.  I am really enjoying JFace at this point.  It really makes building a GUI application with SWT so much easier.

Anyway, at some point during the night, for whatever reason (can't remember now), I decided to setup a build.xml file for Ant.  I did this for two reasons:  1)  I wanted to learn more about using Ant and 2) I wanted to see if I could setup a build properly for the SWT/JFace application that I am working on.

The setup of the build.xml to do a clean compile and to build a simple jar file was quite easy.  The problem came in, however, when I tried to build an executable Jar.  I never could get things to work correctly.  I kept getting a message about java not being able to find the main class when I tried to launch the jar.

A little after 6:00 am this morning, I finally decided that I should get some sleep.  I guess that helped, because after sitting here for the past 30 minutes looking at it again, I figured it out.  Well, I figured it out by accident.  :)

I did a little reading on the Sun Developer Network forums and something made me look at another executable jar file that I know I have on my computer.  When I took a look at the manifest in the jar, I noticed that all of the files listed on the Class-Path entry were in the same deployment directory as the executable jar.  I had tried putting them in a path on the classpath and putting them in the jar, but that never did work this morning.

So, I said what the heck and copied the appropriate external jar files that I knew the project needed into the directory where my jar file was located.  Now, mind you, this is the same jar that had just 30 seconds earlier given me that same “Cannot find the main class” error message.  After copying the files, I tried to launch the jar again and to my surprise, my application came right up.  Sweet!

I hate that I have wasted most of a Saturday, but I have definitely learned a lot about Ant and creating executable jar files in the past 18 hours.

Technorati Tags: , , ,

Thursday, March 17, 2005

JFace ApplicationWindow issue resolved

OK... duh... I read a couple of different articles tonight about using the ApplicationWindow class from JFace, but I missed both times that I had to include the org.eclipse.ui.workbench_<version>/workbench.jar and org.eclipse.core.runtime_<version>/runtime.jar to get it to work.

I was having another problem with my JFace demo application tonight when it came to Actions.  I had created a couple of private inner classes to handle exit and another action that I was playing around with.  Unfortunately, whenever I clicked on the menu item or the toolbar entry, I would get a stack trace in the Console window in Eclipse.

After doing some searching on Google for the past hour, I finally found out that you also have to include org.eclipse.osgi_<version>/core.jar.  I am not sure exactly what this is for as of yet, but it allows everything to work properly, so it got added.  :)

Technorati Tags: , , ,

Build errors using Ant and jCoverage

After using Ant and jCoverage at work as of late, I decided to creat a build.xml for my small application that I am writing for myself at home.  I have run into a problem that I cannot figure out at this point, though.  Whenever I do a full build (including jCoverage reports), I get the error shown below.  Has anyone seen anything like this?

coverage:jcoverage.instrument:
[instrument] jcoverage 1.0.4 copyright (c)2003 jcoverage ltd. http://jcoverage.com/
[instrument] instrumenting 13 classes to C:\Projects\MyContacts\build\instrumented
[instrument] Exception in thread "main" org.apache.bcel.classfile.ClassFormatException:
    Invalid constant pool reference: 1066. Constant pool size is: 1024
I am not sure what the problem is with this build.  It follows everything that we do at work.  Unfortunately, this is definitely something that I do not know enough about and I am unable to find too much information about it on Google.

If anyone has any insight on this problem, I would greatly appreciate any help.

Technorati Tags: , ,

GUI Development with SWT and JFace

OK... I took a dive into JFace tonight.  This is the first time that I have played around with it.  For those who do not know, JFace allows you do UI development using the SWT widgets without a lot of the overhead code that you would require if you were using the raw widgets.

I was following along with an article on JavaWorld called “Rich clients with SWT and JFace”.  The article shows a very simple demo that displays a simple window.  The class extends ApplicationWindow, which is a more robust version of the JFace abstract Window class.  Unfortunately, when I type in everything as shown and try to run it in Eclipse, I get an error that says:


Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IProgressMonitor at com.pshack.application.Application.main(Application.java:30)
I changed the demo to create a separate Window class and use that instead of extending from ApplicationWindow.  This works.  I am not sure why extending from ApplicationWindow does not work for me.  It might be because I am using JDK5, but I doubt it.  I am sure it is just something that I do not understand as of yet.

Technorati Tags: , ,

Wednesday, March 16, 2005

SWT Development

I am currently writing an application that will fill a small bit of functionality that I need.  I am also using this as a way to learn more about JDK 5.0 and SWT development, in particular.  Over the past hour, I have realized, at least in JDK 5.0 (never tried it in earlier versions), you cannot override the SWT Display or Shell objects.

I was thinking about a way to have a main Application class that extends Display.  From that, I was going to have multiple window classes which extended Shell from SWT.  Unfortunately after I put together a couple of classes and tried to launch it, I get an error stating that overrides are not allowed.  I then remembered reading something like that on the web somewhere.  Unfortunately, this is one of those situations where you sort of remember something, but you cannot remember exactly where you saw it, so you have no clue where to find it again.  :)

Oh well, it is all a learning process.  That and the fact that it is just so much fun is the reason I got into software development over 16 years ago anyway.

Technorati Tags: ,

Friday, March 04, 2005

Joda-Time v1.0

I came across a new date and time API for the Java community the other day, but I did not get a chance to do anything with it until tonight.  The package is called Joda-Time v1.0.  Joda-Time provides a quality replacement for the Java date and time classes.

It is very easy to get up to speed with Joda-Time.  The main object that you will deal with if you are just doing regular date and time manipulations is the org.joda.time.DateTime class.  I refactored a couple of my classes tonight and I can say that using DateTime is far simpler than using the Date and Calendar objects from java.util.

I can actually create a new DateTime object, add some specified duration to it, and assign it to a variable all in one line of code.  Using the classes from java.util, I would have to get a Calendar object instance.  From that, I could add some specified duration.  To finally get a Date object, I call a method called getTime().  Geez!

Joda-Time is a very nice project.  The documentation is very clean and the web site has all of their JCoverage information for their unit tests.

Technorati Tags: ,