Skip to main content
summaryrefslogtreecommitdiffstats
blob: d4e5b513962afdd2764aac0c0ed4ebd150966773 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2008 Oracle Corporation.
 * 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:
 *    Xiaonan Jiang/IBM  -- extracted from https://bugs.eclipse.org/bugs/show_bug.cgi?id=206514
 *    Cameron Bateman/Oracle - integrated.
 * 
 ********************************************************************************/
package org.eclipse.jst.jsf.designtime.internal.symbols;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.jst.jsf.context.symbol.ISymbol;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.jsfappconfig.internal.IJSFAppConfigManager;
import org.eclipse.jst.jsf.core.jsfappconfig.internal.JSFAppConfigManagerFactory;
import org.eclipse.jst.jsf.designtime.el.AbstractDTPropertyResolver;
import org.eclipse.jst.jsf.designtime.el.DefaultDTPropertyResolver;
import org.eclipse.jst.jsf.designtime.el.IDecorativeResolver;
import org.eclipse.jst.jsf.designtime.el.IInstancePerProjectResolver;

/**
 * A design time proxy for the runtime ProperyResolver.
 * 
 * @author xnjiang
 * 
 */
public class ConfigBasedDTPropertyResolver extends DefaultDTPropertyResolver
        implements IInstancePerProjectResolver
{
    private IProject _project;

    /**
     * Constructor
     * 
     */
    public ConfigBasedDTPropertyResolver()
    {
        super();
    }

    @Override
    public ISymbol[] getAllProperties(final ISymbol base)
    {
        final Map<String, ISymbol> allSymbols = new HashMap<String, ISymbol>();
        final ISymbol[] superSymbols = super.getAllProperties(base);
        for (final ISymbol superSymbol : superSymbols)
        {
            allSymbols.put(superSymbol.getName(), superSymbol);
        }

        if (JSFCorePlugin.hasDecorativePropertyResolvers())
        {
            final List<AbstractDTPropertyResolver> resolvers = retrieveDecorativePropertyResolvers(_project);
            for (final AbstractDTPropertyResolver resolver : resolvers)
            {
                final ISymbol[] otherSymbols = resolver.getAllProperties(base);
                for (final ISymbol otherSymbol : otherSymbols)
                {
                    // allow subsequent resolvers to replace symbols by name
                    // added previously
                    allSymbols.put(otherSymbol.getName(), otherSymbol);
                }
            }
        }
        return allSymbols.values().toArray(ISymbol.EMPTY_SYMBOL_ARRAY);

    }

    @Override
    public ISymbol getProperty(final ISymbol base, final int offset)
    {
        if (JSFCorePlugin.hasDecorativePropertyResolvers())
        {
            final List<AbstractDTPropertyResolver> resolvers = retrieveDecorativePropertyResolvers(_project);

            // we need to iterate backwards through the resolvers, since
            // the we take the first answer we receive.
            for (int i = resolvers.size() - 1; i >= 0; i--)
            {
                final ISymbol symbol = resolvers.get(i).getProperty(base,
                        offset);
                if (symbol != null)
                {
                    return symbol;
                }
            }
        }
        return super.getProperty(base, offset);
    }

    @Override
    public ISymbol getProperty(final ISymbol base, final Object propertyId)
    {
        if (JSFCorePlugin.hasDecorativePropertyResolvers())
        {
            final List<AbstractDTPropertyResolver> resolvers = retrieveDecorativePropertyResolvers(_project);

            // we need to iterate backwards through the resolvers, since
            // the we take the first answer we receive.
            for (int i = resolvers.size() - 1; i >= 0; i--)
            {
                final ISymbol symbol = resolvers.get(i).getProperty(base,
                        propertyId);
                if (symbol != null)
                {
                    return symbol;
                }
            }
        }
        return super.getProperty(base, propertyId);
    }

    private List<AbstractDTPropertyResolver> retrieveDecorativePropertyResolvers(
            final IProject project)
    {
        final IJSFAppConfigManager manager = JSFAppConfigManagerFactory
                .getJSFAppConfigManagerInstance(project);
        final List<String> propertyResolvers = manager.getPropertyResolvers();
        final List<String> elResolvers = manager.getELResolvers();
        propertyResolvers.addAll(elResolvers);
        final List<AbstractDTPropertyResolver> resolvers = new ArrayList<AbstractDTPropertyResolver>();
        for (final String propertyResolver : propertyResolvers)
        {
            final List<String> ids = JSFCorePlugin
                    .getPropertyResolversForName(propertyResolver);

            for (final String id : ids)
            {
                final AbstractDTPropertyResolver resolver = JSFCorePlugin
                        .getPropertyResolver(id).getInstance(_project);
                if (resolver instanceof IDecorativeResolver)
                {
                    resolvers.add(resolver);
                }
                else
                {
                    JSFCorePlugin.log(new Exception(), String.format("resolver %s must implement the IDecorativeResolver interface to be used", id)); //$NON-NLS-1$
                }
            }
        }
        return resolvers;
    }

    public void setProject(final IProject project)
    {
        _project = project;
    }

    // private void retrievePropertyResolver() {
    // try {
    // String str =
    // project.getPersistentProperty(PERSIST_PROPERTY_KEY_PROPERTY_RESOLVERS);
    // if (str != null) {
    // resolvers = new ArrayList<AbstractDTPropertyResolver>();
    // StringTokenizer tokenizer = new StringTokenizer(str, ",");//$NON-NLS-1$
    // while (tokenizer.hasMoreTokens()) {
    // AbstractDTPropertyResolver resolver =
    // JSFCorePlugin.getPropertyResolvers().get(tokenizer.nextToken());
    // if (resolver != null) {
    // resolvers.add(resolver);
    // }
    // }
    // } else {
    // checkFacesConfigForPropertyResolver();
    // }
    // } catch (CoreException e) {
    // checkFacesConfigForPropertyResolver();
    // }
    // }
    //
    // private void checkFacesConfigForPropertyResolver() {
    // resolvers = new ArrayList<AbstractDTPropertyResolver>();
    // String str = "";//$NON-NLS-1$
    // boolean bFirst = true;
    // List facesConfigs =
    // JSFAppConfigManager.getInstance(project).getFacesConfigModels();
    // Iterator itFacesConfigs = facesConfigs.iterator();
    // while (itFacesConfigs.hasNext()) {
    // FacesConfigType facesConfig = (FacesConfigType)itFacesConfigs.next();
    // EList applications = facesConfig.getApplication();
    // for (int i=0;i<applications.size();i++) {
    // EList resolvers2 =
    // ((ApplicationType)applications.get(i)).getPropertyResolver();
    // for (int j=0;j<resolvers2.size();j++) {
    // PropertyResolverType resolverType =
    // (PropertyResolverType)resolvers2.get(i);
    // AbstractDTPropertyResolver resolver =
    // JSFCorePlugin.getPropertyResolvers().get(resolverType.getTextContent());
    // if (resolver != null) {
    // resolvers.add(resolver);
    // if (bFirst) {
    // bFirst = false;
    // } else {
    // str += ",";//$NON-NLS-1$
    // }
    // str += resolverType.getTextContent();
    // }
    // }
    // }
    // }
    // try {
    // if (!str.equals("")) {//$NON-NLS-1$
    // project.setPersistentProperty(PERSIST_PROPERTY_KEY_PROPERTY_RESOLVERS,
    // str);
    // } else {
    // project.setPersistentProperty(PERSIST_PROPERTY_KEY_PROPERTY_RESOLVERS,
    // null);
    // }
    // } catch (CoreException e) {
    // e.printStackTrace();
    // }
    // }
    //
    // /**
    // * Called when a change in an application configuration model for which
    // * this listener has been registered occurs.
    // *
    // * @param notification EMF {@link Notification} instance that describes
    // the
    // * model change.
    // */
    // public void notifyChanged(Notification notification) {
    // checkFacesConfigForPropertyResolver();
    // }
}

Back to the top