7fb4da24538c71e722d572802a267052df8b144d
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / PHPSourceLocator.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.util.Iterator;
4 import java.util.Map;
5
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;
10
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;
27
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;
34
35   public PHPSourceLocator() {
36
37   }
38
39   public String getAbsoluteWorkingDirectory() {
40     return absoluteWorkingDirectory;
41   }
42   /**
43    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
44    */
45   public String getMemento() throws CoreException {
46     return null;
47   }
48
49   /**
50    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
51    */
52   public void initializeFromMemento(String memento) throws CoreException {
53   }
54
55   /**
56    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
57    */
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, "");
63                 
64                 if (Platform.getOS().equals(Platform.OS_WIN32))
65                         this.remoteSourcePath= new Path((configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "")).toLowerCase());
66                 else
67                         this.remoteSourcePath= new Path(configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
68                 
69 //              system.os.name
70
71   }
72
73   /**
74    * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
75    */
76   public Object getSourceElement(IStackFrame stackFrame) {
77   
78                 String fileName=((PHPStackFrame) stackFrame).getFileName();
79                 String file="";
80                     
81     if (remoteDebug)
82     {
83                         IPath path = new Path(fileName);
84                         if (remoteSourcePath.isPrefixOf(path))
85                         {
86                                 IPath projectPath;
87                                 path=path.removeFirstSegments(remoteSourcePath.matchingFirstSegments(path));
88                                 file=path.toString();
89                                 projectPath=(PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation());
90                                 return (projectPath.append(path)).toOSString();
91                         }
92    
93                         if (pathMap == null) {
94                                 return fileName;
95                         }
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();
104                                 }
105                         }
106                         return fileName;
107                 } else {                
108                         return fileName;
109     }
110   }
111
112   /**
113    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
114    */
115   public String getEditorId(IEditorInput input, Object element) {
116     return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
117   }
118
119   /**
120    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
121    */
122   public IEditorInput getEditorInput(Object element) {
123
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);
129     
130     
131
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 + "\".");
140 //        return null;
141 //      }
142 //    } else 
143     if (eclipseFile == null || !eclipseFile.exists()) {
144       //                Otherwise open the stream directly
145       if (page == null) {
146         PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
147         return null;
148       }
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();
155       //      }
156       return new ExternalEditorInput(storage);
157     }
158     return new FileEditorInput(eclipseFile);
159
160   }
161
162 }