Open Yhzhtk opened 10 years ago
插件完成的功能是选择项目,右键包含插件选项,点击具体选项时进行一些操作。
下面列出几个简单的代码编写点。
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <plugin> <extension point="org.eclipse.ui.popupMenus"> <objectContribution adaptable="true" objectClass="org.eclipse.core.resources.IResource" id="info.yhzhtk.contribution"> <menu label="易查接口文档" // 添加到右键菜单的名称 path="additions" // 路径,action要关联 id="info.yhzhtk.menu" // 标志 > <separator name="additions"> // 以additions区分 </separator> </menu> <action label="导出文档" // 显示名称 class="info.yhzhtk.popup.actions.ExportAction" // 处理类 menubarPath="info.yhzhtk.menu/additions" // 所在位置在menu的下一级 enablesFor="1" // 当选择几个文件的时候可用,*表示多个 id="info.yhzhtk.exportAction" // 标志 > </action> // 此处可有多个action </objectContribution> </extension> </plugin>
run
selectionChanged
在info.yhzhtk.popup.actions.ExportAction 中已自动生成了run和selectionChanged,需要实现具体方法,调用显示对话框完成指定事件。
info.yhzhtk.popup.actions.ExportAction
/** * @see IActionDelegate#run(IAction) */ public void run(IAction action) { ExportDialog dlg = new ExportDialog(shell, projectPath, projectOutFile); int code = dlg.open(); if (code == Dialog.OK) { MessageDialog.openInformation(shell, "导出文档", dlg.lastResultString); OpenUtil.openHtml(dlg.lastOutFile); } else if (code == Dialog.CANCEL) { // MessageDialog.openInformation(shell, "取消导出文档", ExportDialog.lastResultString); } } /** * @see IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { // 获取当前选中项目的路径,并存在projectPath中 if (selection instanceof IStructuredSelection) { IStructuredSelection selection1 = (IStructuredSelection) selection; if (selection1.size() == 1) { Object element = selection1.getFirstElement(); IProject project = null; if (element instanceof IProject) { project = (IProject) element; } else if (element instanceof IAdaptable) { project = (IProject) ((IAdaptable) element) .getAdapter(IProject.class); } if (project != null) { projectPath = Platform.getInstanceLocation().getURL().getFile(); if(projectPath.trim().equals("")){ IWorkspace workspace = ResourcesPlugin.getWorkspace(); projectPath = workspace.getRoot().getLocation().toFile().getPath().toString(); } if (projectPath.startsWith("/")) { projectPath = projectPath.substring(1); } projectPath += project.getFullPath().toString(); projectPath = new File(projectPath).getAbsolutePath(); projectOutFile = projectPath + "/doc/doc.html"; File outFile = new File(projectOutFile); projectOutFile = outFile.getAbsolutePath(); // doc 文件夹不存在则创建 if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdir(); } } } } }
ExportDialog
新建类 ExportDialog 继承自 TitleAreaDialog
TitleAreaDialog
package info.yhzhtk.plugin.dialog; import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; import org.eclipse.jface.dialogs.IMessageProvider; import org.eclipse.jface.dialogs.TitleAreaDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ExportDialog extends TitleAreaDialog { public boolean isLastSucess = false; public String lastOutFile = ""; public String lastResultString = "没有处理"; private String projectPath; private String projectOutFile; private Shell shell; private Text projectRoot; private Text projectDesc; private Text projectOut; public ExportDialog(Shell parentShell) { super(parentShell); } public ExportDialog(Shell parentShell, String projectPath, String projectOutFile) { super(parentShell); this.shell = parentShell; this.projectPath = projectPath == null ? "null" : projectPath; this.projectOutFile = projectOutFile == null ? "null" : projectOutFile; } @Override public void create() { super.create(); setTitle("导出接口文档"); // 标题 setMessage("导出之前请确保文档编写完成,本操作将导出html格式的文档", IMessageProvider.INFORMATION); // 顶部内容 } @Override protected Control createDialogArea(Composite parent) { Composite area = (Composite) super.createDialogArea(parent); Composite container = new Composite(area, SWT.NONE); container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); GridLayout layout = new GridLayout(2, false); // 使用gridlayout布局 container.setLayout(layout); createConfigView(container); return area; } private void createConfigView(Composite container) { Label l1 = new Label(container, SWT.NONE); l1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false)); l1.setText("路径:"); projectRoot = new Text(container, SWT.BORDER | SWT.READ_ONLY); projectRoot.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); projectRoot.setText(projectPath); Label l2 = new Label(container, SWT.NONE); l1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); l2.setText("描述:"); projectDesc = new Text(container, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP); GridData multi = new GridData(SWT.FILL, SWT.FILL, true, false); multi.heightHint = 200; projectDesc.setLayoutData(multi); Label l3 = new Label(container, SWT.NONE); l3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); l3.setText("导出路径:"); projectOut = new Text(container, SWT.BORDER | SWT.READ_ONLY); projectOut.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); projectOut.setText(projectOutFile); Button btn = new Button(container, SWT.NONE); // 开始保存文件对话框 btn.setText("另存为"); btn.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { FileDialog dialog = new FileDialog(shell, SWT.SAVE); dialog.setFilterPath(new File(projectOut.getText()).getParent()); dialog.setFilterExtensions(new String[] { "*.html", "*.*" }); dialog.setFilterNames(new String[] { "Html文件(*.html)", "所有文件(*.*)" }); // 打开窗口,返回用户所选的文件目录 String file = dialog.open(); if (file != null) { projectOut.setText(file); } } @Override public void widgetDefaultSelected(SelectionEvent e) { widgetSelected(e); } }); } @Override protected boolean isResizable() { return true; } @Override protected void okPressed() { // 点击确定之后进行的操作 super.okPressed(); } @Override protected void cancelPressed() { // 点击取消进行的操作 super.cancelPressed(); } }
打开plugin.xml,选择 overview,在右上角有个 export deployable plug-ins and framents,点击按照向导即可导出jar包。
export deployable plug-ins and framents
安装插件方法:将jar包放在eclipse或者myeclipse的dropins目录下,重启即可。
插件开发时,如果要依赖外部jar包。可打开 plugin.xml,选定 Runtime,在右下角有个 Classpath,Add你需要的jar包即可。
插件完成的功能是选择项目,右键包含插件选项,点击具体选项时进行一些操作。
下面列出几个简单的代码编写点。
1、编写plugin.xml
2、实现
run
和selectionChanged
在
info.yhzhtk.popup.actions.ExportAction
中已自动生成了run
和selectionChanged
,需要实现具体方法,调用显示对话框完成指定事件。3、实现导出对话框
ExportDialog
新建类
ExportDialog
继承自TitleAreaDialog
4、导出插件jar包
打开plugin.xml,选择 overview,在右上角有个
export deployable plug-ins and framents
,点击按照向导即可导出jar包。安装插件方法:将jar包放在eclipse或者myeclipse的dropins目录下,重启即可。