KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > ActionResolveMarker


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.views.markers.internal;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
22 import org.eclipse.jface.operation.IRunnableContext;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.ui.IMarkerResolution;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.ide.IDE;
30 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
32 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
33
34 /**
35  * This action displays a list of resolutions for the selected marker
36  *
37  * @since 2.0
38  */

39 public class ActionResolveMarker extends MarkerSelectionProviderAction {
40
41     private MarkerView view;
42
43     /**
44      * Create a new instance of the receiver.
45      *
46      * @param markerView
47      * @param provider
48      */

49     public ActionResolveMarker(MarkerView markerView,
50             ISelectionProvider provider) {
51         super(provider, MarkerMessages.resolveMarkerAction_title);
52         setEnabled(false);
53         setImageDescriptor(IDEInternalWorkbenchImages
54                 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ELCL_QUICK_FIX_ENABLED));
55         setDisabledImageDescriptor(IDEInternalWorkbenchImages
56                 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_DLCL_QUICK_FIX_DISABLED));
57         view = markerView;
58     }
59
60     /**
61      * Displays a list of resolutions and performs the selection.
62      */

63     public void run() {
64
65         IRunnableContext context = new ProgressMonitorDialog(view.getSite()
66                 .getShell());
67         final Object JavaDoc[] resolutions = new Object JavaDoc[1];
68
69         IRunnableWithProgress resolutionsRunnable = new IRunnableWithProgress() {
70             public void run(IProgressMonitor monitor) {
71                 monitor.beginTask(NLS.bind(
72                         MarkerMessages.resolveMarkerAction_computationAction,
73                         getMarkerDescription()), 100);
74                 monitor.worked(25);
75                 resolutions[0] = IDE.getMarkerHelpRegistry().getResolutions(
76                         getSelectedMarker());
77                 monitor.done();
78             }
79         };
80
81         Object JavaDoc service = view.getSite().getAdapter(
82                 IWorkbenchSiteProgressService.class);
83
84         try {
85             if (service == null) {
86                 PlatformUI.getWorkbench().getProgressService().runInUI(context,
87                         resolutionsRunnable, null);
88             } else {
89                 ((IWorkbenchSiteProgressService) service).runInUI(context,
90                         resolutionsRunnable, null);
91             }
92         } catch (InvocationTargetException JavaDoc exception) {
93             handleException(exception);
94             return;
95         } catch (InterruptedException JavaDoc exception) {
96             handleException(exception);
97             return;
98         }
99
100         IMarkerResolution[] foundResolutions = (IMarkerResolution[]) resolutions[0];
101         if (foundResolutions.length == 0)
102             MessageDialog
103                     .openInformation(
104                             view.getSite().getShell(),
105                             MarkerMessages.MarkerResolutionDialog_CannotFixTitle,
106                             NLS
107                                     .bind(
108                                             MarkerMessages.MarkerResolutionDialog_CannotFixMessage,
109                                             getMarkerDescription()));
110         else {
111             Dialog dialog = new MarkerResolutionDialog(view.getSite()
112                     .getShell(), getSelectedMarker(), foundResolutions, view);
113             dialog.open();
114         }
115
116     }
117
118     /**
119      * Handle the exception.
120      *
121      * @param exception
122      */

123     private void handleException(Exception JavaDoc exception) {
124         IDEWorkbenchPlugin.log(exception.getLocalizedMessage(), exception);
125         ErrorDialog.openError(view.getSite().getShell(), MarkerMessages.Error,
126                 NLS.bind(
127                         MarkerMessages.MarkerResolutionDialog_CannotFixMessage,
128                         getMarkerDescription()), Util.errorStatus(exception));
129     }
130
131     /**
132      * Return the description of the marker.
133      *
134      * @return String
135      */

136     private String JavaDoc getMarkerDescription() {
137         return Util.getProperty(IMarker.MESSAGE, getSelectedMarker());
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
144      */

145     public void selectionChanged(IStructuredSelection selection) {
146
147         if (Util.isSingleConcreteSelection(selection)) {
148             if (IDE.getMarkerHelpRegistry().hasResolutions(getSelectedMarker())) {
149                 setEnabled(true);
150                 return;
151             }
152         }
153
154         setEnabled(false);
155     }
156 }
157
Popular Tags