Better event handling for browser preview refreshs
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / BrowserUtil.java
1 package net.sourceforge.phpeclipse.wiki.editor;
2
3 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
4 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
5 import net.sourceforge.phpeclipse.wiki.preferences.Util;
6
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.ui.IEditorInput;
9 import org.eclipse.ui.IFileEditorInput;
10 import org.eclipse.ui.IViewPart;
11 import org.eclipse.ui.IWorkbenchPage;
12 import org.eclipse.ui.texteditor.ITextEditor;
13
14 /**
15  * Set browser preview URL and refresh URL Utilities
16  */
17 public class BrowserUtil {
18
19   public static void setBrowserPreview(ITextEditor editor) {
20     IWorkbenchPage page = WikiEditorPlugin.getDefault().getActivePage();
21     try {
22       IViewPart part = page.findView(BrowserView.ID_BROWSER);
23       if (part == null) {
24         part = page.showView(BrowserView.ID_BROWSER);
25       } else {
26         //                if (bringToTopPreview) {
27         //                  page.bringToTop(part);
28         //                }
29       }
30       IEditorInput editorInput = null;
31       if (editor != null) {
32         editorInput = editor.getEditorInput();
33       }
34       if (editorInput instanceof IFileEditorInput) {
35         IFile file = ((IFileEditorInput) editorInput).getFile();
36         String srcBasePath = Util.getWikiTextsPath(file);
37         String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
38         String htmlName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
39
40         if (htmlName != null) {
41           java.io.File htmlFile = new java.io.File(htmlName);
42           if (htmlFile.exists()) {
43             ((BrowserView) part).setUrl(htmlName);
44           }
45         }
46       }
47     } catch (Exception e) {
48     }
49   }
50
51   /**
52    *  
53    */
54   public static void refreshBrowserPreview(WikiEditor editor) {
55
56     IWorkbenchPage page = WikiEditorPlugin.getDefault().getActivePage();
57     try {
58       IViewPart part = page.findView(BrowserView.ID_BROWSER);
59       if (part == null) {
60         part = page.showView(BrowserView.ID_BROWSER);
61       } else {
62         IEditorInput editorInput = null;
63         editorInput = editor.getEditorInput();
64         if (editorInput instanceof IFileEditorInput) {
65           IFile file = ((IFileEditorInput) editorInput).getFile();
66           CreatePageAction.createPage(file);
67
68           String srcBasePath = Util.getWikiTextsPath(file);
69           String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
70           String htmlName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
71           if (htmlName != null) {
72             java.io.File htmlFile = new java.io.File(htmlName);
73             if (htmlFile.exists()) {
74               ((BrowserView) part).refresh(htmlName);
75             }
76           }
77         }
78       }
79
80     } catch (Exception e) {
81     }
82   }
83
84 }