view external project files in debugger mode
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / PHPSourceLocator.java
1 package net.sourceforge.phpdt.internal.debug.ui;
2
3 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
4 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
5 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
6 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
7 import net.sourceforge.phpeclipse.builder.FileStorage;
8
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.Path;
14 import org.eclipse.debug.core.ILaunchConfiguration;
15 import org.eclipse.debug.core.model.IPersistableSourceLocator;
16 import org.eclipse.debug.core.model.IStackFrame;
17 import org.eclipse.debug.ui.ISourcePresentation;
18 import org.eclipse.ui.IEditorDescriptor;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorRegistry;
21 import org.eclipse.ui.IWorkbench;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.part.FileEditorInput;
26
27 public class PHPSourceLocator implements IPersistableSourceLocator, ISourcePresentation {
28   private String absoluteWorkingDirectory;
29
30   public PHPSourceLocator() {
31
32   }
33
34   public String getAbsoluteWorkingDirectory() {
35     return absoluteWorkingDirectory;
36   }
37   /**
38    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
39    */
40   public String getMemento() throws CoreException {
41     return null;
42   }
43
44   /**
45    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
46    */
47   public void initializeFromMemento(String memento) throws CoreException {
48   }
49
50   /**
51    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
52    */
53   public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
54     this.absoluteWorkingDirectory = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
55   }
56
57   /**
58    * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
59    */
60   public Object getSourceElement(IStackFrame stackFrame) {
61     return ((PHPStackFrame) stackFrame).getFileName();
62   }
63
64   /**
65    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
66    */
67   public String getEditorId(IEditorInput input, Object element) {
68     return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
69   }
70
71   /**
72    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
73    */
74   public IEditorInput getEditorInput(Object element) {
75
76     String filename = (String) element;
77     IWorkbench workbench = PlatformUI.getWorkbench();
78     IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
79     IWorkbenchPage page = window.getActivePage();
80     IPath path = new Path(filename);
81
82     // If the file exists in the workspace, open it
83     IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(path);
84     //    IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
85 //    if (eclipseFile == null) {
86 //      filename = this.getAbsoluteWorkingDirectory() + "/" + filename;
87 //      eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
88 //      if (eclipseFile == null) {
89 //        PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
90 //        return null;
91 //      }
92 //    } else 
93     if (eclipseFile == null || !eclipseFile.exists()) {
94       //                Otherwise open the stream directly
95       if (page == null) {
96         PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
97         return null;
98       }
99       FileStorage storage = new FileStorage(path);
100       storage.setReadOnly();
101       //      IEditorRegistry registry = workbench.getEditorRegistry();
102       //      IEditorDescriptor desc = registry.getDefaultEditor(filename);
103       //      if (desc == null) {
104       //        desc = registry.getDefaultEditor();
105       //      }
106       return new ExternalEditorInput(storage);
107     }
108     return new FileEditorInput(eclipseFile);
109
110   }
111
112 }