~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to java/freeform/src/org/netbeans/modules/java/freeform/LookupProviderImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.java.freeform;
 
43
 
 
44
import org.netbeans.api.project.Project;
 
45
import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
 
46
import org.netbeans.modules.ant.freeform.spi.ProjectAccessor;
 
47
import org.netbeans.spi.project.AuxiliaryConfiguration;
 
48
import org.netbeans.spi.project.LookupProvider;
 
49
import org.netbeans.spi.project.support.ant.AntProjectEvent;
 
50
import org.netbeans.spi.project.support.ant.AntProjectHelper;
 
51
import org.netbeans.spi.project.support.ant.AntProjectListener;
 
52
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
 
53
import org.netbeans.spi.project.ui.PrivilegedTemplates;
 
54
import org.netbeans.spi.project.ui.ProjectOpenedHook;
 
55
import org.netbeans.spi.project.ui.RecommendedTemplates;
 
56
import org.openide.util.Lookup;
 
57
import org.openide.util.lookup.Lookups;
 
58
import org.openide.util.lookup.ProxyLookup;
 
59
import org.w3c.dom.Comment;
 
60
import org.w3c.dom.Document;
 
61
import org.w3c.dom.Element;
 
62
import org.w3c.dom.NamedNodeMap;
 
63
import org.w3c.dom.NodeList;
 
64
import org.w3c.dom.Text;
 
65
 
 
66
/**
 
67
 *
 
68
 * @author mkleint
 
69
 */
 
70
public class LookupProviderImpl implements LookupProvider {
 
71
     private static final String HELP_ID_FRAGMENT = "java"; // NOI18N
 
72
  
 
73
    /** Creates a new instance of LookupProviderImpl */
 
74
    public LookupProviderImpl() {
 
75
    }
 
76
    
 
77
    public Lookup createAdditionalLookup(Lookup baseContext) {
 
78
        Project prj = baseContext.lookup(Project.class);
 
79
        ProjectAccessor acc = baseContext.lookup(ProjectAccessor.class);
 
80
        AuxiliaryConfiguration aux = baseContext.lookup(AuxiliaryConfiguration.class);
 
81
        assert aux != null;
 
82
        assert prj != null;
 
83
        assert acc != null;
 
84
        return new ProjectLookup(prj, acc.getHelper(), acc.getEvaluator(), aux);
 
85
    }
 
86
    
 
87
    private static Lookup initLookup(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
 
88
        Classpaths cp = new Classpaths(projectHelper, projectEvaluator, aux);
 
89
        return Lookups.fixed(
 
90
            cp, // ClassPathProvider
 
91
            new SourceLevelQueryImpl(projectHelper, projectEvaluator, aux), // SourceLevelQueryImplementation
 
92
            new SourceForBinaryQueryImpl(projectHelper, projectEvaluator, aux), // SourceForBinaryQueryImplementation
 
93
            new OpenHook(cp), // ProjectOpenedHook
 
94
            new TestQuery(projectHelper, projectEvaluator, aux), // MultipleRootsUnitTestForSourceQueryImplementation
 
95
            new JavadocQuery(projectHelper, projectEvaluator, aux), // JavadocForBinaryQueryImplementation
 
96
            new PrivilegedTemplatesImpl(), // PrivilegedTemplates
 
97
            new JavaActions(project, projectHelper, projectEvaluator, aux), // ActionProvider
 
98
            new LookupMergerImpl(), // LookupMerger
 
99
            new JavaFreeformFileBuiltQuery(project, projectHelper, projectEvaluator, aux), // FileBuiltQueryImplementation
 
100
            new HelpIDFragmentProviderImpl());
 
101
    }
 
102
    
 
103
    public static boolean isMyProject(AuxiliaryConfiguration aux) {
 
104
        return aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true) != null ||
 
105
               aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true) != null;
 
106
    }
 
107
    
 
108
    private static final class HelpIDFragmentProviderImpl implements HelpIDFragmentProvider {
 
109
        public String getHelpIDFragment() {
 
110
            return HELP_ID_FRAGMENT;
 
111
        }
 
112
    }
 
113
    
 
114
    private static class OpenHook extends ProjectOpenedHook {
 
115
        
 
116
        private final Classpaths cp;
 
117
        
 
118
        public OpenHook(Classpaths cp) {
 
119
            this.cp = cp;
 
120
        }
 
121
        
 
122
        protected void projectOpened() {
 
123
            cp.opened();
 
124
        }
 
125
        
 
126
        protected void projectClosed() {
 
127
            cp.closed();
 
128
        }
 
129
        
 
130
    }
 
131
    
 
132
    /**
 
133
     * Transparently handles /1 -> /2 schema upgrade (on read only, not write!).
 
134
     */
 
135
    static final class UpgradingAuxiliaryConfiguration implements AuxiliaryConfiguration {
 
136
        
 
137
        private final AuxiliaryConfiguration delegate;
 
138
        
 
139
        public UpgradingAuxiliaryConfiguration(AuxiliaryConfiguration delegate) {
 
140
            this.delegate = delegate;
 
141
        }
 
142
 
 
143
        public Element getConfigurationFragment(String elementName, String namespace, boolean shared) {
 
144
            if (elementName.equals(JavaProjectNature.EL_JAVA) && namespace.equals(JavaProjectNature.NS_JAVA_2) && shared) {
 
145
                Element nue = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
 
146
                if (nue == null) {
 
147
                    Element old = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
 
148
                    if (old != null) {
 
149
                        nue = upgradeSchema(old);
 
150
                    }
 
151
                }
 
152
                return nue;
 
153
            } else {
 
154
                return delegate.getConfigurationFragment(elementName, namespace, shared);
 
155
            }
 
156
        }
 
157
 
 
158
        public void putConfigurationFragment(Element fragment, boolean shared) throws IllegalArgumentException {
 
159
            delegate.putConfigurationFragment(fragment, shared);
 
160
        }
 
161
        
 
162
        public boolean removeConfigurationFragment(String elementName, String namespace, boolean shared) throws IllegalArgumentException {
 
163
            return delegate.removeConfigurationFragment(elementName, namespace, shared);
 
164
        }
 
165
        
 
166
    }
 
167
    
 
168
    static Element upgradeSchema(Element old) {
 
169
        Document doc = old.getOwnerDocument();
 
170
        Element nue = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA);
 
171
        copyXMLTree(doc, old, nue, JavaProjectNature.NS_JAVA_2);
 
172
        return nue;
 
173
    }  
 
174
    // Copied from org.netbeans.modules.java.j2seproject.UpdateHelper with changes; could be an API eventually:
 
175
    private static void copyXMLTree(Document doc, Element from, Element to, String newNamespace) {
 
176
        NodeList nl = from.getChildNodes();
 
177
        int length = nl.getLength();
 
178
        for (int i = 0; i < length; i++) {
 
179
            org.w3c.dom.Node node = nl.item(i);
 
180
            org.w3c.dom.Node newNode;
 
181
            switch (node.getNodeType()) {
 
182
                case org.w3c.dom.Node.ELEMENT_NODE:
 
183
                    Element oldElement = (Element) node;
 
184
                    newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
 
185
                    NamedNodeMap attrs = oldElement.getAttributes();
 
186
                    int alength = attrs.getLength();
 
187
                    for (int j = 0; j < alength; j++) {
 
188
                        org.w3c.dom.Attr oldAttr = (org.w3c.dom.Attr) attrs.item(j);
 
189
                        ((Element)newNode).setAttributeNS(oldAttr.getNamespaceURI(), oldAttr.getName(), oldAttr.getValue());
 
190
                    }
 
191
                    copyXMLTree(doc, oldElement, (Element) newNode, newNamespace);
 
192
                    break;
 
193
                case org.w3c.dom.Node.TEXT_NODE:
 
194
                    newNode = doc.createTextNode(((Text) node).getData());
 
195
                    break;
 
196
                case org.w3c.dom.Node.COMMENT_NODE:
 
197
                    newNode = doc.createComment(((Comment) node).getData());
 
198
                    break;
 
199
                default:
 
200
                    // Other types (e.g. CDATA) not yet handled.
 
201
                    throw new AssertionError(node);
 
202
            }
 
203
            to.appendChild(newNode);
 
204
        }
 
205
    }    
 
206
    
 
207
    
 
208
    
 
209
   private static final class ProjectLookup extends ProxyLookup implements AntProjectListener {
 
210
 
 
211
        private AntProjectHelper helper;
 
212
        private PropertyEvaluator evaluator;
 
213
        private Project project;
 
214
        private AuxiliaryConfiguration aux;
 
215
        private boolean isMyProject;
 
216
        
 
217
        public ProjectLookup(Project project, AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
 
218
            super(new Lookup[0]);
 
219
            this.project = project;
 
220
            this.helper = helper;
 
221
            this.evaluator = evaluator;
 
222
            this.aux = new UpgradingAuxiliaryConfiguration(aux);
 
223
            this.isMyProject = isMyProject(aux);
 
224
            updateLookup();
 
225
            helper.addAntProjectListener(this);
 
226
        }
 
227
        
 
228
        private void updateLookup() {
 
229
            Lookup l = Lookup.EMPTY;
 
230
            if (isMyProject) {
 
231
                l = initLookup(project, helper, evaluator, aux);
 
232
            }
 
233
            setLookups(new Lookup[]{l});
 
234
        }
 
235
        
 
236
        public void configurationXmlChanged(AntProjectEvent ev) {
 
237
            if (isMyProject(aux) != isMyProject) {
 
238
                isMyProject = !isMyProject;
 
239
                updateLookup();
 
240
            }
 
241
        }
 
242
        
 
243
        public void propertiesChanged(AntProjectEvent ev) {
 
244
            // ignore
 
245
        }
 
246
        
 
247
    }
 
248
   private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
 
249
        
 
250
        private static final String[] PRIVILEGED_NAMES = new String[] {
 
251
            "Templates/Classes/Class.java", // NOI18N
 
252
            "Templates/Classes/Package", // NOI18N
 
253
            "Templates/Classes/Interface.java", // NOI18N
 
254
        };
 
255
        
 
256
        // List of primarily supported templates = J2SEProject.LIBRARY_TYPES + J2SEProject.APPLICATION_TYPES
 
257
        private static final String[] RECOMENDED_TYPES = new String[] { 
 
258
            "java-classes",         // NOI18N
 
259
            "java-main-class",      // NOI18N
 
260
            "java-forms",           // NOI18N
 
261
            "gui-java-application", // NOI18N
 
262
            "java-beans",           // NOI18N
 
263
            "oasis-XML-catalogs",   // NOI18N
 
264
            "XML",                  // NOI18N
 
265
            "ant-script",           // NOI18N
 
266
            "ant-task",             // NOI18N
 
267
            "servlet-types",        // NOI18N
 
268
            "wsdl",                 // NOI18N
 
269
            "junit",                // NOI18N
 
270
            // "MIDP",              // NOI18N
 
271
            "simple-files"          // NOI18N
 
272
        };
 
273
        
 
274
        public String[] getRecommendedTypes() {            
 
275
            return RECOMENDED_TYPES;
 
276
        }
 
277
        
 
278
        public String[] getPrivilegedTemplates() {
 
279
            return PRIVILEGED_NAMES;
 
280
        }
 
281
        
 
282
    }   
 
283
}