Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to get the secondary id of a view in a command(How to get the secondary id of a view in a command)
How to get the secondary id of a view in a command [message #545478] Wed, 07 July 2010 18:35 Go to next message
syeed is currently offline syeed
Messages: 19
Registered: June 2010
Junior Member
Hi,
I am creating an RCP application. I need to open multiple instances of the same view but with different data. I did it by setting secondary id for different instances of the same view.

Now, i have a view level command which will operate on each of these instances of this view. But in the command i am not able to identify which instane of the view is currently open and operate on. I used the following code in the command, but it gives only the primary id of the view and eventually the command did not work.

IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
IViewPart findView = page.findView("Views.ProjectTreeView");
ProjectTreeView view = (ProjectTreeView) findView;

Could anyone please give a solution for this. How can i identify which instance of a view is currently activated from a command and operate on it accordingly.

It would be a great help to me. Thanks in advance.

-syeed

Re: How to get the secondary id of a view in a command [message #545494 is a reply to message #545478] Wed, 07 July 2010 23:44 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.
Messages: 621
Registered: July 2009
Senior Member
On 08/07/10 4:05 AM, syeed wrote:
> Could anyone please give a solution for this. How can i identify which
> instance of a view is currently activated from a command and operate on
> it accordingly.

The IViewSite has the secondary id. You can call
view.getViewSite().getSecondaryId()

--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: How to get the secondary id of a view in a command [message #545510 is a reply to message #545494] Thu, 08 July 2010 02:38 Go to previous messageGo to next message
syeed is currently offline syeed
Messages: 19
Registered: June 2010
Junior Member
Hi,
Many thanks for your reply. I tried your solution before but it did not work. Actually my problem is as follows: Please take a look,

i have a graph view called "Views.GraphView". I opened different instances of it from a command called "openGraphView" to show different graphs. the command is as follows:
page.showView("Views.GraphView", Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);

Now, I have a command called "TreeLayout" on this "Views.GraphView" toolbar, which suppose to change the layout of the graph and it will operate on each instace of the view. But for this, i think, i need to identify which instance of the view is active. the command is as follows:

IViewPart findView = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). findView( "Views.GraphView"); //i think in the findView i need to give the id of the view [but how can i put the secondary id!!]

GraphView view = (GraphView) findView;
view.changeLayout(); //i wrote this method in the graph view to change the layout

//i just tried to print the secondary id, but it did not print anyting
System.out.println("From treelayout command:- " + view.getViewSite().getSecondaryId());

Please, provide some solution.

Thanks,
-Syeed

[Updated on: Thu, 08 July 2010 02:39]

Report message to a moderator

Re: How to get the secondary id of a view in a command [message #545558 is a reply to message #545510] Thu, 08 July 2010 04:29 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel Krügler
Messages: 853
Registered: July 2009
Senior Member
On 08.07.2010 08:38, syeed wrote:
> Hi,
> Many thanks for your reply. I tried your solution before but it did not
> work. Actually my problem is as follows: Please take a look,
>
> i have a graph view called "Views.GraphView". I opened different
> instances of it from a command called "openGraphView" to show different
> graphs. the command is as follows:
> page.showView("Views.GraphView", Integer.toString(instanceNum++),
> IWorkbenchPage.VIEW_ACTIVATE);
> Now, I have a command called "TreeLayout" on this "Views.GraphView"
> toolbar, which suppose to change the layout of the graph and it will
> operate on each instace of the view. But for this, i think, i need to
> identify which instance of the view is active. the command is as follows:
>
> IViewPart findView =
> HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). findView(
> "Views.GraphView");

The documentation doesn't say it well, but I believe you need to
change the call to

IViewPart findView =
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). findView(
/primaryId/ + ":" + /secondaryId/);

where /primaryId/ = "Views.GraphView" and
/secondaryId/ = Integer.toString(instanceNum++) in your example.

What definitively works is the overload provided findViewReference
which accepts a secondary id. Now you can obtain the view from
the view reference via the getView method.

HTH & Greetings from Bremen,

Daniel Krügler
Re: How to get the secondary id of a view in a command [message #545574 is a reply to message #545558] Thu, 08 July 2010 05:52 Go to previous messageGo to next message
syeed is currently offline syeed
Messages: 19
Registered: June 2010
Junior Member
Hi,
Thanks for your reply. But my problem still remains. let me try to clarify it:
I have two commands: "OpenGraphView" and "TreeLayout".

"OpenGraphView" command is in the main menu of the application. It is used to open multiple instances of a view (Views.GraphView) by providing an integer value to each instance as a secondary id.

The code for this method:
page.showView("Views.GraphView", Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);

"TreeLayout" command is in the toolbar of the view (Views.GraphView). it will change the layout of the content that is in each instance of the view. To do this I need to identify which instance of the view is currently selected by the user (ie, activated). But i am not able to identify it. I tried the following code in "TreeLayout" command:

//get the active page.
IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();

//get all the view references in a array.
IViewReference[] refs = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). getViewReferences();

//from each of these references i tried to get the secondary ids of the view and try to find the currently active instance of the view. But it worked only for the last instance of the GraphView (not for the one opend earlier).

for (int i = 0; i < refs.length; i++)
{
IViewReference viewReference = page.findViewReference("Views.GraphView", refs[i].getSecondaryId());
if (viewReference != null) {
IViewPart view = viewReference.getView(true);
if (view instanceof GraphView) {
GraphView graphView = (GraphView) view;
graphView.setLayoutManager(); //this is the method in the GraphView that will change the layout.
}
}
}

Please provide some solution.

Thanks,
-Syeed
Re: How to get the secondary id of a view in a command [message #545718 is a reply to message #545574] Thu, 08 July 2010 13:23 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

syeed wrote:
>
> "TreeLayout" command is in the toolbar of the view (Views.GraphView). it
> will change the layout of the content that is in each instance of the
> view. To do this I need to identify which instance of the view is
> currently selected by the user (ie, activated). But i am not able to
> identify it. I tried the following code in "TreeLayout" command:

Why not use
org.eclipse.ui.handlers.HandlerUtil.getActivePart(ExecutionE vent) ?

PW



--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: How to get the secondary id of a view in a command [message #545983 is a reply to message #545718] Fri, 09 July 2010 15:18 Go to previous messageGo to next message
syeed is currently offline syeed
Messages: 19
Registered: June 2010
Junior Member
Hi,
Thanks for your reply. I tried org.eclipse.ui.handlers.HandlerUtil.getActivePart(ExecutionE vent), in my TreeLayout command and correctly identifies the current active instane of a view.

But my main problem is not solved yet.. When i tried to apply the command to one instance, it changes all the instances. I think the problem is somwhere else. I am explaining my problem bellow with the code. Please take a look.. I am preety sure you can provide solution.

I have a GraphViewer in the view (GraphView)which displays graphs. Now different instance of the view displays different graphs. Graph contents are provided by the content providers.
Then in TreeLayout command I identity the corrent active instance of the GraphView(using the method provided earlier), then get the GraphViewer in it and try to change the layout and node names. But the problem is, If i change the layout or rename any node it automatically applied to all the instances of the GraphView. Also if I close the last instace of the GraphView, then the command did not work at all for the others. How can i solve it? Its really a problem for me.. please help.

My code is someting like this:

//view to create graphs.
public class GraphView extends ViewPart implements IZoomableWorkbenchPart{

public static final String ID = "Views.GraphView";
static GraphViewer viewer = null;

public void createPartControl(Composite parent) {

//creates the GraphViewer
viewer = new GraphViewer(parent, SWT.NONE);

//setting the content provider for the graph.
viewer.setContentProvider( new GraphViewContentProvider());

//setting the lable provider for the graph
viewer.setLabelProvider(new GraphViewLabelProvider());

//setting the initial layout for the graph.
viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING)) ;
viewer.setInput(new Object());

//creating the toolbar to hold the zoom menu.
fillToolBar();
}


/**
*Returns the GraphViewer. this method is called from TreeLayout
command*/
public GraphViewer getGraphViewer()
{
return viewer;
}

/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
//viewer.getControl().setFocus();
}

private void fillToolBar() {
ZoomContributionViewItem toolbarZoomContributionViewItem = new ZoomContributionViewItem(this);
IActionBars bars = getViewSite().getActionBars();
bars.getMenuManager().add(toolbarZoomContributionViewItem);
}

@Override
public AbstractZoomableViewer getZoomableViewer() {
return viewer;
}

}




TreeLayout command: Identify the current active instace of the GraphView and change the layout and node name in the GraphViewer of that instance.

//Get the current active instance of the GraphView
IWorkbenchPart view = org.eclipse.ui.handlers.HandlerUtil.getActivePart(event);
IViewPart part = (IViewPart)view;
GraphView gView = (GraphView)part;

//Get the GraphViewer on this GraphView.
GraphViewer viewer = gView.getGraphViewer();

//Change the layout. But I applied to the last opened instance of the GraphView.. not on others.
viewer.setLayoutAlgorithm(new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);


//change the node name of the graph. But it changes node names in the current active instance as well as all the instance opende after it.
List<GraphNode> nodeList = viewer.getGraphControl().getNodes();
for (Object c : nodeList) ((GraphNode) c).setText("rajit");



OpenGraphView command: Opens different instances of the GraphView with secondary id's.

page.showView("Views.GraphView", "secondaryId"+ Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);

Many thanks in advance.
-Syeed

Re: How to get the secondary id of a view in a command [message #545994 is a reply to message #545983] Fri, 09 July 2010 15:54 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

syeed wrote:

>
> My code is someting like this:
>
> //view to create graphs.
> public class GraphView extends ViewPart implements IZoomableWorkbenchPart{
>
> public static final String ID = "Views.GraphView";
> static GraphViewer viewer = null;

^^^ static viewer with a parent Composite.

Since you are setting that per instance, that's definitely broken.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: How to get the secondary id of a view in a command [message #545998 is a reply to message #545994] Fri, 09 July 2010 16:24 Go to previous message
syeed is currently offline syeed
Messages: 19
Registered: June 2010
Junior Member
You Man really rocks... It works finally..
Thanks a lot.

-Syeed
Previous Topic:Anyone can suggest a license control solution for RCP application
Next Topic:RCP application freezes
Goto Forum:
  


Current Time: Thu Apr 17 11:27:52 EDT 2014

Powered by FUDForum. Page generated in 0.05545 seconds