Search in sources :

Example 1 with IFunction

use of org.eclipse.wst.jsdt.core.IFunction in project webtools.sourceediting by eclipse.

the class JSDTHyperlinkDetector method createHyperlink.

private IHyperlink createHyperlink(IJsTranslation jsTranslation, IJavaScriptElement element, IRegion region, IDocument document) {
    IHyperlink link = null;
    if (region != null) {
        // open local variable in the JSP file...
        if (element instanceof ISourceReference) {
            IFile file = null;
            IPath outsidePath = null;
            int jspOffset = 0;
            IStructuredModel sModel = null;
            // try to locate the file in the workspace
            try {
                sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
                if (sModel != null) {
                    // URIResolver resolver = sModel.getResolver();
                    // if (resolver != null) {
                    // String uriString = resolver.getFileBaseLocation();
                    String uriString = sModel.getBaseLocation();
                    file = getFile(uriString);
                // }
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            // get Java range, translate coordinate to JSP
            try {
                ISourceRange range = null;
                if (jsTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        IJavaScriptElement unit = ((ILocalVariable) element).getParent();
                        IJavaScriptUnit myUnit = jsTranslation.getCompilationUnit();
                        while (!(unit instanceof IJavaScriptUnit || unit instanceof IClassFile || unit == null)) {
                            unit = ((JavaElement) unit).getParent();
                        }
                        if (unit instanceof IJavaScriptUnit) {
                            IJavaScriptUnit cu = (IJavaScriptUnit) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        } else if (unit instanceof IClassFile) {
                            IClassFile cu = (IClassFile) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        }
                    } else // linking to fields of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.FIELD) {
                        Object cu = ((IField) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    } else // linking to methods of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.METHOD) {
                        Object cu = ((IFunction) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    }
                }
                if (range != null && file != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
                    }
                } else if (range != null && outsidePath != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new ExternalFileHyperlink(region, outsidePath.toFile());
                    }
                }
            } catch (JavaScriptModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null) {
            link = new JSDTHyperlink(region, element);
        }
    }
    return link;
}
Also used : IFile(org.eclipse.core.resources.IFile) IClassFile(org.eclipse.wst.jsdt.core.IClassFile) IPath(org.eclipse.core.runtime.IPath) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IFunction(org.eclipse.wst.jsdt.core.IFunction) ILocalVariable(org.eclipse.wst.jsdt.core.ILocalVariable) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) ISourceReference(org.eclipse.wst.jsdt.core.ISourceReference) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange)

Example 2 with IFunction

use of org.eclipse.wst.jsdt.core.IFunction in project webtools.sourceediting by eclipse.

the class JSPRenameElementActionDelegate method run.

public void run(IAction action) {
    IJavaScriptElement element = getSelectedElement();
    if (element != null) {
        RenameSupport renameSupport = null;
        try {
            switch(element.getElementType()) {
                case IJavaScriptElement.TYPE:
                    renameSupport = RenameSupport.create((IType) element, element.getElementName(), RenameSupport.UPDATE_REFERENCES);
                    break;
                case IJavaScriptElement.METHOD:
                    renameSupport = RenameSupport.create((IFunction) element, element.getElementName(), RenameSupport.UPDATE_REFERENCES);
                    break;
                case IJavaScriptElement.PACKAGE_FRAGMENT:
                    renameSupport = RenameSupport.create((IPackageFragment) element, element.getElementName(), RenameSupport.UPDATE_REFERENCES);
                    break;
            }
            if (renameSupport != null) {
                renameSupport.openDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                PlatformStatusLineUtil.clearStatusLine();
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    } else {
        PlatformStatusLineUtil.displayErrorMessage(JsUIMessages.JSPRenameElementAction_0);
        PlatformStatusLineUtil.addOneTimeClearListener();
    }
}
Also used : IPackageFragment(org.eclipse.wst.jsdt.core.IPackageFragment) CoreException(org.eclipse.core.runtime.CoreException) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) IFunction(org.eclipse.wst.jsdt.core.IFunction) RenameSupport(org.eclipse.wst.jsdt.ui.refactoring.RenameSupport) IType(org.eclipse.wst.jsdt.core.IType)

Aggregations

IFunction (org.eclipse.wst.jsdt.core.IFunction)2 IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)2 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 IClassFile (org.eclipse.wst.jsdt.core.IClassFile)1 IJavaScriptUnit (org.eclipse.wst.jsdt.core.IJavaScriptUnit)1 ILocalVariable (org.eclipse.wst.jsdt.core.ILocalVariable)1 IPackageFragment (org.eclipse.wst.jsdt.core.IPackageFragment)1 ISourceRange (org.eclipse.wst.jsdt.core.ISourceRange)1 ISourceReference (org.eclipse.wst.jsdt.core.ISourceReference)1 IType (org.eclipse.wst.jsdt.core.IType)1 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)1 RenameSupport (org.eclipse.wst.jsdt.ui.refactoring.RenameSupport)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1