Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » How to programmatically customize perspective
How to programmatically customize perspective [message #645088] Wed, 15 December 2010 14:52 Go to next message
KHALFAOUI Imen is currently offline KHALFAOUI ImenFriend
Messages: 9
Registered: April 2010
Junior Member
Hi, I'm extending Eclipse using the Eclipse plug-in infrastructure, and I've come into a problem:

I would like to modify the visibility of my "Menu" (defined in my plugin.xml) programmatically depending on an preferences. Example: When my variable is TRUE. We do: getMenu("ID").setVisible("true"); (just an example)

Is this Possible? if yes, How can I do it?

I found that we can use the class "ApplicationActionBarAdvisor" or "ApplicationWorkbenchWindowAdvisor " to SHOW or HIDE actionSet, using:

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

window.getActivePage().hideActionSet(actionSetID); window.getActivePage().showActionSet(actionSetID);

But I don't know how to use this class? wHere must I defind it in my plugin? in Activator.java?

Some help, please.

Best regards. Imen.
Re: How to programmatically customize perspective [message #645144 is a reply to message #645088] Wed, 15 December 2010 16:56 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Hi Imen,
sure you can do that. But I suggest a different approach.
Let's see if it works for your needs.

To begin with you have to extend AbstractSourceProvider, here is an example:

package com.rcpvision;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.ui.AbstractSourceProvider;
import org.eclipse.ui.ISources;

public class SessionSourceProvider extends AbstractSourceProvider {
	public final static String SESSION_USER_CAN_SEE_PREFS = "com.rcpvision.session.user-can-see-preferences";
	private final static String CAN_SEE = "canSee";
	private final static String CANNOT_SEE = "cannotSee";
	boolean canSee = true;

	@Override
	public String[] getProvidedSourceNames() {
		return new String[] { SESSION_USER_CAN_SEE_PREFS };
	}

	@Override
	public Map<String, String> getCurrentState() {
		Map<String, String> currentState = new HashMap<String, String>(1);
		String currentState1 = canSee ? CAN_SEE : CANNOT_SEE;
		currentState.put(SESSION_USER_CAN_SEE_PREFS, currentState1);
		return currentState;
	}

	@Override
	public void dispose() {
	}

	public void setUserCanSeePreferences(boolean canSee) {
		if (this.canSee == canSee)
			return; // no change
		this.canSee = canSee;
		String currentState = canSee ? CAN_SEE : CANNOT_SEE;
		fireSourceChanged(ISources.WORKBENCH, SESSION_USER_CAN_SEE_PREFS, currentState);
	}
}


then define it in plugin.xml
	<extension 
      point="org.eclipse.ui.services"> 
   <sourceProvider 
         provider="com.rcpvision.SessionSourceProvider"> 
      <variable 
            name="com.rcpvision.session.user-can-see-preferences" 
            priorityLevel="workbench"> 
      </variable> 
   </sourceProvider> 
</extension>


use it, again in, in plugin.xml
			<menu
               label="Show my view">
            <command
                  commandId="org.eclipse.ui.views.showView"
                  label="My View"
                  style="push">
               <parameter
                     name="org.eclipse.ui.views.showView.viewId"
                     value="com.rcpvision.my.view">
               </parameter>
 			<visibleWhen
	          checkEnabled="false"> 
	            <with 
	                  variable="com.rcpvision.session.user-can-see-preferences"> 
	               <equals 
	                     value="canSee"> 
	               </equals> 
	            </with> 
	         </visibleWhen> 
            </command>
         </menu>         


and ... change the visibility at runtime
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 
		ISourceProviderService service = (ISourceProviderService) window.getService(ISourceProviderService.class); 
		SessionSourceProvider sessionSourceProvider = (SessionSourceProvider) service.getSourceProvider(SessionSourceProvider.SESSION_USER_CAN_SEE_PREFS); 
		sessionSourceProvider.setUserCanSeePreferences( true/false ); 		


is this what you were looking for?
Cheers

Vincenzo

Vincenzo Caselli
RCP Vision
Re: How to programmatically customize perspective [message #645636 is a reply to message #645144] Sun, 19 December 2010 09:58 Go to previous messageGo to next message
KHALFAOUI Imen is currently offline KHALFAOUI ImenFriend
Messages: 9
Registered: April 2010
Junior Member
Hi Vincenzo,

Thank's for your reply.

I just have a question regarding this solution. Is this allows me to change the visibility of my MENU when Eclipse is running.
For example: According to the parameter MENU_VISIBLE in preference page of my plugin which takes the value "TRUE" or "FALSE". when the user click on "OK" (of the preference page) I do this:
sessionSourceProvider.setUserCanSeePreferences(getValue("MENU_VISIBLE")); 


Also, for : "and ... change the visibility at runtime..."
What do you mean by "Runtime"?
I try the "Activator.java" but the "window" (activeWorkbenchWindow) was NULL.

sorry, I'm a beginner in the development of plugins.

Best Regards

[Updated on: Sun, 19 December 2010 10:00]

Report message to a moderator

Re: How to programmatically customize perspective [message #645655 is a reply to message #645636] Sun, 19 December 2010 14:18 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Hi Imen,
regarding your question:
Quote:
What do you mean by "Runtime"?

I simply mean that when the line
sessionSourceProvider.setUserCanSeePreferences( true/false );

is executed, then instantly the visibility of the menu item changes.
As far as your other question:
Quote:
I try the "Activator.java" but the "window" (activeWorkbenchWindow) was NULL.


I don't understand. Why you are referring to an Activator?
Use
PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 

instead, just as in the last piece of code of my previous post.
Let me know.
Cheers

Vincenzo Caselli
RCP Vision
Re: How to programmatically customize perspective [message #645658 is a reply to message #645655] Sun, 19 December 2010 15:15 Go to previous messageGo to next message
KHALFAOUI Imen is currently offline KHALFAOUI ImenFriend
Messages: 9
Registered: April 2010
Junior Member
Hi Vincenzo,

I have an preference variable and I would like to verify it at Eclipse's startup to show or hide my menu?
But at the Activator.java (start() method) the "ActiveWorkbenchWindow" doesn't yet exist (value == NULL).

Your solution works fine with a Menu extension. What about ActionSet and popupMenu? How can I change the visibility?

Best Regards.
Re: How to programmatically customize perspective [message #645660 is a reply to message #645658] Sun, 19 December 2010 15:56 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Imen,
it may be that you need to put a listener that react when a view or a perspective is opened, see how to use IPartListener2 for this.
The solution proposed works everywhere you can put a "visibleWhen" or "activeWhen" option.
If you are using a preference variable, chances are that there is a more direct way to handle the visibility. But I don't have a tested solution for this.
As an example of what I mean, see the following pieces of plugin.xml.
This command handler manages the enabling of the command itself based on the type of the current selected object
<handler
            class="com.rcpvision.MyHandler"
            commandId="com.rcpvision.command.my.handler">
               <activeWhen
                     checkEnabled="false">
				<with 
		            variable="selection"> 
			         <iterate 
			               operator="or"> 
			            <instanceof 
			                  value="test.Customer"> 
			            </instanceof> 
			         </iterate> 
			      </with> 
               </activeWhen>
      </handler>

while this controls the visibility upon the perspective selected
 <visibleWhen
                     checkEnabled="false">
                  <with
                        variable="activeWorkbenchWindow.activePerspective">
                     <equals
                           value="com.rcpvision.perspective.orders">
                     </equals>
                  </with>
               </visibleWhen>

with this approach you don't have to write Java code, just use a declarative way.
For this see also
http://wiki.eclipse.org/Command_Core_Expressions

Cheers
Vincenzo
Re: How to programmatically customize perspective [message #645759 is a reply to message #645660] Mon, 20 December 2010 12:42 Go to previous messageGo to next message
KHALFAOUI Imen is currently offline KHALFAOUI ImenFriend
Messages: 9
Registered: April 2010
Junior Member
Hi Vincenzo,

Thank you for the interest you have shown in my problem.

I tested the option "visibleWhen" with an ActionSet and a Popupmenu but it didn't work Sad . There is a piece of my plugin.xml for an ActionSet example:

 <actionSet
        id="actions"
        label="Actions"
        visible="true">
     <action
           class="com.plugin.BuildAction"
           id="com.plugin.action.build"
           label="Build"
           menubarPath="build"
           style="push">
     </action> 
     <visibleWhen
	          checkEnabled="false"> 
	            <with 
	                  variable="com.rcpvision.session.user-can-see-preferences"> 
	               <equals 
	                     value="canSee"> 
	               </equals> 
	            </with> 
	   </visibleWhen>
  </actionSet>


Quote:
If you are using a preference variable, chances are that there is a more direct way to handle the visibility. But I don't have a tested solution for this.

can you give me more informations so I can do research (about using preferences variable in the element visibleWhen)

Best Regards
Re: How to programmatically customize perspective [message #645976 is a reply to message #645759] Tue, 21 December 2010 12:44 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Hi Imen,
indeed, from what I see here
http://wiki.eclipse.org/Command_Core_Expressions
it seems that there are no variables which references to Preferencies variables, so just follow the other way (via the SessionSourceProvider).

As far as the problem about the action and the visibleWhen option, I can say that I rarely use actions so I cannot be of much help.
Can't you use Commands instead (so much powerful and flexible)?

For what I see, aside from the fact that you put the "visibleWhen" block outside from the "action" block (just do this interactively via the "Extensions" tab: this will avoid this kind of errors), I see that "visibleWhen" is not available into an "action" block .. I'm tring it just now).
So: I don't know exactly how to manage this with actions, but I would study these pages:
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench_actionfilters.htm
ftp://ftp.unicamp.br/pub5/eclipse/downloads/documentation/2. 0/html/plugins/org.eclipse.platform.doc.isv/reference/extens ion-points/actionExpressions.html
Cheers

Vincenzo Caselli
RCP Vision
Re: How to programmatically customize perspective [message #650239 is a reply to message #645976] Sun, 23 January 2011 15:52 Go to previous messageGo to next message
KHALFAOUI Imen is currently offline KHALFAOUI ImenFriend
Messages: 9
Registered: April 2010
Junior Member
Hi Vincenzo,

I did what you said. I added the <visibleWhen> element to an menu using the preference "...user-can-see-preferences" (like your first comment). Also, I added a window Listener in the activator:
getWorkbench().addWindowListener(new windowListener());


So I can change the visibility when Eclipse start up depending on my prefrence variable value by implementing the methods bellow:
public void windowOpened(IWorkbenchWindow window) {...}
public void windowActivated(IWorkbenchWindow window){...}


I use these piece of code to controle visibility of ActionSets, in the windowListener:
window.getActivePage().(hide|show)ActionSet("id");

Thank you a lot for your help and your time.

But the visibility of a popup menu still doesn't work!!! How can I control it depending on a preference variable value?

Best regards.
Imen.
Re: How to programmatically customize perspective [message #651191 is a reply to message #650239] Fri, 28 January 2011 10:14 Go to previous message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Hi Khalfaoui,
so you are you calling
sessionSourceProvider.setUserCanSeePreferences()

and nothing changes on the menu?
Make sure you are not using String constants like "true" and "false". I recently added a warning on the original article
http://www.rcp-vision.com/index.php?option=com_content&v iew=article&id=113:condizionare-la-visibilita-di-voci-di -menu&catid=40:tutorialeclipse&Itemid=73&lang=en

where I explain that using constants like these cannot work since,in this case, they are converted into Booleans and equals fails.
Is this your case?

Vincenzo Caselli
RCP Vision

[Updated on: Fri, 28 January 2011 10:16]

Report message to a moderator

Previous Topic:Instatiate an inner class usign createExecutableExtension ()
Next Topic:how to automate Flex-compiling in eclipse
Goto Forum:
  


Current Time: Wed Aug 12 16:34:12 GMT 2020

Powered by FUDForum. Page generated in 0.01905 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top