1 9 10 package org.netbeans.modules.editor.impl; 11 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.List ; 15 import java.util.logging.Level ; 16 import java.util.logging.Logger ; 17 import javax.swing.Action ; 18 import org.openide.cookies.InstanceCookie; 19 import org.openide.filesystems.FileObject; 20 import org.openide.loaders.DataFolder; 21 import org.openide.loaders.DataObject; 22 import org.openide.loaders.DataObjectNotFoundException; 23 import org.openide.util.actions.SystemAction; 24 25 29 public class ActionsList { 30 31 private static final Logger LOG = Logger.getLogger(ActionsList.class.getName()); 32 33 private final List all; 34 private final List actions; 35 36 41 protected ActionsList(List keys) { 42 this(keys, false); 43 } 44 45 56 protected ActionsList(List keys, boolean ignoreFolders) { 57 List [] lists = convertImpl(keys == null ? Collections.emptyList() : keys, ignoreFolders); 58 this.all = lists[0]; 59 this.actions = lists[1]; 60 } 61 62 public List getAllInstances() { 63 return all; 64 } 65 66 public List getActionsOnly() { 67 return actions; 68 } 69 70 public static List convert(List keys) { 71 List [] lists = convertImpl(keys, false); 72 return lists[0]; 73 } 74 75 private static List [] convertImpl(List keys, boolean ignoreFolders) { 76 List all = new ArrayList (); 77 List actions = new ArrayList (); 78 79 for (int i = 0; i < keys.size(); i++){ 80 Object item = keys.get(i); 81 DataObject dob = null; 82 83 if (item instanceof DataObject) { 84 dob = (DataObject) item; 85 } else if (item instanceof FileObject) { 86 try { 87 dob = DataObject.find((FileObject) item); 88 } catch (DataObjectNotFoundException dnfe) { 89 } 91 } 92 93 if (dob != null) { 94 InstanceCookie ic = (InstanceCookie) dob.getLookup().lookup(InstanceCookie.class); 95 if (ic != null){ 96 try{ 97 if (Action .class.isAssignableFrom(ic.instanceClass()) || 98 SystemAction.class.isAssignableFrom(ic.instanceClass())) 99 { 100 Object instance = ic.instanceCreate(); 101 all.add(instance); 102 actions.add(instance); 103 } else if (!DataFolder.class.isAssignableFrom(ic.instanceClass()) || !ignoreFolders) { 104 Object instance = ic.instanceCreate(); 105 all.add(instance); 106 } 107 } catch (Exception e) { 108 LOG.log(Level.WARNING, "Can't instantiate object", e); 109 } 110 } else if (dob instanceof DataFolder) { 111 all.add(dob); 112 } else { 113 all.add(dob.getName()); 114 } 115 } else { 116 all.add(item); 117 if (item instanceof Action || item instanceof SystemAction) { 118 actions.add(item); 119 } 120 } 121 } 122 123 return new List [] { 124 all.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(all), 125 actions.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(actions), 126 }; 127 } 128 } 129 | Popular Tags |