Plugin SWT Main View Tutorial - VuzeWiki [Link]
Plugin SWT Main View Tutorial
From VuzeWiki
Contents
1 Adding Views Tutorial for Azureus [Link]
1.1 Prerequisite For This Tutorial
1.2 Attach your plugin to an UIInstance
1.3 Create a class implementing UISWTViewEventListener
1.4 Displaying your view
1.5 Closing, Cleaning up
1.6 The Final Product
1.7 Why the Change from Previous Versions?
Adding Views Tutorial for Azureus [Link]
Prerequisite For This Tutorial
You must already know how to make a plugin. You must ignore my spelling
mistakes.
Attach your plugin to an UIInstance
In [Link], the UI Plugin Interface has moved to
[Link], and a new way to access it was introduced. In
order to interact with the UI, you must listen to the UIManagerListener. So, in
your initialize method, gain a reference to the UIManager and call addUIListener with a
UIManagerListener:
1 de 6 01/03/11 14:33
Plugin SWT Main View Tutorial - VuzeWiki [Link]
public class SampleMainViewPlugin implements UnloadablePlugin {
public void initialize(PluginInterface pluginInterface)
throws PluginException {
// Get notified when the UI is attached
[Link]().addUIListener(new UIManagerListener() {
public void UIAttached(UIInstance instance) {
if (instance instanceof UISWTInstance) {
UISWTInstance swtInstance = ((UISWTInstance) instance);
// Do code here
}
}
public void UIDetached(UIInstance instance) {
}
});
}
public void unload() throws PluginException {
}
}
is called when any UI interface is attached. In this
UIAttached(UIInstance instance)
tutorial, we are only interested in the SWT interface, so we check if it's a
UISWTInstance, and go from there.
Create a class implementing UISWTViewEventListener
To add a view, you must implement an UISWTViewEventListener class and hook it into
the plugin interface by either calling addView or openMainView. UISWTViewEventListener only
has one trigger, eventOccurred which passes you an UISWTViewEvent. From UISWTViewEvent,
you can get the type of event that was triggered, and access to the UISWTView
object, which gives you control over the view.
The first event your listener will recieve is the TYPE_CREATE event. If you are wanting
to allow your view to have multiple instances, return true. If you want only one
instance of your view at a time, return false. If you want to use AWT objects, now
is the only time to call UISWTView#setControlType(CONTROLTYPE_AWT)
The UISWTView object will always be the same for the same instance of the view, so,
storing it for later use and comparison is acceptable.
The second event sent will be TYPE_INITIALIZE. This is where you create your view.
The getData() function returns a Composite (or an AWT Component) for you to place
your objects into. Go wild.
If you have SWT objects that require frequent update, it's recommended that you
listen to the TYPE_REFRESH event and do your updating then (rather than making
your own timer). It will be called on the GUI thread, and the interval is linked to
the user's preference.
2 de 6 01/03/11 14:33
Plugin SWT Main View Tutorial - VuzeWiki [Link]
For our example, we'll make 2 labels and a button. One label will say welcome,
and change when the button is pressed. The other label will have an
incrementing number, updated on refresh.
3 de 6 01/03/11 14:33
Plugin SWT Main View Tutorial - VuzeWiki [Link]
private class ViewListener implements UISWTViewEventListener {
UISWTView view = null;
Label funLabel;
int iIndex = 0;
public boolean eventOccurred(UISWTViewEvent event) {
switch ([Link]()) {
case UISWTViewEvent.TYPE_CREATE:
[Link]("TYPE_CREATE Called");
/* We only want one view
*
* If we wanted multiple views, we would need a class to handle
* one view. Then, we could set up a Map, with the key
* being the UISWTView, and the value being a new instance of the
* class. When the other types of events are called, we would
* lookup our class using getView(), and then pass the work to
* the our class.
*/
if (view != null)
return false;
view = [Link]();
break;
case UISWTViewEvent.TYPE_INITIALIZE:
[Link]("TYPE_INITIALIZE Called");
initialize((Composite) [Link]());
break;
case UISWTViewEvent.TYPE_REFRESH:
refresh();
break;
case UISWTViewEvent.TYPE_DESTROY:
[Link]("TYPE_DESTROY Called");
view = null;
break;
case UISWTViewEvent.TYPE_DATASOURCE_CHANGED:
[Link]("TYPE_DATASOURCE_CHANGED Called");
break;
case UISWTViewEvent.TYPE_FOCUSGAINED:
[Link]("TYPE_FOCUSGAINED Called");
break;
case UISWTViewEvent.TYPE_FOCUSLOST:
[Link]("TYPE_FOCUSLOST Called");
break;
case UISWTViewEvent.TYPE_LANGUAGEUPDATE:
[Link]("TYPE_LANGUAGEUPDATE Called "
+ [Link]().toString());
break;
}
return true;
}
private void initialize(Composite parent) {
final Label lbl = new Label(parent, [Link]);
[Link]("Welcome to my View!");
[Link](new GridData(GridData.FILL_HORIZONTAL));
funLabel = new Label(parent, [Link]);
[Link](new GridData(GridData.FILL_HORIZONTAL));
4 de 6 Button button = new Button(parent, [Link]); 01/03/11 14:33
[Link]("Push me");
[Link](new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
[Link]("My plugin ID is " + [Link]());
Plugin SWT Main View Tutorial - VuzeWiki [Link]
Displaying your view
Now that we have an UISWTViewEventListener, we need to hook it to the plugin API.
addView will add your view, but not open it. openMainView will open your view in
Azureus' main window, but not add it to the menu. Both of these methods require
you to supply an ID for your view. This ID is used to retrieve text from your
resource bundle. "[Link]." + ID + ".title" will be used for the menu item
and the tab display.
For this example, we want to add it to the menu, and open it immediately (on
startup), so we call both with the same UISWTViewEventListener.
private static final String VIEWID = "[Link]";
private ViewListener viewListener = null;
private UISWTInstance swtInstance = null;
public void initialize(PluginInterface pluginInterface)
throws PluginException {
// Get notified when the UI is attached
[Link]().addUIListener(new UIManagerListener() {
public void UIAttached(UIInstance instance) {
if (instance instanceof UISWTInstance) {
swtInstance = ((UISWTInstance) instance);
viewListener = new ViewListener();
if (viewListener != null) {
// Add it to the menu
[Link](UISWTInstance.VIEW_MAIN, VIEWID, viewListener);
// Open it immediately
[Link](VIEWID, viewListener, null);
}
}
}
public void UIDetached(UIInstance instance) {
if (instance instanceof UISWTInstance)
swtInstance = null;
}
});
}
Closing, Cleaning up
Finally, since we've made this plugin unloadable, we want to close our view when
unload is called. Calling UISWTInstance#removeViews will remove all instances of your
view, and remove the menu item.
5 de 6 01/03/11 14:33
Plugin SWT Main View Tutorial - VuzeWiki [Link]
public void unload() throws PluginException {
if (swtInstance != null)
[Link](UISWTInstance.VIEW_MAIN, VIEWID);
}
If you wanted to just close the view (say, with a button), use UISWTView#closeView().
For this example, we'd add the button inside the initialize method,:
Button buttonClose = new Button(parent, [Link]);
[Link]("Close");
[Link](new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
[Link]();
}
});
There is also a TYPE_DESTROY event, which is triggered when a view is closed (either
by you, azureus, or the user). Put any variable cleanup code, such as releasing
GUI resource, there.
The Final Product
You can download the plugin, with source, with the link provided below. It will
only run with B45 and later.
Sample Main View Plugin Details Page ([Link]
/plugin_details.php?plugin=SampleMainView1)
Why the Change from Previous Versions?
The way to create plugin views was changed from extending a class to listeners
and interface classes. Listeners and Interface classes are prefered because
functionality can be added to Interface classes without breaking any plugins. As
well, listeners (or just new event types) can be added without breaking plugins.
The second reason was to get away from introducing plugins to the non-plugin
class, AbstractIView, which introduced them to AEMonitor and IndentWriter,
both of which are also non-plugin classes.
Retrieved from "[Link]
Category: Technical Information
This page was last modified on 20 February 2006, at 00:32.
6 de 6 01/03/11 14:33