KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > ui > support > UILookupMergerSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.spi.project.ui.support;
22
23 import java.util.Arrays JavaDoc;
24 import java.util.LinkedHashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.spi.project.LookupMerger;
27 import org.netbeans.spi.project.ui.PrivilegedTemplates;
28 import org.netbeans.spi.project.ui.RecommendedTemplates;
29 import org.openide.util.Lookup;
30
31 /**
32  * Factory class for creation of {@link org.netbeans.spi.project.LookupMerger} instances.
33  * @author mkleint
34  * @since org.netbeans.modules.projectuiapi 1.19
35  */

36 public final class UILookupMergerSupport {
37     
38     /** Creates a new instance of LookupMergerSupport */
39     private UILookupMergerSupport() {
40     }
41     
42     /**
43      * Create a {@link org.netbeans.spi.project.LookupMerger} instance
44      * for {@link org.netbeans.spi.project.ui.RecommendedTemplates}. Allows to merge
45      * templates from multiple sources.
46      * @return instance to include in project lookup
47      */

48     public static LookupMerger createRecommendedTemplatesMerger() {
49         return new RecommendedMerger();
50     }
51     
52     /**
53      * Create a {@link org.netbeans.spi.project.LookupMerger} instance
54      * for {@link org.netbeans.spi.project.ui.PrivilegedTemplates}. Allows to merge
55      * templates from multiple sources.
56      * @return instance to include in project lookup
57      */

58     public static LookupMerger createPrivilegedTemplatesMerger() {
59         return new PrivilegedMerger();
60     }
61     
62     private static class PrivilegedMerger implements LookupMerger<PrivilegedTemplates> {
63         public Class JavaDoc<PrivilegedTemplates> getMergeableClass() {
64             return PrivilegedTemplates.class;
65         }
66
67         public PrivilegedTemplates merge(Lookup lookup) {
68             return new PrivilegedTemplatesImpl(lookup);
69         }
70     }
71     
72     private static class RecommendedMerger implements LookupMerger<RecommendedTemplates> {
73         
74         public Class JavaDoc<RecommendedTemplates> getMergeableClass() {
75             return RecommendedTemplates.class;
76         }
77
78         public RecommendedTemplates merge(Lookup lookup) {
79             return new RecommendedTemplatesImpl(lookup);
80         }
81     }
82     
83     private static class PrivilegedTemplatesImpl implements PrivilegedTemplates {
84         
85         private Lookup lkp;
86         
87         public PrivilegedTemplatesImpl(Lookup lkp) {
88             this.lkp = lkp;
89         }
90         
91         public String JavaDoc[] getPrivilegedTemplates() {
92             Set JavaDoc<String JavaDoc> templates = new LinkedHashSet JavaDoc<String JavaDoc>();
93             for (PrivilegedTemplates pt : lkp.lookupAll(PrivilegedTemplates.class)) {
94                 templates.addAll(Arrays.asList(pt.getPrivilegedTemplates()));
95             }
96             return templates.toArray(new String JavaDoc[templates.size()]);
97         }
98     }
99     
100     private static class RecommendedTemplatesImpl implements RecommendedTemplates {
101         
102         private Lookup lkp;
103         
104         public RecommendedTemplatesImpl(Lookup lkp) {
105             this.lkp = lkp;
106         }
107         
108         public String JavaDoc[] getRecommendedTypes() {
109             Set JavaDoc<String JavaDoc> templates = new LinkedHashSet JavaDoc<String JavaDoc>();
110             for (RecommendedTemplates pt : lkp.lookupAll(RecommendedTemplates.class)) {
111                 templates.addAll(Arrays.asList(pt.getRecommendedTypes()));
112             }
113             return templates.toArray(new String JavaDoc[templates.size()]);
114         }
115         
116     }
117     
118     
119 }
120
Popular Tags