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 ビューのポップアップ メニューを作成しましょう。

 <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;

参考:Package Explorer ビューにポップアップメニューを作成する

于 2013-03-05T16:23:51.690 に答える