Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8201802ace1bdc8178c4b0b3ea64d51510522b1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.ui.internal.jspeditor;

import org.eclipse.jdt.core.IType;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory;
import org.eclipse.jst.jsf.context.symbol.IBeanInstanceSymbol;
import org.eclipse.jst.jsf.context.symbol.IBeanMethodSymbol;
import org.eclipse.jst.jsf.context.symbol.IBeanPropertySymbol;
import org.eclipse.jst.jsf.context.symbol.ISymbol;
import org.eclipse.jst.jsf.core.internal.contentassist.el.SymbolInfo;

/**
 * This HyperlinkDetector creates hyperlinks for symbols in JSF EL expressions
 * inside jsp files.
 */
public class ELHyperlinkDetector extends AbstractHyperlinkDetector {

    public final IHyperlink[] detectHyperlinks(final ITextViewer textViewer,
            final IRegion region, final boolean canShowMultipleHyperlinks) {
        final IStructuredDocumentContext context = IStructuredDocumentContextFactory.INSTANCE
        .getContext(textViewer, region.getOffset());
        return detectHyperlinks(context, region);
    }

    /**
     * Broken out for testing.
     * @param context
     * @param region
     * @return the hyperlinks
     */
    protected IHyperlink[] detectHyperlinks(
            final IStructuredDocumentContext context, final IRegion region) {
        final Region elRegion = JSPSourceUtil.findELRegion(context);
        if (elRegion != null) {
            final SymbolInfo symbolInfo = JSPSourceUtil.determineSymbolInfo(
                    context, elRegion, region.getOffset());
            if (symbolInfo != null) {
                IHyperlink link = null;
                final Region linkRegion = new Region(symbolInfo
                        .getRelativeRegion().getOffset()
                        + elRegion.getOffset(), symbolInfo.getRelativeRegion()
                        .getLength());
                final ISymbol symbol = symbolInfo.getSymbol();
                if (symbol instanceof IBeanInstanceSymbol) {
                    link = createBeanInstanceLink(linkRegion,
                            (IBeanInstanceSymbol) symbol);
                } else if (symbol instanceof IBeanPropertySymbol) {
                    link = createBeanPropertyLink(linkRegion,
                            (IBeanPropertySymbol) symbol);
                } else if (symbol instanceof IBeanMethodSymbol) {
                    link = createMethodLink(linkRegion,
                            (IBeanMethodSymbol) symbol);
                }
                if (link != null) {
                    return new IHyperlink[] { link };
                }
            }
        }
        return null;
    }

    private IHyperlink createBeanInstanceLink(final Region region,
            final IBeanInstanceSymbol symbol) {
        if (symbol.isTypeResolved()) {
            final IType type = symbol.getJavaTypeDescriptor().getType();
            return new JavaElementHyperlink(region, type);
        }
        return null;
    }

    private IHyperlink createBeanPropertyLink(final Region region,
            final IBeanPropertySymbol symbol) {
        // defer calculation of access method until user click on link (takes
        // too long otherwise):
        return new BeanSuffixHyperlink(region, symbol);
    }

    private IHyperlink createMethodLink(final Region region,
            final IBeanMethodSymbol symbol) {
        // defer calculation of access method until user click on link (takes
        // too long otherwise):
        return new BeanSuffixHyperlink(region, symbol);
    }

}

Back to the top