KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > LookupMergerImpl


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 package org.netbeans.modules.ant.freeform;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import org.netbeans.spi.project.ActionProvider;
31 import org.netbeans.spi.project.LookupMerger;
32 import org.openide.util.Lookup;
33
34 /**
35  * Merges ActionProvider
36  *
37  * @author David Konecny, Milos Kleint
38  */

39 public class LookupMergerImpl implements LookupMerger<ActionProvider> {
40
41     private static final Logger JavaDoc LOG = Logger.getLogger(LookupMergerImpl.class.getName());
42
43     public LookupMergerImpl() {
44     }
45     
46
47     public Class JavaDoc<ActionProvider> getMergeableClass() {
48         return ActionProvider.class;
49     }
50
51     public ActionProvider merge(Lookup lookup) {
52         return new ActionProviderImpl(lookup);
53     }
54     
55
56     /**
57      * Permits any nature to add actions to the project.
58      */

59     private static class ActionProviderImpl implements ActionProvider {
60         
61         private final Lookup lkp;
62         
63         public ActionProviderImpl(Lookup lkp) {
64             this.lkp = lkp;
65         }
66         
67         private Collection JavaDoc<? extends ActionProvider> delegates() {
68             ActionProvider master = null;
69             List JavaDoc<ActionProvider> aps = new ArrayList JavaDoc<ActionProvider>();
70             for (ActionProvider ap : lkp.lookupAll(ActionProvider.class)) {
71                 if (ap instanceof Actions) {
72                     assert master == null;
73                     master = ap;
74                 } else {
75                     assert ap != this;
76                     aps.add(ap);
77                 }
78             }
79             assert master != null;
80             aps.add(0, master); // #97436: plain Actions takes precedence.
81
return aps;
82         }
83
84         public boolean isActionEnabled(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
85             boolean supported = false;
86             for (ActionProvider ap : delegates()) {
87                 if (Arrays.asList(ap.getSupportedActions()).contains(command)) {
88                     supported = true;
89                     boolean enabled = ap.isActionEnabled(command, context);
90                     LOG.log(Level.FINE, "delegate {0} says enabled={1} for {2} in {3}", new Object JavaDoc[] {ap, enabled, command, context});
91                     if (enabled) {
92                         return true;
93                     }
94                 }
95             }
96             if (supported) {
97                 return false;
98             } else {
99                 // Not supported by anyone.
100
throw new IllegalArgumentException JavaDoc(command);
101             }
102         }
103
104         public void invokeAction(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
105             for (ActionProvider ap : delegates()) {
106                 if (Arrays.asList(ap.getSupportedActions()).contains(command) && ap.isActionEnabled(command, context)) {
107                     LOG.log(Level.FINE, "delegating {0} on {1} to {2}", new Object JavaDoc[] {command, context, ap});
108                     ap.invokeAction(command, context);
109                     return;
110                 }
111             }
112             throw new IllegalArgumentException JavaDoc(command);
113         }
114
115         public String JavaDoc[] getSupportedActions() {
116             Set JavaDoc<String JavaDoc> actions = new HashSet JavaDoc<String JavaDoc>();
117             Collection JavaDoc<? extends ActionProvider> aps = delegates();
118             for (ActionProvider ap : aps) {
119                 actions.addAll(Arrays.asList(ap.getSupportedActions()));
120             }
121             LOG.log(Level.FINE, "delegates {0} report supported actions {1}", new Object JavaDoc[] {aps, actions});
122             return actions.toArray(new String JavaDoc[actions.size()]);
123         }
124         
125     }
126     
127 }
128
Popular Tags