JBuilder ProjectView concepts

General description

The ProjectView is an IDE panel called the project pane that consists of a toolbar and a tree. Any number of projects can be open, but there can be only one active project at a time. The tree provides a hierarchical display of the nodes that make up the currently active project.

Each tree entry is one of the following node types:

If no project is open, then a default project with very limited functionality is active. You can't add a file, package, or folder to the default project, but you can open and edit files. If the default project is active, most wizards either disable themselves, or they automatically invoke the Project wizard to create a project that allows them to add files before the wizard itself appears.

You can't replace the ProjectView using the OpenTools API. Each Browser can have just one instance of ProjectView.


Detailed description of feature/subsystem

Getting a ProjectView reference

Obtain a ProjectView reference from an instance of Browser. To be sure of getting the correct reference, you should attempt to use a Browser reference within your context. If one isn't available, it's usually safe to assume the currently active Browser:
  ProjectView pv = Browser.getActiveBrowser().getProjectView();

Hiding and revealing the ProjectView

The menu command View|Project allows the hiding and revealing of the ProjectView. You can do the same thing through code:
  boolean bVisible = true;
  Browser.getActiveBrowser().getProjectView().setProjectViewVisible(bVisible);

Opening and activating a project

Use setActiveProject() to open a project (if it isn't already) and make it the active project. The getOpenProjects() method of ProjectView returns a list of all open projects.

Adding a node to the project

To add a node to a project, create a node and set its parent. ProjectView has listeners that will detect the parent change and rebuild the tree without any additional help. The following examples use the active project for the parent, but you can use of any PackageNode or FolderNode in the project as the parent.

This example adds a FileNode given only an absolute path:

  String path = "c:/myfile.java";
  ProjectView pv = Browser.getActiveBrowser().getProjectView();
  Project project = pv.getActiveProject();
  Url url = new Url(new File(path));
  Node node = project.getNode(url);
  node.setParent(project);

The following code demonstrates adding a PackageNode to the project given only an absolute path and the root directory from the project source path:

  ProjectView pv = Browser.getActiveBrowser().getProjectView();
  Project project = pv.getActiveProject();

  String projectSourcePath = "c:/myproject/src";
  String packagePath = "c:/myproject/src/com/mypackage";
  String name = packagePath.substring(projectSourcePath.length() + 1);
  name = name.replace(File.separatorChar, '.');
  // name is now in the format "com.mypackage"
  if (project.findNodes(name).length == 0) {
    new PackageNode(project, project, name);
  }

Removing a node from the project

To remove a node from the project, first ensure it is closed, and then reset its parent. Here's an example:
  Browser browser = Browser.getActiveBrowser();
  Node node = browser.getProjectView().getSelectedNode();
  if (node != null) {
    browser.closeNode(node);
    node.setParent(null);
  }

Getting the selected nodes from the ProjectView

The ProjectView tree allows the user to select multiple nodes. You can obtain an array that contains those selected nodes:
  ProjectView pv = Browser.getActiveBrowser().getProjectView();
  Node[] nodes = pv.getSelectedNodes();
  for (int j = 0; j < nodes.length; j++) {
    System.out.println(nodes[j].getLongDisplayName());
  }

Adding an action to the context menu

You can append your own actions to the ProjectView right-click pop-up menu. The following code demonstrates adding a WizardAction. Note that such wizards must be registered using the OpenTools API.
  public static void initOpenTool(byte majorVersion, byte minorVersion) {
    if (majorVersion == PrimeTime.CURRENT_MAJOR_VERSION) {
      ProjectView.registerContextActionProvider(new ContextActionProvider() {
        public Action getContextAction(Browser browser, Node[] nodes) {
          if (browser.getActiveProject() != null) {
            return WIZARD_MyWizard;
          }
          return null;
        }
      });
    }
  }

  public static final WizardAction WIZARD_MyWizard = new WizardAction (
    "My wizard...", 'w', "My ProjectView wizard",
    BrowserIcons.ICON_BLANK,
    BrowserIcons.ICON_BLANK,
      false) {
    protected Wizard createWizard() {
      return new MyWizard();
    }
  };

For more information about writing and registering wizards with the OpenTools API, see JBuilder wizard concepts.

Ensuring a usable project is active

You can't add files to the default project. Therefore, if you are writing a wizard that adds files and the default project is active, your wizard won't be able to function. You can solve this problem in a couple of ways. Your BasicWizard could either disable itself, or it could present the Project wizard and let the user decide whether to create a new project.

The following code demonstrates how to handle the second situation. Your wizard remains enabled, but it invokes the Project wizard to create a new project. If the user fails to create the project, the wizard terminates:

  public WizardPage invokeWizard(WizardHost host) {
    setWizardTitle("My Wizard");

    Project project = host.getBrowser().getProjectView().getActiveUserProject();

    if ((project == null) || !(project instanceof JBProject)) {
      // If not a usable active project, launch Project Wizard
      WizardDialog dialog = new WizardDialog(host.getBrowser(),
        new com.borland.jbuilder.wizard.project.JBProjectWizard());
      dialog.show();
      project = host.getBrowser().getProjectView().getActiveUserProject();
    }

    // If not an active project we can use, abort
    if ((project == null) || !(project instanceof JBProject)) {
      return null;
    }

    JBProject jbProject = (JBProject)project;

    step1 = new MyWizardPage1();
    addWizardPage(step1);

    return super.invokeWizard(host);
  }