01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.views.markers;
11:
12: import org.eclipse.core.resources.IMarker;
13: import org.eclipse.core.runtime.CoreException;
14: import org.eclipse.jface.viewers.StructuredSelection;
15: import org.eclipse.ui.IPageLayout;
16: import org.eclipse.ui.IViewPart;
17: import org.eclipse.ui.IWorkbenchPage;
18: import org.eclipse.ui.views.markers.internal.MarkerView;
19:
20: /**
21: * Utility class for showing markers in the marker views.
22: */
23: public class MarkerViewUtil {
24:
25: /**
26: * The PATH_ATTRIBUTE is the tag for the attribute on a marker
27: * that can be used to supply the String for the path rather than
28: * using the path of the underlying resource.
29: * @see IMarker#getAttribute(java.lang.String)
30: * @since 3.2
31: */
32: public static final String PATH_ATTRIBUTE = "org.eclipse.ui.views.markers.path";//$NON-NLS-1$
33:
34: /**
35: * The NAME_ATTRIBUTE is the tag for the attribute on a marker
36: * that can be used to supply the String for the name rather than
37: * using the name of the underlying resource.
38: * @see IMarker#getAttribute(java.lang.String)
39: * @since 3.2
40: */
41: public static final String NAME_ATTRIBUTE = "org.eclipse.ui.views.markers.name";//$NON-NLS-1$
42:
43: /**
44: * Returns the id of the view used to show markers of the
45: * same type as the given marker.
46: *
47: * @param marker the marker
48: * @return the view id or <code>null</code> if no appropriate view could be determined
49: * @throws CoreException if an exception occurs testing the type of the marker
50: */
51: public static String getViewId(IMarker marker) throws CoreException {
52: if (marker.isSubtypeOf(IMarker.TASK)) {
53: return IPageLayout.ID_TASK_LIST;
54: } else if (marker.isSubtypeOf(IMarker.PROBLEM)) {
55: return IPageLayout.ID_PROBLEM_VIEW;
56: } else if (marker.isSubtypeOf(IMarker.BOOKMARK)) {
57: return IPageLayout.ID_BOOKMARKS;
58: }
59: return null;
60: }
61:
62: /**
63: * Shows the given marker in the appropriate view in the given page.
64: * This must be called from the UI thread.
65: *
66: * @param page the workbench page in which to show the marker
67: * @param marker the marker to show
68: * @param showView <code>true</code> if the view should be shown first
69: * <code>false</code> to only show the marker if the view is already showing
70: * @return <code>true</code> if the marker was successfully shown,
71: * <code>false</code> if not
72: *
73: */
74: public static boolean showMarker(IWorkbenchPage page,
75: IMarker marker, boolean showView) {
76: try {
77: String viewId = getViewId(marker);
78: if (viewId != null) {
79: IViewPart view = showView ? page.showView(viewId)
80: : page.findView(viewId);
81: if (view instanceof MarkerView) {
82: StructuredSelection selection = new StructuredSelection(
83: marker);
84: MarkerView markerView = (MarkerView) view;
85: markerView.setSelection(selection, true);
86: return true;
87:
88: //return markerView.getSelection().equals(selection);
89: }
90: }
91: } catch (CoreException e) {
92: // ignore
93: }
94: return false;
95: }
96:
97: }
|