我想根据我的上下文菜单选项编辑树查看器元素。基本上我需要更新显示的元素值。如果我双击树查看器元素我能够更新值,但通过上下文菜单我也应该能够做到。
添加上下文菜单的示例代码:
protected def void createContextMenu(Viewer viewer) {
val MenuManager contextMenu = new MenuManager("Menu"); // $NON-NLS-1$
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(new IMenuListener() {
public override void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
val Menu menu = contextMenu.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
}
/**
* Fill dynamic context menu
*
* @param contextMenu
*/
protected def void fillContextMenu(IMenuManager contextMenu) {
contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
contextMenu.add(new Action("Rename") {
public override void run() {
val selectedElement = (treeViewer.selection as IStructuredSelection).firstElement
}
});
}
答案 0 :(得分:0)
看看这有帮助还是让我知道
package myFolder;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
public class trying {
public static void main(String[] args){
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(300,300);
TreeViewer viewer = new TreeViewer(shell);
viewer.getTree().setHeaderVisible(true);
viewer.getTree().setLinesVisible(true);
viewer.setContentProvider(new ITreeContentProvider () {
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
@Override
public Object[] getChildren(Object arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] getElements(Object arg0) {
// TODO Auto-generated method stub
return new String[]{"Hoshe","Irani"};
}
@Override
public Object getParent(Object arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasChildren(Object arg0) {
// TODO Auto-generated method stub
return false;
}
});
viewer.setLabelProvider(new ILabelProvider(){
@Override
public void addListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public boolean isLabelProperty(Object arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void removeListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
@Override
public Image getImage(Object arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getText(Object arg0) {
// TODO Auto-generated method stub
return "hoshe";
}
});
viewer.setInput("Hoshe");
Menu popupMenu = new Menu(viewer.getControl());
MenuItem newItem = new MenuItem(popupMenu, SWT.CHECK);
newItem.setText("New");
MenuItem refreshItem = new MenuItem(popupMenu, SWT.CHECK);
refreshItem.setText("Refresh");
MenuItem deleteItem = new MenuItem(popupMenu, SWT.CHECK);
deleteItem.setText("Delete");
popupMenu.addMenuListener(new MenuListener() {
@Override
public void menuShown(MenuEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void menuHidden(MenuEvent arg0) {
// TODO Auto-generated method stub
display.asyncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
TreeItem [] arr = viewer.getTree().getSelection();
for(MenuItem item2 : popupMenu.getItems()){
boolean bool1 = item2.getSelection();
if(bool1){
arr[0].setText(item2.getText());
item2.setSelection(false);
}
}
}
});
}
});
viewer.getTree().setMenu(popupMenu);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}