KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > RailsActionProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ruby.railsprojects;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30 import java.util.Set JavaDoc;
31 import org.netbeans.api.project.ProjectInformation;
32 import org.netbeans.modules.ruby.railsprojects.server.WebrickServer;
33 import org.netbeans.modules.ruby.rubyproject.api.RubyExecution;
34 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionService;
35 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation;
36 import org.netbeans.api.project.ProjectUtils;
37 import org.netbeans.modules.ruby.railsprojects.ui.customizer.RailsProjectProperties;
38 import org.netbeans.modules.ruby.rhtml.RhtmlKit;
39 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionDescriptor;
40 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionService;
41 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation;
42 import org.netbeans.modules.ruby.rubyproject.execution.FileLocator;
43 import org.netbeans.spi.project.ActionProvider;
44 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
45 import org.openide.ErrorManager;
46 import org.openide.LifecycleManager;
47 import org.openide.awt.HtmlBrowser;
48 import org.openide.cookies.SaveCookie;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.loaders.DataObject;
52 import org.openide.loaders.DataObjectNotFoundException;
53 import org.openide.util.Exceptions;
54 import org.openide.util.Lookup;
55
56 /** Action provider of the Ruby project. This is the place where to do
57  * strange things to Ruby actions. E.g. compile-single.
58  */

59 public class RailsActionProvider implements ActionProvider {
60     /**
61      * Standard command for running rdoc on a project.
62      * @see org.netbeans.spi.project.ActionProvider
63      */

64     public static final String JavaDoc COMMAND_RDOC = "rdoc"; // NOI18N
65

66     // Commands available from Ruby project
67
private static final String JavaDoc[] supportedActions = {
68         COMMAND_BUILD,
69         COMMAND_CLEAN,
70         COMMAND_REBUILD,
71         COMMAND_RDOC,
72 // COMMAND_COMPILE_SINGLE,
73
COMMAND_RUN,
74         COMMAND_RUN_SINGLE,
75 // COMMAND_DEBUG,
76
// COMMAND_DEBUG_SINGLE,
77
// COMMAND_TEST,
78
COMMAND_TEST_SINGLE,
79 // COMMAND_DEBUG_TEST_SINGLE,
80
// COMMAND_DEBUG_STEP_INTO,
81
COMMAND_DELETE,
82         COMMAND_COPY,
83         COMMAND_MOVE,
84         COMMAND_RENAME,
85     };
86     
87     
88     // Project
89
RailsProject project;
90     
91     // Ant project helper of the project
92
private UpdateHelper updateHelper;
93     
94         
95     /**Set of commands which are affected by background scanning*/
96     final Set JavaDoc<String JavaDoc> bkgScanSensitiveActions;
97     
98     public RailsActionProvider( RailsProject project, UpdateHelper updateHelper ) {
99         this.bkgScanSensitiveActions = new HashSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[] {
100             COMMAND_RUN,
101             COMMAND_RUN_SINGLE,
102 // COMMAND_DEBUG,
103
// COMMAND_DEBUG_SINGLE,
104
// COMMAND_DEBUG_STEP_INTO
105
}));
106             
107         this.updateHelper = updateHelper;
108         this.project = project;
109     }
110     
111 // private FileObject findBuildXml() {
112
// return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
113
// }
114

115     public String JavaDoc[] getSupportedActions() {
116         return supportedActions;
117     }
118         
119     public void invokeAction( final String JavaDoc command, final Lookup context ) throws IllegalArgumentException JavaDoc {
120         // TODO Check for valid installation of Ruby and Rake
121
if (COMMAND_RUN.equals(command)) {
122             // Save all files first
123
LifecycleManager.getDefault().saveAll();
124             
125             WebrickServer server = project.getLookup().lookup(WebrickServer.class);
126             if (server != null) {
127                 server.showUrl(""); //NOI18N
128
}
129
130             return;
131         } else if (COMMAND_RUN_SINGLE.equals(command)) {
132             FileObject file = findSources(context)[0];
133             
134             // Save the file
135
try {
136                 DataObject dobj = DataObject.find(file);
137                 if (dobj != null) {
138                     SaveCookie saveCookie = dobj.getCookie(SaveCookie.class);
139                     if (saveCookie != null) {
140                         saveCookie.save();
141                     }
142                 }
143             } catch (DataObjectNotFoundException donfe) {
144                 ErrorManager.getDefault().notify(donfe);
145             } catch (IOException JavaDoc ioe) {
146                 ErrorManager.getDefault().notify(ioe);
147             }
148
149             // Try to bring up the relevant URL
150
String JavaDoc path = "";
151             String JavaDoc fileName = file.getName();
152             final String JavaDoc CONTROLLER_SUFFIX = "_controller"; // NOI18N
153
final String JavaDoc HELPER_SUFFIX = "_helper"; // NOI18N
154

155             if (file.getExt().equals("rhtml")) { // NOI18N
156
String JavaDoc view = fileName;
157                 String JavaDoc controller = file.getParent() != null ? file.getParent().getName() : null;
158                 if (controller != null) {
159                     path = controller + "/" + view;
160                 } else {
161                     path = view;
162                 }
163             } else if (fileName.endsWith(CONTROLLER_SUFFIX)) {
164                 path = fileName.substring(0, fileName.length()-CONTROLLER_SUFFIX.length());
165             } else if (fileName.endsWith(HELPER_SUFFIX)) {
166                 path = fileName.substring(0, fileName.length()-HELPER_SUFFIX.length());
167             //} else if (parentName.equals("models")) { // NOI18N
168
// // XXX What do we do here?
169
} else if (fileName.endsWith("_test")) {
170                 // Run test normally - don't pop up browser
171
runRubyScript(FileUtil.toFile(file).getAbsolutePath(), file.getNameExt(), context);
172                 return;
173             }
174
175             WebrickServer server = project.getLookup().lookup(WebrickServer.class);
176             if (server != null) {
177                 server.showUrl(path);
178             }
179             
180             return;
181         } else if (COMMAND_BUILD.equals(command)) {
182             if (!RubyInstallation.getInstance().isValidRake(true)) {
183                 return;
184             }
185             
186             // Save all files first
187
LifecycleManager.getDefault().saveAll();
188
189             RubyFileLocator fileLocator = new RubyFileLocator(context);
190             String JavaDoc displayName = "Rake";
191
192             ProjectInformation info = ProjectUtils.getInformation(project);
193             if (info != null) {
194                 displayName = info.getDisplayName();
195             }
196             
197             File JavaDoc pwd = FileUtil.toFile(project.getProjectDirectory());
198             new ExecutionService(new ExecutionDescriptor(displayName, pwd, RubyInstallation.getInstance().getRake()).
199                     fileLocator(fileLocator).
200                     addOutputRecognizer(RubyExecution.RUBY_COMPILER)).
201                     run();
202             return;
203 // } else if (COMMAND_CLEAN.equals(command)) {
204
// executeTask(new File(RubyInstallation.getInstance().getRake()),
205
// "Rake", "clean", context, null, null); // TODO - internationalize
206
// return;
207
}
208         
209         if (COMMAND_RDOC.equals(command)) {
210             if (!RubyInstallation.getInstance().isValidRake(true)) {
211                 return;
212             }
213
214             // Run rake appdoc, then open <prj>/doc/app/index.html
215
LifecycleManager.getDefault().saveAll();
216             File JavaDoc pwd = FileUtil.toFile(project.getProjectDirectory());
217
218             Runnable JavaDoc showBrowser = new Runnable JavaDoc() {
219                 public void run() {
220                     // TODO - wait for the file to be created
221
// Open brower on the doc directory
222
FileObject doc = project.getProjectDirectory().getFileObject("doc/app");
223                     if (doc != null) {
224                         FileObject index = doc.getFileObject("index.html");
225                         if (index != null) {
226                             try {
227                                 URL JavaDoc url = FileUtil.toFile(index).toURI().toURL();
228
229                                 HtmlBrowser.URLDisplayer.getDefault().showURL(url);
230                             }
231                             catch (MalformedURLException JavaDoc ex) {
232                                 ErrorManager.getDefault().notify(ex);
233                             };
234                         }
235                     }
236                 }
237             };
238             
239             RubyFileLocator fileLocator = new RubyFileLocator(context);
240             String JavaDoc displayName = "Rake - Documentation";
241             new ExecutionService(new ExecutionDescriptor(displayName, pwd, RubyInstallation.getInstance().getRake(), "appdoc").
242                     postBuild(showBrowser).
243                     fileLocator(fileLocator).
244                     addOutputRecognizer(RubyExecution.RUBY_COMPILER)).
245                     run();
246         }
247
248         if (COMMAND_DELETE.equals(command)) {
249             DefaultProjectOperations.performDefaultDeleteOperation(project);
250             return ;
251         }
252         
253         if (COMMAND_COPY.equals(command)) {
254             DefaultProjectOperations.performDefaultCopyOperation(project);
255             return ;
256         }
257         
258         if (COMMAND_MOVE.equals(command)) {
259             DefaultProjectOperations.performDefaultMoveOperation(project);
260             return ;
261         }
262         
263         if (COMMAND_RENAME.equals(command)) {
264             DefaultProjectOperations.performDefaultRenameOperation(project, null);
265             return ;
266         }
267     }
268
269     private void runRubyScript(String JavaDoc target, String JavaDoc displayName, final Lookup context) {
270         String JavaDoc applicationArgs = project.evaluator().getProperty(RailsProjectProperties.APPLICATION_ARGS);
271         String JavaDoc options = project.evaluator().getProperty(RailsProjectProperties.RUN_JVM_ARGS);
272
273         if (options != null && options.trim().length() == 0) {
274             options = null;
275         }
276         if (applicationArgs != null && applicationArgs.trim().length() == 0) {
277             applicationArgs = null;
278         }
279
280         // Set the load path from the source and test folders.
281
// Load paths are additive so users can add their own in the
282
// options field as well.
283
FileObject[] srcPath = project.getSourceRoots().getRoots();
284         FileObject[] testPath = project.getTestSourceRoots().getRoots();
285         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
286         if (srcPath != null && srcPath.length > 0) {
287             for (FileObject root : srcPath) {
288                 if (sb.length() > 0) {
289                     sb.append(' ');
290                 }
291                 sb.append("-I\"");
292                 sb.append(FileUtil.toFile(root).getAbsoluteFile());
293                 sb.append("\"");
294             }
295         }
296         if (testPath != null && testPath.length > 0) {
297             for (FileObject root : testPath) {
298                 if (sb.length() > 0) {
299                     sb.append(' ');
300                 }
301                 sb.append("-I\"");
302                 sb.append(FileUtil.toFile(root).getAbsoluteFile());
303                 sb.append("\"");
304             }
305         }
306         String JavaDoc includePath = sb.toString();
307         if (options != null) {
308             options = includePath + " " + options;
309         } else {
310             options = includePath;
311         }
312
313         // Locate the target and specify it by full path.
314
// This is necessary because JRuby and Ruby don't locate the script from the load
315
// path it seems.
316
if (!new File JavaDoc(target).exists()) {
317             if (srcPath != null && srcPath.length > 0) {
318                 boolean found = false; // Prefer the first match
319
for (FileObject root : srcPath) {
320                     FileObject fo = root.getFileObject(target);
321                     if (fo != null) {
322                         target = FileUtil.toFile(fo).getAbsolutePath();
323                         found = true;
324                         break;
325                     }
326                 }
327                 if (!found && testPath != null) {
328                     for (FileObject root : testPath) {
329                         FileObject fo = root.getFileObject(target);
330                         if (fo != null) {
331                             target = FileUtil.toFile(fo).getAbsolutePath();
332                             break;
333                         }
334                     }
335                 }
336             }
337         }
338
339         String JavaDoc runDir = project.evaluator().getProperty(RailsProjectProperties.RUN_WORK_DIR);
340         File JavaDoc pwd = getSourceFolder();
341         if (runDir != null && runDir.length() > 0) {
342             File JavaDoc dir = new File JavaDoc(runDir);
343             if (!dir.exists()) {
344                 // Is it relative to the project directory?
345
dir = new File JavaDoc(FileUtil.toFile(project.getProjectDirectory()), runDir);
346                 if (!dir.exists()) {
347                     // Could it be relative to one of the source folders?
348
if (srcPath != null && srcPath.length > 0) {
349                         for (FileObject root : srcPath) {
350                             dir = new File JavaDoc(FileUtil.toFile(root), runDir);
351                             if (dir.exists()) {
352                                 break;
353                             }
354                         }
355                     }
356                 }
357             }
358             if (dir.exists()) {
359                 pwd = dir;
360             }
361         }
362
363         new ExecutionService(new ExecutionDescriptor(displayName, pwd, target).
364                 showSuspended(true).
365                 allowInput().
366                 initialArgs(options).
367                 additionalArgs(applicationArgs).
368                 fileLocator(new RubyFileLocator(context)).
369                 addOutputRecognizer(RubyExecution.RUBY_COMPILER)).
370                 run();
371     }
372     
373     
374     private File JavaDoc getSourceFolder() {
375         // Default to using the project source directory
376
FileObject[] srcPath = project.getSourceRoots().getRoots();
377         if (srcPath != null && srcPath.length > 0) {
378             return FileUtil.toFile(srcPath[0]);
379         } else {
380             return FileUtil.toFile(project.getProjectDirectory());
381         }
382     }
383     
384 // /**
385
// * @return array of targets or null to stop execution; can return empty array
386
// */
387
// private String[] getTargetNames(String command, Lookup context, Properties p) throws IllegalArgumentException {
388
// String[] targetNames = new String[0];
389
// if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
390
// throw new RuntimeException("Not yet implemented");
391
// FileObject[] sourceRoots = project.getSourceRoots().getRoots();
392
// FileObject[] files = findSourcesAndPackages( context, sourceRoots);
393
// boolean recursive = (context.lookup(NonRecursiveFolder.class) == null);
394
// if (files != null) {
395
// p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots,files[0]), recursive)); // NOI18N
396
// targetNames = new String[] {"compile-single"}; // NOI18N
397
// }
398
// else {
399
// FileObject[] testRoots = project.getTestSourceRoots().getRoots();
400
// files = findSourcesAndPackages(context, testRoots);
401
// p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); // NOI18N
402
// targetNames = new String[] {"compile-test-single"}; // NOI18N
403
// }
404
// }
405
// else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
406
// FileObject[] files = findTestSourcesForSources(context);
407
// targetNames = setupTestSingle(p, files);
408
// }
409
// else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
410
// FileObject[] files = findTestSourcesForSources(context);
411
// targetNames = setupDebugTestSingle(p, files);
412
// } else if (command.equals (COMMAND_RUN_SINGLE) || command.equals (COMMAND_DEBUG_SINGLE)) {
413
// FileObject[] files = findTestSources(context, false);
414
// if (files != null) {
415
// if (command.equals(COMMAND_RUN_SINGLE)) {
416
// targetNames = setupTestSingle(p, files);
417
// } else {
418
// targetNames = setupDebugTestSingle(p, files);
419
// }
420
// } else {
421
// FileObject file = findSources(context)[0];
422
// String clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),file), file);
423
//// p.setProperty("javac.includes", clazz); // NOI18N
424
// // Convert foo/FooTest.java -> foo.FooTest
425
// // XXX What about Ruby?
426
// if (clazz.endsWith(".java")) { // NOI18N
427
// clazz = clazz.substring(0, clazz.length() - 5);
428
// }
429
// clazz = clazz.replace('/','.');
430
//
431
// if (!RailsProjectUtil.hasMainMethod (file)) {
432
// NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(RailsActionProvider.class, "LBL_No_Main_Classs_Found", clazz), NotifyDescriptor.INFORMATION_MESSAGE);
433
// DialogDisplayer.getDefault().notify(nd);
434
// return null;
435
// } else {
436
// if (command.equals (COMMAND_RUN_SINGLE)) {
437
// p.setProperty("run.class", clazz); // NOI18N
438
// targetNames = (String[])commands.get(COMMAND_RUN_SINGLE);
439
// } else {
440
// p.setProperty("debug.class", clazz); // NOI18N
441
// targetNames = (String[])commands.get(COMMAND_DEBUG_SINGLE);
442
// }
443
// }
444
// }
445
// }
446
// return targetNames;
447
// }
448

449     public boolean isActionEnabled( String JavaDoc command, Lookup context ) {
450 // FileObject buildXml = findBuildXml();
451
// if ( buildXml == null || !buildXml.isValid()) {
452
// return false;
453
// }
454
if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
455             return findSourcesAndPackages( context, project.getSourceRoots().getRoots()) != null
456                     || findSourcesAndPackages( context, project.getTestSourceRoots().getRoots()) != null;
457         }
458         else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
459             return findTestSourcesForSources(context) != null;
460         }
461         else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
462             FileObject[] files = findTestSourcesForSources(context);
463             return files != null && files.length == 1;
464         } else if (command.equals(COMMAND_RUN_SINGLE) ||
465                         command.equals(COMMAND_DEBUG_SINGLE)) {
466             FileObject fos[] = findSources(context);
467             if (fos != null && fos.length == 1) {
468                 return true;
469             }
470             fos = findTestSources(context, false);
471             return fos != null && fos.length == 1;
472         } else {
473             // other actions are global
474
return true;
475         }
476     }
477     
478     
479    
480     // Private methods -----------------------------------------------------------------
481

482     /** Find selected sources, the sources has to be under single source root,
483      * @param context the lookup in which files should be found
484      */

485     private FileObject[] findSources(Lookup context) {
486         FileObject[] srcPath = project.getSourceRoots().getRoots();
487         for (int i=0; i< srcPath.length; i++) {
488             FileObject[] files = findSelectedFiles(context, srcPath[i], RubyInstallation.RUBY_MIME_TYPE, true); // NOI18N
489
if (files != null) {
490                 return files;
491             }
492             files = findSelectedFiles(context, srcPath[i], RhtmlKit.RHTML_MIME_TYPE, true); // NOI18N
493
if (files != null) {
494                 return files;
495             }
496         }
497         return null;
498     }
499
500     private FileObject[] findSourcesAndPackages (Lookup context, FileObject srcDir) {
501         if (srcDir != null) {
502             FileObject[] files = findSelectedFiles(context, srcDir, null, true); // NOI18N
503
//Check if files are either packages or Ruby files
504
if (files != null) {
505                 for (int i = 0; i < files.length; i++) {
506                     if (!files[i].isFolder() && (files[i].getMIMEType().equals(RubyInstallation.RUBY_MIME_TYPE) ||
507                             files[i].getMIMEType().equals(RhtmlKit.RHTML_MIME_TYPE))) {
508                         return null;
509                     }
510                 }
511             }
512             return files;
513         } else {
514             return null;
515         }
516     }
517     
518     private FileObject[] findSourcesAndPackages (Lookup context, FileObject[] srcRoots) {
519         for (int i=0; i<srcRoots.length; i++) {
520             FileObject[] result = findSourcesAndPackages(context, srcRoots[i]);
521             if (result != null) {
522                 return result;
523             }
524         }
525         return null;
526     }
527     
528     /** Find either selected tests or tests which belong to selected source files
529      */

530     private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) {
531         //XXX: Ugly, should be rewritten
532
// FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
533
// for (int i=0; i< testSrcPath.length; i++) {
534
// FileObject[] files = findSelectedFiles(context, testSrcPath[i], RubyInstallation.RUBY_MIME_TYPE, true); // NOI18N
535
// if (files != null) {
536
// return files;
537
// }
538
// }
539
// if (checkInSrcDir && testSrcPath.length>0) {
540
// FileObject[] files = findSources (context);
541
// if (files != null) {
542
// //Try to find the test under the test roots
543
// FileObject srcRoot = getRoot(project.getSourceRoots().getRoots(),files[0]);
544
// for (int i=0; i<testSrcPath.length; i++) {
545
// FileObject[] files2 = ActionUtils.regexpMapFiles(files,srcRoot, SRCDIRJAVA, testSrcPath[i], SUBST, true);
546
// if (files2 != null) {
547
// return files2;
548
// }
549
// }
550
// }
551
// }
552
return null;
553     }
554    
555
556     /** Find tests corresponding to selected sources.
557      */

558     private FileObject[] findTestSourcesForSources(Lookup context) {
559 // FileObject[] sourceFiles = findSources(context);
560
// if (sourceFiles == null) {
561
// return null;
562
// }
563
// FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
564
// if (testSrcPath.length == 0) {
565
// return null;
566
// }
567
// FileObject[] srcPath = project.getSourceRoots().getRoots();
568
// FileObject srcDir = getRoot(srcPath, sourceFiles[0]);
569
// for (int i=0; i<testSrcPath.length; i++) {
570
// FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i], SUBST, true);
571
// if (files2 != null) {
572
// return files2;
573
// }
574
// }
575
return null;
576     }
577     
578     
579     // From the ant module - ActionUtils.
580
// However, I've modified it to do its search based on mime type rather than file suffixes
581
// (since some Ruby files do not use a .rb extension and are discovered based on the initial shebang line)
582

583     public static FileObject[] findSelectedFiles(Lookup context, FileObject dir, String JavaDoc mimeType, boolean strict) {
584         if (dir != null && !dir.isFolder()) {
585             throw new IllegalArgumentException JavaDoc("Not a folder: " + dir); // NOI18N
586
}
587         Collection JavaDoc<FileObject> files = new LinkedHashSet JavaDoc<FileObject>(); // #50644: remove dupes
588
// XXX this should perhaps also check for FileObject's...
589
for (DataObject d : context.lookupAll(DataObject.class)) {
590             FileObject f = d.getPrimaryFile();
591             boolean matches = FileUtil.toFile(f) != null;
592             if (dir != null) {
593                 matches &= (FileUtil.isParentOf(dir, f) || dir == f);
594             }
595             if (mimeType != null) {
596                 matches &= f.getMIMEType().equals(mimeType);
597             }
598             // Generally only files from one project will make sense.
599
// Currently the action UI infrastructure (PlaceHolderAction)
600
// checks for that itself. Should there be another check here?
601
if (matches) {
602                 files.add(f);
603             } else if (strict) {
604                 return null;
605             }
606         }
607         if (files.isEmpty()) {
608             return null;
609         }
610         return files.toArray(new FileObject[files.size()]);
611     }
612     
613
614     private class RubyFileLocator implements FileLocator {
615         private Lookup context;
616
617         RubyFileLocator(Lookup context) {
618             this.context = context;
619         }
620         
621         public FileObject find(String JavaDoc file) {
622             FileObject[] fos = null;
623             if (context != Lookup.EMPTY) {
624         
625                 // First check roots and search by relative path.
626
FileObject[] srcPath = project.getSourceRoots().getRoots();
627                 if (srcPath != null) {
628                     for (FileObject root : srcPath) {
629                         FileObject f = root.getFileObject(file);
630                         if (f != null) {
631                             return f;
632                         }
633                     }
634                 }
635
636                 // Next try searching the set of source files
637
fos = findSources(context);
638                 if (fos != null) {
639                     for (FileObject fo : fos) {
640                         if (fo.getNameExt().equals(file)) {
641                             return fo;
642                         }
643                     }
644                 }
645             }
646
647             // Manual search
648
FileObject[] srcPath = project.getSourceRoots().getRoots();
649             for (FileObject root : srcPath) {
650                 // First see if this path is relative to the root
651
try {
652                     File JavaDoc f = new File JavaDoc(FileUtil.toFile(root), file);
653                     if (f.exists()) {
654                         f = f.getCanonicalFile();
655                         return FileUtil.toFileObject(f);
656                     }
657                 } catch (IOException JavaDoc ioe) {
658                     Exceptions.printStackTrace(ioe);
659                 }
660                 
661                 // Search recursively for the given file below the path
662
FileObject fo = findFile(root, file);
663                 if (fo != null) {
664                     return fo;
665                 }
666             }
667             
668             return null;
669         }
670         
671         private FileObject findFile(FileObject fo, String JavaDoc name) {
672             if (name.equals(fo.getNameExt())) {
673                 return fo;
674             }
675             
676             for (FileObject child : fo.getChildren()) {
677                 FileObject found = findFile(child, name);
678                 if (found != null) {
679                     return found;
680                 }
681             }
682             
683             return null;
684         }
685     }
686 }
687
Popular Tags