blob: 54550662adefc67f48abcb67cb1812ec0fe07bf2 [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2007 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.rcp.ui;
import java.io.IOException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.epf.common.utils.CommandLineRunUtil;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
/**
* This class controls all aspects of the application's execution.
*
* @author Kelvin Low
* @since 1.0
*/
public class MainApplication implements IApplication {
/*
* (non-Javadoc)
*
* @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
*/
public Object start(IApplicationContext context) {
Display display = PlatformUI.createDisplay();
Shell shell = new Shell(display, SWT.ON_TOP);
try {
boolean noLock = false;
String[] rmcArgs = Platform.getApplicationArgs();
for (int i = 0; i < rmcArgs.length; i++) {
if (rmcArgs[i].equalsIgnoreCase("-nolock")) { //$NON-NLS-1$
noLock = true;
} else if (rmcArgs[i].equalsIgnoreCase("-batch")) { //$NON-NLS-1$
CommandLineRunUtil.getInstance().setNeedToRun(true);
}
}
if (!noLock && !checkWorkspaceLock(shell)) {
Platform.endSplash();
return IApplication.EXIT_OK;
}
int returnCode = createWorkbenchAdvisor(display);
if (returnCode == PlatformUI.RETURN_RESTART) {
return IApplication.EXIT_RESTART;
}
return IApplication.EXIT_OK;
} finally {
if (shell != null) {
shell.dispose();
}
display.dispose();
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.equinox.app.IApplication#stop()
*/
public void stop() {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null)
return;
final Display display = workbench.getDisplay();
display.syncExec(new Runnable() {
public void run() {
if (!display.isDisposed()) {
workbench.close();
}
}
});
}
/**
* Creates the workbench advisor.
*/
protected int createWorkbenchAdvisor(Display display) {
return PlatformUI.createAndRunWorkbench(display,
new MainWorkbenchAdvisor());
}
/**
* Checks the workspace lock that is used to prevent a library been opened
* multiple times.
*/
protected boolean checkWorkspaceLock(Shell shell) {
Location workspaceLocation = Platform.getInstanceLocation();
String appName = Platform.getProduct().getName();
try {
if (workspaceLocation.lock()) {
return true;
}
MessageDialog.openInformation(shell, NLS.bind(
RCPUIResources.workspaceCannotLockTitle, appName), NLS
.bind(RCPUIResources.workspaceCannotLockMessage, appName));
} catch (IOException e) {
RCPUIPlugin.getDefault().getLogger().logError(
NLS
.bind(RCPUIResources.workspaceCannotLockMessage,
appName), e);
MessageDialog.openInformation(shell, NLS.bind(
RCPUIResources.workspaceCannotLockTitle, appName), e
.getMessage());
}
return false;
}
}