blob: 7f4ddd146468cf18f8b8664dbd1d76f4a01ebf9c [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 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.library.ui;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.epf.common.serviceability.MsgDialog;
import org.eclipse.epf.library.services.LibraryProcessor;
import org.eclipse.epf.library.ui.actions.ConfigurationContributionItem;
import org.eclipse.epf.library.ui.dialogs.OpenLibraryDialog;
import org.eclipse.epf.library.ui.dialogs.SelectLibraryDirectoryDialog;
import org.eclipse.epf.library.ui.preferences.LibraryUIPreferences;
import org.eclipse.epf.persistence.MultiFileSaveUtil;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.internal.WorkbenchWindow;
/**
* The Library UI Manager.
*
* @author Kelvin Low
* @author Phong Nguyen Le
* @since 1.0
*/
public class LibraryUIManager {
private static final String TOOLBAR_DUMMY_CONTRIBUTION_ID = "toolbar.dummy.contribution"; //$NON-NLS-1$
private static final String TOOLBAR_CONFIG_CONTRIBUTION_ID = "toolbar.config.contribution"; //$NON-NLS-1$
private static final String OPEN_LIBRARY_TITLE = LibraryUIResources
.getString("LibraryUI.openLibraryDialog.title"); //$NON-NLS-1$
private static final String UPGRADE_LIBRARY_TITLE = LibraryUIResources
.getString("LibraryUI.upgradeLibraryDialog.title"); //$NON-NLS-1$
private static final String UPGRADE_LIBRARY_ERROR_MSG = LibraryUIResources
.getString("LibraryUI.upgradeLibraryError.msg"); //$NON-NLS-1$
private static LibraryUIManager instance = null;
private static String cmdLineLibPath = null;
/**
* Returns the singleton instance.
*/
public static LibraryUIManager getInstance() {
if (instance == null) {
synchronized (LibraryUIManager.class) {
if (instance == null) {
instance = new LibraryUIManager();
}
}
}
return instance;
}
/**
* Private default constructor to prevent this class from being
* instantiated.
*/
private LibraryUIManager() {
addConfigurationContribution();
}
/**
* Prompts the user to select a Method Library.
*/
public void promptForMethodLibrary() {
// Retrieve the Library path that was saved in a previous session.
String libPath = LibraryUIPreferences.getSavedLibraryPath();
if (libPath == null || libPath.length() == 0) {
if (cmdLineLibPath != null) {
libPath = cmdLineLibPath;
}
}
// Prompt the user to select a Method Library folder if:
// (1) The saved or command line Library path is invalid AND
// (2) The prompt for Method Library at startup preference is set to
// true (the default)
if (LibraryProcessor.isValidLibrary(libPath, true) == Status.OK_STATUS
&& !LibraryUIPreferences.getPromptForMethodLibraryAtStartup()
&& !LibraryProcessor.getInstance().isLibraryLocked(libPath)) {
openLibrary(libPath);
return;
}
// Open a dialog to prompt the user to select a Method Librray folder.
if (LibraryProcessor.isValidLibrary(libPath) != Status.OK_STATUS) {
libPath = LibraryUIPreferences.getDefaultLibraryPath();
}
Shell shell = Display.getCurrent().getActiveShell();
OpenLibraryDialog dialog = new OpenLibraryDialog(shell, libPath);
while (dialog.open() == Window.OK) {
libPath = dialog.getLibraryPath();
libPath = toAbsoluteLibraryPath(libPath);
if (LibraryProcessor.isValidLibrary(libPath, true) == Status.OK_STATUS) {
if (LibraryProcessor.getInstance().isLibraryLocked(libPath)) {
if (displayLibraryLockedMessage() != 0)
continue;
}
String libFile = LibraryProcessor.getLibraryModelFile(libPath);
if (openLibrary(libPath)) {
return;
}
} else {
MsgDialog msgDialog = LibraryUIPlugin.getDefault()
.getMsgDialog();
boolean rc = msgDialog
.displayConfirmation(
LibraryUIResources
.getString("LibraryUI.openLibraryDialog.title"), //$NON-NLS-1$
LibraryUIResources
.formatString(
"LibraryUI.openLibraryDialog.newLibrary.text", libPath)); //$NON-NLS-1$
if (!rc)
continue;
if (createLibrary(libPath)) {
return;
}
}
LibraryUIPlugin
.getDefault()
.getMsgDialog()
.displayError(
OPEN_LIBRARY_TITLE,
LibraryUIResources
.getString("LibraryUI.invalidLibraryPathError.msg"), //$NON-NLS-1$
LibraryUIResources
.getString("LibraryUI.invalidLibraryPathError.reason")); //$NON-NLS-1$
}
;
// if dialog is closed with the OK buttonbeing clicked -- treat it as
// cancel
System.exit(0);
}
/**
* Opens a Method Library.
*/
public void openLibrary() {
SelectLibraryDirectoryDialog dialog = new SelectLibraryDirectoryDialog(
Display.getCurrent().getActiveShell());
String libPath = LibraryUIPreferences.getSavedLibraryPath();
dialog.setFilterPath(libPath);
do {
libPath = dialog.open();
if (libPath == null) {
return;
}
if (LibraryProcessor.isValidLibrary(libPath, true) == Status.OK_STATUS) {
if (LibraryProcessor.getInstance().isLibraryLocked(libPath)) {
if (displayLibraryLockedMessage() != 0)
continue;
}
String libFile = LibraryProcessor.getLibraryModelFile(libPath);
if (openLibrary(libPath)) {
return;
}
}
MsgDialog msgDialog = LibraryUIPlugin.getDefault().getMsgDialog();
msgDialog
.displayError(
OPEN_LIBRARY_TITLE,
LibraryUIResources
.getString("LibraryUI.invalidLibraryPathError.msg"), //$NON-NLS-1$
LibraryUIResources
.getString("LibraryUI.invalidLibraryPathError.reason")); //$NON-NLS-1$
} while (libPath != null);
}
/**
* Opens a Method Library given the Library path.
*
* @param path
* Path to a Method Library.
* @return <code>true</code> if the Method Library is opened successfully.
*/
public boolean openLibrary(final String path) {
Shell shell = Display.getCurrent().getActiveShell();
final String libFile = LibraryProcessor.getLibraryModelFile(path);
final List errors = new ArrayList();
final IStatus[] status = new IStatus[1];
// Do the work within an operation because this is a long running
// activity that modifies the workspace.
WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
public void execute(IProgressMonitor monitor) {
String taskText = LibraryUIResources
.getString("LibraryUI.openingLibraryTask.name"); //$NON-NLS-1$
monitor.beginTask(taskText, 2);
try {
monitor.setTaskName(taskText);
monitor.worked(1);
status[0] = LibraryProcessor.getInstance().openLibrary(
libFile);
LibraryUIPreferences.setSavedLibraryPath(path);
} catch (Exception e) {
if (!(e instanceof IOException && e.getMessage()
.startsWith("###"))) { //$NON-NLS-1$
LibraryUIPlugin.getDefault().getLogger().logError(e);
}
errors.add(e);
} finally {
monitor.done();
}
}
};
try {
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell) {
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText(OPEN_LIBRARY_TITLE);
}
};
dialog.run(true, false, operation);
if (errors.isEmpty()) {
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (workbenchWindow != null) {
IWorkbenchPage activePage = workbenchWindow.getActivePage();
if (activePage != null) {
activePage.closeAllEditors(false);
}
}
return true;
} else {
Iterator iter = errors.iterator();
while (iter.hasNext()) {
Exception e = (Exception) iter.next();
if (e instanceof IOException) {
String message = e.getMessage();
if (message.startsWith("###")) { //$NON-NLS-1$
String projectFileName = message.substring(3);
String prompt = LibraryUIResources
.formatString(
"LibraryUI.readOnlyProjectFile.text", projectFileName); //$NON-NLS-1$
String[] buttonLabels = {
LibraryUIResources
.getString("LibraryUI.retryButton.text"), //$NON-NLS-1$
LibraryUIResources
.getString("LibraryUI.cancelButton.text") //$NON-NLS-1$
};
MessageDialog msgBox = new MessageDialog(Display
.getCurrent().getActiveShell(),
OPEN_LIBRARY_TITLE, null, prompt,
MessageDialog.WARNING, buttonLabels, 0);
if (msgBox.open() == 0) {
return openLibrary(path);
} else {
return true;
}
}
}
}
}
} catch (Exception e) {
LibraryUIPlugin.getDefault().getLogger().logError(e);
}
return false;
}
/**
* Creates and opens a new Method Library.
*
* @param path
* The Method Library path.
* @return <code>true</code> if the Method Library is opened successfully.
*/
public boolean createLibrary(String path) {
try {
File libraryPath = new File(path);
if (!libraryPath.exists()) {
libraryPath.mkdirs();
}
LibraryProcessor.getInstance().newLibrary(libraryPath.getName(),
path, true);
LibraryUIPreferences.setSavedLibraryPath(path);
return true;
} catch (Exception e) {
return false;
}
}
/**
* Adds the Configuration contribution to the workbench toolbar.
*/
public void addConfigurationContribution() {
IWorkbench workbench = LibraryUIPlugin.getDefault().getWorkbench();
if (workbench != null) {
WorkbenchWindow window = (WorkbenchWindow) workbench
.getActiveWorkbenchWindow();
if (window != null) {
// Check to see if the configuration contribution already
// exists.
ICoolBarManager coolBar = window.getCoolBarManager();
IContributionItem marker = null;
IContributionItem coolBarItem = coolBar
.find(TOOLBAR_CONFIG_CONTRIBUTION_ID);
if (coolBarItem != null) {
if (coolBarItem.isVisible()) {
return;
}
IContributionItem[] items = coolBar.getItems();
for (int i = 0; i < items.length; i++) {
if (items[i] == coolBarItem) {
coolBar.remove(TOOLBAR_CONFIG_CONTRIBUTION_ID);
if (i+1 < items.length) {
marker = items[i+1];
}
}
}
}
IToolBarManager toolbarMgr = new ToolBarManager(SWT.FLAT
| SWT.LEFT);
ConfigurationContributionItem testItem = new ConfigurationContributionItem(null);
toolbarMgr.add(testItem);
ToolBarContributionItem contribItem = new ToolBarContributionItem(
toolbarMgr, TOOLBAR_CONFIG_CONTRIBUTION_ID);
if (marker != null) {
coolBar.insertBefore(marker.getId(), contribItem);
}
else {
coolBar.add(contribItem);
}
}
}
}
/**
* Returns the Library path set via the command line option "-library
* <path>".
*/
public static String getCommandLineLibraryPath() {
return cmdLineLibPath;
}
/**
* Sets the Library path via the command line option "-library <path>".
*/
public static void setCommandLineLibraryPath(String libPath) {
IPath path = Path.fromOSString(libPath);
if (!path.isAbsolute()) {
// TODO: Review implementation.
cmdLineLibPath = System.getProperty("user.dir") + File.separator + libPath; //$NON-NLS-1$
}
if (LibraryProcessor.isValidLibrary(cmdLineLibPath, true) != Status.OK_STATUS) {
cmdLineLibPath = null;
}
}
public static String toAbsoluteLibraryPath(String libPath) {
String absPath = libPath;
IPath ecPath = Path.fromOSString(libPath);
if (!ecPath.isAbsolute()) {
// TODO: Review implementation.
absPath = System.getProperty("user.dir") + File.separator + //$NON-NLS-1$
"Method Libraries" + File.separator + libPath; //$NON-NLS-1$
}
return absPath;
}
private static int displayLibraryLockedMessage() {
Shell shell = null;
Image image = null;
try {
shell = LibraryUIPlugin.getDefault().getWorkbench().getDisplay()
.getActiveShell();
image = shell.getImage();
} catch (Exception e) {
}
MessageDialog msgDlg = new MessageDialog(
shell,
OPEN_LIBRARY_TITLE,
image,
LibraryUIResources.getString("LibraryUI.libraryLocked.msg"), //$NON-NLS-1$
MessageDialog.ERROR,
new String[] {
LibraryUIResources
.getString("LibraryUI.libraryLocked.openButton"), IDialogConstants.CANCEL_LABEL }, //$NON-NLS-1$
1);
return msgDlg.open();
}
}