Tag Archives: Eclipse

Eclipse SWT: Standalone Java Apps on full OS speed!

The Standard Widget Toolkit (SWT) is a graphical widget toolkit for use with the Java platform. It was originally developed by Stephen Northover at IBM and is now maintained by the Eclipse Foundation in tandem with the Eclipse IDE. It is an alternative to the Abstract Window Toolkit (AWT) and Swing Java graphical user interface (GUI) toolkits provided by Sun Microsystems as part of the Java Platform, Standard Edition (J2SE). Both SWING and AWT  have always been criticized for their slowness. They actually ‘draw’ GUI components on screen pixel by pixel, which slows down the building up of screens.

SWT Mac
SWT Mac

This is different for SWT which accesses the native GUI libraries of the operating system using Java Native Interface (JNI) in a manner that is similar to those programs written using operating system-specific application programming interfaces (APIs). Programs that call SWT are portable, but the implementation of the toolkit, despite part of it being written in Java, is unique for each platform.

SWT Win
SWT Win

The results are Native Speed Applications for Mac, Windows
and Linux. Probably the best example is the SWT Webbrowser. An SWT example which is included in the Eclipse IDE and is used by Eclipse to show its help documents.

Thanks for reading this article. If you’re interested in the details of building SWT Applications see the next article .

 

 

Java Architecture in Eclipse

ejwagenaar.com logo

Using Ecipse for Reversed Engineering

Eclipse is one of the most popular IDE’s. In this article we will use Eclipse to investigate an existing class from the java.text package. The class we will investigate is the DateFormat.

DateForma is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date -> text), parsing (text -> date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.
DateFormat provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are provided in the method descriptions.

DateFormat helps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar. Browsing the JavaDoc is always fun. But even more fun is firing up your IDE and have a look. So let’s do so!

Eclipse with DateFormat Chosen.
Eclipse with DateFormat Chosen.

With DateFormat chosen in the TypeHierachy I can see it inherits directly from an Abstract class named Format. (You recognize the Abstract Class by the small A in it’s upper-right corner. As you can see DateFormat is Abstract itself.

Now zoom in on the DateFormat to get the full type hierarchy:

As you can see, SimpleDateFormat inherits from DateFormat, which in turn Inherits from Format, which (like any other Object) inherits from Object.

Conclusion

Eclipse is an excellent tool to study existing Java Classes and their hyrarchie in comparison with other classes. You can also use this to investigate (chains of) method calls by selecting a method and choose ‘Call hierarchy’. Try this yourself and enjoy it!

 

Adding Lucene Search to your Applications (part 1)

Creating the Lucene Index.

Suppose you’re asked  to  add search to an application. You need to do two things:

  1. Index your data.
  2. Make your data searchable.

1. Indexing

For simplicity we will assume that your data will be text-only. (If you have other formats too we will cover that later with Apache Tika).

We will write a small Java command-line program called Indexer. If you have never programmed in Java before you should download an IDE. You want to use the free and Open Source Eclipse because it’s the best available for free.

Eclipse IDE
Eclipse IDE

 

 

 

 

 

 

 

Please note that the .jar file we are going to create here is already available in the download as Demo.jar. To get a better understanding of how it’s created we’ll do it ourselves using Eclipse. Also make sure you installed the latest version of the JDK. You can check the version by issuing the following command:

java -version

This will tell you which version was installed.

Now start with downloading Eclipse. You can download it from the Eclipse Website. Eclipse is available for all platforms. Choose your platform and download.

After you are done, open Eclipse. You will see a nice welcome screen with links to the tutorial, as well as some examples. Take your time to read them and import some of the examples.

Now it’s time to get started. Open the Java perspective and from the main menu select New and choose Java Project.

Eclipse will ask for a name. Enter Demo and click finish.

Next thing to do is importing the Lucene Library’s necessary for Lucene. You do this as follows:

  • First download Lucene from http://lucene.apache.org/. Unzip the archive to a directory of your choice.
  • The archive contains a number of .jar files, documents and examples.
  • Take your time to read the documentation. In particular the Demo.
  • We will use the Demo.
  • In your Eclipse project create a new folder called ‘lib’.
  • Add the Lucene core .jar file in the ‘lib’ Folder. This can be done by dragging and dropping the file.
  • Right-click the file in the ‘lib’ Folder and select ‘Add to build-path’.
  • You have now set up the ClassPath but there isn’t any code yet.
  • To add the code, look in your Lucene Directory for a file named ‘IndexFiles.java’ and drag this into the Eclipse ‘src’ Directory. You will see a red cross appearing in front of the ‘IndexFiles.java’. This is because it’s expected to be inside a package
    org.apache.lucene.demo . Right-click the file and select Move to package org.apache.lucene.demo. If everything went well, you will see the red cross disappear, indicating that everything works.
  • Finally to see it working right-click the IndexFiles.java and select Run as Java Program.
  •  The console Window will open and you should see information on how to use the program.
  • In order to be able to use it as a Java command-line program, you will need to export your project to the FileSystem. Do the following: Select your project in Eclipse, then from the main menu select File > export and select the Executable Java Archive (jar) format. Make sure to also select the ‘Include referenced jar libraries option.
  • Open a command prompt and cd to the same directory where you exported the jar file.
  • You can now execute the command as follows:
  • java -jar IndexFiles.jar

If everything went well, you will see a new Directory appeared in the same location where you exported the .jar file. This  contains the Index files.

2. Check the Index

Luke logo
Luke logo

To make sure your Index is good we will use a tool which enables us to have a look inside it. This is a Java program called Luke. It’s already around for some time and it’s important to use a version which is compatible with our version of Lucene. To look for the latest version, check https://github.com/DmitryKey/luke It comes as an executable .jar file with a SWING GUI. To open it, either duibleclick or execute the command:

java -jar Lukeall.jar

Once opened you will see a screen like this:

Lukeall
Lukeall

Navigate to the Directory where your Index is stored and open it. Once opened you can now see your index and do some searching.

To proceed with searching go to part 2 of this series.