KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > tasklist > ResolveMarkerAction


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.tasklist;
13
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.ui.IMarkerResolution;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.dialogs.MarkerResolutionSelectionDialog;
21 import org.eclipse.ui.ide.IDE;
22 import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
23
24 /**
25  * This action displays a list of resolutions for the selected marker
26  *
27  * @since 2.0
28  */

29 class ResolveMarkerAction extends TaskAction {
30
31     /**
32      * Creates the action.
33      */

34     protected ResolveMarkerAction(TaskList tasklist, String JavaDoc id) {
35         super(tasklist, id);
36         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
37                 ITaskListHelpContextIds.RESOLVE_MARKER_ACTION);
38     }
39
40     /**
41      * Returns whether this action should be enabled given the selection.
42      *
43      * @param selection the selection
44      * @return enablement
45      */

46     public boolean shouldEnable(IStructuredSelection selection) {
47         if (selection.size() != 1) {
48             return false;
49         }
50         IMarker marker = (IMarker) selection.getFirstElement();
51         if (marker == null) {
52             return false;
53         }
54         return IDE.getMarkerHelpRegistry().hasResolutions(marker);
55     }
56
57     /**
58      * Displays a list of resolutions and performs the selection.
59      */

60     public void run() {
61         IMarker marker = getMarker();
62         if (marker == null) {
63             return;
64         }
65         getTaskList().cancelEditing();
66         IMarkerResolution[] resolutions = getResolutions(marker);
67         if (resolutions.length == 0) {
68             MessageDialog.openInformation(getShell(), TaskListMessages.Resolve_title,
69                     TaskListMessages.Resolve_noResolutionsLabel);
70             return;
71         }
72         MarkerResolutionSelectionDialog d = new MarkerResolutionSelectionDialog(
73                 getShell(), resolutions);
74         if (d.open() != Window.OK) {
75             return;
76         }
77         Object JavaDoc[] result = d.getResult();
78         if (result != null && result.length > 0) {
79             ((IMarkerResolution) result[0]).run(marker);
80         }
81     }
82
83     /**
84      * Returns the resolutions for the given marker.
85      *
86      * @param marker the marker for which to obtain resolutions
87      * @return the resolutions for the selected marker
88      */

89     private IMarkerResolution[] getResolutions(IMarker marker) {
90         return IDE.getMarkerHelpRegistry().getResolutions(marker);
91     }
92
93     /**
94      * Returns the selected marker (may be <code>null</code>).
95      *
96      * @return the selected marker
97      */

98     private IMarker getMarker() {
99         IStructuredSelection selection = (IStructuredSelection) getTaskList()
100                 .getSelection();
101         // only enable for single selection
102
if (selection.size() != 1) {
103             return null;
104         }
105         return (IMarker) selection.getFirstElement();
106     }
107 }
108
Popular Tags