Jeevan
Jeevan

Reputation: 477

Eclipse based E3 application, Switch workspace isn't working

I have an Eclipse based e3 application and in it Switch Workspace isn't working properly. Whenever I do that, it switches to the same workspace and not going to the new one.

Before restart is called they are setting a System property for eclipse.exitdata the -data workspace location as variable. But after restart that System variable gets wiped out. In new workspace they are calling

PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());

which internally gets the location from an InternalPlatform class location variable. That InternalPlatform is a .class file and I don't have access to edit it. In InternalPlatform this is the code

public Location getInstanceLocation() {
    assertInitialized();
    return (Location) instanceLocation.getService();
}

/**
 * @see Platform#getLocation()
 */
public IPath getLocation() throws IllegalStateException {
    if (cachedInstanceLocation == null) {
        Location location = getInstanceLocation();
        if (location == null)
            return null;
        //  This makes the assumption that the instance location is a file: URL
        File file = new File(location.getURL().getFile());
        cachedInstanceLocation = new Path(file.toString());
    }
    return cachedInstanceLocation;
}

In this Location location = getInstanceLocation(); gets the workspace location. How and where can I change this variable? Or is there any other way to pass variable for switch workspace? How does switch workspace works in the code?

Upvotes: 1

Views: 250

Answers (1)

s.d
s.d

Reputation: 4185

There's an old but working example of how to switch workspaces at http://hexapixel.com/2009/01/12/rcp-workspaces.

Note: This uses internal Eclipse API, which is discouraged.

Basically do the following.

To enable workspace definition at runtime, put -data @noDefault in the program arguments (.product file/run config). (Guess this would be what you're missing.)

In your Application.java (or whatever you named it) you can then simply do this:

try { 
    Location instanceLoc = Platform.getInstanceLocation();
    instanceLoc.set(new URL("file", null, "/home/temp"), false);
    }
    catch (Exception err) {
        // Do something meaningful with the exception
    } 

This way you can set the workspace manually (e.g., for testing purposes).

To re-use the Eclipse dialogs, implement Application as follows.

public class Application implements IApplication {
    public Object start(IApplicationContext context) {
        Display display = PlatformUI.createDisplay();
        Location instanceLoc = Platform.getInstanceLocation();
        boolean isWorkspaceSelected = false;

        try {
            URL url = new File(System.getProperty(“user.home”), “workspace”).toURI().toURL();
            ChooseWorkspaceData data = new ChooseWorkspaceData(url);
            ChooseWorkspaceDialog dialog = new ChooseWorkspaceDialog(display.getActiveShell(), data, true, true);
            dialog.prompt(true);
            String selection = data.getSelection();
            if (selection != null)  {
                isWorkspaceSelected = true;
                data.writePersistedData();
                url = new File(selection).toURI().toURL();
                instanceLoc.set(url, false);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            if (isWorkspaceSelected)  {
                int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor(display));
                if (returnCode == PlatformUI.RETURN_RESTART) {
                    return IApplication.EXIT_RESTART;
                }
            }
            return IApplication.EXIT_OK;

        } finally {
            display.dispose();
        }
    }
}

Keep in mind though: This uses internal API, i.e., org.eclipse.ui.internal.ide.ChooseWorkspaceData and org.eclipse.ui.internal.ide.ChooseWorkspaceDialog.

Upvotes: 1

Related Questions