From: scorphus Date: Thu, 22 Nov 2007 00:26:18 +0000 (+0000) Subject: Making the view work with all instances of the PHPEditor. Also making a cleanup of... X-Git-Url: http://git.phpeclipse.com?hp=5f250da5fa75b304bb223cfb92c0f05e4c03fd0b Making the view work with all instances of the PHPEditor. Also making a cleanup of the code and adding some docblocks. --- 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 index 84b5f58..09233d3 100644 --- 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 @@ -1,6 +1,5 @@ package net.sourceforge.phpeclipse.phpmanual.views; - import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; @@ -21,31 +20,17 @@ 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.ISelectionListener; +import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.part.ViewPart; import org.htmlparser.Node; import org.htmlparser.Parser; @@ -55,28 +40,40 @@ 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. + * This ViewPart is the implementation of the idea of having the + * PHP Manual easily accessible while coding. It shows the + * under-cursor function's reference inside a browser. + *

+ * The view listens to selection changes both in the (1)workbench, to + * know when the user changes between the instances of the PHPEditor + * or when a new instance is created; and in the (2)PHPEditor, to know + * when the user changes the cursor position. This explains the need + * to implement both ISelectionListener and ISelectionListenerWithAST. *

- * 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. + * Up to now, the ViewPart show reference pages from HTML stored in the + * doc.zip file from the net.sourceforge.phpeclipse.phphelp plugin. It + * also depends on net.sourceforge.phpeclipse.phpmanual.htmlparser to + * parse these HTML files. *

+ * @author scorphus */ +public class PHPManualView extends ViewPart implements ISelectionListener, ISelectionListenerWithAST { -public class PHPManualView extends ViewPart implements ISelectionListenerWithAST { + /** + * The ViewPart's browser + */ private Browser browser; - private Action action1; - private Action action2; - private PHPEditor phpEditor; + + /** + * A reference to store last active editor to know when we've + * got a new instance of the PHPEditor + */ + private PHPEditor lastEditor; + + /** + * The path to the doc.zip file containing the PHP Manual + * in HTML format + */ private final Path docPath = new Path("doc.zip"); /** @@ -86,30 +83,41 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST } /** - * Initializes the view + * This method initializes the ViewPart. It instantiates components + * and add listeners + * + * @param parent The parent control */ 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); + if ((lastEditor = getJavaEditor()) != null) { + SelectionListenerWithASTManager.getDefault().addListener(lastEditor, this); } + getSite().getWorkbenchWindow().getSelectionService() + .addSelectionListener(PHPeclipsePlugin.EDITOR_ID, this); } - /* (non-Javadoc) - * @see net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST#selectionChanged() + /** + * Cleanup to remove the selection listener + */ + public void dispose() { + getSite().getWorkbenchWindow().getSelectionService() + .removeSelectionListener(PHPeclipsePlugin.EDITOR_ID, this); + } + + /** + * Passing the focus request to the viewer's control. + */ + public void setFocus() { + browser.setFocus(); + } + + /** + * Treats selection changes from the PHPEditor */ public void selectionChanged(IEditorPart part, ITextSelection selection) { - if (getJavaEditor() != null) { - phpEditor = getJavaEditor(); - } - IDocument document = phpEditor.getViewer().getDocument(); + IDocument document = ((PHPEditor)part).getViewer().getDocument(); int offset = selection.getOffset(); IRegion iRegion = JavaWordFinder.findWord(document, offset); try { @@ -117,12 +125,22 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST iRegion.getLength()); showReference(wordStr); } catch (Exception e) { - // TODO Auto-generated catch block e.printStackTrace(); } } /** + * Treats selection changes from the workbench. When part is new + * instance of PHPEditor it gets a listener attached + */ + public void selectionChanged(IWorkbenchPart part, ISelection selection) { + if (!((PHPEditor)part).equals(lastEditor)) { + SelectionListenerWithASTManager.getDefault().addListener((PHPEditor)part, this); + lastEditor = (PHPEditor)part; + } + } + + /** * Updates the browser with the reference page for a given function * * @param funcName Function name @@ -141,55 +159,6 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST }).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); - } - /** * Filters the function's reference page extracting only parts of it * @@ -221,7 +190,6 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST return output.toString().replace("—", "-"); //.replace("

Description

", " "); } catch (ParserException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return ""; @@ -231,8 +199,7 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST * Reads the template that defines the style of the reference page * shown inside the view's browser * - * @param funcName Function name - * @return HTML source of reference page + * @return HTML source of the template */ public String getRefPageTemplate() { Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID); @@ -297,13 +264,6 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST } /** - * 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. * @@ -317,7 +277,6 @@ public class PHPManualView extends ViewPart implements ISelectionListenerWithAST else return null; } catch (Exception e) { - e.printStackTrace(); return null; } }