NOTE: If you followed the steps in Creating your extension on the IBM Director Server, then you already know
how to create classes, folders, and files in Eclipse.
Use the following topics to create an interactive task:
The task frame is your main class for an interactive task. This class runs inside the IBM Director Console and it allows you to display a dialog to the user. The idea is that you create a class that extends TWGTaskFrame, implementing a few key methods. When the user activates the task, the code in the IBM Director Console will create an instance of your task frame and then call your code.
Under the source directory, create a new class in the com.bobco package. (Right-click the package and select New->Class. Enter the classname and ensure that the class extends com.tivoli.twg.console.TWGTaskFrame and that it implements ActionListener. The class com.tivoli.twg.console.TWGTaskFrame is always the base class for IBM Director tasks that have a user interface. Implementing ActionListener will allow your code to react to button clicks, keystrokes, and so forth, in order to interact with the user.
Create a new file under the folder deploy/classes/com/bobco ensure that the file extension is .properties. For our example, we will call the file BobCoInteractiveTask.properties
Again, task properties files can be very complex and powerful but most interactive tasks can be defined simply by using only these properties:
Property | How to set the property |
TaskID | This is a unique ID for the task. To avoid naming collisions with other extensions, the naming convention BobCo|Interactive where BobCo represents our example extension and the "Interactive" part of the string is a unique ID within our example extension. |
ParentTaskID | This is the TaskID that was specified in the task properties file for the parent task. In our example: BobCo|Parent |
Title | This is a key to the task title within the resource bundle. In our example: InteractiveTaskTitle. We will add this string to the resource bundle later. |
ResourceBundle | The same resource bundle that contains the translated strings for the task. In our case, we have only one resoruce bundle for the extension: com.bobco.BobCoResourceBundle |
GUI | This is the string "class:" followed by the name of the task frame class (the one that extends TWGTaskFrame) In our example, class:com.bobco.BobCoTaskFrame |
Icon.Large Icon.Large.Selected Icon.Small Icon.Small.Selected |
These are the images to use for the IBM Director Console. To save time, we will reuse the icons from the parent task. Note that for child tasks, you do not need a toolbar icon. |
Subtask.0.ID | Internally, IBM Director actually represents a task with a combination of two classes: TWGTask and TWGSubTask. It is possible to create a TWGTask with two TWGSubtasks,but, for now, the differences between the two classes do not matter. All that is important is that we have to fill in a few properties. Subtask.0.ID must be unique within the task; because we have only one task, it does not matter. Set it to InteractiveSubtask |
Subtask.0.Context | Set this to "interactive:true | server:false |
targeted:none" The "|" characters are separators. In this line, we are specifying three things: 1. Interactive:true - 2. server:false - This means that we do not want to start any extra code on the IBM Director Server for this Task 3. targeted:none - This means that it is not necessary to select a Managed Object before activating the task. |
Subtask.0.Actions | Set this to double-click. This means that the user will activate this task by double clicking on the task icon. |
The final BobCoInteractiveTask.properties file will look like this:
# The task identifier uniquely identifies the task # from all other tasks. # Notice the naming of the TaskId to avoid ID conflicts # with other extensions. TaskID = BobCo|Interactive ParentTaskID = BobCo|Parent # The Title property specifies the title to use on the # Console. It is a key into the Resource Bundle. Title = InteractiveTaskTitle ResourceBundle = com.bobco.BobCoResourceBundle GUI = class:com.bobco.BobCoTaskFrame helpTopicsMapping = com.bobco.BobCoHelp # Icons for the IBM Director Console # 32x32 bit images Icon.Large = /com/bobco/images/bobco32.gif Icon.Large.Selected = /com/bobco/images/bobco32.gif # 16x16 bit images Icon.Small = /com/bobco/images/bobco16.gif Icon.Small.Selected = /com/bobco/images/bobco16.gif Subtask.0.ID = InteractiveSubtask Subtask.0.Context = interactive:true | server:false | targeted:none Subtask.0.Actions = double-click
To make a basic user interface, you will override and implement these
methods:
Method | Description |
public BobCoTaskFrame() | This is the constructor for the task frame. Remember, you never
actually call
new BobCoTaskFrame();because the IBM Director Console will do that for you when the user activates the task. |
public Container buildView() | This class is where you create your GUI components using Swing. Important note: Even thought the return type says Container, you should always return a JPanel from buildView(). |
public Dimension getFrameSize() | This method sets the initial size for the task frame's window. This is used the very first time a user activates your task. If a user resizes the window, then the IBM Director Console will remember their preferred size for the next activation. |
public boolean pInit() | This method is called after the constructor.The important thing to know about this method is that if you return false then the task frame will be closed before it is shown to the user. This is very useful if you want to launch another program on the IBM Director Console without displaying your own window. |
public void actionPerformed(ActionEvent evt) | This is the normal Java method for ActionListener. It will be called when the user takes and action on one of your components that were created in the buildView() method. |
Let's look at the methods one at a time:
Since we are writing the extension to be ready to translate, we need to get our displayable strings from the extension's resource bundle. We will load the resource bundle in the constructor. Here is the code:
public BobCoTaskFrame() { super(true); resourceBundle = ResourceBundle.getBundle("com.bobco.BobCoResourceBundle", Locale.getDefault()); }
Now we need to add a data member for the class for the resource bundle:
private ResourceBundle resourceBundle;
Because we want to display our window, we must return true from this method. Since this method is called before our window is shown to the user, we can do some initialization here. In this case, we will set the window title by invoking the setTitle() method. Notice that we used the resource bundle to get the actual displayable string. Later, we will edit the resource bundle to add the "InteractiveTaskTitle" string.
public boolean pInit() { setTitle(resourceBundle.getString("InteractiveTaskTitle")); return true; }
buildView() is where we create our GUI components. In this simple case, we will create a JPanel with a JLabel and a Cancel JButton. Notice that, as expected, we added our task frame as a listener to the Cancel JButton.
Here's the Code
public Container buildView() { // create the panel JPanel returnPanel = new JPanel(); // create the text message and add it to the center returnPanel.setLayout(new BorderLayout()); String message = resourceBundle.getString("HelloMsg"); returnPanel.add("Center", new JLabel(message)); // Create cancel button and add to a button panel at the bottom. JPanel buttonPane = new JPanel(); JButton cancelBtn = new JButton("Cancel"); buttonPane.add(cancelBtn); cancelBtn.addActionListener(this); returnPanel.add("South", buttonPane); return (returnPanel); }
This will set the window size the first time that the task frame is displayed. Return a dimension object as follows:
public Dimension getFrameSize() { return new Dimension(200, 240); }
This will be called with the user clicks the Cancel JButton. All we need to do is to remove the task frame window from the screen. We do that by calling the consoleCancel() method:
public void actionPerformed(ActionEvent evt) { consoleCancel(); }
We referenced some new strings in the resource bundle that we must now create. Edit the BobCoResourceBundle.class file and add strings for "HelloMsg" and "InteractiveTaskTitle"
The BobCoResourceBundle.java file will now have contents that look like this:
public class BobCoResourceBundle extends ListResourceBundle { static Object[][] contents = { { "BobCoExtension", "Bob Company Extension" }, { "BobCo", "Bob Company, Inc." }, { "MainTaskTitle", "Bob Tasks" }, { "HelloMsg", "Hello From Bob Co" }, { "InteractiveTaskTitle", "Bob Co Interactive Task" } };
All that remains now is to put the call to new TWGDefaultTask() in the extension class. Edit the BobCoExtension.java file and add this code to the initInstances() method:
/* Create the Parent task if it does not already exist*/ if ( ! TWGTask.isTaskID( BOB_CO_INTERACTIVE_TASK_ID ) ) { try { TWGDefaultTask testTask = new TWGDefaultTask("com/bobco/BobCoInteractiveTask.properties"); } catch ( TWGTaskCreationException exception ) { TWGRas.error( TWGRas.EX0, "BobCoExtension.InitClassInstances: exception!", exception ); /* Throwing an exception will stop the entire IBM Director Server */ throw new TWGExtensionInitException( "unable to create BobCo Interactive Task" ); } }
Remember that tasks are persistent, so we have to add the
if ( ! TWGTask.isTaskID( BOB_CO_INTERACTIVE_TASK_ID ) )
statement to ensure that the task does not already exist before we try to create
it. Also, remember that we have to catch the TWGTaskCreationException
and log a TWGRas error so that we can debug the problem if we have done
something incorrectly.
You should be able to double click on the Bob Co Interactive Task and see the task frame window. It should look like this:
Once the task window is working, you will want to procede to Adding help pages.