ものすごい無駄なことしてるだろうから後でなおさなきゃ…
とりあえずメモ
package org.okazuki.sample.plugin.actions; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; public class CreateFileAction implements IWorkbenchWindowActionDelegate { private IWorkbenchWindow window; private IJavaProject javaProject; public void dispose() { } public void init(IWorkbenchWindow window) { this.window = window; } public void run(IAction action) { IFolder srcRes = javaProject.getProject().getFolder("src"); if (srcRes == null) { MessageDialog.openConfirm(window.getShell(), "情報", "srcフォルダがみつからん"); return; } createFile(srcRes); } private void createFile(IFolder srcFolder) { IFile helloFile = srcFolder.getFile("hello.txt"); try { ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("Hello world".getBytes()); os.flush(); helloFile.create(new ByteArrayInputStream(os.toByteArray()), true, new NullProgressMonitor()); } catch (Exception e) { e.printStackTrace(); } } public void selectionChanged(IAction action, ISelection selection) { if (selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object selectionObject = structuredSelection.getFirstElement(); if (selectionObject instanceof IJavaElement) { IJavaElement javaElement = (IJavaElement) selectionObject; javaProject = javaElement.getJavaProject(); } } } }