Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[imp-dev] Try out this patch!

Hi guys,

This is a patch for imp.runtime which allows IDE developers to add
arbitrary things to:
  a) the editor popup menu
  b) the [ct]oolbar
  c) the status bar
  d) the menu bar at the top of the screen

It reuses an Eclipse design mostly, except for the popup menu which is
built on the previous
implementation of ILanguageActionContributor.

The nice thing is that for toolbar,statusbar and menubar the additions
are encapsulated in
a 'subbar', which can be activated and deactivated at leisure. I use
this to hide stuff when
an editor is out of focus, even though all IMP editors have the
UniversalEditor editorid.

Note: this hiding/reappearing stuff does not work for toolbars. It
seems that toolbars
are supposed to be static in Eclipse, as opposed to the menubar and
the status bar.

I've used this feature to make editor additions for ASF+SDF
Meta-Environment on Eclipse,
the status bar now has a dropdown box to select the 'language' for example.
Also, we've made a new extension point which uses the new
ILanguageActionContributor
to generate (nested) menu's from XML descriptions. This also works fine.

In other words, I've tested the code, I like it. Shall I commit it? It
will break all existing
implementations of ILanguageActionContributor which need to be changed to extend
DefaultLanguageActionContributor, after which they will work again,
with a @deprecation warning.

Cheers,

Jurgen
Index: /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/editor/UniversalEditor.java
===================================================================
--- /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/editor/UniversalEditor.java	(revision 17352)
+++ /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/editor/UniversalEditor.java	(working copy)
@@ -78,6 +78,7 @@
 import org.eclipse.imp.services.ISourceHyperlinkDetector;
 import org.eclipse.imp.services.base.DefaultAnnotationHover;
 import org.eclipse.imp.services.base.TreeModelBuilderBase;
+import org.eclipse.imp.ui.DefaultPartListener;
 import org.eclipse.imp.ui.textPresentation.HTMLTextPresenter;
 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
 import org.eclipse.jface.action.Action;
@@ -83,6 +84,8 @@
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IStatusLineManager;
+import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.dialogs.IDialogSettings;
@@ -120,7 +123,6 @@
 import org.eclipse.jface.text.presentation.IPresentationRepairer;
 import org.eclipse.jface.text.presentation.PresentationReconciler;
 import org.eclipse.jface.text.source.Annotation;
-import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
 import org.eclipse.jface.text.source.IAnnotationHover;
 import org.eclipse.jface.text.source.IAnnotationModel;
 import org.eclipse.jface.text.source.IAnnotationModelListener;
@@ -142,6 +144,8 @@
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.IActionBars2;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IFileEditorInput;
@@ -150,6 +154,8 @@
 import org.eclipse.ui.IStorageEditorInput;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.SubActionBars;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.editors.text.TextEditor;
 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
@@ -228,6 +234,10 @@
 
     private ICharacterPairMatcher fBracketMatcher;
 
+	private SubActionBars fActionBars;
+
+	private DefaultPartListener fRefreshContributions;
+
     private static final String BUNDLE_FOR_CONSTRUCTED_KEYS= MESSAGE_BUNDLE;//$NON-NLS-1$
 
     static ResourceBundle fgBundleForConstructedKeys= ResourceBundle.getBundle(BUNDLE_FOR_CONSTRUCTED_KEYS);
@@ -329,10 +339,14 @@
 		ILanguageActionsContributor con=  iter.next();
 
 		try {
+			con.contributeToEditorMenu(this, menu);
+			
+			// TODO: remove getEditorActions support, it is replaced by contributoToEditorMenu
 		    IAction[] conActions= con.getEditorActions(this);
 
-		    for(int i=0; i < conActions.length; i++)
-			editorActions.add(conActions[i]);
+		    for(int i=0; i < conActions.length; i++) {
+			  editorActions.add(conActions[i]);
+		    }
 		} catch(Exception e) {
 		    RuntimePlugin.getInstance().logException("Unable to create editor actions for contributor " + con, e);
 		}
@@ -711,11 +725,91 @@
 		ErrorHandler.reportError("Could not create part", e);
 	    }
 	}
+	    initializeEditorContributors();
     }  
     
+    /**
+	 * Adds elements to toolbars, menubars and statusbars
+	 * 
+	 */
+	private void initializeEditorContributors() {
+		if (fLanguage != null) {
+			addEditorActions();
+			registerEditorContributionsActivator();
+		}
+	}
+
+	/**
+	 * Makes sure that menu items and status bar items dissappear as the editor
+	 * is out of focus, and reappear when it gets focus again. This does
+	 * not work for toolbar items for unknown reasons, they stay visible.
+	 *
+	 */
+	private void registerEditorContributionsActivator() {
+		fRefreshContributions = new DefaultPartListener() {
+			private UniversalEditor editor = UniversalEditor.this;
+
+			public void partActivated(IWorkbenchPart part) {
+				if (part == editor) {
+					editor.fActionBars.activate();
+					editor.fActionBars.updateActionBars();
+				}
+			}
+
+			public void partDeactivated(IWorkbenchPart part) {
+				if (part == editor) {
+					editor.fActionBars.deactivate();
+					editor.fActionBars.updateActionBars();
+				}
+			}
+		};
+		getSite().getPage().addPartListener(fRefreshContributions);
+	}
+	
+	private void unregisterEditorContributionsActivator() {
+	  getSite().getPage().removePartListener(fRefreshContributions);
+	}
+
+	/**
+	 * Uses the LanguageActionsContributor extension point to add
+	 * elements to (sub) action bars. 
+	 *
+	 */
+	private void addEditorActions() {
+		final IActionBars allActionBars = getEditorSite().getActionBars();
+		if (fActionBars == null) {
+			Set<ILanguageActionsContributor> contributors = ServiceFactory
+					.getInstance().getLanguageActionsContributors(fLanguage);
+
+			if (allActionBars instanceof IActionBars2) {
+				fActionBars = new SubActionBars(allActionBars);
+
+				IStatusLineManager status = fActionBars.getStatusLineManager();
+				IToolBarManager toolbar = fActionBars.getToolBarManager();
+				IMenuManager menu = fActionBars.getMenuManager();
+
+				for (ILanguageActionsContributor c : contributors) {
+					c.contributeToStatusLine(this, status);
+					c.contributeToToolBar(this, toolbar);
+					c.contributeToMenuBar(this, menu);
+				}
+
+				fActionBars.updateActionBars();
+				allActionBars.updateActionBars();
+			}
+		}
+		allActionBars.updateActionBars();
+	}
+	
     public void dispose() {
 	// Remove the pref store listener *before* calling super; super nulls out the pref store.
         getPreferenceStore().removePropertyChangeListener(fPrefStoreListener);
+        unregisterEditorContributionsActivator();
+        
+        if (fActionBars != null) {
+          fActionBars.dispose();
+        }
+        
         super.dispose();
     }
 
@@ -748,9 +842,9 @@
                     sb.append(fences[i][0]);
                     sb.append(fences[i][1]);
                 }
-                fBracketMatcher= new DefaultCharacterPairMatcher(sb.toString().toCharArray());
-                support.setCharacterPairMatcher(fBracketMatcher);
-                support.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS, MATCHING_BRACKETS_COLOR);
+//                fBracketMatcher= new DefaultCharacterPairMatcher(sb.toString().toCharArray());
+//                support.setCharacterPairMatcher(fBracketMatcher);
+//                support.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS, MATCHING_BRACKETS_COLOR);
             }
         }
         super.configureSourceViewerDecorationSupport(support);
Index: /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/services/base/DefaultLanguageActionsContributor.java
===================================================================
--- /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/services/base/DefaultLanguageActionsContributor.java	(revision 0)
+++ /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/services/base/DefaultLanguageActionsContributor.java	(revision 0)
@@ -0,0 +1,32 @@
+package org.eclipse.imp.services.base;
+
+import org.eclipse.imp.editor.UniversalEditor;
+import org.eclipse.imp.services.ILanguageActionsContributor;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IStatusLineManager;
+import org.eclipse.jface.action.IToolBarManager;
+
+public class DefaultLanguageActionsContributor implements ILanguageActionsContributor {
+
+	public void contributeToEditorMenu(final UniversalEditor editor, IMenuManager menuManager) {
+		// do nothing, subclasses can override to implement
+	}
+
+	public void contributeToStatusLine(final UniversalEditor editor, IStatusLineManager statusLineManager) {
+       // do nothing, subclasses can override to implement
+	}
+
+	public void contributeToToolBar(final UniversalEditor editor, IToolBarManager toolbarManager) {
+       // do nothing, subclasses can override to implement
+	}
+
+	public void contributeToMenuBar(final UniversalEditor editor, IMenuManager menuManager) {
+	       // do nothing, subclasses can override to implement
+	}
+	
+	@Deprecated
+	public IAction[] getEditorActions(UniversalEditor editor) {
+       return null;
+	}
+}
Index: /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/services/ILanguageActionsContributor.java
===================================================================
--- /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/services/ILanguageActionsContributor.java	(revision 17352)
+++ /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/services/ILanguageActionsContributor.java	(working copy)
@@ -18,7 +18,56 @@
 import org.eclipse.imp.editor.UniversalEditor;
 import org.eclipse.imp.language.ILanguageService;
 import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IStatusLineManager;
+import org.eclipse.jface.action.IToolBarManager;
 
 public interface ILanguageActionsContributor extends ILanguageService {
-    public IAction[] getEditorActions(UniversalEditor editor);
+    /**
+     * Implement this method to add actions to a submenu labelled by the
+     * language name.
+     * 
+     * @deprecated use contributeToEditorMenu() instead
+     * @param editor
+     * @return
+     */
+	@Deprecated()
+	public IAction[] getEditorActions(UniversalEditor editor);
+    
+    /**
+     * Implement this method to add any kind of action to the popup
+     * menu of an editor.
+     * 
+     * @param editor
+     * @param menuManager
+     */
+    public void contributeToEditorMenu(final UniversalEditor editor, IMenuManager menuManager);
+    
+    /**
+     * Implement this method to add any kind of element to the toolbar
+     * for this editor.
+     * 
+     * @param editor
+     * @param toolbarManager
+     */
+    public void contributeToToolBar(final UniversalEditor editor, IToolBarManager toolbarManager);
+    
+    
+    /**
+     * Implement this method to add any kind of element to the statusline
+     * for this editor.
+     * 
+     * @param editor
+     * @param toolbarManager
+     */
+    public void contributeToStatusLine(final UniversalEditor editor, IStatusLineManager statusLineManager);
+
+    /**
+     * Implement this method to add any kind of element to the menu bar
+     * for this editor.
+     * 
+     * @param editor
+     * @param toolbarManager
+     */
+	public void contributeToMenuBar(final UniversalEditor editor, IMenuManager menu);
 }
\ No newline at end of file
Index: /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/ui/DefaultPartListener.java
===================================================================
--- /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/ui/DefaultPartListener.java	(revision 0)
+++ /ufs/jurgenv/glt/src/org.eclipse.imp.runtime/src/org/eclipse/imp/ui/DefaultPartListener.java	(revision 0)
@@ -0,0 +1,28 @@
+package org.eclipse.imp.ui;
+
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IWorkbenchPart;
+
+public class DefaultPartListener implements IPartListener {
+
+	public void partActivated(IWorkbenchPart part) {
+//		 override this
+	}
+
+	public void partBroughtToTop(IWorkbenchPart part) {
+//		 override this
+	}
+
+	public void partClosed(IWorkbenchPart part) {
+//		 override this
+	}
+
+	public void partDeactivated(IWorkbenchPart part) {
+//		 override this
+	}
+
+	public void partOpened(IWorkbenchPart part) {
+//		 override this
+	}
+
+}

Back to the top