84b5f58b23d3aac518f74c721fd5d9f35148d2fd
[phpeclipse.git] / net.sourceforge.phpeclipse.phpmanual / src / net / sourceforge / phpeclipse / phpmanual / views / PHPManualView.java
1 package net.sourceforge.phpeclipse.phpmanual.views;
2
3
4 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipFile;
13
14 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
15 import net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST;
16 import net.sourceforge.phpdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
17 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
20 import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin;
21
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.action.Separator;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.IRegion;
32 import org.eclipse.jface.text.ITextSelection;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredContentProvider;
35 import org.eclipse.jface.viewers.ITableLabelProvider;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.TableViewer;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.browser.Browser;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.swt.widgets.Menu;
45 import org.eclipse.ui.IEditorPart;
46 import org.eclipse.ui.ISharedImages;
47 import org.eclipse.ui.IWorkbenchActionConstants;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.part.ViewPart;
50 import org.htmlparser.Node;
51 import org.htmlparser.Parser;
52 import org.htmlparser.tags.Div;
53 import org.htmlparser.util.ParserException;
54 import org.htmlparser.visitors.TagFindingVisitor;
55 import org.osgi.framework.Bundle;
56
57 /**
58  * This sample class demonstrates how to plug-in a new
59  * workbench view. The view shows data obtained from the
60  * model. The sample creates a dummy model on the fly,
61  * but a real implementation would connect to the model
62  * available either in this or another plug-in (e.g. the workspace).
63  * The view is connected to the model using a content provider.
64  * <p>
65  * The view uses a label provider to define how model
66  * objects should be presented in the view. Each
67  * view can present the same model objects using
68  * different labels and icons, if needed. Alternatively,
69  * a single label provider can be shared between views
70  * in order to ensure that objects of the same type are
71  * presented in the same way everywhere.
72  * <p>
73  */
74
75 public class PHPManualView extends ViewPart implements ISelectionListenerWithAST {
76         private Browser browser;
77         private Action action1;
78         private Action action2;
79         private PHPEditor phpEditor;
80         private final Path docPath = new Path("doc.zip"); 
81
82         /**
83          * The constructor.
84          */
85         public PHPManualView() {
86         }
87
88         /**
89          * Initializes the view
90          */
91         public void createPartControl(Composite parent) {
92                 browser = new Browser(parent, SWT.NONE);
93                 parent.pack();
94                 phpEditor = getJavaEditor();
95                 makeActions();
96                 hookContextMenu();
97                 SelectionListenerWithASTManager.getDefault().addListener(phpEditor, this);
98                 if (phpEditor.getSelectionProvider() != null) {
99                         ISelection its = phpEditor.getSelectionProvider().getSelection();
100                         SelectionListenerWithASTManager.getDefault().forceSelectionChange(
101                                         phpEditor, (ITextSelection) its);
102                 }
103         }
104
105         /* (non-Javadoc)
106          * @see net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST#selectionChanged()
107          */
108         public void selectionChanged(IEditorPart part, ITextSelection selection) {
109                 if (getJavaEditor() != null) {
110                         phpEditor = getJavaEditor();
111                 }
112                 IDocument document = phpEditor.getViewer().getDocument();
113                 int offset = selection.getOffset();
114                 IRegion iRegion = JavaWordFinder.findWord(document, offset);
115                 try {
116                         final String wordStr = document.get(iRegion.getOffset(),
117                                         iRegion.getLength());
118                         showReference(wordStr);
119                 } catch (Exception e) {
120                         // TODO Auto-generated catch block
121                         e.printStackTrace();
122                 }
123         }
124
125         /**
126          * Updates the browser with the reference page for a given function
127          * 
128          * @param funcName Function name
129          */
130         private void showReference(final String funcName) {
131                 System.out.println("Show reference for " + funcName);
132                 new Thread(new Runnable() {
133                         public void run() {
134                                 Display.getDefault().asyncExec(new Runnable() {
135                                         public void run() {
136                                                 String html = getHtmlSource(funcName);
137                                                 browser.setText(html);
138                                         }
139                                 });
140                         }
141                 }).start();
142         }
143
144         private void hookContextMenu() {
145                 MenuManager menuMgr = new MenuManager("#PopupMenu");
146                 menuMgr.setRemoveAllWhenShown(true);
147                 menuMgr.addMenuListener(new IMenuListener() {
148                         public void menuAboutToShow(IMenuManager manager) {
149                                 PHPManualView.this.fillContextMenu(manager);
150                         }
151                 });
152 //              Menu menu = menuMgr.createContextMenu(viewer.getControl());
153 //              viewer.getControl().setMenu(menu);
154 //              getSite().registerContextMenu(menuMgr, viewer);
155         }
156
157         private void fillContextMenu(IMenuManager manager) {
158                 manager.add(action1);
159                 manager.add(action2);
160                 // Other plug-ins can contribute there actions here
161                 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
162         }
163
164         private void makeActions() {
165                 action1 = new Action() {
166                         public void run() {
167                                 showMessage("Action 1 executed");
168                         }
169                 };
170                 action1.setText("Action 1");
171                 action1.setToolTipText("Action 1 tooltip");
172                 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
173                         getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
174                 
175                 action2 = new Action() {
176                         public void run() {
177                                 showMessage("Action 2 executed");
178                         }
179                 };
180                 action2.setText("Action 2");
181                 action2.setToolTipText("Action 2 tooltip");
182                 action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
183                                 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
184         }
185
186         private void showMessage(String message) {
187 //              MessageDialog.openInformation(
188 //                      viewer.getControl().getShell(),
189 //                      "%phpManualView",
190 //                      message);
191         }
192
193         /**
194          * Filters the function's reference page extracting only parts of it
195          * 
196          * @param source HTML source of the reference page
197          * @return HTML source of reference page
198          */
199         private String filterHtmlSource(String source) {
200                 try {
201                         Parser parser = new Parser(source);
202                         String [] tagsToBeFound = {"DIV"};
203                         ArrayList<String> classList = new ArrayList<String>(6);
204                         classList.add("refnamediv");
205                         classList.add("refsect1 description");
206                         classList.add("refsect1 parameters");
207                         classList.add("refsect1 returnvalues");
208                         classList.add("refsect1 examples");
209                         classList.add("refsect1 seealso");
210                         classList.add("refsect1 u");
211                         TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
212                         parser.visitAllNodesWith(visitor);
213                         Node [] allPTags = visitor.getTags(0);
214                         StringBuilder output = new StringBuilder();
215                         for (int i = 0; i < allPTags.length; i++) {
216                                 String tagClass = ((Div)allPTags[i]).getAttribute("class");
217                                 if (classList.contains(tagClass)) {
218                                         output.append(allPTags[i].toHtml());
219                                 }
220                         }
221                         return output.toString().replace("—", "-");
222                         //.replace("<h3 class=\"title\">Description</h3>", " ");
223                 } catch (ParserException e) {
224                         // TODO Auto-generated catch block
225                         e.printStackTrace();
226                 }
227                 return "";
228         }
229
230         /**
231          * Reads the template that defines the style of the reference page
232          * shown inside the view's browser
233          * 
234          * @param funcName Function name
235          * @return HTML source of reference page
236          */
237         public String getRefPageTemplate() {
238                 Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID);
239                 URL fileURL = Platform.find(bundle, new Path("templates"));
240                 StringBuffer contents = new StringBuffer();
241                 BufferedReader input = null;
242                 try {
243                         URL resolve = Platform.resolve(fileURL);
244                         input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
245                         String line = null;
246                         while ((line = input.readLine()) != null){
247                                 contents.append(line);
248                         }
249                 }
250                 catch (FileNotFoundException e) {
251                         e.printStackTrace();
252                 } catch (IOException e) {
253                         e.printStackTrace();
254                 }
255                 finally {
256                         try {
257                                 if (input!= null) {
258                                         input.close();
259                                 }
260                         }
261                         catch (IOException ex) {
262                                 ex.printStackTrace();
263                         }
264                 }
265                 return contents.toString();
266         }
267
268         /**
269          * Looks for the function's reference page inside the doc.zip file
270          * and returns a filtered HTML source of it embedded in the template
271          *  
272          * @param funcName Function name
273          * @return HTML source of reference page
274          */
275         public String getHtmlSource(String funcName) {
276                 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
277                 URL fileURL = Platform.find(bundle, docPath);
278                 byte[] b = null;
279                 try {
280                         URL resolve = Platform.resolve(fileURL);
281                         ZipFile docFile = new ZipFile(resolve.getPath());
282                         ZipEntry entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
283                         InputStream ref = docFile.getInputStream(entry);
284                         b = new byte[(int)entry.getSize()];
285                         ref.read(b, 0, (int)entry.getSize());
286                 } catch (IOException e) {
287                         return "<html></html>";
288                 }
289                 if (b != null) {
290                         String reference = filterHtmlSource(new String(b));
291                         String refPageTpl = getRefPageTemplate();
292                         refPageTpl = refPageTpl.replace("{title}", funcName);
293                         refPageTpl = refPageTpl.replace("{reference}", reference);
294                         return refPageTpl;
295                 }
296                 return "<html></html>";
297         }
298
299         /**
300          * Passing the focus request to the viewer's control.
301          */
302         public void setFocus() {
303 //              viewer.getControl().setFocus();
304         }
305
306         /**
307          * Returns the currently active java editor, or <code>null</code> if it
308          * cannot be determined.
309          * 
310          * @return the currently active java editor, or <code>null</code>
311          */
312         private PHPEditor getJavaEditor() {
313                 try {
314                         IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
315                         if (part instanceof PHPEditor)
316                                 return (PHPEditor) part;
317                         else
318                                 return null;
319                 } catch (Exception e) {
320                         e.printStackTrace();
321                         return null;
322                 }
323         }
324
325 }