/*******************************************************************************
 * Copyright (c) 2011 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.diag.epics.pvtree;

import org.csstudio.csdata.ProcessVariable;
import org.csstudio.ui.util.AdapterUtil;
import org.csstudio.ui.util.dialogs.ExceptionDetailsErrorDialog;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.handlers.HandlerUtil;

/** Command handler for opening PV Tree on the current selection.
 *  Linked from popup menu that is sensitive to {@link ProcessVariable}
 *  @author Kay Kasemir
 */
@SuppressWarnings("nls")
public class OpenPVTree extends AbstractHandler implements IHandler
{
    @Override
    public Object execute(final ExecutionEvent event) throws ExecutionException
    {
        // Open (new) PV Tree view
        final PVTreeView view;
        try
        {
            final IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();
            view = (PVTreeView) page.showView(PVTreeView.ID, PVTreeView.newInstance(), IWorkbenchPage.VIEW_ACTIVATE);
        }
        catch (Exception ex)
        {
            ExceptionDetailsErrorDialog.openError(HandlerUtil.getActiveShell(event),
                        "Cannot open PVTreeView" , ex);
            return null;
        }

        // If there is a currently selected PV
        // (because this was invoked from a context menu),
        // use that PV
        final ISelection selection = HandlerUtil.getActiveMenuSelection(event);
        final ProcessVariable[] pvs = AdapterUtil.convert(selection, ProcessVariable.class);
        if (pvs != null  &&  pvs.length > 0)
            view.setPVName(pvs[0].getName());
        return null;
    }
}

@@ -1341,87 +1341,210 @@
     
-// Open (new)
+try%0A        %7B%0A           
  PV
- 
 Tree
+View
  view
-%0A        final PVTreeView view;%0A        try%0A        %7B
+ = openView(event);%0A%0A            // If there is a currently selected PV%0A            // (because this was invoked from a context menu),%0A            // use that PV
 %0A   
@@ -1563,26 +1563,27 @@
 al I
-WorkbenchPage page
+Selection selection
  = H
@@ -1606,148 +1606,475 @@
 tive
-Site(event).getPage();%0A            view = (PVTreeView) page.showView(PVTreeView.ID, PVTreeView.newInstance(), IWorkbenchPage.VIEW_ACTIVATE);
+MenuSelection(event);%0A            final ProcessVariable%5B%5D pvs = AdapterUtil.convert(selection, ProcessVariable.class);%0A            if (pvs == null)%0A                return null;%0A            // Set PV name(s)%0A            for (int i=0; i%3Cpvs.length; ++i)%0A            %7B%0A                view.setPVName(pvs%5Bi%5D.getName());%0A                // Open new tree for each additional PV%0A                if (i %3C pvs.length-1)%0A                    view = openView(event);%0A            %7D
 %0A   
@@ -2260,24 +2260,30 @@
  , ex);%0A    
+    %7D%0A
         retu
@@ -2295,431 +2295,416 @@
 ll;%0A
-    
     %7D%0A
-%0A
     
+%0A
     /
-/ If there is a currently selected PV
+** Open (new) PV Tree view%0A     *  @param event%0A     *  @return %7B@link PVTreeView%7D
 %0A     
+*
   
- // (because this was invoked from a context menu),%0A        // use that PV%0A        final ISelection selection = HandlerUtil.getActiveMenuSelection(event);%0A        final ProcessVariable%5B%5D pvs = AdapterUtil.convert(selection, ProcessVariable.class);%0A        if (pvs != null  &&  pvs.length %3E 0)%0A            view.setPVName(pvs%5B0%5D.getName());%0A        return null
+@throws Exception%0A     */%0A    private PVTreeView openView(final ExecutionEvent event) throws Exception%0A    %7B%0A        final IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();%0A        return (PVTreeView) page.showView(PVTreeView.ID, PVTreeView.newInstance(), IWorkbenchPage.VIEW_ACTIVATE)
 ;%0A  

Support context menu with multiple PVs in selection