Monday, September 25, 2006

SWT Image creation from jar file

If you need to include your images in the jar when creating SWT images, this is the way to do it.

Image image = new Image(display, ImageDescriptor.createFromFile(yourClass.class, image_path).getImageData());
Label imageContainer = new Label(shell, SWT.NONE);
imageContainer.setImage(image);

yourClass.class is the class that contains the image, display is the current display, and shell is the control that holds the image label.

Tuesday, September 19, 2006

Creating custom events in Java

First, create the event that your application will fire

public MyEvent extends EventObject {
public static final MYEVENT_OPEN = 1;
public static final MYEVENT_CLOSE = 2;

private int type;

public MyEvent(int eventType) {
super(type);
type = eventType;
}
public int getType() {
return type;
}

}

Next, create the interface that has to be implemented by the classes that will receive your event MyEvent.

public interface MyEventListener extends java.util.EventListener {
public void myEventWasReceived(MyEvent event);
}

Then, create a controlComponent that knows what classes that are subscribing to your event MyEvent.

public abstract class MyEventControlComponent {
static EventListenerList myEventListeners = new EventListenerList();

public static addMyEventListener(MyEventListener listener) {
myEventListeners.add(MyEventListener.class, listener);
}

public static removeMyEventListener(MyEventListener listener) {
myEventListerers.remove(MyEventListener.class, listener);
}

protected void fireMyEvent(MyEvent event) {
Object[] lsteners = myEventListeners.getListenerList();
for (int i = 0; i<listeners.length; i+=2) {
if(listeners[i]==MyEventListener.class) ((MyEventListener)listeners[i+1]).myEventWasReceived(event);
}
}
}

Now, this is the framework and it is possible to start creating subscribers and notifiers of MyEvent. Each notifier has to extend the MyControlComponent. To fire a new MyEvent, just

this.fireMyEvent(new MyEvent(MyEvent.MY_EVENT_OPEN));

The subscriber's of MyEvent, has to register itself with the MyEventControlComponent, this is done by

public class MyTest implements MyEventListener {
public MyTest() {
MyEventControlComponent.addMyEventListener(this);
}

Then implement the method(s) from the MyEventListener interface, and handle the events:

public void myEventWasReceived(MyEvent event) {
//Some action based on the event, and its information
}

Sunday, September 17, 2006

Where to get MSN Live Add-in SDK

From Katie Blanch's blog:

To get a build of Messenger that supports add-ins, go here:


Then you'll need to either install the SDK (coming soon), or set the following registry key:

HKEY_CURRENT_USER\Software\MicrosoftMSNMessenger\AddInFeatureEnabled DWORD 1

And finally, you can follow these steps to create your own add-in in C# using Vistual Studio:

1. Launch VS 2005 and choose File->New Project
Choose a Visual C# Class Library
2. Name your solution whatever you'd like. I've chosen 'SmartTalker'.
Go to Project->Add Reference
3. In the dialog that pops up, choose the 'Browse' tab.
4. Navigate to C:\Program Files\MSN Messenger and choose 'MessengerClient.dll'
5. Add 'using Microsoft.Messenger' statement to your Class1.cs file.
6. Create a class that inherits from IMessengerAddIn. I've chosen to name the class 'AddIn'.
7. Navigate to Project->SmartTalker Properties
8. Modify the assembly name to be the fully qualified name of the class that implements IMessengerAddIn. In my case, I'd change this to 'SmartTalker.AddIn'.
9. Once you've created your add-in .dll, you can load it into Messenger by going to Tools->Options and selecting the 'Add-in' page.
10. Finally, you can turn on/off your selected 'Agent' add-in manually via the status menu on Messenger's main window.

http://blogs.msdn.com/katieblanch/archive/2006/05/11/595765.aspx