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

Hello,

Unfortunatelly I dont know how to obtain the view in which the MapViewer is displayed in from the  MapPart editor that method ToolManager.setActiveTool is receiving as a parameter, so I had to write a custom implemtation of the view context just for this view (hardcoding the view id). I think that to make it work for the view where the mapviewer is displayed, toolManager.setCurrentEditor(MapPart editor) needs to have a Part object as a parameter instead.

I was able to access the CursorPosition background tool creating a new ToolProxy object from the extension point list (basically this is what the ToolManager does at the beginning for initializing the tools)

                        List<IConfigurationElement> extensionList = ExtensionPointList.getExtensionPointList(Tool.EXTENSION_ID);
                                for( IConfigurationElement element : extensionList ) {
                                    IExtension extension = element.getDeclaringExtension();
                                    String type = element.getName();
                                    if (type.equals("backgroundTool")) { //$NON-NLS-1$
                                        if (element.getAttribute("id").matches("net.refractions.udig.tools.backgroundTool1")){
                                                ToolProxy proxy = new ToolProxy(extension, element, (ToolManager)toolManager);
                                                proxy.setContext(tools);
                                        }
                                    }
                                }

But this solution is not really good, now I have the CursorPosition Tool added twice to the mapviewer, one using the mapEditor context (added by the ToolManager, and does not work), and the other one added manually using the map viewer context (this one works). The right solution would be to access the already creted instance of the CursorPosition tool and update the context.

So, I think It would be great to add a public method to the toolManager to access all tools (currently only available for modal tools), or at least the background tools.

    public List<ToolProxy> getBackgroundTools() {
        return backgroundTools;
    }

or maybe better extend findTool method:


    /**
     * Find a tool with the provided ID.
     * <p>
     * In the current implementation finds only among modal tools.
     * TODO Extend findTool to search for non modal tools
     * @param toolID toolId to search for
     * @return Modal tool if found, or null
     */
        public Tool findTool(String toolID) {
                for(ModalToolCategory category : modalCategories){
                        for (ModalItem item : category) {
                                if(toolID.equals(item.getId())){
                                        return ((ToolProxy)item).getTool();
                                }
                        }
                }
                return null;
        }

for this:

    /**
     * Find a tool with the provided ID.
     * <p>
     * @param toolID toolId to search for
     * @return tool if found, or null
     */
        public Tool findTool(String toolID) {
                /*for(ModalToolCategory category : modalCategories){
                        for (ModalItem item : category) {
                                if(toolID.equals(item.getId())){
                                        return ((ToolProxy)item).getTool();
                                }
                        }
                }*/
        ToolProxy tool=searchCategoriesForTool(modalCategories, toolID);
        if( tool!=null ) {
                return tool;
        }
        tool=searchCategoriesForTool(actionCategories, toolID);
        if( tool!=null ) {
                return tool;
        }
        tool=searchCategoriesForTool(menuCategories, toolID);
        if( tool!=null ) {
                return tool;
        }
        for (ToolProxy toolproxy : backgroundTools){
                if (toolID.equals(toolproxy.getId())){
                        return tool;
                }
        }
        return null;
        }

        /*
         *
         */
    private ToolProxy searchCategoriesForTool( List categories, String toolID ) {
        for( Iterator iter = categories.iterator(); iter.hasNext(); ) {
            ToolCategory category = (ToolCategory) iter.next();
            for( Iterator titer = category.iterator(); titer.hasNext(); ) {
                ToolProxy tool = (ToolProxy) titer.next();
                if (toolID.equals(tool.getId())){
                        return tool;
                }
            }
        }
        return null;
    }


Regards,

Joaquín Rodríguez-Guerra Urcelay
________________________________________
De: udig-devel-bounces@xxxxxxxxxxxxxxxxxxxxx [udig-devel-bounces@xxxxxxxxxxxxxxxxxxxxx] En nombre de Jody Garnett [jody.garnett@xxxxxxxxx]
Enviado el: miércoles, 09 de febrero de 2011 22:37
Para: User-friendly Desktop Internet GIS
Asunto: Re: [udig-devel] Extending the RCP Example

You are on the right track. And it would be good if you could contribute the improvement back when you are done. MapViewer should have a context that provides access to some of the viewsite resources (for the view the MapViewer is displayed in).

I am not sure about the ToolManager / ApplicationGIS relationship. I would like to see ApplictionGIS replaced with a workbench service (even if the same static methods are used to lookup the workbench service to call the functionality in the correct window).

I will have to catch up with this topic when I have some time to open up the uDig codebase and review the details of what you are talking about. I am not very familiar with ToolManager; perhaps one of the other developers could be of more assistance.

Jody

On 10/02/2011, at 2:39 AM, Joaquín Rodriguez-Guerra Urcelay wrote:

> 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.
>
> ______________________
>
> _______________________________________________
> 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