1 package net.sourceforge.phpdt.internal.debug.ui;
4 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.IEditorDescriptor;
23 import org.eclipse.ui.IEditorInput;
24 //import org.eclipse.ui.IEditorRegistry;
25 import org.eclipse.ui.IWorkbench;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.part.FileEditorInput;
31 public class PHPSourceLocator implements IPersistableSourceLocator, ISourcePresentation {
32 private String absoluteWorkingDirectory;
33 private Map pathMap = null;
34 private boolean remoteDebug;
35 private IPath remoteSourcePath;
36 private String projectName;
38 public PHPSourceLocator() {
42 public String getAbsoluteWorkingDirectory() {
43 return absoluteWorkingDirectory;
46 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
48 public String getMemento() throws CoreException {
53 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
55 public void initializeFromMemento(String memento) throws CoreException {
59 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
61 public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
62 this.absoluteWorkingDirectory = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
63 this.remoteDebug=configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG,false);
64 this.pathMap = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, (Map)null);
65 this.projectName =configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
67 if (Platform.getOS().equals(Platform.OS_WIN32))
68 this.remoteSourcePath= new Path((configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "")).toLowerCase());
70 this.remoteSourcePath= new Path(configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
77 * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
79 public Object getSourceElement(IStackFrame stackFrame) {
81 String fileName=((PHPStackFrame) stackFrame).getFileName();
86 IPath path = new Path(fileName);
87 if (remoteSourcePath.isPrefixOf(path))
90 path=path.removeFirstSegments(remoteSourcePath.matchingFirstSegments(path));
92 projectPath=(PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation());
93 return (projectPath.append(path)).toOSString();
96 if (pathMap == null) {
99 Iterator iterator = pathMap.keySet().iterator();
100 while (iterator.hasNext()) {
101 String local = (String) iterator.next();
102 IPath remotePath = new Path((String) pathMap.get(local));
103 if (remotePath.isPrefixOf(path)) {
104 path=path.removeFirstSegments(remotePath.matchingFirstSegments(path));
105 IPath localPath= new Path(local);
106 return localPath.append(path).toOSString();
116 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
118 public String getEditorId(IEditorInput input, Object element) {
119 return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
123 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
125 public IEditorInput getEditorInput(Object element) {
127 String filename = (String) element;
128 IWorkbench workbench = PlatformUI.getWorkbench();
129 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
130 IWorkbenchPage page = window.getActivePage();
131 IPath path = new Path(filename);
135 // If the file exists in the workspace, open it
136 IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(path);
137 // IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
138 // if (eclipseFile == null) {
139 // filename = this.getAbsoluteWorkingDirectory() + "/" + filename;
140 // eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
141 // if (eclipseFile == null) {
142 // PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
146 if (eclipseFile == null || !eclipseFile.exists()) {
147 // Otherwise open the stream directly
149 PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
152 FileStorage storage = new FileStorage(path);
153 storage.setReadOnly();
154 // IEditorRegistry registry = workbench.getEditorRegistry();
155 // IEditorDescriptor desc = registry.getDefaultEditor(filename);
156 // if (desc == null) {
157 // desc = registry.getDefaultEditor();
159 return new ExternalEditorInput(storage);
161 return new FileEditorInput(eclipseFile);