首页 > Linux操作系统 > Linux操作系统 > Eclipse插件开发——入门级
(一)、 Action与 兑换框
1、首先创建插件工程,选择Plug-in Project,点击下一步。

2、输入插件工程名字,到下一步,填写插件信息

3、这里创建一个空的插件工程,不用选择已有的模板,直接点击finish

4、创建文件plugin.xml ,其存放路径和src同级,直接放在工程目录下
其组织结构如下图

plugin.xml文件的内容为
point="org.eclipse.ui.actionSets">
id="com.dxm.dialog.test.actionSet"
label="ActionSet"
visible="true">
id="com.dxm.dialog.test.menu"
label="Action"
path="additions">
class="test.Action.OpenDialogDelegate"
icon="icons/sample.gif"
id="com.dxm.test.first.dialog.action"
label="Open This Dialog"
style="push"
toolbarPath="Normal/additions"
tooltip="Your life will never be the same ">
5、//创建action的class
class="test.Action.OpenDialogDelegate"
package test.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
public class OpenDialogDelegate implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
private Shell shell;
public void dispose() {
// TODO Auto-generated method stub
}
public void init(IWorkbenchWindow window) {
if (window == null)
return;
this.window = window;
shell = window.getShell();
}
public void run(IAction action) {
EditDialog dialog = new EditDialog(shell);
dialog.open();
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO Auto-generated method stub
}
}
6、//这个action依赖于EditDialog类,下面来看一下这个图类
package test.Action;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class EditDialog extends Dialog {
private Text text_2;
private Text text_1;
private Text text;
private List li = new ArrayList();
protected Object result;
protected Shell shell;
/**
* Create the dialog
*
* @param parent
*/
public EditDialog(Shell parent) {
super(parent, SWT.NONE);
}
/**
* Open the dialog
*
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(485, 321);
shell.setText("工程部署");
final Label label = new Label(shell, SWT.NONE);
label.setText("选择部署的工程:");
label.setBounds(49, 55, 93, 20);
text = new Text(shell, SWT.BORDER);
text.setBounds(153, 55, 201, 20);
final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
});
button.setText("浏览...");
button.setBounds(372, 53, 58, 25);
final Label label_1 = new Label(shell, SWT.NONE);
label_1.setText("选择部署位置:");
label_1.setBounds(49, 95, 93, 20);
text_1 = new Text(shell, SWT.BORDER);
text_1.setBounds(153, 95, 201, 20);
final Button button_1 = new Button(shell, SWT.NONE);
button_1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setFilterPath(text_1.getText());
dlg.setText("选择部署位置");
dlg.setMessage("在目录中选择工程部署的位置");
String dir = dlg.open();
if (dir != null) {
text_1.setText(dir);
}
}
});
button_1.setText("浏览...");
button_1.setBounds(372, 93, 58, 25);
final Label label_2 = new Label(shell, SWT.NONE);
label_2.setText("工程发布的名字:");
label_2.setBounds(49, 135, 93, 20);
text_2 = new Text(shell, SWT.BORDER);
text_2.setBounds(153, 135, 277, 20);
final Label label_3 = new Label(shell, SWT.NONE);
label_3.setText("是否打成war包:");
label_3.setBounds(49, 175, 93, 20);
final Button button_2 = new Button(shell, SWT.RADIO);
button_2.addSelectionListener(new SelectionAdapter() {
});
button_2.setText("是");
button_2.setBounds(197, 175, 83, 20);
final Button button_3 = new Button(shell, SWT.RADIO);
button_3.addSelectionListener(new SelectionAdapter() {
});
button_3.setText("否");
button_3.setBounds(286, 175, 83, 20);
final Button button_4 = new Button(shell, SWT.NONE);
button_4.addSelectionListener(new SelectionAdapter() {
});
button_4.setText("部署");
button_4.setBounds(249, 235, 86, 25);
final Button button_5 = new Button(shell, SWT.NONE);
button_5.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
shell.close();
}
});
button_5.setText("取消");
button_5.setBounds(348, 235, 86, 25);
}
public IPath getOutputDirectory() {
return pathOrNull(text.getText());
}
private static Path pathOrNull(String p) {
if (p == null || p.trim().length() == 0) {
return null;
} else {
return new Path(p);
}
}
}
7、好了讲到这里,就开发完成了一个action调用对话框的插件。那么如何运行这个插件呢
我们来看一下,如何运行
在Package Explorer包资源管理器中右键——》Run as——》Open Run Dialog...

点击Workspace,选中空间内要运行的project
——》Run ——》 启动一个Eclipse application
注:为了确保万一可以看一下Arguments
8、下面我们来看一下运行效果
在新启动的这个Eclipse application中让我们来看一下,效果。

配置文件中的这句,起到了作用
tooltip="Your life will never be the same "
点击这个按钮,弹出如下所示的兑换框。

9、所有的插件开发,只为一个目的,contributing to eclipse
那么,我们一定要知道如何打包和安装
1)、既然是打包带走,就要有选择的打包,如何选择我们去build.properties里做取舍

2)、经过一番挑选,我们开始打包
在File——》Export…

进入下一步

Tab页Destination的Directory是生成后,存放的目录。
点击Finish
打包完成,生成一个jar文件

这个将是你的第一个产品
呵呵,我也可以做产品啦
10、下面就来看看我们的贡献吧——安装
(1)、关闭开发者本地Eclipse——》在Eclipse\plugins下删除该插件的旧一版本的
(2)、在configuration下,删除org.eclipse.update
(3)、启动Eclipse进行查看——》关闭Eclipse
(4)、把我们生成的产品,放到\eclipse\plugins下
(5)、重新启动Eclipse
OK 可以用啦
其中/test/Activator.java,是管理插件生命周期的类,也叫插件启动类
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13726712/viewspace-622058/ ,如需转载,请注明出处,否则将追究法律责任。