1 package net.sourceforge.phpeclipse.phpmanual.views;
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;
10 import java.util.ArrayList;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipFile;
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;
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;
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.
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.
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");
85 public PHPManualView() {
89 * This is a callback that will allow us
90 * to create the viewer and initialize it.
92 public void createPartControl(Composite parent) {
93 browser = new Browser(parent, SWT.NONE);
95 phpEditor = getJavaEditor();
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);
107 * @see net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST#selectionChanged()
109 public void selectionChanged(IEditorPart part, ITextSelection selection) {
110 if (getJavaEditor() != null) {
111 phpEditor = getJavaEditor();
113 IDocument document = phpEditor.getViewer().getDocument();
114 int offset = selection.getOffset();
115 IRegion iRegion = JavaWordFinder.findWord(document, offset);
117 final String wordStr = document.get(iRegion.getOffset(),
118 iRegion.getLength());
119 showReference(wordStr);
120 } catch (Exception e) {
121 // TODO Auto-generated catch block
126 private void showReference(final String occurrence) {
127 System.out.println("Show reference for " + occurrence);
128 new Thread(new Runnable() {
130 Display.getDefault().asyncExec(new Runnable() {
132 String html = getHtmlSource(occurrence);
133 browser.setText(html);
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);
148 // Menu menu = menuMgr.createContextMenu(viewer.getControl());
149 // viewer.getControl().setMenu(menu);
150 // getSite().registerContextMenu(menuMgr, viewer);
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));
160 private void makeActions() {
161 action1 = new Action() {
163 showMessage("Action 1 executed");
166 action1.setText("Action 1");
167 action1.setToolTipText("Action 1 tooltip");
168 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
169 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
171 action2 = new Action() {
173 showMessage("Action 2 executed");
176 action2.setText("Action 2");
177 action2.setToolTipText("Action 2 tooltip");
178 action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
179 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
182 private void showMessage(String message) {
183 // MessageDialog.openInformation(
184 // viewer.getControl().getShell(),
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
195 private String filterHtmlSource(String source) {
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());
217 return output.toString().replace("—", "-");
218 //.replace("<h3 class=\"title\">Description</h3>", " ");
219 } catch (ParserException e) {
220 // TODO Auto-generated catch block
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
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;
238 URL resolve = Platform.resolve(fileURL);
239 input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
241 while ((line = input.readLine()) != null){
242 contents.append(line);
245 catch (FileNotFoundException e) {
247 } catch (IOException e) {
256 catch (IOException ex) {
257 ex.printStackTrace();
260 return contents.toString();
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
269 public String getHtmlSource(String funcName) {
270 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
271 URL fileURL = Platform.find(bundle, docPath);
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>";
284 String reference = filterHtmlSource(new String(b));
285 String refPageTpl = getRefPageTemplate();
286 refPageTpl = refPageTpl.replace("{title}", funcName);
287 refPageTpl = refPageTpl.replace("{reference}", reference);
290 return "<html></html>";
294 * Passing the focus request to the viewer's control.
296 public void setFocus() {
297 // viewer.getControl().setFocus();
301 * Returns the currently active java editor, or <code>null</code> if it
302 * cannot be determined.
304 * @return the currently active java editor, or <code>null</code>
306 private PHPEditor getJavaEditor() {
308 IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
309 if (part instanceof PHPEditor)
310 return (PHPEditor) part;
313 } catch (Exception e) {