📄 myactiongroup.java
字号:
package source;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.actions.ActionGroup;
public class MyActionGroup extends ActionGroup {
private TableViewer tv;
private CheckboxTableViewer ctv;
private Display display;
protected Shell shell;
/**
* 用来接受TableViewer对象的构造函数。因为在Action会要使用到TableViewer对象 所以一定要把TableViewer传进来。
*/
public MyActionGroup(TableViewer tableViewer,Shell parent,Display display) {
this.tv = tableViewer;
}
public MyActionGroup(TableViewer v, CheckboxTableViewer ctv,Shell parent,Display display) {
this.tv = v;
this.ctv = ctv;
}
/**
* 生成菜单Menu,并将两个Action传入
*/
public void fillContextMenu(IMenuManager mgr) {
/*
* 加入两个Action对象到菜单管理器
*/
MenuManager menuManager = (MenuManager) mgr; //类型转换一下,注意参数是接口
menuManager.add(new RefreshAction());
/*
* 生成Menu并挂在表格table上。menu和table两个对象互为对方的参数。
*/
Table table = tv.getTable();
}
private final class addAction extends Action {
public addAction() {
//给Action设置图像。getImageDesc为自定义方法,得到一个图像
setHoverImageDescriptor(getImageDesc("add.gif"));
setText("添加");
}
public void run() {
new AddPeople(shell,display);
}
}
private final class RefreshAction extends Action {
public RefreshAction() {
//给Action设置图像。getImageDesc为自定义方法,得到一个图像
setHoverImageDescriptor(getImageDesc("refresh.gif"));
setText("刷新");
}
public void run() {
tv.setInput(PeopleFactory.getPeoples());
tv.refresh();
}
}
private class RemoveAction extends Action {
public RemoveAction() {
//正常情况下的图标
setHoverImageDescriptor(getImageDesc("remove.gif"));
//置灰(removeAction.setEnabled(false))情况下的图标
setDisabledImageDescriptor(getImageDesc("disremove.gif"));
setText("删除");
}
/**
* 这里演示了如何从表格中删除所选的记录(可选多个)
*/
public void run() {
if (ctv != null) {
Object[] checkObj = ctv.getCheckedElements(); //取得打勾的记录
if (checkObj.length == 0) //判断是否有勾选复选框
MessageDialog.openInformation(null, "提示", "请先选择");
for (int i = 0; i < checkObj.length; i++) {
PeopleEntity o = (PeopleEntity) checkObj[i];
long id=0;
String strl=o.getId().toString();
id=(long)Integer.parseInt(strl);
DataBase db=new DataBase();
db.connectToDB();
String sql="";
sql="delete from people where id="+id;
db.executeDelete(sql);
db.close();
tv.setInput(PeopleFactory.getPeoples());
tv.refresh();
ctv.remove(o);
}
} else {
//得到选择的对象集
IStructuredSelection s = (IStructuredSelection) tv.getSelection();
if (s.isEmpty())
MessageDialog.openInformation(null, "提示", "请先选择");
else
for (Iterator it = s.iterator(); it.hasNext();) {
PeopleEntity o = (PeopleEntity) it.next();
long id=0;
String strl=o.getId().toString();
id=(long)Integer.parseInt(strl);
DataBase db=new DataBase();
db.connectToDB();
String sql="";
sql="delete from people where id="+id;
db.executeDelete(sql);
db.close();
tv.setInput(PeopleFactory.getPeoples());
tv.refresh();
tv.remove(o);
}
}
}
}
private class FirstAction extends Action {
public FirstAction() {
setHoverImageDescriptor(getImageDesc("first.gif"));
setText("首页");
}
public void run() {
}
}
private class PrevAction extends Action {
public PrevAction() {
this.setHoverImageDescriptor(getImageDesc("prev.gif"));
setText("上一页");
}
public void run() {
}
}
private class NextAction extends Action {
public NextAction() {
this.setHoverImageDescriptor(getImageDesc("next.gif"));
setText("下一页");
}
public void run() {
}
}
private class LastAction extends Action {
public LastAction() {
setHoverImageDescriptor(getImageDesc("last.gif"));
setText("末页");
}
public void run() {
}
}
//新增的“全选”Action
private class SelectAllAction extends Action {
public SelectAllAction() {
setHoverImageDescriptor(getImageDesc("selectall.gif"));
setText("全选");
}
public void run() {
if (ctv != null)
ctv.setAllChecked(true); //将所有复选框打勾
}
}
//新增的“全不选”Action
private class DeselectAction extends Action {
public DeselectAction() {
setHoverImageDescriptor(getImageDesc("deselect.gif"));
setText("全不选");
}
public void run() {
if (ctv != null)
ctv.setAllChecked(false); //取消所有复选框打勾
}
}
/**
* 得到一个图像的方法
*/
public ImageDescriptor getImageDesc(String fileName) {
try {
URL url = new URL("file:icons/" + fileName);
return ImageDescriptor.createFromURL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
/**
* 自定义方法。生成六个Action对象,并通过工具栏管理器ToolBarManager填充进工具栏
*/
public void fillActionToolBars(ToolBarManager actionBarManager) {
//生成按钮,按钮就是一个个的Action
Action removeAction = new RemoveAction();
Action addAction = new addAction();
Action refreshAction = new RefreshAction();
Action firstAction = new FirstAction();
Action preAction = new PrevAction();
Action nextAction = new NextAction();
Action lastAction = new LastAction();
Action selAllAction = new SelectAllAction();
Action deselAction = new DeselectAction();
actionBarManager.add(createActionContributionItem(removeAction));
actionBarManager.add(createActionContributionItem(addAction));
actionBarManager.add(createActionContributionItem(refreshAction));
actionBarManager.add(createActionContributionItem(firstAction));
actionBarManager.add(createActionContributionItem(preAction));
actionBarManager.add(createActionContributionItem(nextAction));
actionBarManager.add(createActionContributionItem(lastAction));
actionBarManager.add(createActionContributionItem(selAllAction));
actionBarManager.add(createActionContributionItem(deselAction));
//更新工具栏。没有这一句,工具栏上会没有任何显示
actionBarManager.update(true);
}
ActionContributionItem createActionContributionItem(IAction action) {
ActionContributionItem aci = new ActionContributionItem(action);
aci.setMode(ActionContributionItem.MODE_FORCE_TEXT);//显示图像+文字
return aci;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -