# Opening A Workflow Top Component

To open a workflow top component, the developer has two options:

- use the WorkflowUtil class
- extend the AbstractPersistentWorkflowTopComponent class


### Using the WorkflowUtil class

The **first step** is to create an Action class, such as :

DEMOWORKFLOWACTION.JAVA

```
package com.mycompany.myviewer.mydemoworkflow;
import com.interactive.intviewerapi.plugins.workflow.WorkflowUtil;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DemoWorkflowAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String xmlFile = "com/mycompany/myviewer/mydemoworkflow/DemoWorkflowTasks.xml";
        String title = "My Tasks";
        WorkflowUtil.openWorkflowTopComponent(xmlFile, title);
    }
}
```

The **second step** is to reference this action class in the layer.xml file, such as:

LAYER.XML

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
    <folder name="Actions">
        <folder name="ToolsActions">
            <file name="com-mycompany-myviewer-mydemoworkflow-DemoWorkflowAction.instance">
                <attr name="delegate" newvalue="com.mycompany.myviewer.mydemoworkflow.DemoWorkflowAction"/>
                <attr name="displayName" stringvalue="DemoWorkflowDisplayName"/>
                <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
                <attr name="noIconInMenu" boolvalue="false"/>
            </file>
        </folder>
    </folder>
    <folder name="Menu">
        <folder name="Tools">
            <file name="com-mycompany-myviewer-mydemoworkflow-DemoWorkflowAction.shadow">
                <attr name="originalFile" stringvalue="Actions/ToolsActions/com-mycompany-myviewer-mydemoworkflow-DemoWorkflowAction.instance"/>
                <attr name="position" intvalue="0"/>
            </file>
        </folder>
    </folder>
</filesystem>
```

In this example, the menu item is called *DemoWorkflowDisplayName* and is made available from the *Tools* menu. A [walkthrough](/solutions/intviewer/intviewer-tutorial/using-netbeans-swing-components-walkthroughs/adding-a-menu-item-walkthrough) is available detailing the registration of an action class.

Workflow windows created through this mechanism are transient. If you close the workflow window, the state of all associated tasks is lost.

### Extending the AbstractPersistentWorkflowTopComponent class

This mechanism is NOT available in INTViewer 4.0. It leverages the NetBeans windowing system, requiring registrations that are specific to the NetBeans platform.

The main benefit of this method is that the state of the workflow window will be remembered between two consecutive INTViewer sessions: if you open a workflow top component, close INTViewer then reopens INTViewer, the position and size of the workflow window have been retained.

Workflow windows created through this mechanism are persistent: if you close a workflow window, this window is hidden (instead of closed) and the state of all associated tasks is kept until INTViewer itself is closed, or the "Clear All Windows" option is chosen.

NetBeans provides a wizard to create a TopComponent. Follow these three steps to create a workflow top component:

**Step 1:** Right-click a package name and select **New** -> **Window Component**

Select a **Window Position**, click **Next**. The window position **properties** is a good default for workflows. Workflows will be displayed on the top right side of the screen if **properties** is selected. Consult the [Creating and Manipulating TopComponent Windows](/solutions/intviewer/intviewer-apis-for-windows/creating-a-topcomponent-window) section for a list of all available window positions.

**Step 2**: Enter a **Class Name Prefix**

**Step 3:** Customize the TopComponent class to extend AbstractPersistentWorkflowTopComponent, using the following template:

MYTESTWORKFLOWTOPCOMPONENT.JAVA

```
package com.mycompany.myviewer.mydemoworkflow;
import com.interactive.intviewerapi.plugins.workflow.AbstractPersistentWorkflowTopComponent;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
public class MyTestWorkflowTopComponent extends AbstractPersistentWorkflowTopComponent {
    private static MyTestWorkflowTopComponent _instance;
    private static final String PREFERRED_ID = "MyTestWorkflowTopComponent";
    public MyTestWorkflowTopComponent() {
        super(PREFERRED_ID, "com/mycompany/myviewer/mydemoworkflow/DemoWorkflowTasks.xml", "My Tasks");
    }
    public static synchronized MyTestWorkflowTopComponent getDefault() {
        if (_instance == null) {
            _instance = new MyTestWorkflowTopComponent();
        }
        return _instance;
    }
    public static synchronized MyTestWorkflowTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            return getDefault();
        }
        if (win instanceof MyTestWorkflowTopComponent) {
            return (MyTestWorkflowTopComponent) win;
        }
        return getDefault();
    }
}
```

After these 3 steps are complete, the workflow window you just created should be available from the "Window" menu. Remember that only one instance of each workflow top component can be displayed at a time. If you have two workflows, you need to create two components with two different class names and PREFERRED_ID.