]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchWindowAdvisor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / SimanticsWorkbenchWindowAdvisor.java
index 78c4928def89229688507f6682712956094c3d6f..74abd0c35e47d89bca18b3f53da5862f7aeca63a 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.workbench.internal;\r
-\r
-import java.net.URL;\r
-import java.util.ArrayList;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.concurrent.atomic.AtomicBoolean;\r
-\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.eclipse.core.runtime.IProduct;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.jface.action.ToolBarManager;\r
-import org.eclipse.jface.dialogs.ErrorDialog;\r
-import org.eclipse.jface.dialogs.IDialogConstants;\r
-import org.eclipse.jface.dialogs.MessageDialogWithToggle;\r
-import org.eclipse.jface.preference.IPreferenceStore;\r
-import org.eclipse.jface.resource.JFaceResources;\r
-import org.eclipse.jface.util.IPropertyChangeListener;\r
-import org.eclipse.jface.util.PropertyChangeEvent;\r
-import org.eclipse.osgi.util.NLS;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.dnd.FileTransfer;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.layout.GridLayout;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.swt.widgets.ToolBar;\r
-import org.eclipse.ui.IEditorPart;\r
-import org.eclipse.ui.IEditorReference;\r
-import org.eclipse.ui.IPageListener;\r
-import org.eclipse.ui.IPartListener2;\r
-import org.eclipse.ui.IPerspectiveDescriptor;\r
-import org.eclipse.ui.IPerspectiveRegistry;\r
-import org.eclipse.ui.IPropertyListener;\r
-import org.eclipse.ui.IWorkbench;\r
-import org.eclipse.ui.IWorkbenchPage;\r
-import org.eclipse.ui.IWorkbenchPartConstants;\r
-import org.eclipse.ui.IWorkbenchPartReference;\r
-import org.eclipse.ui.IWorkbenchWindow;\r
-import org.eclipse.ui.PartInitException;\r
-import org.eclipse.ui.PerspectiveAdapter;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.WorkbenchException;\r
-import org.eclipse.ui.actions.ActionFactory;\r
-import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;\r
-import org.eclipse.ui.application.ActionBarAdvisor;\r
-import org.eclipse.ui.application.IActionBarConfigurer;\r
-import org.eclipse.ui.application.IWorkbenchWindowConfigurer;\r
-import org.eclipse.ui.application.WorkbenchWindowAdvisor;\r
-import org.eclipse.ui.internal.ide.AboutInfo;\r
-import org.eclipse.ui.internal.ide.EditorAreaDropAdapter;\r
-import org.eclipse.ui.internal.ide.IDEInternalPreferences;\r
-import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;\r
-import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;\r
-import org.eclipse.ui.internal.ide.dialogs.WelcomeEditorInput;\r
-import org.eclipse.ui.part.EditorInputTransfer;\r
-import org.eclipse.ui.part.MarkerTransfer;\r
-import org.eclipse.ui.part.ResourceTransfer;\r
-import org.eclipse.ui.statushandlers.StatusManager;\r
-import org.osgi.framework.Bundle;\r
-import org.osgi.framework.BundleContext;\r
-import org.osgi.framework.BundleException;\r
-import org.osgi.framework.ServiceReference;\r
-import org.simantics.db.management.ISessionContextChangedListener;\r
-import org.simantics.db.management.ISessionContextProvider;\r
-import org.simantics.db.management.SessionContextChangedEvent;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.workbench.WorkbenchShutdownService;\r
-\r
-\r
-/**\r
- * System properties used to configure how this class works:\r
- * <ul>\r
- * <li><code>org.simantics.workbench.application.showProgressIndicator=true|false (default=true)</code> - Used to initialize IWorkbenchWindowConfigurer.setShowProgressIndicator</li>\r
- * <li><code>org.simantics.workbench.application.showFastViewBars=true|false (default=true)</code> - Used to initialize IWorkbenchWindowConfigurer.setShowFastViewBars</li>\r
- * <li><code>org.simantics.workbench.application.showPerspectiveBar=true|false (default=true)</code> - Used to initialize IWorkbenchWindowConfigurer.setShowPerspectiveBar</li>\r
- * <li><code>org.simantics.workbench.application.excludePerspectiveFromTitle=true|false (default=false)</code> - Used to exclude perspective name in {@link #computeTitle()}</li>\r
- * </ul>\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class SimanticsWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {\r
-\r
-    private static final String WELCOME_EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; //$NON-NLS-1$\r
-\r
-    protected SimanticsWorkbenchAdvisor wbAdvisor;\r
-    protected boolean editorsAndIntrosOpened = false;\r
-    protected IEditorPart lastActiveEditor = null;\r
-    protected IPerspectiveDescriptor lastPerspective = null;\r
-\r
-    protected IWorkbenchPage lastActivePage;\r
-    protected String lastEditorTitle = ""; //$NON-NLS-1$\r
-\r
-    private final IPropertyListener editorPropertyListener = new IPropertyListener() {\r
-        @Override\r
-        public void propertyChanged(Object source, int propId) {\r
-            if (propId == IWorkbenchPartConstants.PROP_TITLE) {\r
-                if (lastActiveEditor != null) {\r
-                    String newTitle = lastActiveEditor.getTitle();\r
-                    if (!lastEditorTitle.equals(newTitle)) {\r
-                        recomputeTitle();\r
-                    }\r
-                }\r
-            }\r
-        }\r
-    };\r
-\r
-    private IAdaptable lastInput;\r
-    private IWorkbenchAction openPerspectiveAction;\r
-\r
-    /**\r
-     * The property change listener.\r
-     * @since 3.6.1\r
-     */\r
-    private IPropertyChangeListener propertyChangeListener;\r
-\r
-    /**\r
-     * Return the style bits for the shortcut bar.\r
-     * \r
-     * @return style bitmask\r
-     */\r
-    protected int perspectiveBarStyle() {\r
-        return SWT.FLAT | SWT.WRAP | SWT.RIGHT | SWT.HORIZONTAL;\r
-    }\r
-\r
-    /**\r
-     * Crates a new IDE workbench window advisor.\r
-     * \r
-     * @param wbAdvisor the workbench advisor\r
-     * @param configurer the window configurer\r
-     */\r
-    public SimanticsWorkbenchWindowAdvisor(SimanticsWorkbenchAdvisor wbAdvisor, IWorkbenchWindowConfigurer configurer) {\r
-        super(configurer);\r
-        this.wbAdvisor = wbAdvisor;\r
-    }\r
-\r
-    /* (non-Javadoc)\r
-     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)\r
-     */\r
-    @Override\r
-    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {\r
-        return new SimanticsWorkbenchActionBarAdvisor(configurer);\r
-    }\r
-\r
-    /**\r
-     * Returns the workbench.\r
-     * \r
-     * @return the workbench\r
-     */\r
-    private IWorkbench getWorkbench() {\r
-        return getWindowConfigurer().getWorkbenchConfigurer().getWorkbench();\r
-    }\r
-\r
-    private volatile boolean allWindowShellsClosed = false;\r
-\r
-    /*\r
-     * (non-Javadoc)\r
-     * \r
-     * @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowShellClose\r
-     */\r
-    @Override\r
-    public boolean preWindowShellClose() {\r
-        if (getWorkbench().getWorkbenchWindowCount() > 1) {\r
-            return true;\r
-        }\r
-\r
-        // WORKAROUND for a workbench problem which causes Eclipse to invoke this method twice:\r
-        // Always return false after true has been returned.\r
-        // Eclipse/SWT or something seems to have a bug of some sort that causes it to receive\r
-        // See https://www.simantics.org/redmine/issues/3848.\r
-        if (allWindowShellsClosed)\r
-            return false;\r
-\r
-        // the user has asked to close the last window, while will cause the\r
-        // workbench to close in due course - prompt the user for confirmation\r
-        IPreferenceStore store = IDEWorkbenchPlugin.getDefault()\r
-        .getPreferenceStore();\r
-        boolean promptOnExit = store\r
-        .getBoolean(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW);\r
-\r
-        if (promptOnExit) {\r
-            String message;\r
-\r
-            String productName = null;\r
-            IProduct product = Platform.getProduct();\r
-            if (product != null) {\r
-                productName = product.getName();\r
-            }\r
-            if (productName == null) {\r
-                message = IDEWorkbenchMessages.PromptOnExitDialog_message0;\r
-            } else {\r
-                message = NLS.bind(IDEWorkbenchMessages.PromptOnExitDialog_message1, productName);\r
-            }\r
-\r
-            MessageDialogWithToggle dlg = MessageDialogWithToggle\r
-            .openOkCancelConfirm(getWindowConfigurer().getWindow()\r
-                    .getShell(), IDEWorkbenchMessages.PromptOnExitDialog_shellTitle,\r
-                    message, IDEWorkbenchMessages.PromptOnExitDialog_choice,\r
-                    false, null, null);\r
-            if (dlg.getReturnCode() != IDialogConstants.OK_ID) {\r
-                return false;\r
-            }\r
-            if (dlg.getToggleState()) {\r
-                store\r
-                .setValue(\r
-                        IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW,\r
-                        false);\r
-                IDEWorkbenchPlugin.getDefault().savePluginPreferences();\r
-            }\r
-        }\r
-\r
-        // Make sure that it's OK to close even if there are unsaved changes.\r
-        AtomicBoolean saveAtExit = new AtomicBoolean(false);\r
-        if (!ApplicationUtil.allowShutdown(saveAtExit))\r
-            return false;\r
-        wbAdvisor.setSaveAtExit(saveAtExit.get());\r
-\r
-        BundleContext context = Activator.getDefault().getBundle().getBundleContext();\r
-        ServiceReference<?> ref = context.getServiceReference(WorkbenchShutdownService.class.getName());\r
-        WorkbenchShutdownService shutdown = (WorkbenchShutdownService)context.getService(ref);\r
-        shutdown.doShutdown();\r
-        context.ungetService(ref);\r
-\r
-        allWindowShellsClosed = true;\r
-\r
-        return true;\r
-    }\r
-\r
-    /**\r
-     * Asks the user whether the workbench should really be closed. Only asks if\r
-     * the preference is enabled.\r
-     * \r
-     * @param parentShell\r
-     *            the parent shell to use for the confirmation dialog\r
-     * @return <code>true</code> if OK to exit, <code>false</code> if the user\r
-     *         canceled\r
-     * @since 3.6\r
-     */\r
-    static boolean promptOnExit(Shell parentShell) {\r
-        IPreferenceStore store = IDEWorkbenchPlugin.getDefault()\r
-                .getPreferenceStore();\r
-        boolean promptOnExit = store\r
-                .getBoolean(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW);\r
-\r
-        if (promptOnExit) {\r
-            if (parentShell == null) {\r
-                IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
-                if (workbenchWindow != null) {\r
-                    parentShell = workbenchWindow.getShell();\r
-                }\r
-            }\r
-            if (parentShell != null) {\r
-                parentShell.setMinimized(false);\r
-                parentShell.forceActive();\r
-            }\r
-\r
-            String message;\r
-\r
-            String productName = null;\r
-            IProduct product = Platform.getProduct();\r
-            if (product != null) {\r
-                productName = product.getName();\r
-            }\r
-            if (productName == null) {\r
-                message = IDEWorkbenchMessages.PromptOnExitDialog_message0;\r
-            } else {\r
-                message = NLS.bind(\r
-                        IDEWorkbenchMessages.PromptOnExitDialog_message1,\r
-                        productName);\r
-            }\r
-\r
-            MessageDialogWithToggle dlg = MessageDialogWithToggle\r
-                    .openOkCancelConfirm(parentShell,\r
-                            IDEWorkbenchMessages.PromptOnExitDialog_shellTitle,\r
-                            message,\r
-                            IDEWorkbenchMessages.PromptOnExitDialog_choice,\r
-                            false, null, null);\r
-            if (dlg.getReturnCode() != IDialogConstants.OK_ID) {\r
-                return false;\r
-            }\r
-            if (dlg.getToggleState()) {\r
-                store\r
-                .setValue(\r
-                        IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW,\r
-                        false);\r
-                IDEWorkbenchPlugin.getDefault().savePluginPreferences();\r
-            }\r
-        }\r
-\r
-        return true;\r
-    }\r
-\r
-    /* (non-Javadoc)\r
-     * @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowOpen\r
-     */\r
-    @Override\r
-    public void preWindowOpen() {\r
-        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();\r
-\r
-        boolean showProgressIndicator = !"false".equals(System.getProperty("org.simantics.workbench.application.showProgressIndicator", "true").toLowerCase());\r
-        boolean showPerspectiveBar    = !"false".equals(System.getProperty("org.simantics.workbench.application.showPerspectiveBar", "true").toLowerCase());\r
-\r
-        // show the shortcut bar and progress indicator, which are hidden by default\r
-        configurer.setShowProgressIndicator(showProgressIndicator);\r
-        configurer.setShowPerspectiveBar(showPerspectiveBar);\r
-\r
-        // add the drag and drop support for the editor area\r
-        configurer.addEditorAreaTransfer(EditorInputTransfer.getInstance());\r
-        configurer.addEditorAreaTransfer(ResourceTransfer.getInstance());\r
-        configurer.addEditorAreaTransfer(FileTransfer.getInstance());\r
-        configurer.addEditorAreaTransfer(MarkerTransfer.getInstance());\r
-        configurer.configureEditorAreaDropListener(new EditorAreaDropAdapter(\r
-                configurer.getWindow()));\r
-\r
-        hookTitleUpdateListeners(configurer);\r
-    }\r
-\r
-    /**\r
-     * Hooks the listeners needed on the window\r
-     * @param configurer\r
-     */\r
-    private void hookTitleUpdateListeners(IWorkbenchWindowConfigurer configurer) {\r
-        // hook up the listeners to update the window title\r
-        configurer.getWindow().addPageListener(new IPageListener() {\r
-            @Override\r
-            public void pageActivated(IWorkbenchPage page) {\r
-                updateTitle(false);\r
-            }\r
-\r
-            @Override\r
-            public void pageClosed(IWorkbenchPage page) {\r
-                updateTitle(false);\r
-            }\r
-\r
-            @Override\r
-            public void pageOpened(IWorkbenchPage page) {\r
-                // do nothing\r
-            }\r
-        });\r
-        configurer.getWindow().addPerspectiveListener(\r
-                new PerspectiveAdapter() {\r
-                    @Override\r
-                    public void perspectiveActivated(IWorkbenchPage page,\r
-                            IPerspectiveDescriptor perspective) {\r
-                        updateTitle(false);\r
-                    }\r
-                    @Override\r
-                    public void perspectiveSavedAs(IWorkbenchPage page,IPerspectiveDescriptor oldPerspective,IPerspectiveDescriptor newPerspective){\r
-                        updateTitle(false);\r
-                    }\r
-                    @Override\r
-                    public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {\r
-                        updateTitle(false);\r
-                    }\r
-                });\r
-        configurer.getWindow().getPartService().addPartListener(\r
-                new IPartListener2() {\r
-                    @Override\r
-                    public void partActivated(IWorkbenchPartReference ref) {\r
-                        if (ref instanceof IEditorReference) {\r
-                            updateTitle(false);\r
-                        }\r
-                    }\r
-\r
-                    @Override\r
-                    public void partBroughtToTop(IWorkbenchPartReference ref) {\r
-                        if (ref instanceof IEditorReference) {\r
-                            updateTitle(false);\r
-                        }\r
-                    }\r
-\r
-                    @Override\r
-                    public void partClosed(IWorkbenchPartReference ref) {\r
-                        updateTitle(false);\r
-                    }\r
-\r
-                    @Override\r
-                    public void partDeactivated(IWorkbenchPartReference ref) {\r
-                        // do nothing\r
-                    }\r
-\r
-                    @Override\r
-                    public void partOpened(IWorkbenchPartReference ref) {\r
-                        // do nothing\r
-                    }\r
-\r
-                    @Override\r
-                    public void partHidden(IWorkbenchPartReference ref) {\r
-                        if (ref.getPart(false) == lastActiveEditor\r
-                                && lastActiveEditor != null) {\r
-                            updateTitle(true);\r
-                        }\r
-                    }\r
-\r
-                    @Override\r
-                    public void partVisible(IWorkbenchPartReference ref) {\r
-                        if (ref.getPart(false) == lastActiveEditor\r
-                                && lastActiveEditor != null) {\r
-                            updateTitle(false);\r
-                        }\r
-                    }\r
-\r
-                    @Override\r
-                    public void partInputChanged(IWorkbenchPartReference ref) {\r
-                        // do nothing\r
-                    }\r
-                });\r
-\r
-        // Update the title when the active UI database session is changed.\r
-        ISessionContextProvider provider = SimanticsUI.getSessionContextProvider(configurer.getWindow());\r
-        provider.addContextChangedListener(new ISessionContextChangedListener() {\r
-            @Override\r
-            public void sessionContextChanged(SessionContextChangedEvent event) {\r
-                IWorkbenchWindowConfigurer configurer = getWindowConfigurer();\r
-                IWorkbenchWindow window = configurer.getWindow();\r
-                if (window == null)\r
-                    return;\r
-                Shell shell = window.getShell();\r
-                if (shell == null || shell.isDisposed())\r
-                    return;\r
-                shell.getDisplay().asyncExec(new Runnable() {\r
-                    @Override\r
-                    public void run() {\r
-                        updateTitle(true);\r
-                    }\r
-                });\r
-            }\r
-        });\r
-\r
-        // Listen for changes of the workspace name.\r
-        propertyChangeListener = new IPropertyChangeListener() {\r
-            public void propertyChange(PropertyChangeEvent event) {\r
-                String property = event.getProperty();\r
-                if (IDEInternalPreferences.WORKSPACE_NAME.equals(property)\r
-                        || IDEInternalPreferences.SHOW_LOCATION.equals(property)) {\r
-                    // Make sure the title is actually updated by\r
-                    // setting last active page.\r
-                    lastActivePage = null;\r
-                    updateTitle(false);\r
-                }\r
-            }\r
-        };\r
-        IDEWorkbenchPlugin.getDefault().getPreferenceStore()\r
-                .addPropertyChangeListener(propertyChangeListener);\r
-    }\r
-\r
-    protected String computeTitle() {\r
-        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();\r
-        IWorkbenchPage currentPage = configurer.getWindow().getActivePage();\r
-        IEditorPart activeEditor = null;\r
-        if (currentPage != null) {\r
-            activeEditor = lastActiveEditor;\r
-        }\r
-\r
-        String title = null;\r
-        IProduct product = Platform.getProduct();\r
-        if (product != null) {\r
-            title = product.getName();\r
-        }\r
-        if (title == null) {\r
-            title = ""; //$NON-NLS-1$\r
-        }\r
-\r
-//        ISessionContext ctx = SimanticsUI.getSessionContext(configurer.getWindow());\r
-//        if (ctx != null) {\r
-//            ServerAddress addr = ctx.getAddress();\r
-//            if (addr != null) {\r
-//                title += " - Connected to " + addr.getAddress().getHostName();//InetAddressUtils.toHostString(addr);\r
-//            }\r
-//            Session session = ctx.getSession();\r
-//            if (session != null) {\r
-//                SessionUserSupport sessionUser = session.getService(SessionUserSupport.class);\r
-//                final Resource user = sessionUser.getUser();\r
-//                if (user != null) {\r
-//                    String userName;\r
-//                    try {\r
-//                        userName = session.syncRequest(new Read<String>() {\r
-//                            @Override\r
-//                            public String perform(ReadGraph graph) throws DatabaseException {\r
-//                                try {\r
-//                                    return graph.adapt(user, String.class);\r
-//                                } catch (AdaptionException e) {\r
-//                                    return NameUtils.getSafeName(graph, user);\r
-//                                }\r
-//                            }\r
-//                        });\r
-//                    } catch (DatabaseException e) {\r
-//                        e.printStackTrace();\r
-//                        userName = "ERROR";\r
-//                    }\r
-//\r
-//                    title += " - " + userName;\r
-//                }\r
-//            }\r
-//\r
-//        } else {\r
-//            //title += " - Disconnected";\r
-//        }\r
-\r
-        if (currentPage != null) {\r
-            if (activeEditor != null) {\r
-                lastEditorTitle = activeEditor.getTitleToolTip();\r
-                title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, lastEditorTitle, title);\r
-            }\r
-\r
-            boolean excludePerspectiveFromTitle = "true".equals(System.getProperty("org.simantics.workbench.application.excludePerspectiveFromTitle", "false").toLowerCase());\r
-            if (!excludePerspectiveFromTitle) {\r
-                IPerspectiveDescriptor persp = currentPage.getPerspective();\r
-                String label = ""; //$NON-NLS-1$\r
-                if (persp != null) {\r
-                    label = persp.getLabel();\r
-                }\r
-                IAdaptable input = currentPage.getInput();\r
-                if (input != null && !input.equals(wbAdvisor.getDefaultPageInput())) {\r
-                    label = currentPage.getLabel();\r
-                }\r
-                if (label != null && !label.equals("")) { //$NON-NLS-1$\r
-                    title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, label, title);\r
-                }\r
-            }\r
-        }\r
-\r
-        String workspaceLocation = wbAdvisor.getWorkspaceLocation();\r
-        if (workspaceLocation != null) {\r
-            title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, title, workspaceLocation);\r
-        }\r
-\r
-        // Bug 284447: Prepend workspace name to the title\r
-        String workspaceName = IDEWorkbenchPlugin.getDefault()\r
-        .getPreferenceStore().getString(\r
-                IDEInternalPreferences.WORKSPACE_NAME);\r
-        if (workspaceName != null && workspaceName.length() > 0) {\r
-            title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle,\r
-                    workspaceName, title);\r
-        }\r
-\r
-        return title;\r
-    }\r
-\r
-    private void recomputeTitle() {\r
-        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();\r
-        String oldTitle = configurer.getTitle();\r
-        String newTitle = computeTitle();\r
-        if (!newTitle.equals(oldTitle)) {\r
-            configurer.setTitle(newTitle);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Updates the window title. Format will be:\r
-     * [pageInput -] [currentPerspective -] [editorInput -] [workspaceLocation -] productName\r
-     */\r
-    private void updateTitle(boolean force) {\r
-        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();\r
-        IWorkbenchWindow window = configurer.getWindow();\r
-        IEditorPart activeEditor = null;\r
-        IWorkbenchPage currentPage = window.getActivePage();\r
-        IPerspectiveDescriptor persp = null;\r
-        IAdaptable input = null;\r
-\r
-        if (currentPage != null) {\r
-            activeEditor = currentPage.getActiveEditor();\r
-            persp = currentPage.getPerspective();\r
-            input = currentPage.getInput();\r
-        }\r
-\r
-        // Nothing to do if the editor hasn't changed or change is not forced.\r
-        if (!force && activeEditor == lastActiveEditor && currentPage == lastActivePage\r
-                && persp == lastPerspective && input == lastInput) {\r
-            return;\r
-        }\r
-\r
-        if (lastActiveEditor != null) {\r
-            lastActiveEditor.removePropertyListener(editorPropertyListener );\r
-        }\r
-\r
-        lastActiveEditor = activeEditor;\r
-        lastActivePage = currentPage;\r
-        lastPerspective = persp;\r
-        lastInput = input;\r
-\r
-        if (activeEditor != null) {\r
-            activeEditor.addPropertyListener(editorPropertyListener);\r
-        }\r
-\r
-        recomputeTitle();\r
-    }\r
-\r
-    /* (non-Javadoc)\r
-     * @see org.eclipse.ui.application.WorkbenchAdvisor#postWindowRestore\r
-     */\r
-    @Override\r
-    public void postWindowRestore()\r
-    throws WorkbenchException {\r
-        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();\r
-        IWorkbenchWindow window = configurer.getWindow();\r
-\r
-        int index = getWorkbench().getWorkbenchWindowCount() - 1;\r
-\r
-        AboutInfo[] welcomePerspectiveInfos = wbAdvisor.getWelcomePerspectiveInfos();\r
-        if (index >= 0 && welcomePerspectiveInfos != null\r
-                && index < welcomePerspectiveInfos.length) {\r
-            // find a page that exist in the window\r
-            IWorkbenchPage page = window.getActivePage();\r
-            if (page == null) {\r
-                IWorkbenchPage[] pages = window.getPages();\r
-                if (pages != null && pages.length > 0) {\r
-                    page = pages[0];\r
-                }\r
-            }\r
-\r
-            // if the window does not contain a page, create one\r
-            String perspectiveId = welcomePerspectiveInfos[index].getWelcomePerspectiveId();\r
-            if (page == null) {\r
-                IAdaptable root = wbAdvisor.getDefaultPageInput();\r
-                page = window.openPage(perspectiveId, root);\r
-            } else {\r
-                IPerspectiveRegistry reg = getWorkbench()\r
-                        .getPerspectiveRegistry();\r
-                IPerspectiveDescriptor desc = reg\r
-                        .findPerspectiveWithId(perspectiveId);\r
-                if (desc != null) {\r
-                    page.setPerspective(desc);\r
-                }\r
-            }\r
-\r
-            // set the active page and open the welcome editor\r
-            window.setActivePage(page);\r
-            page.openEditor(new WelcomeEditorInput(\r
-                    welcomePerspectiveInfos[index]), WELCOME_EDITOR_ID, true);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Tries to open the intro, if one exists and otherwise will open the legacy\r
-     * Welcome pages.\r
-     * \r
-     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#openIntro()\r
-     */\r
-    @Override\r
-    public void openIntro() {\r
-        if (editorsAndIntrosOpened ) {\r
-            return;\r
-        }\r
-\r
-        editorsAndIntrosOpened = true;\r
-\r
-        // don't try to open the welcome editors if there is an intro\r
-        if (wbAdvisor.hasIntro()) {\r
-            super.openIntro();\r
-        } else {\r
-            openWelcomeEditors(getWindowConfigurer().getWindow());\r
-            // save any preferences changes caused by the above actions\r
-            IDEWorkbenchPlugin.getDefault().savePluginPreferences();\r
-        }\r
-    }\r
-\r
-    /*\r
-     * Open the welcome editor for the primary feature and for any newly\r
-     * installed features.\r
-     */\r
-    private void openWelcomeEditors(IWorkbenchWindow window) {\r
-        if (IDEWorkbenchPlugin.getDefault().getPreferenceStore().getBoolean(\r
-                IDEInternalPreferences.WELCOME_DIALOG)) {\r
-            // show the welcome page for the product the first time the\r
-            // workbench opens\r
-            IProduct product = Platform.getProduct();\r
-            if (product == null) {\r
-                return;\r
-            }\r
-\r
-            AboutInfo productInfo = new AboutInfo(product);\r
-            URL url = productInfo.getWelcomePageURL();\r
-            if (url == null) {\r
-                return;\r
-            }\r
-\r
-            IDEWorkbenchPlugin.getDefault().getPreferenceStore().setValue(\r
-                    IDEInternalPreferences.WELCOME_DIALOG, false);\r
-            openWelcomeEditor(window, new WelcomeEditorInput(productInfo), null);\r
-        } else {\r
-            // Show the welcome page for any newly installed features\r
-            List<AboutInfo> welcomeFeatures = new ArrayList<AboutInfo>();\r
-            for (Iterator<?> it = wbAdvisor.getNewlyAddedBundleGroups().entrySet()\r
-                    .iterator(); it.hasNext();) {\r
-                Map.Entry<?,?> entry = (Map.Entry<?,?>) it.next();\r
-                AboutInfo info = (AboutInfo) entry.getValue();\r
-\r
-                if (info != null && info.getWelcomePageURL() != null) {\r
-                    welcomeFeatures.add(info);\r
-                    // activate the feature plug-in so it can run some install\r
-                    // code\r
-                    String pi = info.getBrandingBundleId();\r
-                    if (pi != null) {\r
-                        // Start the bundle if there is one\r
-                        Bundle bundle = Platform.getBundle(pi);\r
-                        if (bundle != null) {\r
-                            try {\r
-                                bundle.start(Bundle.START_TRANSIENT);\r
-                            } catch (BundleException exception) {\r
-                                StatusManager\r
-                                .getManager()\r
-                                .handle(\r
-                                        new Status(\r
-                                                IStatus.ERROR,\r
-                                                Activator.PLUGIN_ID,\r
-                                                "Failed to load feature", exception));//$NON-NLS-1$\r
-                            }\r
-                        }\r
-                    }\r
-                }\r
-            }\r
-\r
-            int wCount = getWorkbench().getWorkbenchWindowCount();\r
-            for (int i = 0; i < welcomeFeatures.size(); i++) {\r
-                AboutInfo newInfo = (AboutInfo) welcomeFeatures.get(i);\r
-                String id = newInfo.getWelcomePerspectiveId();\r
-                // Other editors were already opened in postWindowRestore(..)\r
-                if (id == null || i >= wCount) {\r
-                    openWelcomeEditor(window, new WelcomeEditorInput(newInfo),\r
-                            id);\r
-                }\r
-            }\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Open a welcome editor for the given input\r
-     */\r
-    private void openWelcomeEditor(IWorkbenchWindow window,\r
-            WelcomeEditorInput input, String perspectiveId) {\r
-        if (getWorkbench().getWorkbenchWindowCount() == 0) {\r
-            // Something is wrong, there should be at least\r
-            // one workbench window open by now.\r
-            return;\r
-        }\r
-\r
-        IWorkbenchWindow win = window;\r
-        if (perspectiveId != null) {\r
-            try {\r
-                win = getWorkbench().openWorkbenchWindow(perspectiveId,\r
-                        wbAdvisor.getDefaultPageInput());\r
-                if (win == null) {\r
-                    win = window;\r
-                }\r
-            } catch (WorkbenchException e) {\r
-                IDEWorkbenchPlugin\r
-                .log(\r
-                        "Error opening window with welcome perspective.", e.getStatus()); //$NON-NLS-1$\r
-                return;\r
-            }\r
-        }\r
-\r
-        if (win == null) {\r
-            win = getWorkbench().getWorkbenchWindows()[0];\r
-        }\r
-\r
-        IWorkbenchPage page = win.getActivePage();\r
-        String id = perspectiveId;\r
-        if (id == null) {\r
-            id = getWorkbench().getPerspectiveRegistry()\r
-            .getDefaultPerspective();\r
-        }\r
-\r
-        if (page == null) {\r
-            try {\r
-                page = win.openPage(id, wbAdvisor.getDefaultPageInput());\r
-            } catch (WorkbenchException e) {\r
-                ErrorDialog.openError(win.getShell(),\r
-                        IDEWorkbenchMessages.Problems_Opening_Page, e\r
-                        .getMessage(), e.getStatus());\r
-            }\r
-        }\r
-        if (page == null) {\r
-            return;\r
-        }\r
-\r
-        if (page.getPerspective() == null) {\r
-            try {\r
-                page = getWorkbench().showPerspective(id, win);\r
-            } catch (WorkbenchException e) {\r
-                ErrorDialog\r
-                .openError(\r
-                        win.getShell(),\r
-                        IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle,\r
-                        IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage,\r
-                        e.getStatus());\r
-                return;\r
-            }\r
-        }\r
-\r
-        page.setEditorAreaVisible(true);\r
-\r
-        // see if we already have an editor\r
-        IEditorPart editor = page.findEditor(input);\r
-        if (editor != null) {\r
-            page.activate(editor);\r
-            return;\r
-        }\r
-\r
-        try {\r
-            page.openEditor(input, WELCOME_EDITOR_ID);\r
-        } catch (PartInitException e) {\r
-            ErrorDialog\r
-            .openError(\r
-                    win.getShell(),\r
-                    IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle,\r
-                    IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage,\r
-                    e.getStatus());\r
-        }\r
-        return;\r
-    }\r
-\r
-    /* (non-Javadoc)\r
-     * @see org.eclipse.ui.application.WorkbenchAdvisor#createEmptyWindowContents(org.eclipse.ui.application.IWorkbenchWindowConfigurer, org.eclipse.swt.widgets.Composite)\r
-     */\r
-    @Override\r
-    public Control createEmptyWindowContents(Composite parent) {\r
-        final IWorkbenchWindow window = getWindowConfigurer().getWindow();\r
-        Composite composite = new Composite(parent, SWT.NONE);\r
-        composite.setLayout(new GridLayout(2, false));\r
-        Display display = composite.getDisplay();\r
-        Color bgCol = display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND);\r
-        composite.setBackground(bgCol);\r
-        Label label = new Label(composite, SWT.WRAP);\r
-        label.setForeground(display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));\r
-        label.setBackground(bgCol);\r
-        label.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));\r
-        String msg = IDEWorkbenchMessages.IDEWorkbenchAdvisor_noPerspective;\r
-        label.setText(msg);\r
-        ToolBarManager toolBarManager = new ToolBarManager();\r
-        openPerspectiveAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG.create(window);\r
-        toolBarManager.add(openPerspectiveAction);\r
-        ToolBar toolBar = toolBarManager.createControl(composite);\r
-        toolBar.setBackground(bgCol);\r
-        return composite;\r
-    }\r
-\r
-    @Override\r
-    public void dispose() {\r
-        if (propertyChangeListener != null) {\r
-            IDEWorkbenchPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(propertyChangeListener);\r
-            propertyChangeListener = null;\r
-        }\r
-\r
-        if (openPerspectiveAction!=null) {\r
-            openPerspectiveAction.dispose();\r
-            openPerspectiveAction = null;\r
-        }\r
-        super.dispose();\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * 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:
+ * <ul>
+ * <li><code>org.simantics.workbench.application.showProgressIndicator=true|false (default=true)</code> - Used to initialize IWorkbenchWindowConfigurer.setShowProgressIndicator</li>
+ * <li><code>org.simantics.workbench.application.showFastViewBars=true|false (default=true)</code> - Used to initialize IWorkbenchWindowConfigurer.setShowFastViewBars</li>
+ * <li><code>org.simantics.workbench.application.showPerspectiveBar=true|false (default=true)</code> - Used to initialize IWorkbenchWindowConfigurer.setShowPerspectiveBar</li>
+ * <li><code>org.simantics.workbench.application.excludePerspectiveFromTitle=true|false (default=false)</code> - Used to exclude perspective name in {@link #computeTitle()}</li>
+ * </ul>
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class SimanticsWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
+
+    private static final String WELCOME_EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; //$NON-NLS-1$
+
+    protected SimanticsWorkbenchAdvisor wbAdvisor;
+    protected boolean editorsAndIntrosOpened = false;
+    protected IEditorPart lastActiveEditor = null;
+    protected IPerspectiveDescriptor lastPerspective = null;
+
+    protected IWorkbenchPage lastActivePage;
+    protected String lastEditorTitle = ""; //$NON-NLS-1$
+
+    private final IPropertyListener editorPropertyListener = new IPropertyListener() {
+        @Override
+        public void propertyChanged(Object source, int propId) {
+            if (propId == IWorkbenchPartConstants.PROP_TITLE) {
+                if (lastActiveEditor != null) {
+                    String newTitle = lastActiveEditor.getTitle();
+                    if (!lastEditorTitle.equals(newTitle)) {
+                        recomputeTitle();
+                    }
+                }
+            }
+        }
+    };
+
+    private IAdaptable lastInput;
+    private IWorkbenchAction openPerspectiveAction;
+
+    /**
+     * The property change listener.
+     * @since 3.6.1
+     */
+    private IPropertyChangeListener propertyChangeListener;
+
+    /**
+     * Return the style bits for the shortcut bar.
+     * 
+     * @return style bitmask
+     */
+    protected int perspectiveBarStyle() {
+        return SWT.FLAT | SWT.WRAP | SWT.RIGHT | SWT.HORIZONTAL;
+    }
+
+    /**
+     * Crates a new IDE workbench window advisor.
+     * 
+     * @param wbAdvisor the workbench advisor
+     * @param configurer the window configurer
+     */
+    public SimanticsWorkbenchWindowAdvisor(SimanticsWorkbenchAdvisor wbAdvisor, IWorkbenchWindowConfigurer configurer) {
+        super(configurer);
+        this.wbAdvisor = wbAdvisor;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
+     */
+    @Override
+    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
+        return new SimanticsWorkbenchActionBarAdvisor(configurer);
+    }
+
+    /**
+     * Returns the workbench.
+     * 
+     * @return the workbench
+     */
+    private IWorkbench getWorkbench() {
+        return getWindowConfigurer().getWorkbenchConfigurer().getWorkbench();
+    }
+
+    private volatile boolean allWindowShellsClosed = false;
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowShellClose
+     */
+    @Override
+    public boolean preWindowShellClose() {
+        if (getWorkbench().getWorkbenchWindowCount() > 1) {
+            return true;
+        }
+
+        // WORKAROUND for a workbench problem which causes Eclipse to invoke this method twice:
+        // Always return false after true has been returned.
+        // Eclipse/SWT or something seems to have a bug of some sort that causes it to receive
+        // See https://www.simantics.org/redmine/issues/3848.
+        if (allWindowShellsClosed)
+            return false;
+
+        // the user has asked to close the last window, while will cause the
+        // workbench to close in due course - prompt the user for confirmation
+        IPreferenceStore store = IDEWorkbenchPlugin.getDefault()
+        .getPreferenceStore();
+        boolean promptOnExit = store
+        .getBoolean(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW);
+
+        if (promptOnExit) {
+            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(getWindowConfigurer().getWindow()
+                    .getShell(), 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();
+            }
+        }
+
+        // Make sure that it's OK to close even if there are unsaved changes.
+        AtomicBoolean saveAtExit = new AtomicBoolean(false);
+        if (!ApplicationUtil.allowShutdown(saveAtExit))
+            return false;
+        wbAdvisor.setSaveAtExit(saveAtExit.get());
+
+        BundleContext context = Activator.getDefault().getBundle().getBundleContext();
+        ServiceReference<?> ref = context.getServiceReference(WorkbenchShutdownService.class.getName());
+        WorkbenchShutdownService shutdown = (WorkbenchShutdownService)context.getService(ref);
+        shutdown.doShutdown();
+        context.ungetService(ref);
+
+        allWindowShellsClosed = true;
+
+        return true;
+    }
+
+    /**
+     * Asks the user whether the workbench should really be closed. Only asks if
+     * the preference is enabled.
+     * 
+     * @param parentShell
+     *            the parent shell to use for the confirmation dialog
+     * @return <code>true</code> if OK to exit, <code>false</code> 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<String>() {
+//                            @Override
+//                            public String perform(ReadGraph graph) throws DatabaseException {
+//                                try {
+//                                    return graph.adapt(user, String.class);
+//                                } catch (AdaptionException e) {
+//                                    return NameUtils.getSafeName(graph, user);
+//                                }
+//                            }
+//                        });
+//                    } catch (DatabaseException e) {
+//                        e.printStackTrace();
+//                        userName = "ERROR";
+//                    }
+//
+//                    title += " - " + userName;
+//                }
+//            }
+//
+//        } else {
+//            //title += " - Disconnected";
+//        }
+
+        if (currentPage != null) {
+            if (activeEditor != null) {
+                lastEditorTitle = activeEditor.getTitleToolTip();
+                title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, lastEditorTitle, title);
+            }
+
+            boolean excludePerspectiveFromTitle = "true".equals(System.getProperty("org.simantics.workbench.application.excludePerspectiveFromTitle", "false").toLowerCase());
+            if (!excludePerspectiveFromTitle) {
+                IPerspectiveDescriptor persp = currentPage.getPerspective();
+                String label = ""; //$NON-NLS-1$
+                if (persp != null) {
+                    label = persp.getLabel();
+                }
+                IAdaptable input = currentPage.getInput();
+                if (input != null && !input.equals(wbAdvisor.getDefaultPageInput())) {
+                    label = currentPage.getLabel();
+                }
+                if (label != null && !label.equals("")) { //$NON-NLS-1$
+                    title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, label, title);
+                }
+            }
+        }
+
+        String workspaceLocation = wbAdvisor.getWorkspaceLocation();
+        if (workspaceLocation != null) {
+            title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, title, workspaceLocation);
+        }
+
+        // Bug 284447: Prepend workspace name to the title
+        String workspaceName = IDEWorkbenchPlugin.getDefault()
+        .getPreferenceStore().getString(
+                IDEInternalPreferences.WORKSPACE_NAME);
+        if (workspaceName != null && workspaceName.length() > 0) {
+            title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle,
+                    workspaceName, title);
+        }
+
+        return title;
+    }
+
+    private void recomputeTitle() {
+        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
+        String oldTitle = configurer.getTitle();
+        String newTitle = computeTitle();
+        if (!newTitle.equals(oldTitle)) {
+            configurer.setTitle(newTitle);
+        }
+    }
+
+    /**
+     * Updates the window title. Format will be:
+     * [pageInput -] [currentPerspective -] [editorInput -] [workspaceLocation -] productName
+     */
+    private void updateTitle(boolean force) {
+        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
+        IWorkbenchWindow window = configurer.getWindow();
+        IEditorPart activeEditor = null;
+        IWorkbenchPage currentPage = window.getActivePage();
+        IPerspectiveDescriptor persp = null;
+        IAdaptable input = null;
+
+        if (currentPage != null) {
+            activeEditor = currentPage.getActiveEditor();
+            persp = currentPage.getPerspective();
+            input = currentPage.getInput();
+        }
+
+        // Nothing to do if the editor hasn't changed or change is not forced.
+        if (!force && activeEditor == lastActiveEditor && currentPage == lastActivePage
+                && persp == lastPerspective && input == lastInput) {
+            return;
+        }
+
+        if (lastActiveEditor != null) {
+            lastActiveEditor.removePropertyListener(editorPropertyListener );
+        }
+
+        lastActiveEditor = activeEditor;
+        lastActivePage = currentPage;
+        lastPerspective = persp;
+        lastInput = input;
+
+        if (activeEditor != null) {
+            activeEditor.addPropertyListener(editorPropertyListener);
+        }
+
+        recomputeTitle();
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.application.WorkbenchAdvisor#postWindowRestore
+     */
+    @Override
+    public void postWindowRestore()
+    throws WorkbenchException {
+        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
+        IWorkbenchWindow window = configurer.getWindow();
+
+        int index = getWorkbench().getWorkbenchWindowCount() - 1;
+
+        AboutInfo[] welcomePerspectiveInfos = wbAdvisor.getWelcomePerspectiveInfos();
+        if (index >= 0 && welcomePerspectiveInfos != null
+                && index < welcomePerspectiveInfos.length) {
+            // find a page that exist in the window
+            IWorkbenchPage page = window.getActivePage();
+            if (page == null) {
+                IWorkbenchPage[] pages = window.getPages();
+                if (pages != null && pages.length > 0) {
+                    page = pages[0];
+                }
+            }
+
+            // if the window does not contain a page, create one
+            String perspectiveId = welcomePerspectiveInfos[index].getWelcomePerspectiveId();
+            if (page == null) {
+                IAdaptable root = wbAdvisor.getDefaultPageInput();
+                page = window.openPage(perspectiveId, root);
+            } else {
+                IPerspectiveRegistry reg = getWorkbench()
+                        .getPerspectiveRegistry();
+                IPerspectiveDescriptor desc = reg
+                        .findPerspectiveWithId(perspectiveId);
+                if (desc != null) {
+                    page.setPerspective(desc);
+                }
+            }
+
+            // set the active page and open the welcome editor
+            window.setActivePage(page);
+            page.openEditor(new WelcomeEditorInput(
+                    welcomePerspectiveInfos[index]), WELCOME_EDITOR_ID, true);
+        }
+    }
+
+    /**
+     * Tries to open the intro, if one exists and otherwise will open the legacy
+     * Welcome pages.
+     * 
+     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#openIntro()
+     */
+    @Override
+    public void openIntro() {
+        if (editorsAndIntrosOpened ) {
+            return;
+        }
+
+        editorsAndIntrosOpened = true;
+
+        // don't try to open the welcome editors if there is an intro
+        if (wbAdvisor.hasIntro()) {
+            super.openIntro();
+        } else {
+            openWelcomeEditors(getWindowConfigurer().getWindow());
+            // save any preferences changes caused by the above actions
+            IDEWorkbenchPlugin.getDefault().savePluginPreferences();
+        }
+    }
+
+    /*
+     * Open the welcome editor for the primary feature and for any newly
+     * installed features.
+     */
+    private void openWelcomeEditors(IWorkbenchWindow window) {
+        if (IDEWorkbenchPlugin.getDefault().getPreferenceStore().getBoolean(
+                IDEInternalPreferences.WELCOME_DIALOG)) {
+            // show the welcome page for the product the first time the
+            // workbench opens
+            IProduct product = Platform.getProduct();
+            if (product == null) {
+                return;
+            }
+
+            AboutInfo productInfo = new AboutInfo(product);
+            URL url = productInfo.getWelcomePageURL();
+            if (url == null) {
+                return;
+            }
+
+            IDEWorkbenchPlugin.getDefault().getPreferenceStore().setValue(
+                    IDEInternalPreferences.WELCOME_DIALOG, false);
+            openWelcomeEditor(window, new WelcomeEditorInput(productInfo), null);
+        } else {
+            // Show the welcome page for any newly installed features
+            List<AboutInfo> welcomeFeatures = new ArrayList<AboutInfo>();
+            for (Iterator<?> it = wbAdvisor.getNewlyAddedBundleGroups().entrySet()
+                    .iterator(); it.hasNext();) {
+                Map.Entry<?,?> entry = (Map.Entry<?,?>) it.next();
+                AboutInfo info = (AboutInfo) entry.getValue();
+
+                if (info != null && info.getWelcomePageURL() != null) {
+                    welcomeFeatures.add(info);
+                    // activate the feature plug-in so it can run some install
+                    // code
+                    String pi = info.getBrandingBundleId();
+                    if (pi != null) {
+                        // Start the bundle if there is one
+                        Bundle bundle = Platform.getBundle(pi);
+                        if (bundle != null) {
+                            try {
+                                bundle.start(Bundle.START_TRANSIENT);
+                            } catch (BundleException exception) {
+                                StatusManager
+                                .getManager()
+                                .handle(
+                                        new Status(
+                                                IStatus.ERROR,
+                                                Activator.PLUGIN_ID,
+                                                "Failed to load feature", exception));//$NON-NLS-1$
+                            }
+                        }
+                    }
+                }
+            }
+
+            int wCount = getWorkbench().getWorkbenchWindowCount();
+            for (int i = 0; i < welcomeFeatures.size(); i++) {
+                AboutInfo newInfo = (AboutInfo) welcomeFeatures.get(i);
+                String id = newInfo.getWelcomePerspectiveId();
+                // Other editors were already opened in postWindowRestore(..)
+                if (id == null || i >= wCount) {
+                    openWelcomeEditor(window, new WelcomeEditorInput(newInfo),
+                            id);
+                }
+            }
+        }
+    }
+
+    /**
+     * Open a welcome editor for the given input
+     */
+    private void openWelcomeEditor(IWorkbenchWindow window,
+            WelcomeEditorInput input, String perspectiveId) {
+        if (getWorkbench().getWorkbenchWindowCount() == 0) {
+            // Something is wrong, there should be at least
+            // one workbench window open by now.
+            return;
+        }
+
+        IWorkbenchWindow win = window;
+        if (perspectiveId != null) {
+            try {
+                win = getWorkbench().openWorkbenchWindow(perspectiveId,
+                        wbAdvisor.getDefaultPageInput());
+                if (win == null) {
+                    win = window;
+                }
+            } catch (WorkbenchException e) {
+                IDEWorkbenchPlugin
+                .log(
+                        "Error opening window with welcome perspective.", e.getStatus()); //$NON-NLS-1$
+                return;
+            }
+        }
+
+        if (win == null) {
+            win = getWorkbench().getWorkbenchWindows()[0];
+        }
+
+        IWorkbenchPage page = win.getActivePage();
+        String id = perspectiveId;
+        if (id == null) {
+            id = getWorkbench().getPerspectiveRegistry()
+            .getDefaultPerspective();
+        }
+
+        if (page == null) {
+            try {
+                page = win.openPage(id, wbAdvisor.getDefaultPageInput());
+            } catch (WorkbenchException e) {
+                ErrorDialog.openError(win.getShell(),
+                        IDEWorkbenchMessages.Problems_Opening_Page, e
+                        .getMessage(), e.getStatus());
+            }
+        }
+        if (page == null) {
+            return;
+        }
+
+        if (page.getPerspective() == null) {
+            try {
+                page = getWorkbench().showPerspective(id, win);
+            } catch (WorkbenchException e) {
+                ErrorDialog
+                .openError(
+                        win.getShell(),
+                        IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle,
+                        IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage,
+                        e.getStatus());
+                return;
+            }
+        }
+
+        page.setEditorAreaVisible(true);
+
+        // see if we already have an editor
+        IEditorPart editor = page.findEditor(input);
+        if (editor != null) {
+            page.activate(editor);
+            return;
+        }
+
+        try {
+            page.openEditor(input, WELCOME_EDITOR_ID);
+        } catch (PartInitException e) {
+            ErrorDialog
+            .openError(
+                    win.getShell(),
+                    IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle,
+                    IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage,
+                    e.getStatus());
+        }
+        return;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.application.WorkbenchAdvisor#createEmptyWindowContents(org.eclipse.ui.application.IWorkbenchWindowConfigurer, org.eclipse.swt.widgets.Composite)
+     */
+    @Override
+    public Control createEmptyWindowContents(Composite parent) {
+        final IWorkbenchWindow window = getWindowConfigurer().getWindow();
+        Composite composite = new Composite(parent, SWT.NONE);
+        composite.setLayout(new GridLayout(2, false));
+        Display display = composite.getDisplay();
+        Color bgCol = display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND);
+        composite.setBackground(bgCol);
+        Label label = new Label(composite, SWT.WRAP);
+        label.setForeground(display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
+        label.setBackground(bgCol);
+        label.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
+        String msg = IDEWorkbenchMessages.IDEWorkbenchAdvisor_noPerspective;
+        label.setText(msg);
+        ToolBarManager toolBarManager = new ToolBarManager();
+        openPerspectiveAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG.create(window);
+        toolBarManager.add(openPerspectiveAction);
+        ToolBar toolBar = toolBarManager.createControl(composite);
+        toolBar.setBackground(bgCol);
+        return composite;
+    }
+
+    @Override
+    public void dispose() {
+        if (propertyChangeListener != null) {
+            IDEWorkbenchPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(propertyChangeListener);
+            propertyChangeListener = null;
+        }
+
+        if (openPerspectiveAction!=null) {
+            openPerspectiveAction.dispose();
+            openPerspectiveAction = null;
+        }
+        super.dispose();
+    }
+
+}