3.x RC1 compatibility
[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 java.util.Map;
4 import java.util.Iterator;
5
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;
11
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;
30
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;
37
38   public PHPSourceLocator() {
39
40   }
41
42   public String getAbsoluteWorkingDirectory() {
43     return absoluteWorkingDirectory;
44   }
45   /**
46    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
47    */
48   public String getMemento() throws CoreException {
49     return null;
50   }
51
52   /**
53    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
54    */
55   public void initializeFromMemento(String memento) throws CoreException {
56   }
57
58   /**
59    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
60    */
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, "");
66                 
67                 if (Platform.getOS().equals(Platform.OS_WIN32))
68                         this.remoteSourcePath= new Path((configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "")).toLowerCase());
69                 else
70                         this.remoteSourcePath= new Path(configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
71                 
72 //              system.os.name
73
74   }
75
76   /**
77    * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
78    */
79   public Object getSourceElement(IStackFrame stackFrame) {
80   
81                 String fileName=((PHPStackFrame) stackFrame).getFileName();
82                 String file="";
83                     
84     if (remoteDebug)
85     {
86                         IPath path = new Path(fileName);
87                         if (remoteSourcePath.isPrefixOf(path))
88                         {
89                                 IPath projectPath;
90                                 path=path.removeFirstSegments(remoteSourcePath.matchingFirstSegments(path));
91                                 file=path.toString();
92                                 projectPath=(PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation());
93                                 return (projectPath.append(path)).toOSString();
94                         }
95    
96                         if (pathMap == null) {
97                                 return fileName;
98                         }
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();
107                                 }
108                         }
109                         return fileName;
110                 } else {                
111                         return fileName;
112     }
113   }
114
115   /**
116    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
117    */
118   public String getEditorId(IEditorInput input, Object element) {
119     return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
120   }
121
122   /**
123    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
124    */
125   public IEditorInput getEditorInput(Object element) {
126
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);
132     
133     
134
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 + "\".");
143 //        return null;
144 //      }
145 //    } else 
146     if (eclipseFile == null || !eclipseFile.exists()) {
147       //                Otherwise open the stream directly
148       if (page == null) {
149         PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
150         return null;
151       }
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();
158       //      }
159       return new ExternalEditorInput(storage);
160     }
161     return new FileEditorInput(eclipseFile);
162
163   }
164
165 }