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