1 package net.sourceforge.phpdt.internal.launching;
3 import java.util.Iterator;
6 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
9 import net.sourceforge.phpeclipse.builder.FileStorage;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.model.IPersistableSourceLocator;
19 import org.eclipse.debug.core.model.IStackFrame;
20 import org.eclipse.debug.ui.ISourcePresentation;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.part.FileEditorInput;
28 public class PHPSourceLocator implements IPersistableSourceLocator, ISourcePresentation {
29 private String absoluteWorkingDirectory;
30 private Map pathMap = null;
31 private boolean remoteDebug;
32 private IPath remoteSourcePath;
33 private String projectName;
35 public PHPSourceLocator() {
39 public String getAbsoluteWorkingDirectory() {
40 return absoluteWorkingDirectory;
43 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
45 public String getMemento() throws CoreException {
50 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
52 public void initializeFromMemento(String memento) throws CoreException {
56 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
58 public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
59 this.absoluteWorkingDirectory = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
60 this.remoteDebug=configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG,false);
61 this.pathMap = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, (Map)null);
62 this.projectName =configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
64 if (Platform.getOS().equals(Platform.OS_WIN32))
65 this.remoteSourcePath= new Path((configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "")).toLowerCase());
67 this.remoteSourcePath= new Path(configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
74 * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
76 public Object getSourceElement(IStackFrame stackFrame) {
78 String fileName=((PHPStackFrame) stackFrame).getFileName();
83 IPath path = new Path(fileName);
84 if (remoteSourcePath.isPrefixOf(path))
87 path=path.removeFirstSegments(remoteSourcePath.matchingFirstSegments(path));
89 projectPath=(PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation());
90 return (projectPath.append(path)).toOSString();
93 if (pathMap == null) {
96 Iterator iterator = pathMap.keySet().iterator();
97 while (iterator.hasNext()) {
98 String local = (String) iterator.next();
99 IPath remotePath = new Path((String) pathMap.get(local));
100 if (remotePath.isPrefixOf(path)) {
101 path=path.removeFirstSegments(remotePath.matchingFirstSegments(path));
102 IPath localPath= new Path(local);
103 return localPath.append(path).toOSString();
113 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
115 public String getEditorId(IEditorInput input, Object element) {
116 return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
120 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
122 public IEditorInput getEditorInput(Object element) {
124 String filename = (String) element;
125 IWorkbench workbench = PlatformUI.getWorkbench();
126 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
127 IWorkbenchPage page = window.getActivePage();
128 IPath path = new Path(filename);
132 // If the file exists in the workspace, open it
133 IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(path);
134 // IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
135 // if (eclipseFile == null) {
136 // filename = this.getAbsoluteWorkingDirectory() + "/" + filename;
137 // eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
138 // if (eclipseFile == null) {
139 // PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
143 if (eclipseFile == null || !eclipseFile.exists()) {
144 // Otherwise open the stream directly
146 PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
149 FileStorage storage = new FileStorage(path);
150 storage.setReadOnly();
151 // IEditorRegistry registry = workbench.getEditorRegistry();
152 // IEditorDescriptor desc = registry.getDefaultEditor(filename);
153 // if (desc == null) {
154 // desc = registry.getDefaultEditor();
156 return new ExternalEditorInput(storage);
158 return new FileEditorInput(eclipseFile);