0
|
1 |
/*
|
|
2 |
* Copyright (c) 2007 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 |
package com.nokia.s60tools.apiquery.popup.actions;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.net.URI;
|
|
21 |
import java.net.URISyntaxException;
|
|
22 |
import java.util.ArrayList;
|
|
23 |
import java.util.Collection;
|
|
24 |
import java.util.List;
|
|
25 |
import java.util.Vector;
|
|
26 |
|
|
27 |
import org.eclipse.core.resources.IFile;
|
|
28 |
import org.eclipse.core.resources.IFolder;
|
|
29 |
import org.eclipse.core.resources.IProject;
|
|
30 |
import org.eclipse.core.resources.IResource;
|
|
31 |
import org.eclipse.core.resources.ResourcesPlugin;
|
|
32 |
import org.eclipse.core.runtime.CoreException;
|
|
33 |
import org.eclipse.core.runtime.IPath;
|
|
34 |
import org.eclipse.core.runtime.NullProgressMonitor;
|
|
35 |
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
|
36 |
import org.eclipse.core.runtime.jobs.IJobChangeListener;
|
|
37 |
import org.eclipse.core.runtime.jobs.Job;
|
|
38 |
import org.eclipse.swt.SWT;
|
|
39 |
import org.eclipse.swt.widgets.Display;
|
|
40 |
import org.eclipse.ui.IEditorDescriptor;
|
|
41 |
import org.eclipse.ui.IEditorRegistry;
|
|
42 |
import org.eclipse.ui.IWorkbench;
|
|
43 |
import org.eclipse.ui.IWorkbenchPage;
|
|
44 |
import org.eclipse.ui.PlatformUI;
|
|
45 |
import org.eclipse.ui.ide.IDE;
|
|
46 |
|
|
47 |
import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
|
|
48 |
import com.nokia.carbide.cdt.builder.EpocEngineHelper;
|
|
49 |
import com.nokia.carbide.cdt.builder.ICarbideBuildManager;
|
|
50 |
import com.nokia.carbide.cdt.builder.project.ICarbideBuildConfiguration;
|
|
51 |
import com.nokia.carbide.cdt.builder.project.ICarbideProjectInfo;
|
|
52 |
import com.nokia.s60tools.apiquery.shared.exceptions.QueryOperationFailedException;
|
|
53 |
import com.nokia.s60tools.apiquery.shared.job.FindFileFromFoldersJob;
|
|
54 |
import com.nokia.s60tools.apiquery.shared.plugin.APIQueryPlugin;
|
|
55 |
import com.nokia.s60tools.apiquery.shared.resources.Messages;
|
|
56 |
import com.nokia.s60tools.apiquery.shared.ui.dialogs.APIQueryMessageBox;
|
|
57 |
import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Action class for find and then open wanted file to Editor area.
|
|
61 |
* File can be seeked under SDK include paths or
|
|
62 |
* under given project in Carbides Workspace.
|
|
63 |
*/
|
|
64 |
public class OpenFileAction implements IJobChangeListener{
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Current default build configuration SDK ID.
|
|
68 |
*/
|
|
69 |
private String sdkUniqueId = null;
|
|
70 |
private String fileName = null;
|
|
71 |
private String projectName;
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Opens file from given project in Carbide workspace.
|
|
75 |
* @param fileName
|
|
76 |
* @param projectName
|
|
77 |
*/
|
|
78 |
public void openFileFromProject(String fileName, String projectName) {
|
|
79 |
|
|
80 |
this.fileName = fileName;
|
|
81 |
this.projectName = projectName;
|
|
82 |
try {
|
|
83 |
|
|
84 |
APIQueryConsole.getInstance().println(Messages.getString("OpenFileAction.StartingSeek_Msg_Part1") +fileName +Messages.getString("OpenFileAction.StartingSeek_Msg_Part2") +projectName +"'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
85 |
|
|
86 |
IProject project = getProject(projectName);
|
|
87 |
|
|
88 |
IResource[] members = project.members();
|
|
89 |
|
|
90 |
Vector<IResource> files = getMatchingResources(fileName, members);
|
|
91 |
|
|
92 |
if(!files.isEmpty()){
|
|
93 |
|
|
94 |
//It should not be possible that there is multiple foundings for resource, because path is added when file was seeked
|
|
95 |
IResource res = files.get(0);
|
|
96 |
IPath path = res.getFullPath();
|
|
97 |
URI uri = res.getLocationURI();
|
|
98 |
openFile(uri, path.lastSegment());
|
|
99 |
}else{
|
|
100 |
String message = Messages.getString("OpenFileAction.CannotFoundFile_ErrMsg_Part1") +fileName +Messages.getString("OpenFileAction.CannotFoundFile_ErrMsg_Part2") +projectName +"'."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
101 |
showErrorMessage(message);
|
|
102 |
|
|
103 |
}
|
|
104 |
|
|
105 |
} catch (Exception e) {
|
|
106 |
showUnexpectedErrorMsg(e);
|
|
107 |
}
|
|
108 |
|
|
109 |
}
|
|
110 |
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Logs and shows error message for unexpected error situations.
|
|
114 |
* @param fileName
|
|
115 |
* @param projectName
|
|
116 |
* @param e {@link Exception}
|
|
117 |
*/
|
|
118 |
private void showUnexpectedErrorMsg(Exception e) {
|
|
119 |
e.printStackTrace();
|
|
120 |
String msg = Messages.getString("OpenFileAction.Unexpected_ErrMsg_Part1")+fileName +Messages.getString("OpenFileAction.Unexpected_ErrMsg_Part2") +projectName +"'."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
121 |
String consoleMsg = msg +Messages.getString("OpenFileAction.Unexpected_ErrMsg_Part3") +e; //$NON-NLS-1$
|
|
122 |
APIQueryConsole.getInstance().println(consoleMsg, APIQueryConsole.MSG_ERROR);
|
|
123 |
showErrorMessage(msg);
|
|
124 |
}
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Show an error message to user and logs same message to console.
|
|
130 |
* @param message
|
|
131 |
*/
|
|
132 |
private void showErrorMessage(final String message) {
|
|
133 |
APIQueryConsole.getInstance().println(message, APIQueryConsole.MSG_ERROR);
|
|
134 |
Runnable run = new Runnable(){
|
|
135 |
public void run(){
|
|
136 |
new APIQueryMessageBox(message,
|
|
137 |
SWT.ICON_ERROR | SWT.OK).open();
|
|
138 |
|
|
139 |
}
|
|
140 |
};
|
|
141 |
Display.getDefault().asyncExec(run);
|
|
142 |
}
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Check recursively all folders inside given resources and seeks give file
|
|
148 |
* @param fileName
|
|
149 |
* @param members
|
|
150 |
* @return resources found
|
|
151 |
* @throws CoreException
|
|
152 |
*/
|
|
153 |
private Vector<IResource> getMatchingResources(String fileName,
|
|
154 |
IResource[] members) throws CoreException {
|
|
155 |
Vector<IResource> files = new Vector<IResource>();
|
|
156 |
|
|
157 |
for (int i = 0; i < members.length; i++) {
|
|
158 |
IResource res = members[i];
|
|
159 |
//If resource is folder, seeking all folders and files under it
|
|
160 |
if (res instanceof IFolder) {
|
|
161 |
IFolder fold = (IFolder) res;
|
|
162 |
IResource[] folderMembers = fold.members();
|
|
163 |
files.addAll(getMatchingResources(fileName, folderMembers));
|
|
164 |
}
|
|
165 |
//If resource is file, checking if thats file we are interest of
|
|
166 |
else if (res instanceof IFile) {
|
|
167 |
|
|
168 |
String name = res.getName();
|
|
169 |
//If file name match to resource name adding to
|
|
170 |
if (fileName.equalsIgnoreCase(name)) {
|
|
171 |
files.add(res);
|
|
172 |
}else if(fileName.equalsIgnoreCase(res.getProjectRelativePath().toOSString())){
|
|
173 |
files.add(res);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
//Else we have no interest of resource
|
|
177 |
}
|
|
178 |
return files;
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Open file given to Editor area.
|
|
183 |
* @param uri
|
|
184 |
* @param fileName
|
|
185 |
*/
|
|
186 |
public void openFile(final URI uri, final String fileName) {
|
|
187 |
|
|
188 |
//Runnable to open new file
|
|
189 |
final Runnable runOpen = new Runnable() {
|
|
190 |
public void run() {
|
|
191 |
// do the actual work in here
|
|
192 |
|
|
193 |
try {
|
|
194 |
IWorkbench workbench = PlatformUI.getWorkbench();
|
|
195 |
IWorkbenchPage page = APIQueryPlugin.getCurrentlyActivePage();
|
|
196 |
//Find default editor for that file
|
|
197 |
IEditorRegistry reg = workbench.getEditorRegistry();
|
|
198 |
IEditorDescriptor editor = reg.getDefaultEditor(fileName);
|
|
199 |
//We open editor by it's ID
|
|
200 |
final String editorId = editor.getId();
|
|
201 |
//Opening file in editor
|
|
202 |
IDE.openEditor(page, uri, editorId, true);
|
|
203 |
|
|
204 |
} catch (Exception e) {
|
|
205 |
showUnexpectedErrorMsg(e);
|
|
206 |
}
|
|
207 |
|
|
208 |
}
|
|
209 |
};
|
|
210 |
Display.getDefault().asyncExec(runOpen);
|
|
211 |
}
|
|
212 |
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Open file to Editor area
|
|
216 |
* @param file
|
|
217 |
* @throws URISyntaxException
|
|
218 |
*/
|
|
219 |
private void openFile(File file) throws URISyntaxException {
|
|
220 |
String path = file.getAbsolutePath();
|
|
221 |
String uriStr = path.replace("\\", "/");; //$NON-NLS-1$ //$NON-NLS-2$
|
|
222 |
uriStr = "file://" + uriStr; //$NON-NLS-1$
|
|
223 |
URI uri = new URI(uriStr);
|
|
224 |
openFile(uri, file.getName());
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Opens file from SDK. Projects default build configuration is used as SDK,
|
|
229 |
* MMP files from that project is seeked, and file is seeked under inc paths found in
|
|
230 |
* MMP files.
|
|
231 |
*
|
|
232 |
* File is opened or error message is shown when done.
|
|
233 |
*
|
|
234 |
* @param fileName
|
|
235 |
* @param projectName
|
|
236 |
*/
|
|
237 |
public void openFileFromSDK(String fileName, String projectName) {
|
|
238 |
|
|
239 |
this.fileName = fileName;
|
|
240 |
this.projectName = projectName;
|
|
241 |
try {
|
|
242 |
//Get project by name
|
|
243 |
IProject project = getProject(projectName);
|
|
244 |
//Gets Include paths for project. Header files must be found from includepaths.
|
|
245 |
List<File> paths = getIncludePaths(project);
|
|
246 |
APIQueryConsole.getInstance().println(
|
|
247 |
Messages.getString("OpenFileAction.TargetFound_Msg_Part1") +sdkUniqueId //$NON-NLS-1$
|
|
248 |
+Messages.getString("OpenFileAction.TargetFound_Msg_Part2") +projectName //$NON-NLS-1$
|
|
249 |
+Messages.getString("OpenFileAction.TargetFound_Msg_Part3") +fileName +"'."); //$NON-NLS-1$ //$NON-NLS-2$
|
|
250 |
|
|
251 |
//Seeking paths where file(s) are found
|
|
252 |
//it will happened asynchronously, and when done, file will be opened.
|
|
253 |
findFiles(fileName, paths);
|
|
254 |
|
|
255 |
} catch (QueryOperationFailedException e) {
|
|
256 |
showErrorMessage(e.getMessage());
|
|
257 |
}catch (Exception e) {
|
|
258 |
showUnexpectedErrorMsg(e);
|
|
259 |
}
|
|
260 |
|
|
261 |
|
|
262 |
}
|
|
263 |
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Get include paths for project
|
|
267 |
* @param project
|
|
268 |
* @return absolutely include paths for project, found in MMP files.
|
|
269 |
* @throws QueryOperationFailedException
|
|
270 |
*/
|
|
271 |
private List<File> getIncludePaths(IProject project) throws QueryOperationFailedException {
|
|
272 |
List<File> paths = new ArrayList<File>();
|
|
273 |
|
|
274 |
ICarbideBuildManager buildMgr = CarbideBuilderPlugin.getBuildManager();
|
|
275 |
ICarbideProjectInfo cpi = null;
|
|
276 |
if (buildMgr.isCarbideProject(project)){
|
|
277 |
// check to make sure this is a Carbide project
|
|
278 |
cpi = buildMgr.getProjectInfo(project);
|
|
279 |
// Get the default build configuration
|
|
280 |
ICarbideBuildConfiguration defaultConfig = cpi.getDefaultConfiguration();
|
|
281 |
sdkUniqueId = defaultConfig.getSDK().getUniqueId();
|
|
282 |
|
|
283 |
List<File> _userPaths = new ArrayList<File>();
|
|
284 |
List<File> _systemPaths = new ArrayList<File>();
|
|
285 |
EpocEngineHelper.getProjectIncludePaths(cpi, defaultConfig, _userPaths, _systemPaths) ;
|
|
286 |
paths.addAll(_userPaths);
|
|
287 |
paths.addAll(_systemPaths);
|
|
288 |
}
|
|
289 |
else{
|
|
290 |
throw new QueryOperationFailedException(Messages.getString("OpenFileAction.NotCarbideProject_ErrMsg_Part1") +projectName +Messages.getString("OpenFileAction.NotCarbideProject_ErrMsg_Part2")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
291 |
}
|
|
292 |
|
|
293 |
|
|
294 |
return paths;
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Get project by name.
|
|
299 |
* @param projectName
|
|
300 |
* @return project
|
|
301 |
* @throws CoreException
|
|
302 |
* @throws QueryOperationFailedException
|
|
303 |
*/
|
|
304 |
private IProject getProject(String projectName) throws CoreException, QueryOperationFailedException {
|
|
305 |
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
|
|
306 |
if(!prj.exists()){
|
|
307 |
throw new QueryOperationFailedException (Messages.getString("OpenFileAction.ProjectNotExist_ErrMsg_Part1") +projectName +Messages.getString("OpenFileAction.ProjectNotExist_ErrMsg_Part2")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
308 |
}
|
|
309 |
if(!prj.isOpen()){
|
|
310 |
//If project is closed, just opening it before returning
|
|
311 |
prj.open(new NullProgressMonitor());
|
|
312 |
}
|
|
313 |
prj.getWorkspace();
|
|
314 |
return prj;
|
|
315 |
}
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
/**
|
|
320 |
* Find file under folders given
|
|
321 |
* @param fileName
|
|
322 |
* @param includePaths
|
|
323 |
*/
|
|
324 |
private void findFiles(String fileName, List<File> includePaths) {
|
|
325 |
// System.out.println(Messages.getString("OpenFileAction.18")); //$NON-NLS-1$
|
|
326 |
FindFileFromFoldersJob job = new FindFileFromFoldersJob(Messages.getString("OpenFileAction.JobName_Msg") +fileName, includePaths, fileName, sdkUniqueId); //$NON-NLS-1$
|
|
327 |
job.setPriority(Job.DECORATE);
|
|
328 |
//Listener will be called when seeking is done
|
|
329 |
job.addJobChangeListener(this);
|
|
330 |
job.schedule();
|
|
331 |
|
|
332 |
}
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
/* (non-Javadoc)
|
|
338 |
* @see org.eclipse.core.runtime.jobs.IJobChangeListener#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
|
|
339 |
*/
|
|
340 |
public void done(IJobChangeEvent event) {
|
|
341 |
|
|
342 |
//
|
|
343 |
// When job is done, this will be called, and found file will be opened
|
|
344 |
|
|
345 |
try {
|
|
346 |
FindFileFromFoldersJob job = (FindFileFromFoldersJob) event
|
|
347 |
.getJob();
|
|
348 |
Collection<File> files = job.getFoundSourceFiles();
|
|
349 |
|
|
350 |
if (files.isEmpty()) {
|
|
351 |
String message = Messages.getString("OpenFileAction.CannotFoundFile_Msg_Part1") + fileName //$NON-NLS-1$
|
|
352 |
+ Messages.getString("OpenFileAction.CannotFoundFile_Msg_Part2") + sdkUniqueId //$NON-NLS-1$
|
|
353 |
+ "'."; //$NON-NLS-1$
|
|
354 |
showErrorMessage(message);
|
|
355 |
}
|
|
356 |
//Else we just open first file that we found
|
|
357 |
else{
|
|
358 |
File file = ((File[]) files.toArray(new File[0]))[0];
|
|
359 |
openFile(file);
|
|
360 |
}
|
|
361 |
} catch (Exception e) {
|
|
362 |
showUnexpectedErrorMsg(e);
|
|
363 |
}
|
|
364 |
|
|
365 |
}
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
/* (non-Javadoc)
|
|
370 |
* @see org.eclipse.core.runtime.jobs.IJobChangeListener#aboutToRun(org.eclipse.core.runtime.jobs.IJobChangeEvent)
|
|
371 |
*/
|
|
372 |
public void aboutToRun(IJobChangeEvent event) {
|
|
373 |
//Not needed
|
|
374 |
}
|
|
375 |
|
|
376 |
|
|
377 |
/* (non-Javadoc)
|
|
378 |
* @see org.eclipse.core.runtime.jobs.IJobChangeListener#awake(org.eclipse.core.runtime.jobs.IJobChangeEvent)
|
|
379 |
*/
|
|
380 |
public void awake(IJobChangeEvent event) {
|
|
381 |
//Not needed
|
|
382 |
|
|
383 |
}
|
|
384 |
|
|
385 |
/* (non-Javadoc)
|
|
386 |
* @see org.eclipse.core.runtime.jobs.IJobChangeListener#running(org.eclipse.core.runtime.jobs.IJobChangeEvent)
|
|
387 |
*/
|
|
388 |
public void running(IJobChangeEvent event) {
|
|
389 |
//Not needed
|
|
390 |
}
|
|
391 |
|
|
392 |
|
|
393 |
/* (non-Javadoc)
|
|
394 |
* @see org.eclipse.core.runtime.jobs.IJobChangeListener#scheduled(org.eclipse.core.runtime.jobs.IJobChangeEvent)
|
|
395 |
*/
|
|
396 |
public void scheduled(IJobChangeEvent event) {
|
|
397 |
//Not needed
|
|
398 |
}
|
|
399 |
|
|
400 |
|
|
401 |
/* (non-Javadoc)
|
|
402 |
* @see org.eclipse.core.runtime.jobs.IJobChangeListener#sleeping(org.eclipse.core.runtime.jobs.IJobChangeEvent)
|
|
403 |
*/
|
|
404 |
public void sleeping(IJobChangeEvent event) {
|
|
405 |
//Not needed
|
|
406 |
}
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
}
|