0

我正在尝试在 Eclipse 中构建一个弹出菜单。实际上,该插件有一个弹出操作,当您在文件中单击鼠标右键时会显示一个新选项。我需要知道我右键单击的文件名和项目名称。有谁知道该怎么做?

谢谢。

4

2 回答 2

1
IStructuredSelection currentSelection = (IStructuredSelection)getContext().getSelection();

if(!currentSelection.isEmpty() && ResourceSelectionUtil.allResourcesAreOfType(currentSelection, IResource.PROJECT | IResource.FOLDER | IResource.FILE)){
    IResource resource = (IResource)currentSelection.getFirstElement();
}
于 2012-09-19T16:01:34.240 回答
0

首先,让我们为 PackageExplorer View 创建一个弹出菜单。

 <plugin>
   <extension
         point="org.eclipse.ui.commands">
      <command
            categoryId="TestPopupMenu.commands.category"
            id="TestPopupMenu.commands.sampleCommand"
            name="Sample Command">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="testpopupmenu.handlers.SampleHandler"
            commandId="TestPopupMenu.commands.sampleCommand">
      </handler>
   </extension>

   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
            <command
                  commandId="TestPopupMenu.commands.sampleCommand"
                  id="TestPopupMenu.menus.sampleCommand"
                  mnemonic="S">

                    <visibleWhen>
                           <with variable="activeMenuSelection">
                            <iterate
                                 ifEmpty="false">
                             <adapt type="org.eclipse.core.resources.IResource">
                               <test property="org.eclipse.core.resources.name" value="*.*" />
                             </adapt>
                            </iterate>
                           </with>
                    </visibleWhen>
            </command>
      </menuContribution>
   </extension>
</plugin>

然后,将以下代码添加到execute()处理程序的方法中,它将在控制台中打印出绝对路径:

IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
//get selection service 
ISelectionService service = window.getSelectionService();
//get selection
IStructuredSelection structured = (IStructuredSelection) service
        .getSelection();
//get selected file
IFile file = (IFile) structured.getFirstElement();
//get the path
IPath path = file.getLocation();

System.out.println(path.toPortableString());
return null;

参考:在包资源管理器视图中创建弹出菜单

于 2013-03-05T16:23:51.690 回答