| /******************************************************************************* |
| * Copyright (c) 2010 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - Initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.wst.server.test.util.views; |
| |
| import java.util.ArrayList; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IMenuListener; |
| import org.eclipse.jface.action.IMenuManager; |
| import org.eclipse.jface.action.IToolBarManager; |
| import org.eclipse.jface.action.MenuManager; |
| import org.eclipse.jface.action.Separator; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.viewers.DoubleClickEvent; |
| import org.eclipse.jface.viewers.IDoubleClickListener; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredContentProvider; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.ITreeContentProvider; |
| import org.eclipse.jface.viewers.LabelProvider; |
| import org.eclipse.jface.viewers.TreeViewer; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.jface.viewers.ViewerSorter; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Menu; |
| import org.eclipse.ui.IActionBars; |
| import org.eclipse.ui.ISharedImages; |
| import org.eclipse.ui.IWorkbenchActionConstants; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.part.DrillDownAdapter; |
| import org.eclipse.ui.part.ViewPart; |
| import org.eclipse.wst.server.core.IModule; |
| import org.eclipse.wst.server.core.IServer; |
| import org.eclipse.wst.server.core.ServerCore; |
| import org.eclipse.wst.server.core.ServerUtil; |
| import org.eclipse.wst.server.core.internal.Module; |
| import org.eclipse.wst.server.core.internal.Server; |
| import org.eclipse.wst.server.core.model.IModuleFile; |
| import org.eclipse.wst.server.core.model.IModuleFolder; |
| import org.eclipse.wst.server.core.model.IModuleResource; |
| import org.eclipse.wst.server.core.util.ProjectModule; |
| |
| |
| /** |
| * This sample class demonstrates how to plug-in a new |
| * workbench view. The view shows data obtained from the |
| * model. The sample creates a dummy model on the fly, |
| * but a real implementation would connect to the model |
| * available either in this or another plug-in (e.g. the workspace). |
| * The view is connected to the model using a content provider. |
| * <p> |
| * The view uses a label provider to define how model |
| * objects should be presented in the view. Each |
| * view can present the same model objects using |
| * different labels and icons, if needed. Alternatively, |
| * a single label provider can be shared between views |
| * in order to ensure that objects of the same type are |
| * presented in the same way everywhere. |
| * <p> |
| */ |
| |
| public class ServerCacheView extends ViewPart { |
| |
| /** |
| * The ID of the view as specified by the extension. |
| */ |
| public static final String ID = "org.eclipse.wst.server.test.util.views.ServerCacheView"; |
| |
| private TreeViewer viewer; |
| private DrillDownAdapter drillDownAdapter; |
| private Action action1; |
| private Action action2; |
| private Action doubleClickAction; |
| |
| /* |
| * The content provider class is responsible for |
| * providing objects to the view. It can wrap |
| * existing objects in adapters or simply return |
| * objects as-is. These objects may be sensitive |
| * to the current input of the view, or ignore |
| * it and always show the same content |
| * (like Task List, for example). |
| */ |
| |
| class TreeObject implements IAdaptable { |
| private String name; |
| private TreeParent parent; |
| |
| public TreeObject(String name) { |
| this.name = name; |
| } |
| public String getName() { |
| return name; |
| } |
| public void setParent(TreeParent parent) { |
| this.parent = parent; |
| } |
| public TreeParent getParent() { |
| return parent; |
| } |
| public String toString() { |
| return getName(); |
| } |
| public Object getAdapter(Class key) { |
| if (key.equals(IServer.class)){ |
| return ServerCore.findServer(name); |
| } |
| else if (key.equals(IModule.class)){ |
| return ServerUtil.getModule(name); |
| } |
| return null; |
| } |
| public boolean equals(Object o){ |
| if (o instanceof IModule || o instanceof Module){ |
| return (this.name.equalsIgnoreCase(((Module)o).getId())); |
| } |
| else if (o instanceof TreeObject){ |
| return (this.name.equalsIgnoreCase(((TreeObject)o).name)); |
| } |
| |
| return false; |
| } |
| } |
| |
| class TreeParent extends TreeObject { |
| private ArrayList children; |
| public TreeParent(String name) { |
| super(name); |
| children = new ArrayList(); |
| } |
| public void addChild(TreeObject child) { |
| children.add(child); |
| child.setParent(this); |
| } |
| public void removeChild(TreeObject child) { |
| children.remove(child); |
| child.setParent(null); |
| } |
| public TreeObject [] getChildren() { |
| // If this is a server, return the modules |
| if (super.getAdapter(IServer.class)!= null){ |
| Server server = (Server)super.getAdapter(IServer.class); |
| IModule[] modules = server.getModules(); |
| for (IModule module : modules){ |
| TreeParent mElement = new TreeParent(module.getId()); |
| if (!children.contains(mElement)) |
| addChild(mElement); |
| } |
| return (TreeParent [])children.toArray(new TreeParent[children.size()]); |
| } |
| // if this is a module |
| else if (super.getAdapter(IModule.class)!= null){ |
| Module module = (Module)super.getAdapter(IModule.class); |
| // Add the child modules |
| IModule[] modules = module.getChildModules(null); |
| for (IModule m : modules){ |
| TreeParent mElement = new TreeParent(m.getId()); |
| if (!children.contains(mElement)) |
| addChild(mElement); |
| } |
| // Add the members |
| ProjectModule pm = (ProjectModule)module.getAdapter(ProjectModule.class); |
| try{ |
| IModuleResource[] members = pm.members(); |
| for (IModuleResource r: members){ |
| // Check if it is a folder |
| if (r instanceof IModuleFolder){ |
| TreeParent mElement = new TreeParent("Folder: "+r.getName()); |
| if (!children.contains(mElement)) |
| addChild(mElement); |
| } |
| // If it isn't not a folder, assuming is a file |
| else { |
| TreeObject mElement = new TreeObject(r.getName()); |
| if (!children.contains(mElement)) |
| addChild(mElement); |
| } |
| } |
| } |
| catch (CoreException e){ |
| // In case of exception add a child with the error message |
| TreeObject errElement= new TreeObject("Unable to retrieve members due to: "+e.getMessage()); |
| addChild(errElement); |
| } |
| |
| return (TreeObject[])children.toArray(new TreeObject[children.size()]); |
| } |
| |
| |
| return (TreeParent [])children.toArray(new TreeParent[children.size()]); |
| } |
| public boolean hasChildren() { |
| return getChildren().length > 0; |
| } |
| } |
| |
| class ViewContentProvider implements IStructuredContentProvider, |
| ITreeContentProvider { |
| private TreeParent invisibleRoot; |
| |
| public void inputChanged(Viewer v, Object oldInput, Object newInput) { |
| } |
| public void dispose() { |
| } |
| public Object[] getElements(Object parent) { |
| if (parent.equals(getViewSite())) { |
| if (invisibleRoot==null) initialize(); |
| return getChildren(invisibleRoot); |
| } |
| return getChildren(parent); |
| } |
| public Object getParent(Object child) { |
| if (child instanceof TreeObject) { |
| return ((TreeObject)child).getParent(); |
| } |
| return null; |
| } |
| public Object [] getChildren(Object parent) { |
| if (parent instanceof TreeParent) { |
| return ((TreeParent)parent).getChildren(); |
| } |
| return new Object[0]; |
| } |
| public boolean hasChildren(Object parent) { |
| if (parent instanceof TreeParent) |
| return ((TreeParent)parent).hasChildren(); |
| return false; |
| } |
| /* |
| * We will set up a dummy model to initialize tree heararchy. |
| * In a real code, you will connect to a real model and |
| * expose its hierarchy. |
| */ |
| private void initialize() { |
| invisibleRoot = new TreeParent(""); |
| |
| IServer[] servers = ServerCore.getServers(); |
| |
| for (IServer server:servers){ |
| invisibleRoot.addChild(new TreeParent(server.getName())); |
| } |
| } |
| } |
| class ViewLabelProvider extends LabelProvider { |
| |
| public String getText(Object obj) { |
| return obj.toString(); |
| } |
| public Image getImage(Object obj) { |
| String imageKey = ISharedImages.IMG_OBJ_ELEMENT; |
| if (obj instanceof TreeParent) |
| imageKey = ISharedImages.IMG_OBJ_FOLDER; |
| return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey); |
| } |
| } |
| class NameSorter extends ViewerSorter { |
| } |
| |
| /** |
| * The constructor. |
| */ |
| public ServerCacheView() { |
| } |
| |
| /** |
| * This is a callback that will allow us |
| * to create the viewer and initialize it. |
| */ |
| public void createPartControl(Composite parent) { |
| viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
| drillDownAdapter = new DrillDownAdapter(viewer); |
| viewer.setContentProvider(new ViewContentProvider()); |
| viewer.setLabelProvider(new ViewLabelProvider()); |
| viewer.setSorter(new NameSorter()); |
| viewer.setInput(getViewSite()); |
| makeActions(); |
| hookContextMenu(); |
| hookDoubleClickAction(); |
| contributeToActionBars(); |
| } |
| |
| private void hookContextMenu() { |
| MenuManager menuMgr = new MenuManager("#PopupMenu"); |
| menuMgr.setRemoveAllWhenShown(true); |
| menuMgr.addMenuListener(new IMenuListener() { |
| public void menuAboutToShow(IMenuManager manager) { |
| ServerCacheView.this.fillContextMenu(manager); |
| } |
| }); |
| Menu menu = menuMgr.createContextMenu(viewer.getControl()); |
| viewer.getControl().setMenu(menu); |
| getSite().registerContextMenu(menuMgr, viewer); |
| } |
| |
| private void contributeToActionBars() { |
| IActionBars bars = getViewSite().getActionBars(); |
| fillLocalPullDown(bars.getMenuManager()); |
| fillLocalToolBar(bars.getToolBarManager()); |
| } |
| |
| private void fillLocalPullDown(IMenuManager manager) { |
| manager.add(action1); |
| manager.add(new Separator()); |
| manager.add(action2); |
| } |
| |
| private void fillContextMenu(IMenuManager manager) { |
| manager.add(action1); |
| manager.add(action2); |
| manager.add(new Separator()); |
| drillDownAdapter.addNavigationActions(manager); |
| // Other plug-ins can contribute there actions here |
| manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
| } |
| |
| private void fillLocalToolBar(IToolBarManager manager) { |
| manager.add(action1); |
| manager.add(action2); |
| manager.add(new Separator()); |
| drillDownAdapter.addNavigationActions(manager); |
| } |
| |
| private void makeActions() { |
| action1 = new Action() { |
| public void run() { |
| showMessage("Action 1 executed"); |
| } |
| }; |
| action1.setText("Action 1"); |
| action1.setToolTipText("Action 1 tooltip"); |
| action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). |
| getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); |
| |
| action2 = new Action() { |
| public void run() { |
| showMessage("Action 2 executed"); |
| } |
| }; |
| action2.setText("Action 2"); |
| action2.setToolTipText("Action 2 tooltip"); |
| action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). |
| getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); |
| doubleClickAction = new Action() { |
| public void run() { |
| ISelection selection = viewer.getSelection(); |
| Object obj = ((IStructuredSelection)selection).getFirstElement(); |
| showMessage("Double-click detected on "+obj.toString()); |
| } |
| }; |
| } |
| |
| private void hookDoubleClickAction() { |
| viewer.addDoubleClickListener(new IDoubleClickListener() { |
| public void doubleClick(DoubleClickEvent event) { |
| doubleClickAction.run(); |
| } |
| }); |
| } |
| private void showMessage(String message) { |
| MessageDialog.openInformation( |
| viewer.getControl().getShell(), |
| "Server Cache", |
| message); |
| } |
| |
| /** |
| * Passing the focus request to the viewer's control. |
| */ |
| public void setFocus() { |
| viewer.getControl().setFocus(); |
| } |
| } |