1 package net.sourceforge.phpdt.internal.debug.ui;
3 import java.util.Iterator;
6 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
7 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
10 import net.sourceforge.phpeclipse.builder.FileStorage;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.debug.core.model.IPersistableSourceLocator;
20 import org.eclipse.debug.core.model.IStackFrame;
21 import org.eclipse.debug.ui.ISourcePresentation;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.part.FileEditorInput;
29 public class PHPSourceLocator implements IPersistableSourceLocator, ISourcePresentation {
30 private String absoluteWorkingDirectory;
31 private Map pathMap = null;
32 private boolean remoteDebug;
33 private IPath remoteSourcePath;
34 private String projectName;
36 public PHPSourceLocator() {
40 public String getAbsoluteWorkingDirectory() {
41 return absoluteWorkingDirectory;
44 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
46 public String getMemento() throws CoreException {
51 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
53 public void initializeFromMemento(String memento) throws CoreException {
57 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
59 public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
60 this.absoluteWorkingDirectory = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
61 this.remoteDebug=configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG,false);
62 this.pathMap = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, (Map)null);
63 this.projectName =configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
65 if (Platform.getOS().equals(Platform.OS_WIN32))
66 this.remoteSourcePath= new Path((configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "")).toLowerCase());
68 this.remoteSourcePath= new Path(configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
75 * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
77 public Object getSourceElement(IStackFrame stackFrame) {
79 String fileName=((PHPStackFrame) stackFrame).getFileName();
84 IPath path = new Path(fileName);
85 if (remoteSourcePath.isPrefixOf(path))
88 path=path.removeFirstSegments(remoteSourcePath.matchingFirstSegments(path));
90 projectPath=(PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation());
91 return (projectPath.append(path)).toOSString();
94 if (pathMap == null) {
97 Iterator iterator = pathMap.keySet().iterator();
98 while (iterator.hasNext()) {
99 String local = (String) iterator.next();
100 IPath remotePath = new Path((String) pathMap.get(local));
101 if (remotePath.isPrefixOf(path)) {
102 path=path.removeFirstSegments(remotePath.matchingFirstSegments(path));
103 IPath localPath= new Path(local);
104 return localPath.append(path).toOSString();
114 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
116 public String getEditorId(IEditorInput input, Object element) {
117 return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
121 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
123 public IEditorInput getEditorInput(Object element) {
125 String filename = (String) element;
126 IWorkbench workbench = PlatformUI.getWorkbench();
127 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
128 IWorkbenchPage page = window.getActivePage();
129 IPath path = new Path(filename);
133 // If the file exists in the workspace, open it
134 IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(path);
135 // IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
136 // if (eclipseFile == null) {
137 // filename = this.getAbsoluteWorkingDirectory() + "/" + filename;
138 // eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
139 // if (eclipseFile == null) {
140 // PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
144 if (eclipseFile == null || !eclipseFile.exists()) {
145 // Otherwise open the stream directly
147 PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
150 FileStorage storage = new FileStorage(path);
151 storage.setReadOnly();
152 // IEditorRegistry registry = workbench.getEditorRegistry();
153 // IEditorDescriptor desc = registry.getDefaultEditor(filename);
154 // if (desc == null) {
155 // desc = registry.getDefaultEditor();
157 return new ExternalEditorInput(storage);
159 return new FileEditorInput(eclipseFile);