X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java new file mode 100644 index 0000000..fe2cace --- /dev/null +++ b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java @@ -0,0 +1,319 @@ +package net.sourceforge.phpeclipse.phpmanual.views; + + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder; +import net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST; +import net.sourceforge.phpdt.internal.ui.viewsupport.SelectionListenerWithASTManager; +import net.sourceforge.phpdt.phphelp.PHPHelpPlugin; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.phpeditor.PHPEditor; +import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin; + +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.IMenuListener; +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.action.Separator; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.browser.Browser; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.ViewPart; +import org.htmlparser.Node; +import org.htmlparser.Parser; +import org.htmlparser.tags.Div; +import org.htmlparser.util.ParserException; +import org.htmlparser.visitors.TagFindingVisitor; +import org.osgi.framework.Bundle; + +/** + * This sample class demonstrates how to plug-in a new + * workbench view. The view shows data obtained from the + * model. The sample creates a dummy model on the fly, + * but a real implementation would connect to the model + * available either in this or another plug-in (e.g. the workspace). + * The view is connected to the model using a content provider. + *
+ * The view uses a label provider to define how model + * objects should be presented in the view. Each + * view can present the same model objects using + * different labels and icons, if needed. Alternatively, + * a single label provider can be shared between views + * in order to ensure that objects of the same type are + * presented in the same way everywhere. + *
+ */
+
+public class PHPManualView extends ViewPart implements ISelectionListenerWithAST {
+ private Browser browser;
+ private Action action1;
+ private Action action2;
+ private PHPEditor phpEditor;
+ private final Path docPath = new Path("doc.zip");
+
+ /**
+ * The constructor.
+ */
+ public PHPManualView() {
+ }
+
+ /**
+ * This is a callback that will allow us
+ * to create the viewer and initialize it.
+ */
+ public void createPartControl(Composite parent) {
+ browser = new Browser(parent, SWT.NONE);
+ parent.pack();
+ phpEditor = getJavaEditor();
+ makeActions();
+ hookContextMenu();
+ SelectionListenerWithASTManager.getDefault().addListener(phpEditor, this);
+ if (phpEditor.getSelectionProvider() != null) {
+ ISelection its = phpEditor.getSelectionProvider().getSelection();
+ SelectionListenerWithASTManager.getDefault().forceSelectionChange(
+ phpEditor, (ITextSelection) its);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST#selectionChanged()
+ */
+ public void selectionChanged(IEditorPart part, ITextSelection selection) {
+ if (getJavaEditor() != null) {
+ phpEditor = getJavaEditor();
+ }
+ IDocument document = phpEditor.getViewer().getDocument();
+ int offset = selection.getOffset();
+ IRegion iRegion = JavaWordFinder.findWord(document, offset);
+ try {
+ final String wordStr = document.get(iRegion.getOffset(),
+ iRegion.getLength());
+ showReference(wordStr);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ private void showReference(final String occurrence) {
+ System.out.println("Show reference for " + occurrence);
+ new Thread(new Runnable() {
+ public void run() {
+ Display.getDefault().asyncExec(new Runnable() {
+ public void run() {
+ String html = getHtmlSource(occurrence);
+ browser.setText(html);
+ }
+ });
+ }
+ }).start();
+ }
+
+ private void hookContextMenu() {
+ MenuManager menuMgr = new MenuManager("#PopupMenu");
+ menuMgr.setRemoveAllWhenShown(true);
+ menuMgr.addMenuListener(new IMenuListener() {
+ public void menuAboutToShow(IMenuManager manager) {
+ PHPManualView.this.fillContextMenu(manager);
+ }
+ });
+// Menu menu = menuMgr.createContextMenu(viewer.getControl());
+// viewer.getControl().setMenu(menu);
+// getSite().registerContextMenu(menuMgr, viewer);
+ }
+
+ private void fillContextMenu(IMenuManager manager) {
+ manager.add(action1);
+ manager.add(action2);
+ // Other plug-ins can contribute there actions here
+ manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
+ }
+
+ private void makeActions() {
+ action1 = new Action() {
+ public void run() {
+ showMessage("Action 1 executed");
+ }
+ };
+ action1.setText("Action 1");
+ action1.setToolTipText("Action 1 tooltip");
+ action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
+ getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
+
+ action2 = new Action() {
+ public void run() {
+ showMessage("Action 2 executed");
+ }
+ };
+ action2.setText("Action 2");
+ action2.setToolTipText("Action 2 tooltip");
+ action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
+ getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
+ }
+
+ private void showMessage(String message) {
+// MessageDialog.openInformation(
+// viewer.getControl().getShell(),
+// "%phpManualView",
+// message);
+ }
+
+ /**
+ * Looks for the function's reference page inside the doc.zip file and
+ * returns a filtered HTML source of it
+ * @param funcName Function name
+ * @return HTML source of reference page
+ */
+ private String filterHtmlSource(String source) {
+ try {
+ Parser parser = new Parser(source);
+ String [] tagsToBeFound = {"DIV"};
+ ArrayListDescription
", " ");
+ } catch (ParserException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ /**
+ * Looks for the function's reference page inside the doc.zip file and
+ * returns a filtered HTML source of it
+ * @param funcName Function name
+ * @return HTML source of reference page
+ */
+ public String getRefPageTemplate() {
+ Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID);
+ URL fileURL = Platform.find(bundle, new Path("templates"));
+ StringBuffer contents = new StringBuffer();
+ BufferedReader input = null;
+ try {
+ URL resolve = Platform.resolve(fileURL);
+ input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
+ String line = null;
+ while ((line = input.readLine()) != null){
+ contents.append(line);
+ }
+ }
+ catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ finally {
+ try {
+ if (input!= null) {
+ input.close();
+ }
+ }
+ catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+ return contents.toString();
+ }
+
+ /**
+ * Looks for the function's reference page inside the doc.zip file and
+ * returns a filtered HTML source of it
+ * @param funcName Function name
+ * @return HTML source of reference page
+ */
+ public String getHtmlSource(String funcName) {
+ Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
+ URL fileURL = Platform.find(bundle, docPath);
+ byte[] b = null;
+ try {
+ URL resolve = Platform.resolve(fileURL);
+ ZipFile docFile = new ZipFile(resolve.getPath());
+ ZipEntry entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
+ InputStream ref = docFile.getInputStream(entry);
+ b = new byte[(int)entry.getSize()];
+ ref.read(b, 0, (int)entry.getSize());
+ } catch (IOException e) {
+ return "";
+ }
+ if (b != null) {
+ String reference = filterHtmlSource(new String(b));
+ String refPageTpl = getRefPageTemplate();
+ refPageTpl = refPageTpl.replace("{title}", funcName);
+ refPageTpl = refPageTpl.replace("{reference}", reference);
+ return refPageTpl;
+ }
+ return "";
+ }
+
+ /**
+ * Passing the focus request to the viewer's control.
+ */
+ public void setFocus() {
+// viewer.getControl().setFocus();
+ }
+
+ /**
+ * Returns the currently active java editor, or null
if it
+ * cannot be determined.
+ *
+ * @return the currently active java editor, or null
+ */
+ private PHPEditor getJavaEditor() {
+ try {
+ IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
+ if (part instanceof PHPEditor)
+ return (PHPEditor) part;
+ else
+ return null;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+}
\ No newline at end of file