From: Edward Mann Date: Sat, 26 Jan 2008 23:45:36 +0000 (+0000) Subject: Added addLocationListener() to the PHPManualView.java file. This will catch the links... X-Git-Url: http://secure.phpeclipse.com Added addLocationListener() to the PHPManualView.java file. This will catch the links whey they are clicked on, and load the correct PHP Manual page. Still need to catch the http:// links and direct them to either the default browser, or to ignore them. This addresses ticket #379 Also may need to do something about when you click on a function and are navigating in the page, if you change focus the Manual will reset to the function that your cursor is active in. --- 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 e14af65..8c8123a 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 @@ -7,7 +7,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.regex.Matcher; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -19,6 +18,7 @@ import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpeclipse.phpeditor.PHPEditor; import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin; +import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.text.IDocument; @@ -26,12 +26,13 @@ import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; +import org.eclipse.swt.browser.Browser; +import org.eclipse.swt.browser.LocationAdapter; +import org.eclipse.swt.browser.LocationEvent; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.browser.Browser; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.INullSelectionListener; -import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.part.ViewPart; import org.htmlparser.Node; @@ -97,6 +98,24 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I */ public void createPartControl(Composite parent) { browser = new Browser(parent, SWT.NONE); + browser.addLocationListener(new LocationAdapter(){ + public void changing(LocationEvent event){ + String loc = event.location.toString(); + if(!loc.equalsIgnoreCase("about:blank")){ + String func = loc.replace("file:///", ""); + func = func.replaceAll("#.+$", ""); + String[] afunc = loc.split("\\."); + if(!afunc[1].equalsIgnoreCase(lastOccurrence)) { + lastOccurrence = afunc[1]; + // TODO find a better way of not showing the location error page. This is a cheap trick + // to keep the page from showing. + // ed_mann + browser.setText(""); + showLinkReference(func); + } + } + } + }); parent.pack(); if ((lastEditor = getJavaEditor()) != null) { SelectionListenerWithASTManager.getDefault().addListener(lastEditor, this); @@ -171,6 +190,24 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I } }).start(); } + + /** + * Updates the browser with the reference page for a given function + * + * @param funcName Function name + */ + private void showLinkReference(final String funcName) { + new Thread(new Runnable() { + public void run() { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + String html = getLinkHtmlSource(funcName); + browser.setText(html); + } + }); + } + }).start(); + } /** * Filters the function's reference page extracting only parts of it @@ -178,6 +215,115 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I * @param source HTML source of the reference page * @return HTML source of reference page */ + private String filterIniHtmlSource(String source) { + try { + Parser parser = new Parser(source); + String [] tagsToBeFound = {"DIV"}; + ArrayList classList = new ArrayList(8); + classList.add("section"); + classList.add("title"); + classList.add("refsect1 parameters"); + classList.add("refsect1 returnvalues"); + classList.add("refsect1 examples"); + classList.add("refsect1 seealso"); + classList.add("refsect1 u"); + TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound); + parser.visitAllNodesWith(visitor); + Node [] allPTags = visitor.getTags(0); + StringBuffer output = new StringBuffer(); + for (int i = 0; i < allPTags.length; i++) { + String tagClass = ((Div)allPTags[i]).getAttribute("class"); + if (classList.contains(tagClass)) { + output.append(allPTags[i].toHtml()); + } + } + return output.toString().replaceAll("—", "-"); + //.replace("

Description

", " "); + } catch (ParserException e) { + e.printStackTrace(); + } + return ""; + } + + /** + * Filters the function's reference page extracting only parts of it + * + * @param source HTML source of the reference page + * @return HTML source of reference page + */ + private String filterLangHtmlSource(String source) { + try { + Parser parser = new Parser(source); + String [] tagsToBeFound = {"DIV"}; + ArrayList classList = new ArrayList(8); + classList.add("sect1"); + classList.add("title"); + classList.add("refsect1 parameters"); + classList.add("refsect1 returnvalues"); + classList.add("refsect1 examples"); + classList.add("refsect1 seealso"); + classList.add("refsect1 u"); + TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound); + parser.visitAllNodesWith(visitor); + Node [] allPTags = visitor.getTags(0); + StringBuffer output = new StringBuffer(); + for (int i = 0; i < allPTags.length; i++) { + String tagClass = ((Div)allPTags[i]).getAttribute("class"); + if (classList.contains(tagClass)) { + output.append(allPTags[i].toHtml()); + } + } + return output.toString().replaceAll("—", "-"); + //.replace("

Description

", " "); + } catch (ParserException e) { + e.printStackTrace(); + } + return ""; + } + + /** + * Filters the function's reference page extracting only parts of it + * + * @param source HTML source of the reference page + * @return HTML source of reference page + */ + private String filterRefHtmlSource(String source) { + try { + Parser parser = new Parser(source); + String [] tagsToBeFound = {"DIV"}; + ArrayList classList = new ArrayList(8); + classList.add("partintro"); + classList.add("section"); + classList.add("title"); + classList.add("refsect1 parameters"); + classList.add("refsect1 returnvalues"); + classList.add("refsect1 examples"); + classList.add("refsect1 seealso"); + classList.add("refsect1 u"); + TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound); + parser.visitAllNodesWith(visitor); + Node [] allPTags = visitor.getTags(0); + StringBuffer output = new StringBuffer(); + for (int i = 0; i < allPTags.length; i++) { + String tagClass = ((Div)allPTags[i]).getAttribute("class"); + if (classList.contains(tagClass)) { + output.append(allPTags[i].toHtml()); + } + } + return output.toString().replaceAll("—", "-"); + //.replace("

Description

", " "); + } catch (ParserException e) { + e.printStackTrace(); + } + return ""; + } + + /** + * Filters the function's reference page extracting only parts of it + * + * @param source HTML source of the reference page + * @return HTML source of reference page + */ private String filterHtmlSource(String source) { try { Parser parser = new Parser(source); @@ -207,7 +353,6 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I } return ""; } - /** * Reads the template that defines the style of the reference page * shown inside the view's browser @@ -216,11 +361,11 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I */ public String getRefPageTemplate() { Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID); - URL fileURL = Platform.find(bundle, new Path("templates")); + URL fileURL = FileLocator.find(bundle, new Path("templates"), null); StringBuffer contents = new StringBuffer(); BufferedReader input = null; try { - URL resolve = Platform.resolve(fileURL); + URL resolve = FileLocator.resolve(fileURL); input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html")); String line = null; while ((line = input.readLine()) != null){ @@ -282,12 +427,16 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I */ public String getHtmlSource(String funcName) { Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID); - URL fileURL = Platform.find(bundle, docPath); + URL fileURL = FileLocator.find(bundle, docPath, null); + ZipEntry entry; byte[] b = null; try { - URL resolve = Platform.resolve(fileURL); + URL resolve = FileLocator.resolve(fileURL); ZipFile docFile = new ZipFile(resolve.getPath()); - ZipEntry entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html"); + entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html"); + if(entry == null){ + entry = docFile.getEntry("doc/ini."+funcName.replace('_', '-')+".html"); + } InputStream ref = docFile.getInputStream(entry); b = new byte[(int)entry.getSize()]; ref.read(b, 0, (int)entry.getSize()); @@ -307,6 +456,52 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I } /** + * Looks for the function's reference page inside the doc.zip file and + * returns a filtered HTML source of it embedded in the template + * + * @param funcName + * Function name + * @return HTML source of reference page + */ + public String getLinkHtmlSource(String funcName) { + Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID); + URL fileURL = FileLocator.find(bundle, docPath, null); + ZipEntry entry; + byte[] b = null; + try { + URL resolve = FileLocator.resolve(fileURL); + ZipFile docFile = new ZipFile(resolve.getPath()); + entry = docFile.getEntry("doc/"+funcName); + InputStream ref = docFile.getInputStream(entry); + b = new byte[(int)entry.getSize()]; + ref.read(b, 0, (int)entry.getSize()); + if (b != null) { + String reference = null; + String aFuncName = funcName.toString(); + if(aFuncName.startsWith("function")){ + reference = filterHtmlSource(new String(b)); + } else if (aFuncName.startsWith("ini")){ + reference = filterIniHtmlSource(new String(b)); + } else if (aFuncName.startsWith("install")){ + reference = filterIniHtmlSource(new String(b)); + } else if (aFuncName.startsWith("language")){ + reference = filterLangHtmlSource(new String(b)); + } else if (aFuncName.startsWith("ref")){ + reference = filterRefHtmlSource(new String(b)); + } + String refPageTpl = getRefPageTemplate(); + refPageTpl = refPageTpl.replaceAll("%title%", funcName); + refPageTpl = replace(refPageTpl, "%reference%", reference); + return refPageTpl; + } + } catch (IOException e) { + return ""; + } catch (Exception e) { + return null; + } + return ""; + } + /** * Returns the currently active java editor, or null if it * cannot be determined. *