⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treeactiongroup.java

📁 TTreeview 的 Java实例 Eclipes环境下
💻 JAVA
字号:
package demo.pluginA.treeview.internal;

import java.util.ArrayList;
import java.util.Iterator;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.jface.action.*;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.actions.ActionGroup;
import demo.pluginA.treeview.data.*;
import demo.pluginA.treeview.xmlutils.*;

import java.util.List;

/**
 * Generate the menu and assign actions to it
 */
public class TreeActionGroup extends ActionGroup {
	private TreeViewer tv;

	public TreeActionGroup(TreeViewer treeViewer) {
		this.tv = treeViewer;
		final Tree testTree = tv.getTree();
	}

	public void fillContextMenu(IMenuManager mgr) {
		// assign actions to menuManager
		MenuManager menuManager = (MenuManager) mgr;

		// ���� ��ÿ����ʾ֮ǰ��ɾ��ȫ���ϲ˵� ������Ϊtrue
		menuManager.setRemoveAllWhenShown(true);

		// ����˵�����
		menuManager.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				IStructuredSelection selection = (IStructuredSelection) tv
						.getSelection();// ȡ�õ�ǰ����
				ITreeEntry entry = (ITreeEntry) (selection.getFirstElement());
				if ((entry instanceof QueryEntity)) {
					manager.add(new OpenAction());
				}
				manager.add(new RefreshAction());
				// �����ProcessEntity����ʾ���Activity
				if ((entry instanceof ProcessEntity)) {
					manager.add(new AddEntryAction("Add Activities"));
				}
				// �����Activity����ʾ���Task
				else if ((entry instanceof ActivityEntity)) {
					manager.add(new AddEntryAction("Add Tasks"));
				}
				// �����Task����ʾ���Step
				else if ((entry instanceof TaskEntity)) {
					manager.add(new AddEntryAction("Add Steps"));
				}
				// �����Query����ʾ���Query
				if (!(entry instanceof QueryEntity)) {
					manager.add(new AddQueryAction());
				}
				manager.add(new EditAction());
				manager.add(new RemoveAction());
			}
		});
		// generate the menu and assign it the menu to the tree
		Tree tree = tv.getTree();
		Menu menu = menuManager.createContextMenu(tree);
		tree.setMenu(menu);
	}

	// actions of executing the queries
	private class OpenAction extends Action {
		public OpenAction() {
			setText("Open");
		}

		// inherit from the 'Action' class
		public void run() {
			IStructuredSelection selection = (IStructuredSelection) tv
					.getSelection();
			ITreeEntry obj = (ITreeEntry) (selection.getFirstElement());
			if (obj != null & obj instanceof QueryEntity) {
				MessageDialog.openInformation(null, null, obj.getName()
						+ " executed");
			} else {
				MessageDialog.openInformation(null, null,
						"only queries can be executed");
			}
		}
	}

	// Refresh the tree view
	private class RefreshAction extends Action {
		// private IWorkbenchWindow window;
		public RefreshAction() {
			setText("Refresh");
		}

		public void run() {
			tv.refresh();
		}
	}

	private class AddEntryAction extends Action {
		public AddEntryAction(String itemName) {
			setText(itemName);
		}

		public void run() {
			IStructuredSelection selection = (IStructuredSelection) tv
					.getSelection();
			ITreeEntry obj = (ITreeEntry) (selection.getFirstElement());
			// can not add a child to QueryEntity
			InputWindow dialog = new InputWindow(null, "Add a node", "", "",
					true, false);
			// InputDialog dialog2 = new InputDialog()
			if (dialog.open() == InputDialog.OK) {// if press OK
				String entryName = dialog.getName();// get the name
				String entryDescription = dialog.getDescription();// get the
				// description
				List list = (List) tv.getInput();
				ITreeEntry newObj = null;
				if (obj instanceof ProcessEntity) {
					newObj = new ActivityEntity("Activity: " + entryName,
							entryDescription);
					newObj.setId(generateId(list));// set Id
				} else if (obj instanceof ActivityEntity) {
					newObj = new TaskEntity("Task: " + entryName,
							entryDescription);
					newObj.setId(generateId(list));// set Id
				} else if (obj instanceof TaskEntity) {
					newObj = new StepEntity("Step: " + entryName,
							entryDescription);
					newObj.setId(generateId(list));// set Id
				}
				// change the model
				obj.getChildren().add(newObj);
				tv.add(obj, newObj);
				if (!tv.getExpandedState(obj))
					tv.expandToLevel(obj, 1);
			}
		}

		private Long generateId(List<ITreeEntry> list) {
			if (list == null)
				return -1l;
			Long id = 0l;
			Boolean flag = false;// if flag is true means that the id can be
			// used
			while (flag != true) {
				flag = checkUsable(list, id);
				id++;
			}// end while
			return id - 1;
		}// end generate

		private Boolean checkUsable(List<ITreeEntry> list, Long id) {
			if (list == null)
				return true;
			Boolean result = true;
			for (Iterator it = list.iterator(); it.hasNext() && result != false;) {
				ITreeEntry o = (ITreeEntry) it.next();
				if (o.getId() == id) {
					result = false;
				} else {
					result = checkUsable(o.getChildren(), id);
				}
			}
			return result;
		}
	}

	// actions of removing the queries
	private class RemoveAction extends Action {
		public RemoveAction() {
			setText("Remove");
		}

		// inherit from the 'Action' class
		public void run() {
			IStructuredSelection selection = (IStructuredSelection) tv
					.getSelection();
			// ITreeEntry entry = getSelTreeEntry();
			ITreeEntry obj = (ITreeEntry) (selection.getFirstElement());
			if (obj != null) {
				List list = (List) tv.getInput();
				remove(list, obj);
				remove(list, obj);
				tv.remove(obj); // only remove the view
				tv.refresh();
				// tv.refresh(obj);
				MessageDialog.openInformation(null, null, obj.getName()
						+ " has been removed");
			}
		}

		private void remove(List<ITreeEntry> list, ITreeEntry entry) {
			if (list == null)
				return;
			for (Iterator it = list.iterator(); it.hasNext();) {
				ITreeEntry o = (ITreeEntry) it.next();
				if (o.getId().equals(entry.getId())) {
					it.remove();
					return;
				} else {
					remove(o.getChildren(), entry);
				}
			}
		}
	}

	private class AddQueryAction extends Action {
		public AddQueryAction() {
			setText("Add a Query");
		}

		public void run() {
			IStructuredSelection selection = (IStructuredSelection) tv
					.getSelection();
			ITreeEntry obj = (ITreeEntry) (selection.getFirstElement());
			InputWindow dialog = new InputWindow(null, "Query Information", "",
					"", true, true);
			if (dialog.open() == InputDialog.OK) {// if press OK
				String entryName = dialog.getName();// get the name
				String entryDescription = dialog.getDescription();// get description
				Boolean entryOpt = dialog.getOption();// get the option
				// ��ݵ�����IJ�ͬ���������Ӧ���ӽ��
				ITreeEntry newObj = new QueryEntity("Query: " + entryName,
						entryDescription, entryOpt);
				// �½����ӵ����ģ����
				obj.getChildren().add(newObj);
				// newObj�����ӵ�obj֮�£�����tv.refresh(obj,true)
				tv.add(obj, newObj);
				// ���obj��δչ�������չ����
				if (!tv.getExpandedState(obj))
					tv.expandToLevel(obj, 1);
			}
		}
	}

	private class EditAction extends Action {
		public EditAction() {
			setText("Edit");
		}

		public void run() {
			IStructuredSelection selection = (IStructuredSelection) tv
					.getSelection();
			ITreeEntry obj = (ITreeEntry) (selection.getFirstElement());
			String entryName = obj.getName();// get the name
			String entryDescription = obj.getDiscription();// get the
			// description
			if (entryDescription == null)
				entryDescription = "";
			InputWindow dialog;
			if (obj instanceof QueryEntity) {
				Boolean entryOpt = ((QueryEntity) obj).getOption();
				dialog = new InputWindow(null, true, "Edit node", entryName,
						entryDescription, entryOpt);
			} else {
				dialog = new InputWindow(null, false, "Edit node", entryName,
						entryDescription, false);
			}
			if (dialog.open() == InputDialog.OK) {// if press OK
				obj.setName(dialog.getName());
				obj.setDescription(dialog.getDescription());
				// we can not change the option when we edit the query
				// obj.setOption(dialog.getOption());
				tv.refresh();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -