public static void main(String[] args) { LoggerContext loggerContext = new LoggerContext(); Display display = new Display(); ResourceUtil.init(display); Shell shell = new Shell(display); final Tree tree = new Tree(shell, SWT.BORDER); Rectangle clientArea = shell.getClientArea(); tree.setBounds(clientArea.x, clientArea.y, 200, 200); LoggerTree loggerTree = new LoggerTree(loggerContext, tree); tree.setMenu(TreeMenuBuilder.buildTreeMenu(loggerTree, null)); loggerTree.update("com.foo.Bar"); loggerTree.update("com.foo.Bar"); loggerContext.getLogger("org.apache.struts").setLevel(Level.ERROR); loggerTree.update("org.apache.struts.BlahAction"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
/** * Creates the view. * * @param root */ private void create(final Composite root) { root.setLayout(new FillLayout()); Tree tree = new Tree(root, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); tree.setHeaderVisible(true); treeViewer = new TreeViewer(tree); tree.setMenu(new ClipboardHandlerTree(treeViewer).getMenu()); final TreeColumn column1 = new TreeColumn(tree, SWT.LEFT); tree.setLinesVisible(true); column1.setAlignment(SWT.LEFT); column1.setText(Resources.getMessage("PropertiesView.3")); // $NON-NLS-1$ column1.setWidth(160); final TreeColumn column2 = new TreeColumn(tree, SWT.RIGHT); column2.setAlignment(SWT.LEFT); column2.setText(Resources.getMessage("PropertiesView.4")); // $NON-NLS-1$ column2.setWidth(100); final TreeColumn column6 = new TreeColumn(tree, SWT.RIGHT); column6.setAlignment(SWT.LEFT); column6.setText(Resources.getMessage("PropertiesView.5")); // $NON-NLS-1$ column6.setWidth(100); final TreeColumn column7 = new TreeColumn(tree, SWT.RIGHT); column7.setAlignment(SWT.LEFT); column7.setText(Resources.getMessage("PropertiesView.101")); // $NON-NLS-1$ column7.setWidth(80); final TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT); column3.setAlignment(SWT.LEFT); column3.setText(Resources.getMessage("PropertiesView.6")); // $NON-NLS-1$ column3.setWidth(50); final TreeColumn column4 = new TreeColumn(tree, SWT.RIGHT); column4.setAlignment(SWT.LEFT); column4.setText(Resources.getMessage("PropertiesView.7")); // $NON-NLS-1$ column4.setWidth(50); final TreeColumn column5 = new TreeColumn(tree, SWT.RIGHT); column5.setAlignment(SWT.LEFT); column5.setText(Resources.getMessage("PropertiesView.8")); // $NON-NLS-1$ column5.setWidth(50); final TreeColumn column8 = new TreeColumn(tree, SWT.RIGHT); column8.setAlignment(SWT.LEFT); column8.setText(Resources.getMessage("PropertiesView.113")); // $NON-NLS-1$ column8.setWidth(50); final TreeColumn column9 = new TreeColumn(tree, SWT.RIGHT); column9.setAlignment(SWT.LEFT); column9.setText(Resources.getMessage("PropertiesView.126")); // $NON-NLS-1$ column9.setWidth(50); treeViewer.setContentProvider(new InputContentProvider()); treeViewer.setLabelProvider(new InputLabelProvider()); treeViewer.setInput(roots); treeViewer.expandAll(); }
private void addMenuToSimpleResources() { if (null != singleResTreeViewer) { final Tree resourceTreeHead = singleResTreeViewer.getTree(); if (null != resourceTreeHead) { // Below code creates menu entries and shows them on right // clicking a resource final Menu menu = new Menu(resourceTreeHead); resourceTreeHead.setMenu(menu); menu.addMenuListener( new MenuAdapter() { @Override public void menuShown(MenuEvent e) { // Clear existing menu items MenuItem[] items = menu.getItems(); for (int index = 0; index < items.length; index++) { items[index].dispose(); } IStructuredSelection selection = ((IStructuredSelection) singleResTreeViewer.getSelection()); final SingleResource resource = (SingleResource) selection.getFirstElement(); if (null == resource) { return; } addAutomationMenu(menu, resource); // Menu to remove the resource. MenuItem deleteResource = new MenuItem(menu, SWT.NONE); deleteResource.setText("Delete"); deleteResource.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { try { resourceManager.removeResource(resource); singleResTreeViewer.refresh(); resourceManager.resourceSelectionChanged(null); } catch (SimulatorException e1) { MessageDialog.openInformation( Display.getDefault().getActiveShell(), "Deletion Failed", "Failed to delete the resource."); } changeDeleteVisibility(); } }); } }); } } }
/** * Creates the context menu that is associated with the tree. * * <p><strong>The {@link #createActions} method must have been invoked prior to this * method</strong> */ private void createContextMenu() { // Create menu manager. MenuManager menuManager = new MenuManager(); menuManager.add(newPolicyAction); menuManager.add(new Separator()); menuManager.add(deletePolicyAction); // Create the menu and add it to the tree. final Tree tree = categoriesComposite.getTreeViewer().getTree(); Menu menu = menuManager.createContextMenu(tree); tree.setMenu(menu); }
private void setupPopupMenu() { final Menu menuPopup = new Menu(this.getShell(), SWT.POP_UP); final MenuItem itemDelete = new MenuItem(menuPopup, SWT.PUSH); itemDelete.setText("删除联系人..."); menuPopup.addListener( SWT.Show, new Listener() { @Override public void handleEvent(Event event) { TreeItem[] treeItems = tree.getSelection(); List<ContactInfo> infoList = new ArrayList<ContactInfo>(); if (treeItems != null) { for (TreeItem treeItem : treeItems) { ContactInfo info = (ContactInfo) treeItem.getData(); if (info != null) { infoList.add(info); } } } if (infoList.size() > 0) { itemDelete.setEnabled(true); } else { itemDelete.setEnabled(false); } } }); itemDelete.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TreeItem[] treeItems = tree.getSelection(); List<ContactInfo> infoList = new ArrayList<ContactInfo>(); if (treeItems != null) { for (TreeItem treeItem : treeItems) { ContactInfo info = (ContactInfo) treeItem.getData(); if (info != null) { infoList.add(info); } } } if (infoList.size() > 0) { MessageBox mb = new MessageBox(getShell(), SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION); mb.setMessage("确定要删除选择的联系人?"); mb.setText("提示"); if (mb.open() == SWT.OK) { deleteContact(infoList); } } } }); tree.setMenu(menuPopup); }
/** Creates the context menu for the table */ private void createContextMenu() { MenuManager popupMenuManager = new MenuManager("PopupMenu"); IMenuListener listener = new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { manager.add(exportSelectionAction); manager.add(exportSelectionToOrigPathAction); manager.add(renameAction); manager.add(deleteSelectionAction); manager.add(expandChildrenAction); manager.add(collapseChildrenAction); manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); } }; popupMenuManager.addMenuListener(listener); popupMenuManager.setRemoveAllWhenShown(true); getSite().registerContextMenu(popupMenuManager, getSite().getSelectionProvider()); Menu menu = popupMenuManager.createContextMenu(tree); tree.setMenu(menu); }
/** * Creates and returns the composite of the view. * * @return Composite */ public Composite getComposite(Composite parent) { // composite is the composite we are building, it gets a formlayout Composite composite = new Composite(parent, SWT.NONE); FormLayout layout = new FormLayout(); composite.setLayout(layout); layout.marginHeight = 5; layout.marginWidth = 5; FormData data; // creates the sash on the right of the tree final Sash vertSash = new Sash(composite, SWT.VERTICAL); data = new FormData(); data.top = new FormAttachment(0, 0); data.bottom = new FormAttachment(100, 0); data.left = new FormAttachment(25, 0); vertSash.setLayoutData(data); vertSash.addSelectionListener( new SelectionAdapter() { // makes the sashes resizeable public void widgetSelected(SelectionEvent event) { ((FormData) vertSash.getLayoutData()).left = new FormAttachment(0, event.x); vertSash.getParent().layout(); } }); // construct tree in left side of composite Tree leftTree = new Tree(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); data = new FormData(); data.bottom = new FormAttachment(100, 0); data.top = new FormAttachment(0, 0); data.right = new FormAttachment(vertSash, 0); data.left = new FormAttachment(0, 0); leftTree.setLayoutData(data); treeViewer = new TreeViewer(leftTree); treeViewer.setContentProvider(new MailBoxTreeContentProvider(control)); treeViewer.setLabelProvider(new MailBoxTreeLabelProvider()); treeViewer.setInput("root"); // constructs the web browser Composite window = getFightPane(composite); data = new FormData(); data.left = new FormAttachment(vertSash, 0); data.top = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.bottom = new FormAttachment(100, 0); window.setLayoutData(data); // references to the underlying tree object final Tree tree = treeViewer.getTree(); // keylistener to handle deletions tree.addKeyListener( new KeyListener() { public void keyPressed(KeyEvent e) { if (e.character == 0x7f) { if (tree.getSelection().length == 1 && tree.getSelection()[0].getData() instanceof Folder) { // if there is one item selected and it is a folder, delete it deleteFolderAction.run(); } else { // otherwise delete selected feeds unSubscribeAction.run(); } } } public void keyReleased(KeyEvent e) {} }); /// edits the right click menu to add appropriate actions final MenuManager treeMenuManager = new MenuManager(); treeMenuManager.addMenuListener( new IMenuListener() { public void menuAboutToShow(IMenuManager arg0) { // first remove all actions, then add appropriate ones based on context treeMenuManager.removeAll(); if (treeViewer.getSelection() instanceof IStructuredSelection) { IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection(); if (selection.size() == 1) { Object o = selection.getFirstElement(); // of the object is a folder add the following actions to the menu if (o instanceof Folder) { Folder f = (Folder) o; try { treeMenuManager.add(subscribeAction); treeMenuManager.add(new Separator()); treeMenuManager.add(newFolderAction); // as long as its not the top folder we let the user delete it if (f.getId() != control.getSubscribedFeeds().getId()) treeMenuManager.add(deleteFolderAction); } catch (ControlException e) { control.setStatus("Problem fetching top level folder"); } } // if its a feed we want different actions if (o instanceof Feed) { Feed f = (Feed) o; try { // special actions for the trash and outbox if (f.getId() == control.getTrash().getId()) { treeMenuManager.add(emptyTrashAction); } else if (f.getId() == control.getOutbox().getId()) { treeMenuManager.add(newArticleAction); treeMenuManager.add(exportOutboxAction); } else { // actions for normal feeds treeMenuManager.add(updateFeedAction); treeMenuManager.add(unSubscribeAction); treeMenuManager.add(feedPropertiesAction); } } catch (ControlException e) { control.setStatus("Problem fetching top level folder"); } } } // if there are multiple selections, do different things else if (selection.size() > 1) { boolean allFeeds = true; int trashId, outBoxId; // gets the trashid and boxid once so we don't need to query the database every // iteration // to check if the feed is trash or outbox try { trashId = control.getTrash().getId(); outBoxId = control.getOutbox().getId(); } catch (ControlException e) { trashId = 0; outBoxId = 0; } // gets iterator over selection Iterator iter = selection.iterator(); // iterates through selection to check if the selection is only feeds while (iter.hasNext()) { Object o = iter.next(); if (!(o instanceof Feed)) { allFeeds = false; break; } if (((Feed) o).getId() == trashId || ((Feed) o).getId() == outBoxId) { allFeeds = false; break; } } if (allFeeds) { // if the only selected items are feeds, then let them update all the selected // feeds or unsubscribe treeMenuManager.add(updateFeedAction); treeMenuManager.add(unSubscribeAction); } } } } }); // set the menu tree.setMenu(treeMenuManager.createContextMenu(tree)); return composite; }
public void setTreeMenu() { Menu mTree = null; TreeItem[] ti = wTree.getSelection(); // use SWT.SINGLE in wTree!!!! if (ti.length == 1) { mTree = new Menu(wTree); /* * NEW Sub-directory */ MenuItem miNew = new MenuItem(mTree, SWT.CASCADE); miNew.setText(BaseMessages.getString(PKG, "SelectDirectoryDialog.PopupMenu.Directory.New")); miNew.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!readOnly) { TreeItem ti = wTree.getSelection()[0]; String[] str = ConstUI.getTreeStrings(ti); // // In which directory do we want create a subdirectory? // RepositoryDirectoryInterface dir = repositoryTree.findDirectory(str); if (dir != null) { // // What's the name of the new directory? // EnterStringDialog etd = new EnterStringDialog( shell, BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.EnterDirectoryName.Title"), BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.EnterDirectoryName.Message"), BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.EnterDirectoryName.Default")); String newdir = etd.open(); if (newdir != null) { RepositoryDirectory subdir = new RepositoryDirectory(dir, newdir); try { rep.saveRepositoryDirectory(subdir); dir.addSubdirectory(subdir); TreeItem tiNew = new TreeItem(ti, SWT.NONE); tiNew.setText(newdir); tiNew.setImage(GUIResource.getInstance().getImageFolder()); wTree.setSelection(new TreeItem[] {tiNew}); } catch (Exception exception) { new ErrorDialog( shell, BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.UnableToCreateDirectory.Message"), BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.UnableToCreateDirectory.Title"), exception); } } } else { MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); mb.setMessage( BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.UnableToLocateDirectory.Message")); mb.setText( BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.UnableToLocateDirectory.Title")); mb.open(); } } else { MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); mb.setMessage( BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.PermissionDenied.Message1") + (rep.getUserInfo() == null ? "" : rep.getUserInfo().getLogin()) + BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.PermissionDenied.Message2")); mb.setText( BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.PermissionDenied.Title")); mb.open(); } } }); /* * RENAME directory */ MenuItem miRen = new MenuItem(mTree, SWT.CASCADE); miRen.setText( BaseMessages.getString(PKG, "SelectDirectoryDialog.PopupMenu.Directory.Rename")); miRen.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!readOnly) { TreeItem ti = wTree.getSelection()[0]; String[] str = ConstUI.getTreeStrings(ti); RepositoryDirectoryInterface dir = repositoryTree.findDirectory(str); if (dir != null) { // // What's the new name of the directory? // String oldName = dir.getName(); EnterStringDialog etd = new EnterStringDialog( shell, oldName, BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.EnterDirectoryNewName.Message"), BaseMessages.getString( PKG, "SelectDirectoryDialog.Dialog.EnterDirectoryNewName.Title")); String newName = etd.open(); if (newName != null && !newName.equals(oldName)) { dir.setName(newName); try { rep.renameRepositoryDirectory(dir.getObjectId(), dir.getParent(), newName); ti.setText(newName); wTree.setSelection(ti); } catch (Exception exception) { new ErrorDialog( shell, BaseMessages.getString( PKG, "RepositoryExplorerDialog.Directory.Rename.UnexpectedError.Message1") + oldName + "]" + Const.CR + BaseMessages.getString( PKG, "RepositoryExplorerDialog.Directory.Rename.UnexpectedError.Message2"), BaseMessages.getString( PKG, "RepositoryExplorerDialog.Directory.Rename.UnexpectedError.Title"), exception); } } } } } }); MenuItem miDel = new MenuItem(mTree, SWT.CASCADE); miDel.setText( BaseMessages.getString(PKG, "SelectDirectoryDialog.PopupMenu.Directory.Delete")); miDel.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!readOnly) { TreeItem ti = wTree.getSelection()[0]; String[] str = ConstUI.getTreeStrings(ti); RepositoryDirectoryInterface dir = repositoryTree.findDirectory(str); if (dir != null) { try { rep.deleteRepositoryDirectory(dir); ti.dispose(); } catch (KettleException exception) { new ErrorDialog( shell, BaseMessages.getString( PKG, "RepositoryExplorerDialog.Directory.Delete.ErrorRemoving.Title"), BaseMessages.getString( PKG, "RepositoryExplorerDialog.Directory.Delete.ErrorRemoving.Message1"), exception); } } } } }); } wTree.setMenu(mTree); }