KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > JobView


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.progress;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IMenuListener;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.action.Separator;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.IContentProvider;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.IWorkbenchActionConstants;
30 import org.eclipse.ui.internal.WorkbenchImages;
31 import org.eclipse.ui.part.ViewPart;
32 import org.eclipse.ui.preferences.ViewPreferencesAction;
33
34 /**
35  * The ProgressView is the class that shows the details of the current
36  * workbench progress.
37  */

38 public class JobView extends ViewPart implements IViewPart {
39
40     NewProgressViewer viewer;
41
42     Action cancelAction;
43
44     Action clearAllAction;
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
50      */

51     public void createPartControl(Composite parent) {
52         viewer = new NewProgressViewer(parent, SWT.MULTI | SWT.H_SCROLL
53                 | SWT.V_SCROLL);
54         viewer.setUseHashlookup(true);
55         viewer.setSorter(ProgressManagerUtil.getProgressViewerSorter());
56         initContentProvider();
57         createClearAllAction();
58         createCancelAction();
59         initContextMenu();
60         initPulldownMenu();
61         initToolBar();
62         getSite().setSelectionProvider(viewer);
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
69      */

70     public void setFocus() {
71         if (viewer != null)
72             viewer.setFocus();
73     }
74
75     /**
76      * Sets the content provider for the viewer.
77      */

78     protected void initContentProvider() {
79         IContentProvider provider = new ProgressTreeContentProvider(viewer);
80         viewer.setContentProvider(provider);
81         viewer.setInput(provider);
82     }
83
84     /**
85      * Initialize the context menu for the receiver.
86      */

87     private void initContextMenu() {
88         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
89
Menu menu = menuMgr.createContextMenu(viewer.getControl());
90         menuMgr.add(cancelAction);
91         menuMgr.addMenuListener(new IMenuListener() {
92             public void menuAboutToShow(IMenuManager manager) {
93                 JobInfo info = getSelectedInfo();
94                 if (info == null)
95                     return;
96             }
97         });
98         menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
99         getSite().registerContextMenu(menuMgr, viewer);
100         viewer.getControl().setMenu(menu);
101     }
102
103     private void initPulldownMenu() {
104         IMenuManager menuMgr = getViewSite().getActionBars().getMenuManager();
105         menuMgr.add(clearAllAction);
106         menuMgr.add(new ViewPreferencesAction(){
107             /* (non-Javadoc)
108              * @see org.eclipse.ui.internal.preferences.ViewPreferencesAction#openViewPreferencesDialog()
109              */

110             public void openViewPreferencesDialog() {
111                 new JobsViewPreferenceDialog(viewer.getControl().getShell()).open();
112
113             }
114         });
115
116     }
117
118     private void initToolBar() {
119         IActionBars bars = getViewSite().getActionBars();
120         IToolBarManager tm = bars.getToolBarManager();
121         tm.add(clearAllAction);
122     }
123
124     /**
125      * Return the selected objects. If any of the selections are not JobInfos
126      * or there is no selection then return null.
127      *
128      * @return JobInfo[] or <code>null</code>.
129      */

130     private IStructuredSelection getSelection() {
131         //If the provider has not been set yet move on.
132
ISelectionProvider provider = getSite().getSelectionProvider();
133         if (provider == null)
134             return null;
135         ISelection currentSelection = provider.getSelection();
136         if (currentSelection instanceof IStructuredSelection) {
137             return (IStructuredSelection) currentSelection;
138         }
139         return null;
140     }
141
142     /**
143      * Get the currently selected job info. Only return it if it is the only
144      * item selected and it is a JobInfo.
145      *
146      * @return JobInfo
147      */

148     JobInfo getSelectedInfo() {
149         IStructuredSelection selection = getSelection();
150         if (selection != null && selection.size() == 1) {
151             JobTreeElement element = (JobTreeElement) selection
152                     .getFirstElement();
153             if (element.isJobInfo())
154                 return (JobInfo) element;
155         }
156         return null;
157     }
158
159     /**
160      * Create the cancel action for the receiver.
161      */

162     private void createCancelAction() {
163         cancelAction = new Action(ProgressMessages.ProgressView_CancelAction) {
164             /*
165              * (non-Javadoc)
166              *
167              * @see org.eclipse.jface.action.Action#run()
168              */

169             public void run() {
170                 viewer.cancelSelection();
171             }
172         };
173
174     }
175
176     /**
177      * Create the clear all action for the receiver.
178      */

179     private void createClearAllAction() {
180         clearAllAction = new Action(ProgressMessages.ProgressView_ClearAllAction) {
181             /*
182              * (non-Javadoc)
183              *
184              * @see org.eclipse.jface.action.Action#run()
185              */

186             public void run() {
187                 viewer.clearAll();
188             }
189         };
190         clearAllAction.setToolTipText(ProgressMessages.NewProgressView_RemoveAllJobsToolTip);
191         ImageDescriptor id = WorkbenchImages.getWorkbenchImageDescriptor("/elcl16/progress_remall.gif"); //$NON-NLS-1$
192
if (id != null)
193             clearAllAction.setImageDescriptor(id);
194         id = WorkbenchImages.getWorkbenchImageDescriptor("/dlcl16/progress_remall.gif"); //$NON-NLS-1$
195
if (id != null)
196             clearAllAction.setDisabledImageDescriptor(id);
197     }
198 }
199
Popular Tags