001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.progress;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IMenuListener;
014: import org.eclipse.jface.action.IMenuManager;
015: import org.eclipse.jface.action.IToolBarManager;
016: import org.eclipse.jface.action.MenuManager;
017: import org.eclipse.jface.action.Separator;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.jface.viewers.ISelectionProvider;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Menu;
026: import org.eclipse.ui.IActionBars;
027: import org.eclipse.ui.IViewPart;
028: import org.eclipse.ui.IWorkbenchActionConstants;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
031: import org.eclipse.ui.internal.WorkbenchImages;
032: import org.eclipse.ui.part.ViewPart;
033: import org.eclipse.ui.preferences.ViewPreferencesAction;
034:
035: /**
036: * The ProgressView is the class that shows the details of the current workbench
037: * progress.
038: */
039: public class ProgressView extends ViewPart implements IViewPart {
040:
041: DetailedProgressViewer viewer;
042:
043: Action cancelAction;
044:
045: Action clearAllAction;
046:
047: /*
048: * (non-Javadoc)
049: *
050: * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
051: */
052: public void createPartControl(Composite parent) {
053: viewer = new DetailedProgressViewer(parent, SWT.MULTI);
054: viewer.setComparator(ProgressManagerUtil
055: .getProgressViewerComparator());
056:
057: viewer.getControl().setLayoutData(
058: new GridData(SWT.FILL, SWT.FILL, true, true));
059:
060: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
061: IWorkbenchHelpContextIds.RESPONSIVE_UI);
062:
063: initContentProvider();
064: createClearAllAction();
065: createCancelAction();
066: initContextMenu();
067: initPulldownMenu();
068: initToolBar();
069: getSite().setSelectionProvider(viewer);
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.eclipse.ui.IWorkbenchPart#setFocus()
076: */
077: public void setFocus() {
078: if (viewer != null) {
079: viewer.setFocus();
080: }
081: }
082:
083: /**
084: * Sets the content provider for the viewer.
085: */
086: protected void initContentProvider() {
087: ProgressViewerContentProvider provider = new ProgressViewerContentProvider(
088: viewer, true, true);
089: viewer.setContentProvider(provider);
090: viewer.setInput(ProgressManager.getInstance());
091: }
092:
093: /**
094: * Initialize the context menu for the receiver.
095: */
096: private void initContextMenu() {
097: MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
098: Menu menu = menuMgr.createContextMenu(viewer.getControl());
099: 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(
109: IWorkbenchActionConstants.MB_ADDITIONS));
110: getSite().registerContextMenu(menuMgr, viewer);
111: viewer.getControl().setMenu(menu);
112: }
113:
114: private void initPulldownMenu() {
115: IMenuManager menuMgr = getViewSite().getActionBars()
116: .getMenuManager();
117: menuMgr.add(clearAllAction);
118: menuMgr.add(new ViewPreferencesAction() {
119: /*
120: * (non-Javadoc)
121: *
122: * @see org.eclipse.ui.internal.preferences.ViewPreferencesAction#openViewPreferencesDialog()
123: */
124: public void openViewPreferencesDialog() {
125: new JobsViewPreferenceDialog(viewer.getControl()
126: .getShell()).open();
127:
128: }
129: });
130:
131: }
132:
133: private void initToolBar() {
134: IActionBars bars = getViewSite().getActionBars();
135: IToolBarManager tm = bars.getToolBarManager();
136: tm.add(clearAllAction);
137: }
138:
139: /**
140: * Return the selected objects. If any of the selections are not JobInfos or
141: * there is no selection then return null.
142: *
143: * @return JobInfo[] or <code>null</code>.
144: */
145: private IStructuredSelection getSelection() {
146: // If the provider has not been set yet move on.
147: ISelectionProvider provider = getSite().getSelectionProvider();
148: if (provider == null) {
149: return null;
150: }
151: ISelection currentSelection = provider.getSelection();
152: if (currentSelection instanceof IStructuredSelection) {
153: return (IStructuredSelection) currentSelection;
154: }
155: return null;
156: }
157:
158: /**
159: * Get the currently selected job info. Only return it if it is the only
160: * item selected and it is a JobInfo.
161: *
162: * @return JobInfo
163: */
164: JobInfo getSelectedInfo() {
165: IStructuredSelection selection = getSelection();
166: if (selection != null && selection.size() == 1) {
167: JobTreeElement element = (JobTreeElement) selection
168: .getFirstElement();
169: if (element.isJobInfo()) {
170: return (JobInfo) element;
171: }
172: }
173: return null;
174: }
175:
176: /**
177: * Create the cancel action for the receiver.
178: */
179: private void createCancelAction() {
180: cancelAction = new Action(
181: ProgressMessages.ProgressView_CancelAction) {
182: /*
183: * (non-Javadoc)
184: *
185: * @see org.eclipse.jface.action.Action#run()
186: */
187: public void run() {
188: viewer.cancelSelection();
189: }
190: };
191:
192: }
193:
194: /**
195: * Create the clear all action for the receiver.
196: */
197: private void createClearAllAction() {
198: clearAllAction = new Action(
199: ProgressMessages.ProgressView_ClearAllAction) {
200: /*
201: * (non-Javadoc)
202: *
203: * @see org.eclipse.jface.action.Action#run()
204: */
205: public void run() {
206: FinishedJobs.getInstance().clearAll();
207: }
208: };
209: clearAllAction
210: .setToolTipText(ProgressMessages.NewProgressView_RemoveAllJobsToolTip);
211: ImageDescriptor id = WorkbenchImages
212: .getWorkbenchImageDescriptor("/elcl16/progress_remall.gif"); //$NON-NLS-1$
213: if (id != null) {
214: clearAllAction.setImageDescriptor(id);
215: }
216: id = WorkbenchImages
217: .getWorkbenchImageDescriptor("/dlcl16/progress_remall.gif"); //$NON-NLS-1$
218: if (id != null) {
219: clearAllAction.setDisabledImageDescriptor(id);
220: }
221: }
222:
223: }
|