1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import org.netbeans.spi.project.ActionProvider; 31 import org.netbeans.spi.project.LookupMerger; 32 import org.openide.util.Lookup; 33 34 39 public class LookupMergerImpl implements LookupMerger<ActionProvider> { 40 41 private static final Logger LOG = Logger.getLogger(LookupMergerImpl.class.getName()); 42 43 public LookupMergerImpl() { 44 } 45 46 47 public Class <ActionProvider> getMergeableClass() { 48 return ActionProvider.class; 49 } 50 51 public ActionProvider merge(Lookup lookup) { 52 return new ActionProviderImpl(lookup); 53 } 54 55 56 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 <? extends ActionProvider> delegates() { 68 ActionProvider master = null; 69 List <ActionProvider> aps = new ArrayList <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); return aps; 82 } 83 84 public boolean isActionEnabled(String command, Lookup context) throws IllegalArgumentException { 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 [] {ap, enabled, command, context}); 91 if (enabled) { 92 return true; 93 } 94 } 95 } 96 if (supported) { 97 return false; 98 } else { 99 throw new IllegalArgumentException (command); 101 } 102 } 103 104 public void invokeAction(String command, Lookup context) throws IllegalArgumentException { 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 [] {command, context, ap}); 108 ap.invokeAction(command, context); 109 return; 110 } 111 } 112 throw new IllegalArgumentException (command); 113 } 114 115 public String [] getSupportedActions() { 116 Set <String > actions = new HashSet <String >(); 117 Collection <? 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 [] {aps, actions}); 122 return actions.toArray(new String [actions.size()]); 123 } 124 125 } 126 127 } 128 | Popular Tags |