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 * Initializes the view
91 public void createPartControl(Composite parent) {
92 browser = new Browser(parent, SWT.NONE);
94 phpEditor = getJavaEditor();
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);
106 * @see net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST#selectionChanged()
108 public void selectionChanged(IEditorPart part, ITextSelection selection) {
109 if (getJavaEditor() != null) {
110 phpEditor = getJavaEditor();
112 IDocument document = phpEditor.getViewer().getDocument();
113 int offset = selection.getOffset();
114 IRegion iRegion = JavaWordFinder.findWord(document, offset);
116 final String wordStr = document.get(iRegion.getOffset(),
117 iRegion.getLength());
118 showReference(wordStr);
119 } catch (Exception e) {
120 // TODO Auto-generated catch block
126 * Updates the browser with the reference page for a given function
128 * @param funcName Function name
130 private void showReference(final String funcName) {
131 System.out.println("Show reference for " + funcName);
132 new Thread(new Runnable() {
134 Display.getDefault().asyncExec(new Runnable() {
136 String html = getHtmlSource(funcName);
137 browser.setText(html);
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);
152 // Menu menu = menuMgr.createContextMenu(viewer.getControl());
153 // viewer.getControl().setMenu(menu);
154 // getSite().registerContextMenu(menuMgr, viewer);
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));
164 private void makeActions() {
165 action1 = new Action() {
167 showMessage("Action 1 executed");
170 action1.setText("Action 1");
171 action1.setToolTipText("Action 1 tooltip");
172 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
173 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
175 action2 = new Action() {
177 showMessage("Action 2 executed");
180 action2.setText("Action 2");
181 action2.setToolTipText("Action 2 tooltip");
182 action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
183 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
186 private void showMessage(String message) {
187 // MessageDialog.openInformation(
188 // viewer.getControl().getShell(),
194 * Filters the function's reference page extracting only parts of it
196 * @param source HTML source of the reference page
197 * @return HTML source of reference page
199 private String filterHtmlSource(String source) {
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());
221 return output.toString().replace("—", "-");
222 //.replace("<h3 class=\"title\">Description</h3>", " ");
223 } catch (ParserException e) {
224 // TODO Auto-generated catch block
231 * Reads the template that defines the style of the reference page
232 * shown inside the view's browser
234 * @param funcName Function name
235 * @return HTML source of reference page
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;
243 URL resolve = Platform.resolve(fileURL);
244 input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
246 while ((line = input.readLine()) != null){
247 contents.append(line);
250 catch (FileNotFoundException e) {
252 } catch (IOException e) {
261 catch (IOException ex) {
262 ex.printStackTrace();
265 return contents.toString();
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
272 * @param funcName Function name
273 * @return HTML source of reference page
275 public String getHtmlSource(String funcName) {
276 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
277 URL fileURL = Platform.find(bundle, docPath);
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>";
290 String reference = filterHtmlSource(new String(b));
291 String refPageTpl = getRefPageTemplate();
292 refPageTpl = refPageTpl.replace("{title}", funcName);
293 refPageTpl = refPageTpl.replace("{reference}", reference);
296 return "<html></html>";
300 * Passing the focus request to the viewer's control.
302 public void setFocus() {
303 // viewer.getControl().setFocus();
307 * Returns the currently active java editor, or <code>null</code> if it
308 * cannot be determined.
310 * @return the currently active java editor, or <code>null</code>
312 private PHPEditor getJavaEditor() {
314 IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
315 if (part instanceof PHPEditor)
316 return (PHPEditor) part;
319 } catch (Exception e) {