1) Fixed issue #764: Ignore strings when looking for single line comments.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / ShowExternalPreviewAction.java
1 package net.sourceforge.phpeclipse.ui.editor;
2
3 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
4 import net.sourceforge.phpeclipse.ui.WebUI;
5 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
6 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.runtime.IPath;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.ui.IEditorInput;
15 import org.eclipse.ui.IFileEditorInput;
16 import org.eclipse.ui.IViewPart;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.PartInitException;
19 import org.eclipse.ui.progress.WorkbenchJob;
20 import org.eclipse.ui.texteditor.ITextEditor;
21 import org.eclipse.ui.texteditor.TextEditorAction;
22
23 /**
24  * ClassDeclaration that defines the action for parsing the current PHP file
25  */
26 public class ShowExternalPreviewAction extends TextEditorAction {
27         public final static int XML_TYPE = 1;
28
29         public final static int HTML_TYPE = 2;
30
31         public final static int SMARTY_TYPE = 3;
32
33         public final static int PHP_TYPE = 4;
34
35         private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
36
37         /**
38          * Constructs and updates the action.
39          */
40         private ShowExternalPreviewAction() {
41                 super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
42                 update();
43         }
44
45         public static ShowExternalPreviewAction getInstance() {
46                 return instance;
47         }
48
49         /**
50          * Code called when the action is fired.
51          */
52         public void run() {
53                 doRun(PHP_TYPE);
54         }
55
56         public void doRun(int type) {
57                 IFile previewFile = getFile();
58                 BrowserUtil.showPreview(previewFile, false, "");
59         }
60
61         public void refresh(int type) {
62                 IFile previewFile = getFile();
63                 if (previewFile == null) {
64                         // should never happen
65                         return;
66                 }
67                 boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
68                                 previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
69                 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
70                                 previewFile,
71                                 IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
72                 boolean stickyBrowserURL = ProjectPrefUtil.getPreviewBooleanValue(
73                                 previewFile,
74                                 IPreferenceConstants.PHP_STICKY_BROWSER_URL_DEFAULT);
75
76                 if (autoPreview) {
77                         IWorkbenchPage page = WebUI.getActivePage();
78                         if (page == null) {
79                                 // startup stage
80                                 return;
81                         }
82                         try {
83                                 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
84                                 // if (part == null) {
85                                 // part = page.showView(BrowserView.ID_BROWSER);
86                                 // }
87                                 IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
88                                                 IWorkbenchPage.VIEW_CREATE);
89                                 if (part != null) {
90                                         if (bringToTopPreview) {
91                                                 // page.bringToTop(part);
92                                                 new WorkbenchJob(getClass().getName()) {
93                                                         public IStatus runInUIThread(
94                                                                         IProgressMonitor monitor) {
95                                                                 IWorkbenchPage page = WebUI.getActivePage();
96                                                                 if (page != null) {
97                                                                         IViewPart part = page
98                                                                                         .findView(BrowserView.ID_BROWSER);
99                                                                         if (part != null) {
100                                                                                 page.bringToTop(part);
101                                                                         }
102                                                                 }
103                                                                 return Status.OK_STATUS;
104                                                         }
105                                                 }.schedule();
106                                         }
107                                         // ((BrowserView) part).refresh();
108                                         if (stickyBrowserURL
109                                                         && ((BrowserView) part).getUrl() != null
110                                                         && ((BrowserView) part).getUrl().length() > 0) {
111                                                 ((BrowserView) part).refresh();
112                                         } else {
113                                                 String localhostURL = getLocalhostURL(null, previewFile);
114                                                 ((BrowserView) part).refresh(localhostURL);
115                                         }
116                                 }
117                         } catch (PartInitException e) {
118                                 // ad hoc
119                                 WebUI.getDefault().getLog().log(
120                                                 new Status(IStatus.ERROR,
121                                                                 "net.sourceforge.phpeclipse.ui", IStatus.OK,
122                                                                 "Failed to show Browser View", e));
123                                 // PHPeclipsePlugin.log(e);
124                         }
125                 }
126         }
127
128         /**
129          * Finds the file that's currently opened in the PHP Text Editor
130          */
131         protected IFile getFile() {
132                 ITextEditor editor = getTextEditor();
133                 IEditorInput editorInput = null;
134                 if (editor != null) {
135                         editorInput = editor.getEditorInput();
136                 }
137                 if (editorInput instanceof IFileEditorInput)
138                         return ((IFileEditorInput) editorInput).getFile();
139                 // if nothing was found, which should never happen
140                 return null;
141         }
142
143         public static String getLocalhostURL(IPreferenceStore store, IFile file) {
144                 if (file != null) {
145                         if (store == null) {
146                                 store = WebUI.getDefault().getPreferenceStore();
147                         }
148                         // IPath path = file.getFullPath();
149                         String localhostURL = file.getFullPath().toString();
150                         String lowerCaseFileName = localhostURL.toLowerCase();
151                         //removed by ed_mann for RSE fixes testing
152                         // String documentRoot =
153                         // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
154                         //IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
155                         //              .getProject());
156                         IPath documentRootPath = file.getProject().getFullPath();
157                         String documentRoot = documentRootPath.toString().toLowerCase();
158                         if (lowerCaseFileName.startsWith(documentRoot)) {
159                                 localhostURL = localhostURL.substring(documentRoot.length());
160                         } else {
161                                 return null;
162                         }
163                         // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
164                         // localhostURL;
165                         String projectPath = ProjectPrefUtil.getMiscProjectsPreferenceValue(file
166                     .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF);
167                         if(projectPath.endsWith("/") && localhostURL.startsWith("/")) {
168                             localhostURL = localhostURL.substring(1);
169                         }
170                         return projectPath + localhostURL;
171                 }
172                 return "http://localhost";
173         }
174 }