fe2caced9d31631f416ee0bbe7e9c9dfeaa148fb
[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          * This is a callback that will allow us
90          * to create the viewer and initialize it.
91          */
92         public void createPartControl(Composite parent) {
93                 browser = new Browser(parent, SWT.NONE);
94                 parent.pack();
95                 phpEditor = getJavaEditor();
96                 makeActions();
97                 hookContextMenu();
98                 SelectionListenerWithASTManager.getDefault().addListener(phpEditor, this);
99                 if (phpEditor.getSelectionProvider() != null) {
100                         ISelection its = phpEditor.getSelectionProvider().getSelection();
101                         SelectionListenerWithASTManager.getDefault().forceSelectionChange(
102                                         phpEditor, (ITextSelection) its);
103                 }
104         }
105
106         /* (non-Javadoc)
107          * @see net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST#selectionChanged()
108          */
109         public void selectionChanged(IEditorPart part, ITextSelection selection) {
110                 if (getJavaEditor() != null) {
111                         phpEditor = getJavaEditor();
112                 }
113                 IDocument document = phpEditor.getViewer().getDocument();
114                 int offset = selection.getOffset();
115                 IRegion iRegion = JavaWordFinder.findWord(document, offset);
116                 try {
117                         final String wordStr = document.get(iRegion.getOffset(),
118                                         iRegion.getLength());
119                         showReference(wordStr);
120                 } catch (Exception e) {
121                         // TODO Auto-generated catch block
122                         e.printStackTrace();
123                 }
124         }
125
126         private void showReference(final String occurrence) {
127                 System.out.println("Show reference for " + occurrence);
128                 new Thread(new Runnable() {
129                         public void run() {
130                                 Display.getDefault().asyncExec(new Runnable() {
131                                         public void run() {
132                                                 String html = getHtmlSource(occurrence);
133                                                 browser.setText(html);
134                                         }
135                                 });
136                         }
137                 }).start();
138         }
139
140         private void hookContextMenu() {
141                 MenuManager menuMgr = new MenuManager("#PopupMenu");
142                 menuMgr.setRemoveAllWhenShown(true);
143                 menuMgr.addMenuListener(new IMenuListener() {
144                         public void menuAboutToShow(IMenuManager manager) {
145                                 PHPManualView.this.fillContextMenu(manager);
146                         }
147                 });
148 //              Menu menu = menuMgr.createContextMenu(viewer.getControl());
149 //              viewer.getControl().setMenu(menu);
150 //              getSite().registerContextMenu(menuMgr, viewer);
151         }
152
153         private void fillContextMenu(IMenuManager manager) {
154                 manager.add(action1);
155                 manager.add(action2);
156                 // Other plug-ins can contribute there actions here
157                 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
158         }
159
160         private void makeActions() {
161                 action1 = new Action() {
162                         public void run() {
163                                 showMessage("Action 1 executed");
164                         }
165                 };
166                 action1.setText("Action 1");
167                 action1.setToolTipText("Action 1 tooltip");
168                 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
169                         getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
170                 
171                 action2 = new Action() {
172                         public void run() {
173                                 showMessage("Action 2 executed");
174                         }
175                 };
176                 action2.setText("Action 2");
177                 action2.setToolTipText("Action 2 tooltip");
178                 action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
179                                 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
180         }
181
182         private void showMessage(String message) {
183 //              MessageDialog.openInformation(
184 //                      viewer.getControl().getShell(),
185 //                      "%phpManualView",
186 //                      message);
187         }
188
189         /**
190          * Looks for the function's reference page inside the doc.zip file and
191          * returns a filtered HTML source of it
192          * @param funcName Function name
193          * @return HTML source of reference page
194          */
195         private String filterHtmlSource(String source) {
196                 try {
197                         Parser parser = new Parser(source);
198                         String [] tagsToBeFound = {"DIV"};
199                         ArrayList<String> classList = new ArrayList<String>(6);
200                         classList.add("refnamediv");
201                         classList.add("refsect1 description");
202                         classList.add("refsect1 parameters");
203                         classList.add("refsect1 returnvalues");
204                         classList.add("refsect1 examples");
205                         classList.add("refsect1 seealso");
206                         classList.add("refsect1 u");
207                         TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
208                         parser.visitAllNodesWith(visitor);
209                         Node [] allPTags = visitor.getTags(0);
210                         StringBuilder output = new StringBuilder();
211                         for (int i = 0; i < allPTags.length; i++) {
212                                 String tagClass = ((Div)allPTags[i]).getAttribute("class");
213                                 if (classList.contains(tagClass)) {
214                                         output.append(allPTags[i].toHtml());
215                                 }
216                         }
217                         return output.toString().replace("—", "-");
218                         //.replace("<h3 class=\"title\">Description</h3>", " ");
219                 } catch (ParserException e) {
220                         // TODO Auto-generated catch block
221                         e.printStackTrace();
222                 }
223                 return "";
224         }
225
226         /**
227          * Looks for the function's reference page inside the doc.zip file and
228          * returns a filtered HTML source of it
229          * @param funcName Function name
230          * @return HTML source of reference page
231          */
232         public String getRefPageTemplate() {
233                 Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID);
234                 URL fileURL = Platform.find(bundle, new Path("templates"));
235                 StringBuffer contents = new StringBuffer();
236                 BufferedReader input = null;
237                 try {
238                         URL resolve = Platform.resolve(fileURL);
239                         input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
240                         String line = null;
241                         while ((line = input.readLine()) != null){
242                                 contents.append(line);
243                         }
244                 }
245                 catch (FileNotFoundException e) {
246                         e.printStackTrace();
247                 } catch (IOException e) {
248                         e.printStackTrace();
249                 }
250                 finally {
251                         try {
252                                 if (input!= null) {
253                                         input.close();
254                                 }
255                         }
256                         catch (IOException ex) {
257                                 ex.printStackTrace();
258                         }
259                 }
260                 return contents.toString();
261         }
262
263         /**
264          * Looks for the function's reference page inside the doc.zip file and
265          * returns a filtered HTML source of it
266          * @param funcName Function name
267          * @return HTML source of reference page
268          */
269         public String getHtmlSource(String funcName) {
270                 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
271                 URL fileURL = Platform.find(bundle, docPath);
272                 byte[] b = null;
273                 try {
274                         URL resolve = Platform.resolve(fileURL);
275                         ZipFile docFile = new ZipFile(resolve.getPath());
276                         ZipEntry entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
277                         InputStream ref = docFile.getInputStream(entry);
278                         b = new byte[(int)entry.getSize()];
279                         ref.read(b, 0, (int)entry.getSize());
280                 } catch (IOException e) {
281                         return "<html></html>";
282                 }
283                 if (b != null) {
284                         String reference = filterHtmlSource(new String(b));
285                         String refPageTpl = getRefPageTemplate();
286                         refPageTpl = refPageTpl.replace("{title}", funcName);
287                         refPageTpl = refPageTpl.replace("{reference}", reference);
288                         return refPageTpl;
289                 }
290                 return "<html></html>";
291         }
292
293         /**
294          * Passing the focus request to the viewer's control.
295          */
296         public void setFocus() {
297 //              viewer.getControl().setFocus();
298         }
299
300         /**
301          * Returns the currently active java editor, or <code>null</code> if it
302          * cannot be determined.
303          * 
304          * @return the currently active java editor, or <code>null</code>
305          */
306         private PHPEditor getJavaEditor() {
307                 try {
308                         IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
309                         if (part instanceof PHPEditor)
310                                 return (PHPEditor) part;
311                         else
312                                 return null;
313                 } catch (Exception e) {
314                         e.printStackTrace();
315                         return null;
316                 }
317         }
318
319 }