c61f815bafe8f15ba10d229c2b3e15c7d8df9c56
[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
5 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
6 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
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.boot.BootLoader;
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.IEditorDescriptor;
22 import org.eclipse.ui.IEditorInput;
23 //import org.eclipse.ui.IEditorRegistry;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.part.FileEditorInput;
29
30 public class PHPSourceLocator implements IPersistableSourceLocator, ISourcePresentation {
31   private String absoluteWorkingDirectory;
32   private Map fileMap = null;
33         private boolean remoteDebug;
34         private IPath remotePath;
35         private String projectName;
36
37   public PHPSourceLocator() {
38
39   }
40
41   public String getAbsoluteWorkingDirectory() {
42     return absoluteWorkingDirectory;
43   }
44   /**
45    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
46    */
47   public String getMemento() throws CoreException {
48     return null;
49   }
50
51   /**
52    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
53    */
54   public void initializeFromMemento(String memento) throws CoreException {
55   }
56
57   /**
58    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
59    */
60   public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
61     this.absoluteWorkingDirectory = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
62                 this.remoteDebug=configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG,false);
63                 this.fileMap = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, (Map)null);
64                 this.projectName =configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
65                 
66                 if (BootLoader.getOS().equals(BootLoader.OS_WIN32))
67                         this.remotePath= new Path((configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "")).toLowerCase());
68                 else
69                         this.remotePath= new Path(configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
70                 
71 //              system.os.name
72
73   }
74
75   /**
76    * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
77    */
78   public Object getSourceElement(IStackFrame stackFrame) {
79   
80                 String key=((PHPStackFrame) stackFrame).getFileName();
81
82                 
83                 String file="";
84                     
85     if (remoteDebug)
86     {
87                         IPath path = new Path(key);
88                         if (remotePath.isPrefixOf(path))
89                         {
90                                 IPath projectPath;
91                                 path=path.removeFirstSegments(remotePath.matchingFirstSegments(path));
92                                 file=path.toString();
93                                 projectPath=(PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation());
94                                 file=(projectPath.append(path)).toString();                     
95                         }
96 //      file=(String)(fileMap.get(key));
97     } else
98     {
99         file=key;
100     }
101     
102     return file;
103   }
104
105   /**
106    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
107    */
108   public String getEditorId(IEditorInput input, Object element) {
109     return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
110   }
111
112   /**
113    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
114    */
115   public IEditorInput getEditorInput(Object element) {
116
117     String filename = (String) element;
118     IWorkbench workbench = PlatformUI.getWorkbench();
119     IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
120     IWorkbenchPage page = window.getActivePage();
121     IPath path = new Path(filename);
122     
123     
124
125     // If the file exists in the workspace, open it
126     IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(path);
127     //    IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
128 //    if (eclipseFile == null) {
129 //      filename = this.getAbsoluteWorkingDirectory() + "/" + filename;
130 //      eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
131 //      if (eclipseFile == null) {
132 //        PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
133 //        return null;
134 //      }
135 //    } else 
136     if (eclipseFile == null || !eclipseFile.exists()) {
137       //                Otherwise open the stream directly
138       if (page == null) {
139         PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
140         return null;
141       }
142       FileStorage storage = new FileStorage(path);
143       storage.setReadOnly();
144       //      IEditorRegistry registry = workbench.getEditorRegistry();
145       //      IEditorDescriptor desc = registry.getDefaultEditor(filename);
146       //      if (desc == null) {
147       //        desc = registry.getDefaultEditor();
148       //      }
149       return new ExternalEditorInput(storage);
150     }
151     return new FileEditorInput(eclipseFile);
152
153   }
154
155 }