Skip to main content
summaryrefslogtreecommitdiffstats
blob: c32010233d6e2f3756b8f04dcb1256c9d590a54e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
 * Copyright (c) 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 ******************************************************************************/
/*
 * Created on Jun 18, 2003
 */
package org.eclipse.cdt.internal.ui.search;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.internal.ui.util.SelectionUtil;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.search.ui.ISearchResultView;
import org.eclipse.search.ui.ISearchResultViewEntry;
import org.eclipse.search.ui.SearchUI;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.ide.IDE;

/**
 * @author aniefer
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class GotoMarkerAction extends Action {

	public GotoMarkerAction(){
			
	}
	
	public void run() {
		ISearchResultView view = SearchUI.getSearchResultView();
		Object element = SelectionUtil.getSingleElement( view.getSelection() );
		if( element instanceof ISearchResultViewEntry ) {
			ISearchResultViewEntry entry = (ISearchResultViewEntry) element;
			show( entry.getSelectedMarker() );
		}
	}
	
	private void show( IMarker marker ){
		IResource resource = marker.getResource();
		if( resource == null || !resource.exists() ){
			return;
		}
		
		IWorkbenchPage page = CUIPlugin.getActivePage();
		ICElement element = CSearchUtil.getCElement( marker );
		
		if( SearchUI.reuseEditor() ){
			showWithReuse( marker, resource, element, page );
		} else {
			showWithoutReuse( marker, element, page );
		}
	}
	
	private void showWithoutReuse( IMarker marker, ICElement element, IWorkbenchPage page ){
		IEditorPart editor = null;
		try{
			Object objectToOpen = ( element != null ) ? (Object) element : (Object) marker.getResource();
			editor = EditorUtility.openInEditor( objectToOpen, false );
		} catch ( CoreException e ) {
			//boo
		}
		if( editor != null ){
			IDE.gotoMarker(editor, marker);
		}
	}
	
	private void showWithReuse( IMarker marker, IResource resource, ICElement element, IWorkbenchPage page ) {
		if( !(resource instanceof IFile) ){
			return;
		}
		
		IEditorPart editor = EditorUtility.isOpenInEditor( element );
		if( editor != null ){
			page.bringToTop( editor );
		} else {
			
		}
	}
}

Back to the top