0
|
1 |
/*
|
|
2 |
* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
package com.nokia.s60tools.appdep.ui.actions;
|
|
20 |
|
|
21 |
import java.io.File;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.net.URI;
|
|
24 |
import java.net.URISyntaxException;
|
|
25 |
|
|
26 |
import org.eclipse.core.resources.IWorkspace;
|
|
27 |
import org.eclipse.core.resources.IWorkspaceRunnable;
|
|
28 |
import org.eclipse.core.resources.ResourcesPlugin;
|
|
29 |
import org.eclipse.core.runtime.CoreException;
|
|
30 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
31 |
import org.eclipse.core.runtime.IStatus;
|
|
32 |
import org.eclipse.core.runtime.Status;
|
|
33 |
import org.eclipse.jface.text.IDocument;
|
|
34 |
import org.eclipse.swt.SWT;
|
|
35 |
import org.eclipse.ui.IEditorDescriptor;
|
|
36 |
import org.eclipse.ui.IEditorPart;
|
|
37 |
import org.eclipse.ui.IEditorRegistry;
|
|
38 |
import org.eclipse.ui.IWorkbenchPage;
|
|
39 |
import org.eclipse.ui.PartInitException;
|
|
40 |
import org.eclipse.ui.PlatformUI;
|
|
41 |
import org.eclipse.ui.editors.text.TextEditor;
|
|
42 |
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
|
|
43 |
import org.eclipse.ui.ide.IDE;
|
|
44 |
|
|
45 |
import com.nokia.s60tools.appdep.core.AppDepSettings;
|
|
46 |
import com.nokia.s60tools.appdep.core.ITargetPlatform;
|
|
47 |
import com.nokia.s60tools.appdep.core.data.ComponentNode;
|
|
48 |
import com.nokia.s60tools.appdep.exceptions.CacheFileDoesNotExistException;
|
|
49 |
import com.nokia.s60tools.appdep.exceptions.CacheIndexNotReadyException;
|
|
50 |
import com.nokia.s60tools.appdep.resources.Messages;
|
|
51 |
import com.nokia.s60tools.appdep.ui.dialogs.AppDepMessageBox;
|
|
52 |
import com.nokia.s60tools.appdep.ui.views.main.MainView;
|
|
53 |
import com.nokia.s60tools.appdep.ui.views.main.MainViewDataPopulator;
|
|
54 |
import com.nokia.s60tools.appdep.util.AppDepConsole;
|
|
55 |
import com.nokia.s60tools.sdk.SdkInformation;
|
|
56 |
import com.nokia.s60tools.util.sourcecode.ISourceFinder;
|
|
57 |
import com.nokia.s60tools.util.sourcecode.SourceFileLocation;
|
|
58 |
import com.nokia.s60tools.util.sourcecode.SourceFinderFactory;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Real implementation for show source action.
|
|
62 |
*/
|
|
63 |
public abstract class AbstractShowSourceFileAction extends AbstractMainViewAction{
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Build variant used for source file search (=target Platform used).
|
|
67 |
*/
|
|
68 |
protected String variant;
|
|
69 |
/**
|
|
70 |
* SDK used for source file search.
|
|
71 |
*/
|
|
72 |
protected SdkInformation sdkInfo;
|
|
73 |
/**
|
|
74 |
* Set to <code>true</code> if current target selection contains SIS file targets.
|
|
75 |
*/
|
|
76 |
protected boolean showSISfileWarningDialog = false;
|
|
77 |
/**
|
|
78 |
* EPOCROOT used for source file search.
|
|
79 |
*/
|
|
80 |
protected String epocRootPath;
|
|
81 |
/**
|
|
82 |
* Build type used for source file search.
|
|
83 |
*/
|
|
84 |
protected String build;
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Constructor.
|
|
88 |
* @param view Reference to the view.
|
|
89 |
*/
|
|
90 |
public AbstractShowSourceFileAction(MainView view){
|
|
91 |
super(view);
|
|
92 |
|
|
93 |
setText(Messages.getString("ShowSourceFileMainViewAction.ShowSource_Action_Text")); //$NON-NLS-1$
|
|
94 |
setToolTipText(Messages.getString("ShowSourceFileMainViewAction.ShowSource_Action_Tooltip")); //$NON-NLS-1$
|
|
95 |
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Real run method implementation for run() method.
|
|
100 |
* @see com.nokia.s60tools.appdep.ui.actions.AbstractMainViewAction#run()
|
|
101 |
* @param methodName method name from user selection
|
|
102 |
* @param ordinal from user selection
|
|
103 |
*/
|
|
104 |
protected void runImpl(String methodName,
|
|
105 |
String ordinal) {
|
|
106 |
Object obj = view.getComponentTreeSelectedElement();
|
|
107 |
|
|
108 |
if (obj == null) {
|
|
109 |
// We might get null-selections when
|
|
110 |
// tree is expanded/collapsed.
|
|
111 |
// Getting component node that is cached.
|
|
112 |
obj = view.getMostRecentlySelectedComponentNode();
|
|
113 |
if(obj == null){
|
|
114 |
return;
|
|
115 |
}
|
|
116 |
}
|
|
117 |
try {
|
|
118 |
// Component is for sure a component node
|
|
119 |
ComponentNode node = (ComponentNode) obj;
|
|
120 |
String componentName = node.getName();
|
|
121 |
|
|
122 |
AppDepSettings settings = AppDepSettings.getActiveSettings();
|
|
123 |
if (settings.getCurrentlyAnalyzedComponentName() != null) {
|
|
124 |
|
|
125 |
AppDepConsole.getInstance().println(
|
|
126 |
Messages.getString("ShowSourceFileMainViewAction.ShowSource_Query_Start_Console_Msg_Part1") //$NON-NLS-1$
|
|
127 |
+ componentName//$NON-NLS-1$
|
|
128 |
+Messages.getString("ShowSourceFileMainViewAction.ShowSource_Query_Start_Console_Msg_Part2") //$NON-NLS-1$
|
|
129 |
+ ordinal//$NON-NLS-1$
|
|
130 |
+ "'..."); //$NON-NLS-1$
|
|
131 |
|
|
132 |
// Collection needed information to get source file path
|
|
133 |
ISourceFinder finder = SourceFinderFactory
|
|
134 |
.createSourceFinder(AppDepConsole.getInstance());
|
|
135 |
|
|
136 |
// Resolving source search parameters based on the settings and component name
|
|
137 |
resolveSearchParameters(componentName, settings);
|
|
138 |
|
|
139 |
//Search source file location by collected information
|
|
140 |
SourceFileLocation location = finder.findSourceFile(ordinal,
|
|
141 |
methodName, componentName, variant, build, epocRootPath);
|
|
142 |
|
|
143 |
if(location.getSourceFileLocation() == null){
|
|
144 |
|
|
145 |
String msg = Messages.getString("ShowSourceFileMainViewAction.SourceFileCannotBeFound_Msg_Part1") //$NON-NLS-1$
|
|
146 |
+ componentName//$NON-NLS-1$
|
|
147 |
+Messages.getString("ShowSourceFileMainViewAction.SourceFileCannotBeFound_Msg_Part2") //$NON-NLS-1$
|
|
148 |
+ ordinal//$NON-NLS-1$
|
|
149 |
+Messages.getString("ShowSourceFileMainViewAction.SourceFileCannotBeFound_Msg_Part3") //$NON-NLS-1$
|
|
150 |
+ methodName//$NON-NLS-1$
|
|
151 |
+"'";//$NON-NLS-1$
|
|
152 |
|
|
153 |
AppDepConsole.getInstance().println(msg, AppDepConsole.MSG_WARNING);
|
|
154 |
|
|
155 |
showErrorMsgDialog(msg);
|
|
156 |
|
|
157 |
}else{
|
|
158 |
//Open found file, and set focus to method line
|
|
159 |
openFileAndSetFocus(location);
|
|
160 |
}
|
|
161 |
|
|
162 |
//If SIS file is in analysis, showing on information dialog, that opened suorce file is not neccessarily same than used when SIS was build.
|
|
163 |
if(showSISfileWarningDialog ){
|
|
164 |
AppDepMessageBox msgBox = new AppDepMessageBox(
|
|
165 |
Messages.getString("ShowSourceFileMainViewAction.SourceFileOpenedFromSISFile_Info_Msg"), //$NON-NLS-1$
|
|
166 |
SWT.ICON_INFORMATION | SWT.OK);
|
|
167 |
msgBox.open();
|
|
168 |
}
|
|
169 |
|
|
170 |
}
|
|
171 |
} catch (Exception e) {
|
|
172 |
e.printStackTrace();
|
|
173 |
String msg = Messages.getString("ShowSourceFileMainViewAction.SourceFileCannotBeFound_ErrMsg") //$NON-NLS-1$
|
|
174 |
+" "//$NON-NLS-1$
|
|
175 |
+ e;
|
|
176 |
AppDepConsole.getInstance().println(
|
|
177 |
msg, //$NON-NLS-1$
|
|
178 |
AppDepConsole.MSG_ERROR);
|
|
179 |
showErrorMsgDialog(Messages.getString("ShowSourceFileMainViewAction.SourceFileCannotBeFound_ErrMsg_ToUser"));//$NON-NLS-1$
|
|
180 |
|
|
181 |
}
|
|
182 |
// Remember to always call AbstractMainViewAction
|
|
183 |
// base class implementation
|
|
184 |
super.run();
|
|
185 |
}
|
|
186 |
|
|
187 |
/**
|
|
188 |
* Resolves source search parameters based on the settings and component name
|
|
189 |
* @param componentName Component name.
|
|
190 |
* @param settings Used settings.
|
|
191 |
* @throws IOException
|
|
192 |
* @throws CacheIndexNotReadyException
|
|
193 |
* @throws CacheFileDoesNotExistException
|
|
194 |
*/
|
|
195 |
protected void resolveSearchParameters(String componentName,
|
|
196 |
AppDepSettings settings) throws IOException,
|
|
197 |
CacheIndexNotReadyException, CacheFileDoesNotExistException {
|
|
198 |
sdkInfo = settings.getCurrentlyUsedSdk();
|
|
199 |
epocRootPath = sdkInfo.getEpocRootDir();
|
|
200 |
build = settings.getBuildType().getBuildTypeName();
|
|
201 |
|
|
202 |
variant = MainViewDataPopulator.getTargetPlatformIdStringForComponent(settings, componentName);
|
|
203 |
|
|
204 |
showSISfileWarningDialog = includesSISFileTarget(settings);
|
|
205 |
}
|
|
206 |
|
|
207 |
/**
|
|
208 |
* Checks if target platform array includes a SIS file target.
|
|
209 |
* @param settings settings object to be used for getting target platform data
|
|
210 |
* @return <code>true</code> if includes a SIS file target, otherwise <code>false</code>.
|
|
211 |
*/
|
|
212 |
private boolean includesSISFileTarget(AppDepSettings settings) {
|
|
213 |
ITargetPlatform[] usedPlatforms = settings.getCurrentlyUsedTargetPlatforms();
|
|
214 |
for (int i = 0; i < usedPlatforms.length; i++) {
|
|
215 |
ITargetPlatform platform = usedPlatforms[i];
|
|
216 |
if(platform.getId().equals(AppDepSettings.TARGET_TYPE_ID_SIS)){
|
|
217 |
return true;
|
|
218 |
}
|
|
219 |
}
|
|
220 |
return false;
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Show on error message
|
|
225 |
* @param msg Error message.
|
|
226 |
*/
|
|
227 |
protected void showErrorMsgDialog(String msg){
|
|
228 |
AppDepMessageBox msgBox = new AppDepMessageBox(msg, SWT.ICON_ERROR | SWT.OK);
|
|
229 |
msgBox.open();
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* OpensFile in editor
|
|
234 |
* @param location Source file location
|
|
235 |
* @throws URISyntaxException
|
|
236 |
* @throws CoreException
|
|
237 |
*/
|
|
238 |
private void openFileAndSetFocus(final SourceFileLocation location) throws URISyntaxException, CoreException {
|
|
239 |
|
|
240 |
AppDepConsole.getInstance().println(
|
|
241 |
Messages.getString("ShowSourceFileMainViewAction.OpeningFile_Msg") //$NON-NLS-1$
|
|
242 |
+ location.getSourceFileLocation() //$NON-NLS-1$)
|
|
243 |
+ "'"); //$NON-NLS-1$)
|
|
244 |
|
|
245 |
File file = new File(location.getSourceFileLocation());
|
|
246 |
if(file == null || !file.exists()){
|
|
247 |
AppDepConsole.getInstance().println(
|
|
248 |
Messages.getString("ShowSourceFileMainViewAction.SourceFileDoesNotExist_ErrMsg_Part1") //$NON-NLS-1$
|
|
249 |
+ location.getSourceFileLocation()
|
|
250 |
+Messages.getString("ShowSourceFileMainViewAction.SourceFileDoesNotExist_ErrMsg_Part2"), AppDepConsole.MSG_ERROR); //$NON-NLS-1$)
|
|
251 |
return;
|
|
252 |
}
|
|
253 |
|
|
254 |
//Create URI to open file
|
|
255 |
String uriStr = location.getSourceFileLocation().replace("\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
256 |
uriStr = "file://" + uriStr; //$NON-NLS-1$
|
|
257 |
final URI srcURI = new URI(uriStr);
|
|
258 |
|
|
259 |
//Find default editor for that file
|
|
260 |
IEditorRegistry reg = PlatformUI.getWorkbench().getEditorRegistry();
|
|
261 |
|
|
262 |
|
|
263 |
IEditorDescriptor editor = reg.getDefaultEditor(file.getName());
|
|
264 |
//We open editor by it's ID
|
|
265 |
final String editorId = editor.getId();
|
|
266 |
|
|
267 |
|
|
268 |
//Runnable to open new file
|
|
269 |
final IWorkspaceRunnable runOpen = new IWorkspaceRunnable() {
|
|
270 |
public void run(IProgressMonitor monitor) throws CoreException {
|
|
271 |
// do the actual work in here
|
|
272 |
|
|
273 |
IWorkbenchPage page = PlatformUI.getWorkbench()
|
|
274 |
.getActiveWorkbenchWindow().getActivePage();
|
|
275 |
try {
|
|
276 |
|
|
277 |
IEditorPart part = IDE.openEditor(page, srcURI, editorId, true);
|
|
278 |
|
|
279 |
if(part != null){
|
|
280 |
//Set focus to correct line
|
|
281 |
setFocusToLineWhereMethodIs(location);
|
|
282 |
}
|
|
283 |
|
|
284 |
} catch (PartInitException e) {
|
|
285 |
e.printStackTrace();
|
|
286 |
Status status = new Status(IStatus.ERROR,
|
|
287 |
"com.nokia.s60tools.metadataeditor", 0, e //$NON-NLS-1$
|
|
288 |
.getMessage(), e);
|
|
289 |
|
|
290 |
throw new CoreException(status);
|
|
291 |
}
|
|
292 |
|
|
293 |
}
|
|
294 |
};
|
|
295 |
ResourcesPlugin.getWorkspace().run(runOpen, null, IWorkspace.AVOID_UPDATE, null);
|
|
296 |
|
|
297 |
}
|
|
298 |
|
|
299 |
/**
|
|
300 |
* Setting the focus in opened file there where the method name occurs,
|
|
301 |
* must call after file is opened and only if opening was successful
|
|
302 |
* @param location Source file location
|
|
303 |
* @throws CoreException
|
|
304 |
*/
|
|
305 |
protected void setFocusToLineWhereMethodIs(
|
|
306 |
final SourceFileLocation location) throws CoreException {
|
|
307 |
//Runnable to open new file
|
|
308 |
final IWorkspaceRunnable runSetFocus = new IWorkspaceRunnable() {
|
|
309 |
public void run(IProgressMonitor monitor) throws CoreException {
|
|
310 |
// do the actual work in here
|
|
311 |
|
|
312 |
try {
|
|
313 |
//Setting focus to correct line
|
|
314 |
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
|
315 |
IEditorPart activeEditor = activePage.getActiveEditor();
|
|
316 |
|
|
317 |
if(activeEditor != null && activeEditor instanceof TextEditor){
|
|
318 |
|
|
319 |
// This is actually an instance of 'org.eclipse.cdt.internal.ui.editor.CEditor'
|
|
320 |
// that extends org.eclipse.ui.editors.text.TextEditor
|
|
321 |
TextEditor editor = (TextEditor) activeEditor;
|
|
322 |
IDocument doc = getDocument(editor);
|
|
323 |
if(doc != null){
|
|
324 |
|
|
325 |
String text = doc.get();
|
|
326 |
int methodOffset = location.getMethodOffset();
|
|
327 |
if(methodOffset == SourceFileLocation.OFFSET_NOT_FOUND){
|
|
328 |
// Removing parameters and getting new offset.
|
|
329 |
String methodNameWithOutParams = location.getMethodName();
|
|
330 |
methodNameWithOutParams = methodNameWithOutParams.substring(0, methodNameWithOutParams.indexOf("(")); //$NON-NLS-1$
|
|
331 |
methodOffset = text.indexOf(methodNameWithOutParams);
|
|
332 |
|
|
333 |
if(methodOffset == SourceFileLocation.OFFSET_NOT_FOUND){
|
|
334 |
// Removing possible namespace and getting new offset.
|
|
335 |
String separator = "::"; //$NON-NLS-1$
|
|
336 |
int separatorLocation = methodNameWithOutParams.lastIndexOf(separator);
|
|
337 |
if(separatorLocation > 0){
|
|
338 |
methodNameWithOutParams = methodNameWithOutParams.substring(separatorLocation + separator.length());
|
|
339 |
methodOffset = text.indexOf(methodNameWithOutParams);
|
|
340 |
}
|
|
341 |
}
|
|
342 |
}
|
|
343 |
|
|
344 |
editor.setHighlightRange(methodOffset, 0, true);
|
|
345 |
}
|
|
346 |
}
|
|
347 |
|
|
348 |
} catch (Exception e) {
|
|
349 |
e.printStackTrace();
|
|
350 |
Status status = new Status(IStatus.ERROR,
|
|
351 |
Messages.getString("ShowSourceFileActionGeneralMessage.OpenFileAndSetLineFocus_ErrMsg"), 0, e //$NON-NLS-1$
|
|
352 |
.getMessage(), e);
|
|
353 |
throw new CoreException(status);
|
|
354 |
}
|
|
355 |
|
|
356 |
}
|
|
357 |
};
|
|
358 |
|
|
359 |
|
|
360 |
ResourcesPlugin.getWorkspace().run(runSetFocus, null, IWorkspace.AVOID_UPDATE, null);
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Returns the document interface for the currently active document
|
|
365 |
* in the given editor.
|
|
366 |
* @param editor Editor to ask currently active document from.
|
|
367 |
* @return Document interface if found, otherwise <code>null</code>.
|
|
368 |
*/
|
|
369 |
private IDocument getDocument(TextEditor editor) {
|
|
370 |
|
|
371 |
TextFileDocumentProvider documentProvider = (TextFileDocumentProvider) editor.getDocumentProvider();
|
|
372 |
if(documentProvider != null){
|
|
373 |
return documentProvider.getDocument(editor.getEditorInput());
|
|
374 |
}
|
|
375 |
return null;
|
|
376 |
}
|
|
377 |
}
|