Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [udig-devel] Extending the RCP Example

Thanks Jody, 

After what you said I looked at how the IToolContext was implemented by ToolContextImpl and getActionBars method points to the actionBars of the active editor, and since I am using the mapviewer which is a view, I get null. 

I needed to rewrite the following methods for the IToolContext so the tools can access the view ActionBars.
getActionBars
getStatusBar

What is the the best way to do this? without RCP knowledge what I have done is:

- Write MapViewerToolContextImpl as a subclass of ToolContextImpl to rewrite these 2 methods pointing to the view ActionBars, and pointing the StatusLineManager to the one I have added in the mapviewer (class attached at the end of the email)
- Then I would need to rewrite ToolManager.setActiveTool( MapPart editor ) to use the MapViewerToolContextImp when the map is in a view (my case) and not in a editor

        ToolContext tools = null;
        if (editor instanceof MapEditorPart){
        	tools = new ToolContextImpl();
        }
        else  {
        	tools = new MapViewerToolContextImpl();
        }
        
        tools.setMapInternal(editor.getMap());
        setContext(modalCategories, tools);
        setContext(actionCategories, tools);
        setContext(menuCategories, tools);

        for( ToolProxy tool : backgroundTools ) {
            tool.setContext(tools);

But I dont know how to rewrite methods of the ToolManager. I think subclassing is not an option, because ApplicationGIS.getToolManager will return the ToolManager class any ways.
So what I tried next was to invoke toolManager.setCurrentEditor(mapviewer) and just after that, run the part of code from ToolManager.setActiveTool that I wanted to change to set the new context that I wrote above in the CursorPositionTool.

IToolManager toolManager= ApplicationGIS.getToolManager();
            	toolManager.setCurrentEditor(mapviewer);
                ToolContext tools = new MapViewerToolContextImpl();
                tools.setMapInternal(mapviewer.getMap());
                
                for (Tool tool : ((ToolManager)toolManager).getBGToolCategories()){
                	tool.setContext(tools);
                }

This worked for mi :) But unfortunately I had to add the getBGToolCategories() method to the ToolManager to have access to the CursorPosition tool, and I cant modify ToolManager, so... is there any other way to access a background tool??? I need that to change the set the context..

Again, I dont know if I there is a better way to do this using maybe some RCP tricks...

public class MapViewerToolContextImpl extends ToolContextImpl  {
	
    public IStatusLineManager getStatusBar() {
        MapView mapview = getMapView();
        if (mapview == null)
            return null;
        return mapview.getStatusLineManager();
    }
    
    public IActionBars2 getActionBars() {
    	System.out.println("COGIENDO ACTION BARS OK");
        IViewSite site = getMapView().getViewSite();
        if (site == null)
            return null;
        if (getMapView() == null)
            return null;
        return getIActionBars2Wrapper(site.getActionBars(),getMapView());
    }
	
  
   
    private MapView getMapView() {
        IWorkbenchWindow window = getWindow();
        if (window == null)
            return null;
        IWorkbenchPage page = window.getActivePage();
        if (page == null)
            return null;
        IViewPart part = page.findView("net.refractions.udig.tutorials.rcp.mapView");
        if (part == null || !(part instanceof MapView) )
            return null;
        return ((MapView)part);
    }

    private IWorkbenchWindow getWindow() {
        IWorkbench bench = PlatformUI.getWorkbench();
        if (bench == null)
            return null;
        IWorkbenchWindow window = bench.getActiveWorkbenchWindow();
        if (window == null) {
            if (bench.getWorkbenchWindowCount() > 0)
                window = bench.getWorkbenchWindows()[0];
        }
        return window;
    }
    
	/**
	 * Creates an IActionBars2 as a wrapper of an IActionBars.
	 */
	protected IActionBars2 getIActionBars2Wrapper(final IActionBars actionBars, final MapView mapviewer) {
		return new IActionBars2() {
			public ICoolBarManager getCoolBarManager() {
				return new CoolBarManager();
			}
			public void clearGlobalActionHandlers() {
				actionBars.clearGlobalActionHandlers();
			}
			public IAction getGlobalActionHandler(String actionId) {
				return actionBars.getGlobalActionHandler(actionId);
			}
			public IMenuManager getMenuManager() {
				return actionBars.getMenuManager();
			}
			public IStatusLineManager getStatusLineManager() {
				return mapviewer.getStatusLineManager();
			}
			public IToolBarManager getToolBarManager() {
				return actionBars.getToolBarManager();
			}
			public void setGlobalActionHandler(String actionId, IAction handler) {
				actionBars.setGlobalActionHandler(actionId, handler);
			}
			public void updateActionBars() {
				actionBars.updateActionBars();
			}
			public IServiceLocator getServiceLocator() {
				return actionBars.getServiceLocator();
			}
		};
	}
}

Regards,

Joaquín Rodríguez-Guerra Urcelay

Unidad de Sistemas de Procesamiento de Ciencia y de Observación de la Tierra / Science and Earth Observing Processing Systems Unit

GMV AEROSPACE AND DEFENCE, S.A.
Isaac Newton, 11
P.T.M. Tres Cantos
E-28760 Madrid
Tel. +34 91 807 21 00 / 7694
Fax +34 91 807 21 99
www.gmv.com
________________________________________
De: udig-devel-bounces@xxxxxxxxxxxxxxxxxxxxx [udig-devel-bounces@xxxxxxxxxxxxxxxxxxxxx] En nombre de Jody Garnett [jody.garnett@xxxxxxxxx]
Enviado el: martes, 08 de febrero de 2011 23:24
Para: User-friendly Desktop Internet GIS
Asunto: Re: [udig-devel] Extending the RCP Example

Interesting.

So ActionBars are a facade used by a view to get at various stuff (toolbar, menu, status bar etc..).  For the MapEditor those have been configured - to point to the application menus, toolbars and status bar.

Perhaps for the MapViewer they have not been set up correctly? They should be pointing to the view menu, view toolbar and application status bar. The view.getSite() should have the needed information; can the MapViewer determine which view it is in and proceed from there?

Jody


If you stop in a debugger can you check what
On 09/02/2011, at 2:05 AM, Joaquín Rodriguez-Guerra Urcelay wrote:

> Dear all,
>
> Taking a look at udig's source code I am trying to extend the rcp example app.
>
> I fist added the layers view:
>
> mapview.java:
>
>       // Display Layer Manager and set it to use this map.
>       Display.getDefault().asyncExec(new Runnable(){
>            public void run() {
>                       LayersView layersview=(LayersView) ApplicationGIS.getView(false, LayersView.ID);
>                               layersview.setCurrentMap(map);
>            }
>        });
>
> and then some tools:
>
>        // initialize ToolManager
>        IToolManager toolManager= ApplicationGIS.getToolManager();
>        toolManager.setCurrentEditor(mapviewer);
>       IToolBarManager toolbar = getViewSite().getActionBars().getToolBarManager();
>       ...
>       toolbar.add(toolManager.getToolAction(InfoTool.ID, InfoTool.CATEGORY_ID));
>       ...
>
> And now I am trying to add the cursor position background tool. I have checked that method toolManager.setCurrentEditor starts the tool (invoking setActiveTool) by doing:
>
>       ToolContext tools = new ToolContextImpl();
>        tools.setMapInternal(editor.getMap());
>        setContext(modalCategories, tools);
>        setContext(actionCategories, tools);
>        setContext(menuCategories, tools);
>        for( ToolProxy tool : backgroundTools ) {
>            tool.setContext(tools);
>
> and it is here where I get a null value, because the method setContext for CursorPosition invokes getLabel(), which checks this:
> if( getContext().getActionBars()==null )
>
> I dont understand why getContext().getActionBars is null when I invoke toolManager.setCurrentEditor from my mapview.java, but it is != null when it is invoked from the MapEditor.java. The context is defined by the "tools" variable which is created within the setActiveTool method, why is there any difference?? I am invoking toolManager.setCurrentEditor from the CreateViewPart, but I have also tried doing it in partActivated in a partlistener as it is done in mapeditor.java...
>
> I guess that I could subclass CursorPosition coding a new getLabel method to draw the mouse position in other place, but I would like to understand the problem because some other tools,  like the distanceTool for example, also uses getContext().getActionBars(), which is null in my case.
>
> Does this question make any sense? sorry if it is silly, I am learning uDig and RCP at the same time :s
>
> Thank you!
>
> Regards,
>
> Joaquín Rodríguez-Guerra Urcelay
>
>
> ______________________
> This message including any attachments may contain confidential
> information, according to our Information Security Management System,
> and intended solely for a specific individual to whom they are addressed.
> Any unauthorised copy, disclosure or distribution of this message
> is strictly forbidden. If you have received this transmission in error,
> please notify the sender immediately and delete it.
>
> ______________________
> Este mensaje, y en su caso, cualquier fichero anexo al mismo,
> puede contener informacion clasificada por su emisor como confidencial
> en el marco de su Sistema de Gestion de Seguridad de la
> Informacion siendo para uso exclusivo del destinatario, quedando
> prohibida su divulgacion copia o distribucion a terceros sin la
> autorizacion expresa del remitente. Si Vd. ha recibido este mensaje
> erroneamente, se ruega lo notifique al remitente y proceda a su borrado.
> Gracias por su colaboracion.
>
> ______________________
>
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel

_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel

______________________
This message including any attachments may contain confidential 
information, according to our Information Security Management System,
 and intended solely for a specific individual to whom they are addressed.
 Any unauthorised copy, disclosure or distribution of this message
 is strictly forbidden. If you have received this transmission in error,
 please notify the sender immediately and delete it.

______________________
Este mensaje, y en su caso, cualquier fichero anexo al mismo,
 puede contener informacion clasificada por su emisor como confidencial
 en el marco de su Sistema de Gestion de Seguridad de la 
Informacion siendo para uso exclusivo del destinatario, quedando 
prohibida su divulgacion copia o distribucion a terceros sin la 
autorizacion expresa del remitente. Si Vd. ha recibido este mensaje 
 erroneamente, se ruega lo notifique al remitente y proceda a su borrado. 
Gracias por su colaboracion.

______________________



Back to the top