You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2010/03/09 06:45:24 UTC

svn commit: r920697 - /directory/studio/trunk/rcp/src/main/java/org/apache/directory/studio/ApplicationWorkbenchWindowAdvisor.java

Author: seelmann
Date: Tue Mar  9 05:45:24 2010
New Revision: 920697

URL: http://svn.apache.org/viewvc?rev=920697&view=rev
Log:
Fix for DIRSTUDIO-625 (Add Connection Context in the LDAP Browser Window)

Modified:
    directory/studio/trunk/rcp/src/main/java/org/apache/directory/studio/ApplicationWorkbenchWindowAdvisor.java

Modified: directory/studio/trunk/rcp/src/main/java/org/apache/directory/studio/ApplicationWorkbenchWindowAdvisor.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/rcp/src/main/java/org/apache/directory/studio/ApplicationWorkbenchWindowAdvisor.java?rev=920697&r1=920696&r2=920697&view=diff
==============================================================================
--- directory/studio/trunk/rcp/src/main/java/org/apache/directory/studio/ApplicationWorkbenchWindowAdvisor.java (original)
+++ directory/studio/trunk/rcp/src/main/java/org/apache/directory/studio/ApplicationWorkbenchWindowAdvisor.java Tue Mar  9 05:45:24 2010
@@ -21,7 +21,22 @@
 package org.apache.directory.studio;
 
 
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProduct;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.graphics.Point;
+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.IPropertyListener;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPartConstants;
+import org.eclipse.ui.IWorkbenchPartReference;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PerspectiveAdapter;
 import org.eclipse.ui.application.ActionBarAdvisor;
 import org.eclipse.ui.application.IActionBarConfigurer;
 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
@@ -54,6 +69,30 @@ import org.eclipse.ui.application.Workbe
 public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor
 {
 
+    private IEditorPart lastActiveEditor = null;
+    private IPerspectiveDescriptor lastPerspective = null;
+    private IWorkbenchPage lastActivePage;
+    private String lastEditorTitle = ""; //$NON-NLS-1$
+    private IAdaptable lastInput;
+    private IPropertyListener editorPropertyListener = new IPropertyListener()
+    {
+        public void propertyChanged( Object source, int propId )
+        {
+            if ( propId == IWorkbenchPartConstants.PROP_TITLE )
+            {
+                if ( lastActiveEditor != null )
+                {
+                    String newTitle = lastActiveEditor.getTitle();
+                    if ( !lastEditorTitle.equals( newTitle ) )
+                    {
+                        recomputeTitle();
+                    }
+                }
+            }
+        }
+    };
+
+
     /**
      * Default constructor
      * @param configurer 
@@ -96,6 +135,242 @@ public class ApplicationWorkbenchWindowA
         configurer.setShowPerspectiveBar( true );
         configurer.setShowProgressIndicator( true );
         configurer.setShowFastViewBars( true );
+
+        // hopk up the listeners to update the window title
+        // adapted from org.eclipse.ui.internal.ide.application.IDEWorkbenchWindowAdvisor 
+        // http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEWorkbenchWindowAdvisor.java?view=markup 
+        hookTitleUpdateListeners( configurer );
+    }
+
+
+    /**
+     * Hooks up the listeners to update the window title.
+     * 
+     * @param configurer
+     */
+    private void hookTitleUpdateListeners( IWorkbenchWindowConfigurer configurer )
+    {
+        configurer.getWindow().addPageListener( new IPageListener()
+        {
+            public void pageActivated( IWorkbenchPage page )
+            {
+                updateTitle( false );
+            }
+
+
+            public void pageClosed( IWorkbenchPage page )
+            {
+                updateTitle( false );
+            }
+
+
+            public void pageOpened( IWorkbenchPage page )
+            {
+                // do nothing
+            }
+        } );
+        configurer.getWindow().addPerspectiveListener( new PerspectiveAdapter()
+        {
+            public void perspectiveActivated( IWorkbenchPage page, IPerspectiveDescriptor perspective )
+            {
+                updateTitle( false );
+            }
+
+
+            public void perspectiveSavedAs( IWorkbenchPage page, IPerspectiveDescriptor oldPerspective,
+                IPerspectiveDescriptor newPerspective )
+            {
+                updateTitle( false );
+            }
+
+
+            public void perspectiveDeactivated( IWorkbenchPage page, IPerspectiveDescriptor perspective )
+            {
+                updateTitle( false );
+            }
+        } );
+        configurer.getWindow().getPartService().addPartListener( new IPartListener2()
+        {
+            public void partActivated( IWorkbenchPartReference ref )
+            {
+                if ( ref instanceof IEditorReference )
+                {
+                    updateTitle( false );
+                }
+            }
+
+
+            public void partBroughtToTop( IWorkbenchPartReference ref )
+            {
+                if ( ref instanceof IEditorReference )
+                {
+                    updateTitle( false );
+                }
+            }
+
+
+            public void partClosed( IWorkbenchPartReference ref )
+            {
+                updateTitle( false );
+            }
+
+
+            public void partDeactivated( IWorkbenchPartReference ref )
+            {
+                // do nothing
+            }
+
+
+            public void partOpened( IWorkbenchPartReference ref )
+            {
+                // do nothing
+            }
+
+
+            public void partHidden( IWorkbenchPartReference ref )
+            {
+                if ( ref.getPart( false ) == lastActiveEditor && lastActiveEditor != null )
+                {
+                    updateTitle( true );
+                }
+            }
+
+
+            public void partVisible( IWorkbenchPartReference ref )
+            {
+                if ( ref.getPart( false ) == lastActiveEditor && lastActiveEditor != null )
+                {
+                    updateTitle( false );
+                }
+            }
+
+
+            public void partInputChanged( IWorkbenchPartReference ref )
+            {
+                // do nothing
+            }
+        } );
+
+    }
+
+
+    /**
+     * Computes the title.
+     * 
+     * @return the computed title
+     */
+    private 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$
+        }
+
+        if ( currentPage != null )
+        {
+            if ( activeEditor != null )
+            {
+                lastEditorTitle = activeEditor.getTitleToolTip();
+                title = NLS.bind( "{0} - {1}", lastEditorTitle, title ); //$NON-NLS-1$ 
+            }
+            IPerspectiveDescriptor persp = currentPage.getPerspective();
+            String label = ""; //$NON-NLS-1$
+            if ( persp != null )
+            {
+                label = persp.getLabel();
+            }
+            IAdaptable input = currentPage.getInput();
+            if ( input != null )
+            {
+                label = currentPage.getLabel();
+            }
+            if ( label != null && !label.equals( "" ) ) { //$NON-NLS-1$ 
+                title = NLS.bind( "{0} - {1}", label, title ); //$NON-NLS-1$ 
+            }
+        }
+
+        return title;
+    }
+
+
+    /**
+     * Recomputes the 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
+     * @param editorHidden 
+     */
+    private void updateTitle( boolean editorHidden )
+    {
+        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();
+        }
+
+        if ( editorHidden )
+        {
+            activeEditor = null;
+        }
+
+        // Nothing to do if the editor hasn't changed
+        if ( 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();
     }
 
 }