Developing a Desktop Widget in JAVA

This widget will be developed using Eclipse SWT. Hence assuming you already have java and eclipse, you will also require to download Eclipse SWT zip. Which you can download on,

http://www.eclipse.org/swt/

Though to develop this particular widget you will not require a expertise knowledge in Eclipse SWT. You can go through,

http://www.eclipse.org/swt/eclipse.php

in order to get started with SWT. But it’s not really necessary here and you can still go through this even if you are an absolute starter in SWT. And still you will have a fair knowledge on SWT at the end of this post. If you wanna have more of SWT, you can go through,

http://www.eclipse.org/swt/snippets/

Which should cover all what you need to know about SWT. Let’s get to the business end of life.

1. Open up your eclipse and create new project and name it with whatever name you wish (In my case it was “WidgetPro”).

2. Now again go to file menu and select import. And under general select “select archivecheck box (it will not be selected by default) and browse to point the downloaded “swt-3.x*.zip”.

3. Create a new folder inside your project’s src (source). And name it “images”. Download the following images and place them inside the folder which you just created.


N.B. Your widget will take the shape of the first image.

4. Now you can create new class, name it “GSearchWidget”. And paste the following code into it.

import java.awt.TextField;

import org.eclipse.swt.*;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;

import java.awt.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Lonely Coder
 * @date 12/05/2009
 * http://hamzeen.wordpress.com
 *
 * @since 3.2
 */
public class GSearchWidget {
	public static void main(String[] args) {

	final Display display = new Display();
	String currentDirectory = System.getProperty("user.dir");
	final Image image = new Image(display, "images/searchBox.png");
	final Image icon = new Image(display, "images/ico.png");
	Color green = display.getSystemColor(SWT.COLOR_DARK_GREEN);
	GC gc = new GC(image);
	// final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
	final Shell shell = new Shell(display, SWT.NO_TRIM);
	shell.setText("G Search.");
	display.setAppName("G App.");
	shell.setImage(icon);
	shell.setAlpha(480);
	shell.setLocation(800, 20);
	shell.setToolTipText("Lonely Coder Labs.");
	Region region = new Region();
	final ImageData imageData = image.getImageData();

	if (imageData.alphaData != null) {
		Rectangle pixel = new Rectangle(0, 0, 1, 1);
		for (int y = 0; y < imageData.height; y++) {
			for (int x = 0; x < imageData.width; x++) {
				if (imageData.getAlpha(x, y) == 255) {
					pixel.x = imageData.x + x;
					pixel.y = imageData.y + y;
					region.add(pixel);
				}
			}
		}
	} else {
		ImageData mask = imageData.getTransparencyMask();
		Rectangle pixel = new Rectangle(0, 0, 1, 1);
		for (int y = 0; y < mask.height; y++) {
			for (int x = 0; x < mask.width; x++) {
				if (mask.getPixel(x, y) != 0) {
					pixel.x = imageData.x + x;
					pixel.y = imageData.y + y;
					region.add(pixel);
				}
			}
		}
	}
	shell.setRegion(region);

	Listener l = new Listener() {
	int startX, startY;

	public void handleEvent(Event e) {
		if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
			shell.dispose();
		}
		if (e.type == SWT.MouseDown && e.button == 1) {
			startX = e.x;
			startY = e.y;
		}
		if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
			Point p = shell.toDisplay(e.x, e.y);
			p.x -= startX;
			p.y -= startY;
			shell.setLocation(p);
		}
		if (e.type == SWT.Paint) {
			e.gc.drawImage(image, imageData.x, imageData.y);
		}
		}
	};
		shell.addListener(SWT.KeyDown, l);
		shell.addListener(SWT.MouseDown, l);
		shell.addListener(SWT.MouseMove, l);
		shell.addListener(SWT.Paint, l);

		shell.setSize(imageData.x + imageData.width, imageData.y
				+ imageData.height);

		final Text txt = new Text(shell, SWT.WRAP | SWT.BORDER);
		txt.setBounds(20, 20, 100, 20);

		txt.addListener (SWT.Verify, new Listener () {
			public void handleEvent (Event e) {
				String string = e.text;
				System.out.println(e.text);
			}
		});

		Button go = new Button(shell, SWT.PUSH);
		go.setBackground(shell.getBackground());
		go.setText("Search");
		go.pack();
		go.setLocation(125, 18);
		go.setFocus();

	go.addListener(SWT.Selection, new Listener() {
	public void handleEvent(Event e) {

	//System.out.println("Clicked" + txt.getText());
	try {
	URL query = new URL("http://www.google.com/
        search?q=" + txt.getText());
	Runtime.getRuntime().exec("rundll32 url.dll,File
        ProtocolHandler " + query);
	} catch (MalformedURLException mue) {
		System.out.println("Ouch - a MalformedURLException happened.");
		mue.printStackTrace();
		System.exit(1);
	} catch(IOException io){
		System.out.println("Exception");
		//throw Exception("");
	}
	txt.setText("");
	}

	});

		Button b = new Button(shell, SWT.PUSH);
		b.setBackground(shell.getBackground());
		b.setText("X");
		b.pack();
		b.setLocation(190, 5);
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				shell.close();
			}
		});

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		region.dispose();
		image.dispose();
		display.dispose();
	}
}

That’s it you have just completed the widget. Hit the run button and enjoy it. This widget is a good example of a desktop application interacting directly with a web page. To make it more interesting I have also given some transparency. With include and exclude features of SWT developing transparent applications becomes seamlessly easy. Here Firefox was opened explicitly. Since, in ma case, the default browser was found to be Internet Explorer and I wanted to view results in Firefox.

Nice to be back with a post after a while. Hope you enjoyed it, thank you.

SLAX. Linux on a USB flash drive.

As things have turned out this happens to be the first post for this year. Though I wanted to blog a lot, ma work doesn’t seem to be giving me any room to do so. Anyway, Since of late I really wanted to try out a Linux Distro either in an emulated mode or to have it on a USB drive.

First I tried to run it in an emulated mode by following some of the tutorials given on PenDrive Linux. But after a series of failure I decided to change to ma second option. When it comes to Portable distributions two famous one’s that came to ma mind were Puppy Linux and SLAX. And they both have KDE.

After considering some of the capabilities/features of both Puppy Linux and SLAX, I decided to stick with SLAX because it could offer me most of the software that I was looking for and moreover, you don’t need to install any extra plug-ins to get your MP3’s and other commonly used video formats (specially avi) working. This is something I would really like to have on other distros like Ubuntu and which I unfortunately, don’t find on them due to certain restrictions (I think it’s better for me to stay out of this coz then I will be getting into patent and copyright laws and put ma self in a spot of bother).

You can find more about SLAX on, slax.org. And since, I’m using a hp pavilion dv2000 which has Windows Vista installed on it, this post should ideally suite vista users because it’s bit different than setting up your boot stick from being on other (older) versions of windows. But actually, creating it on older versions of windows seems to be lot easier than on Vista.

But to start with I should admit that there are some pre-requisites to ensure you can boot into SLAX using a pen drive. Because not all the PCs and Laptops supports booting from a USB drive. below I list few pre-requisites.

  • The PC or Laptop that you are using should support booting from a USB drive.
  • A pen drive with a minimum of 256 MB free space.

Moving on with the configurations,

  1. download SLAX usb edition (you will be downloading tar ball)
  2. download 7-Zip. (to extract the contents of the tar ball)
  3. extract the contents of the tar archive to the root of the pen drive.(boot and slax)
  4. download syslinux-3.xx.zip and extract it to your local file system
  5. copy the contents of syslinux-3.7.2/win32 folder to boot/syslinux on the pen drive.
  6. Now click on your windows start menu, type ‘command’ on the search box of the start menu.
  7. Once you are shown command prompt in the start menu right click and select run as administrator.
  8. Move the prompt to your USB drive by typing “driveLetter:” and enter.
  9. Type cd boot and press enter (in order to go to the boot dirctory in the usb drive).
  10. Type bootinst.bat to overwrite the MBR on your usb drive.

That’s it, we are done with the configurations. Now you can reboot your machine enter bios settings and then make USB drive as your first boot device in the boot sequence list. Now you should be logging into SLAX.!! below I attach a screenshot of slax while I was listening to some great tunes of A.R. Rahman.

a screenshot of slax

One of the coolest things about SLAX is that it not only shows other file systems but it also allows you to write to those which really makes life lot easier. Since SLAX is module based and modules get installed on-the-fly you never need to login and install anything. All what you need to do is just download your preferred module and copy to slax and next time you boot into slax, you should be able to use your new app, easy as that.

I really enjoy ma time on slax and I hope you will consider SLAX next time when you wanna go portable as well. Cheers!.

« Previous entries Next Page » Next Page »