KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.ISelection;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
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.PlatformUI;
31 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
32 import org.eclipse.ui.internal.WorkbenchImages;
33 import org.eclipse.ui.part.ViewPart;
34 import org.eclipse.ui.preferences.ViewPreferencesAction;
35
36 /**
37  * The ProgressView is the class that shows the details of the current workbench
38  * progress.
39  */

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

54     public void createPartControl(Composite parent) {
55         viewer = new DetailedProgressViewer(parent, SWT.MULTI);
56         viewer.setComparator(ProgressManagerUtil.getProgressViewerComparator());
57
58         viewer.getControl().setLayoutData(
59                 new GridData(SWT.FILL, SWT.FILL, true, true));
60
61         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
62                 IWorkbenchHelpContextIds.RESPONSIVE_UI);
63         
64         initContentProvider();
65         createClearAllAction();
66         createCancelAction();
67         initContextMenu();
68         initPulldownMenu();
69         initToolBar();
70         getSite().setSelectionProvider(viewer);
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
77      */

78     public void setFocus() {
79         if (viewer != null) {
80             viewer.setFocus();
81         }
82     }
83
84     /**
85      * Sets the content provider for the viewer.
86      */

87     protected void initContentProvider() {
88         ProgressViewerContentProvider provider = new ProgressViewerContentProvider(viewer, true ,true);
89         viewer.setContentProvider(provider);
90         viewer.setInput(ProgressManager.getInstance());
91     }
92
93     /**
94      * Initialize the context menu for the receiver.
95      */

96     private void initContextMenu() {
97         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
98
Menu menu = menuMgr.createContextMenu(viewer.getControl());
99         menuMgr.add(cancelAction);
100         menuMgr.addMenuListener(new IMenuListener() {
101             public void menuAboutToShow(IMenuManager manager) {
102                 JobInfo info = getSelectedInfo();
103                 if (info == null) {
104                     return;
105                 }
106             }
107         });
108         menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
109         getSite().registerContextMenu(menuMgr, viewer);
110         viewer.getControl().setMenu(menu);
111     }
112
113     private void initPulldownMenu() {
114         IMenuManager menuMgr = getViewSite().getActionBars().getMenuManager();
115         menuMgr.add(clearAllAction);
116         menuMgr.add(new ViewPreferencesAction() {
117             /*
118              * (non-Javadoc)
119              *
120              * @see org.eclipse.ui.internal.preferences.ViewPreferencesAction#openViewPreferencesDialog()
121              */

122             public void openViewPreferencesDialog() {
123                 new JobsViewPreferenceDialog(viewer.getControl().getShell())
124                         .open();
125
126             }
127         });
128
129     }
130
131     private void initToolBar() {
132         IActionBars bars = getViewSite().getActionBars();
133         IToolBarManager tm = bars.getToolBarManager();
134         tm.add(clearAllAction);
135     }
136
137     /**
138      * Return the selected objects. If any of the selections are not JobInfos or
139      * there is no selection then return null.
140      *
141      * @return JobInfo[] or <code>null</code>.
142      */

143     private IStructuredSelection getSelection() {
144         // If the provider has not been set yet move on.
145
ISelectionProvider provider = getSite().getSelectionProvider();
146         if (provider == null) {
147             return null;
148         }
149         ISelection currentSelection = provider.getSelection();
150         if (currentSelection instanceof IStructuredSelection) {
151             return (IStructuredSelection) currentSelection;
152         }
153         return null;
154     }
155
156     /**
157      * Get the currently selected job info. Only return it if it is the only
158      * item selected and it is a JobInfo.
159      *
160      * @return JobInfo
161      */

162     JobInfo getSelectedInfo() {
163         IStructuredSelection selection = getSelection();
164         if (selection != null && selection.size() == 1) {
165             JobTreeElement element = (JobTreeElement) selection
166                     .getFirstElement();
167             if (element.isJobInfo()) {
168                 return (JobInfo) element;
169             }
170         }
171         return null;
172     }
173
174     /**
175      * Create the cancel action for the receiver.
176      */

177     private void createCancelAction() {
178         cancelAction = new Action(ProgressMessages.ProgressView_CancelAction) {
179             /*
180              * (non-Javadoc)
181              *
182              * @see org.eclipse.jface.action.Action#run()
183              */

184             public void run() {
185                 viewer.cancelSelection();
186             }
187         };
188
189     }
190
191     /**
192      * Create the clear all action for the receiver.
193      */

194     private void createClearAllAction() {
195         clearAllAction = new Action(
196                 ProgressMessages.ProgressView_ClearAllAction) {
197             /*
198              * (non-Javadoc)
199              *
200              * @see org.eclipse.jface.action.Action#run()
201              */

202             public void run() {
203                 FinishedJobs.getInstance().clearAll();
204             }
205         };
206         clearAllAction
207                 .setToolTipText(ProgressMessages.NewProgressView_RemoveAllJobsToolTip);
208         ImageDescriptor id = WorkbenchImages
209                 .getWorkbenchImageDescriptor("/elcl16/progress_remall.gif"); //$NON-NLS-1$
210
if (id != null) {
211             clearAllAction.setImageDescriptor(id);
212         }
213         id = WorkbenchImages
214                 .getWorkbenchImageDescriptor("/dlcl16/progress_remall.gif"); //$NON-NLS-1$
215
if (id != null) {
216             clearAllAction.setDisabledImageDescriptor(id);
217         }
218     }
219
220 }
221
Popular Tags