/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * 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: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.workbench.internal; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProduct; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialogWithToggle; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.FileTransfer; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IPageListener; import org.eclipse.ui.IPartListener2; import org.eclipse.ui.IPerspectiveDescriptor; import org.eclipse.ui.IPerspectiveRegistry; import org.eclipse.ui.IPropertyListener; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPartConstants; import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PerspectiveAdapter; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.WorkbenchException; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchWindowAdvisor; import org.eclipse.ui.internal.ide.AboutInfo; import org.eclipse.ui.internal.ide.EditorAreaDropAdapter; import org.eclipse.ui.internal.ide.IDEInternalPreferences; import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; import org.eclipse.ui.internal.ide.dialogs.WelcomeEditorInput; import org.eclipse.ui.part.EditorInputTransfer; import org.eclipse.ui.part.MarkerTransfer; import org.eclipse.ui.part.ResourceTransfer; import org.eclipse.ui.statushandlers.StatusManager; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; import org.osgi.framework.ServiceReference; import org.simantics.db.management.ISessionContextChangedListener; import org.simantics.db.management.ISessionContextProvider; import org.simantics.db.management.SessionContextChangedEvent; import org.simantics.ui.SimanticsUI; import org.simantics.ui.workbench.WorkbenchShutdownService; /** * System properties used to configure how this class works: *
org.simantics.workbench.application.showProgressIndicator=true|false (default=true)
- Used to initialize IWorkbenchWindowConfigurer.setShowProgressIndicatororg.simantics.workbench.application.showFastViewBars=true|false (default=true)
- Used to initialize IWorkbenchWindowConfigurer.setShowFastViewBarsorg.simantics.workbench.application.showPerspectiveBar=true|false (default=true)
- Used to initialize IWorkbenchWindowConfigurer.setShowPerspectiveBarorg.simantics.workbench.application.excludePerspectiveFromTitle=true|false (default=false)
- Used to exclude perspective name in {@link #computeTitle()}true
if OK to exit, false
if the user
* canceled
* @since 3.6
*/
static boolean promptOnExit(Shell parentShell) {
IPreferenceStore store = IDEWorkbenchPlugin.getDefault()
.getPreferenceStore();
boolean promptOnExit = store
.getBoolean(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW);
if (promptOnExit) {
if (parentShell == null) {
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (workbenchWindow != null) {
parentShell = workbenchWindow.getShell();
}
}
if (parentShell != null) {
parentShell.setMinimized(false);
parentShell.forceActive();
}
String message;
String productName = null;
IProduct product = Platform.getProduct();
if (product != null) {
productName = product.getName();
}
if (productName == null) {
message = IDEWorkbenchMessages.PromptOnExitDialog_message0;
} else {
message = NLS.bind(
IDEWorkbenchMessages.PromptOnExitDialog_message1,
productName);
}
MessageDialogWithToggle dlg = MessageDialogWithToggle
.openOkCancelConfirm(parentShell,
IDEWorkbenchMessages.PromptOnExitDialog_shellTitle,
message,
IDEWorkbenchMessages.PromptOnExitDialog_choice,
false, null, null);
if (dlg.getReturnCode() != IDialogConstants.OK_ID) {
return false;
}
if (dlg.getToggleState()) {
store
.setValue(
IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW,
false);
IDEWorkbenchPlugin.getDefault().savePluginPreferences();
}
}
return true;
}
/* (non-Javadoc)
* @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowOpen
*/
@Override
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
boolean showProgressIndicator = !"false".equals(System.getProperty("org.simantics.workbench.application.showProgressIndicator", "true").toLowerCase());
boolean showPerspectiveBar = !"false".equals(System.getProperty("org.simantics.workbench.application.showPerspectiveBar", "true").toLowerCase());
// show the shortcut bar and progress indicator, which are hidden by default
configurer.setShowProgressIndicator(showProgressIndicator);
configurer.setShowPerspectiveBar(showPerspectiveBar);
// add the drag and drop support for the editor area
configurer.addEditorAreaTransfer(EditorInputTransfer.getInstance());
configurer.addEditorAreaTransfer(ResourceTransfer.getInstance());
configurer.addEditorAreaTransfer(FileTransfer.getInstance());
configurer.addEditorAreaTransfer(MarkerTransfer.getInstance());
configurer.configureEditorAreaDropListener(new EditorAreaDropAdapter(
configurer.getWindow()));
hookTitleUpdateListeners(configurer);
}
/**
* Hooks the listeners needed on the window
* @param configurer
*/
private void hookTitleUpdateListeners(IWorkbenchWindowConfigurer configurer) {
// hook up the listeners to update the window title
configurer.getWindow().addPageListener(new IPageListener() {
@Override
public void pageActivated(IWorkbenchPage page) {
updateTitle(false);
}
@Override
public void pageClosed(IWorkbenchPage page) {
updateTitle(false);
}
@Override
public void pageOpened(IWorkbenchPage page) {
// do nothing
}
});
configurer.getWindow().addPerspectiveListener(
new PerspectiveAdapter() {
@Override
public void perspectiveActivated(IWorkbenchPage page,
IPerspectiveDescriptor perspective) {
updateTitle(false);
}
@Override
public void perspectiveSavedAs(IWorkbenchPage page,IPerspectiveDescriptor oldPerspective,IPerspectiveDescriptor newPerspective){
updateTitle(false);
}
@Override
public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
updateTitle(false);
}
});
configurer.getWindow().getPartService().addPartListener(
new IPartListener2() {
@Override
public void partActivated(IWorkbenchPartReference ref) {
if (ref instanceof IEditorReference) {
updateTitle(false);
}
}
@Override
public void partBroughtToTop(IWorkbenchPartReference ref) {
if (ref instanceof IEditorReference) {
updateTitle(false);
}
}
@Override
public void partClosed(IWorkbenchPartReference ref) {
updateTitle(false);
}
@Override
public void partDeactivated(IWorkbenchPartReference ref) {
// do nothing
}
@Override
public void partOpened(IWorkbenchPartReference ref) {
// do nothing
}
@Override
public void partHidden(IWorkbenchPartReference ref) {
if (ref.getPart(false) == lastActiveEditor
&& lastActiveEditor != null) {
updateTitle(true);
}
}
@Override
public void partVisible(IWorkbenchPartReference ref) {
if (ref.getPart(false) == lastActiveEditor
&& lastActiveEditor != null) {
updateTitle(false);
}
}
@Override
public void partInputChanged(IWorkbenchPartReference ref) {
// do nothing
}
});
// Update the title when the active UI database session is changed.
ISessionContextProvider provider = SimanticsUI.getSessionContextProvider(configurer.getWindow());
provider.addContextChangedListener(new ISessionContextChangedListener() {
@Override
public void sessionContextChanged(SessionContextChangedEvent event) {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
IWorkbenchWindow window = configurer.getWindow();
if (window == null)
return;
Shell shell = window.getShell();
if (shell == null || shell.isDisposed())
return;
shell.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
updateTitle(true);
}
});
}
});
// Listen for changes of the workspace name.
propertyChangeListener = new IPropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
String property = event.getProperty();
if (IDEInternalPreferences.WORKSPACE_NAME.equals(property)
|| IDEInternalPreferences.SHOW_LOCATION.equals(property)) {
// Make sure the title is actually updated by
// setting last active page.
lastActivePage = null;
updateTitle(false);
}
}
};
IDEWorkbenchPlugin.getDefault().getPreferenceStore()
.addPropertyChangeListener(propertyChangeListener);
}
protected String computeTitle() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
IWorkbenchPage currentPage = configurer.getWindow().getActivePage();
IEditorPart activeEditor = null;
if (currentPage != null) {
activeEditor = lastActiveEditor;
}
String title = null;
IProduct product = Platform.getProduct();
if (product != null) {
title = product.getName();
}
if (title == null) {
title = ""; //$NON-NLS-1$
}
// ISessionContext ctx = SimanticsUI.getSessionContext(configurer.getWindow());
// if (ctx != null) {
// ServerAddress addr = ctx.getAddress();
// if (addr != null) {
// title += " - Connected to " + addr.getAddress().getHostName();//InetAddressUtils.toHostString(addr);
// }
// Session session = ctx.getSession();
// if (session != null) {
// SessionUserSupport sessionUser = session.getService(SessionUserSupport.class);
// final Resource user = sessionUser.getUser();
// if (user != null) {
// String userName;
// try {
// userName = session.syncRequest(new Read