001: /*******************************************************************************
002: * Copyright (c) 2000, 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.views.markers.internal;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.resources.IMarker;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.jface.dialogs.Dialog;
017: import org.eclipse.jface.dialogs.ErrorDialog;
018: import org.eclipse.jface.dialogs.MessageDialog;
019: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
020: import org.eclipse.jface.operation.IRunnableContext;
021: import org.eclipse.jface.operation.IRunnableWithProgress;
022: import org.eclipse.jface.viewers.ISelectionProvider;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.osgi.util.NLS;
025: import org.eclipse.ui.IMarkerResolution;
026: import org.eclipse.ui.PlatformUI;
027: import org.eclipse.ui.ide.IDE;
028: import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
029: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
030: import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
031:
032: /**
033: * This action displays a list of resolutions for the selected marker
034: *
035: * @since 2.0
036: */
037: public class ActionResolveMarker extends MarkerSelectionProviderAction {
038:
039: private MarkerView view;
040:
041: /**
042: * Create a new instance of the receiver.
043: *
044: * @param markerView
045: * @param provider
046: */
047: public ActionResolveMarker(MarkerView markerView,
048: ISelectionProvider provider) {
049: super (provider, MarkerMessages.resolveMarkerAction_title);
050: setEnabled(false);
051: setImageDescriptor(IDEInternalWorkbenchImages
052: .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ELCL_QUICK_FIX_ENABLED));
053: setDisabledImageDescriptor(IDEInternalWorkbenchImages
054: .getImageDescriptor(IDEInternalWorkbenchImages.IMG_DLCL_QUICK_FIX_DISABLED));
055: view = markerView;
056: }
057:
058: /**
059: * Displays a list of resolutions and performs the selection.
060: */
061: public void run() {
062:
063: IRunnableContext context = new ProgressMonitorDialog(view
064: .getSite().getShell());
065: final Object[] resolutions = new Object[1];
066:
067: IRunnableWithProgress resolutionsRunnable = new IRunnableWithProgress() {
068: public void run(IProgressMonitor monitor) {
069: monitor
070: .beginTask(
071: NLS
072: .bind(
073: MarkerMessages.resolveMarkerAction_computationAction,
074: getMarkerDescription()),
075: 100);
076: monitor.worked(25);
077: resolutions[0] = IDE.getMarkerHelpRegistry()
078: .getResolutions(getSelectedMarker());
079: monitor.done();
080: }
081: };
082:
083: Object service = view.getSite().getAdapter(
084: IWorkbenchSiteProgressService.class);
085:
086: try {
087: if (service == null) {
088: PlatformUI.getWorkbench().getProgressService().runInUI(
089: context, resolutionsRunnable, null);
090: } else {
091: ((IWorkbenchSiteProgressService) service).runInUI(
092: context, resolutionsRunnable, null);
093: }
094: } catch (InvocationTargetException exception) {
095: handleException(exception);
096: return;
097: } catch (InterruptedException exception) {
098: handleException(exception);
099: return;
100: }
101:
102: IMarkerResolution[] foundResolutions = (IMarkerResolution[]) resolutions[0];
103: if (foundResolutions.length == 0)
104: MessageDialog
105: .openInformation(
106: view.getSite().getShell(),
107: MarkerMessages.MarkerResolutionDialog_CannotFixTitle,
108: NLS
109: .bind(
110: MarkerMessages.MarkerResolutionDialog_CannotFixMessage,
111: getMarkerDescription()));
112: else {
113: Dialog dialog = new MarkerResolutionDialog(view.getSite()
114: .getShell(), getSelectedMarker(), foundResolutions,
115: view);
116: dialog.open();
117: }
118:
119: }
120:
121: /**
122: * Handle the exception.
123: *
124: * @param exception
125: */
126: private void handleException(Exception exception) {
127: IDEWorkbenchPlugin.log(exception.getLocalizedMessage(),
128: exception);
129: ErrorDialog
130: .openError(
131: view.getSite().getShell(),
132: MarkerMessages.Error,
133: NLS
134: .bind(
135: MarkerMessages.MarkerResolutionDialog_CannotFixMessage,
136: getMarkerDescription()), Util
137: .errorStatus(exception));
138: }
139:
140: /**
141: * Return the description of the marker.
142: *
143: * @return String
144: */
145: private String getMarkerDescription() {
146: return Util.getProperty(IMarker.MESSAGE, getSelectedMarker());
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
153: */
154: public void selectionChanged(IStructuredSelection selection) {
155:
156: if (Util.isSingleConcreteSelection(selection)) {
157: if (IDE.getMarkerHelpRegistry().hasResolutions(
158: getSelectedMarker())) {
159: setEnabled(true);
160: return;
161: }
162: }
163:
164: setEnabled(false);
165: }
166: }
|