1 19 20 package org.openide.loaders; 21 22 23 import java.io.IOException ; 24 import java.util.*; 25 import org.openide.filesystems.FileSystem; 26 import org.openide.util.Exceptions; 27 import org.openide.util.actions.SystemAction; 28 29 33 final class DataLdrActions extends FolderInstance { 34 35 private java.lang.ref.Reference <DataLoader> ref; 36 37 private org.openide.util.Task creation; 38 39 private static org.openide.util.RequestProcessor RP = new org.openide.util.RequestProcessor ("Loader Actions"); 40 41 public DataLdrActions (DataFolder f, DataLoader l) { 42 super (f); 43 44 this.ref = new java.lang.ref.WeakReference <DataLoader> (l); 45 } 46 47 50 public synchronized void setActions (final SystemAction[] arr) { 51 class DoTheWork implements Runnable , FileSystem.AtomicAction { 52 private int state; 53 54 57 private void work () throws IOException { 58 DataObject[] now = folder.getChildren (); 59 Map<Object , DataObject> nowToObj = new HashMap<Object , DataObject> (); 60 LinkedList<DataObject> sepObjs = new LinkedList<DataObject> (); 61 for (int i = 0; i < now.length; i++) { 62 org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie) now[i].getCookie(org.openide.cookies.InstanceCookie.class); 63 64 if (ic != null) { 65 try { 66 java.lang.Object instance = ic.instanceCreate(); 67 68 if (instance instanceof javax.swing.Action ) { 69 nowToObj.put(instance, now[i]); 70 continue; 71 } 72 if (instance instanceof javax.swing.JSeparator ) { 73 sepObjs.add(now[i]); 74 continue; 75 } 76 } 77 catch (java.lang.ClassNotFoundException ex) { 78 Exceptions.printStackTrace(ex); 79 } 80 } 81 } 82 83 ArrayList<DataObject> order = new ArrayList<DataObject> (); 84 85 for (int i = 0; i < arr.length; i++) { 86 DataObject obj = (DataObject)nowToObj.remove (arr[i]); 87 if (obj == null) { 88 if (arr[i] != null) { 89 obj = InstanceDataObject.create (folder, null, arr[i].getClass ()); 90 } else { 91 if (!sepObjs.isEmpty ()) { 92 obj = sepObjs.removeFirst (); 93 } else { 94 obj = InstanceDataObject.create (folder, "Separator" + order.size (), javax.swing.JSeparator .class); 95 } 96 } 97 } 98 order.add (obj); 99 } 100 101 for (DataObject obj: nowToObj.values ()) { 103 obj.delete (); 104 } 105 for (DataObject obj: sepObjs) { 106 obj.delete (); 107 } 108 109 folder.setOrder (order.toArray (new DataObject[0])); 110 } 111 112 public void run () { 113 try { 114 switch (state) { 115 case 0: 116 state = 1; 117 folder.getPrimaryFile ().getFileSystem ().runAtomicAction (this); 118 break; 119 case 1: 120 work (); 121 break; 122 } 123 } catch (IOException ex) { 124 Exceptions.printStackTrace(ex); 125 } 126 } 127 } 128 129 DoTheWork dtw = new DoTheWork (); 130 creation = RP.post (dtw); 131 } 132 133 134 136 protected Object createInstance (org.openide.cookies.InstanceCookie[] cookies) throws java.io.IOException , ClassNotFoundException { 137 ArrayList<javax.swing.Action > list = new ArrayList<javax.swing.Action > (); 138 for (int i = 0; i < cookies.length; i++) { 139 Class clazz = cookies[i].instanceClass (); 140 if (javax.swing.JSeparator .class.isAssignableFrom (clazz)) { 141 list.add (null); 142 continue; 143 } 144 145 Object action = cookies[i].instanceCreate (); 146 if (action instanceof javax.swing.Action ) { 147 list.add ((javax.swing.Action )action); 148 continue; 149 } 150 } 151 152 DataLoader l = ref.get (); 153 if (l != null) { 154 l.setSwingActions (list); 155 } 156 157 return list.toArray (new javax.swing.Action [0]); 158 } 159 160 161 protected org.openide.cookies.InstanceCookie acceptFolder (DataFolder df) { 162 return null; 163 } 164 165 166 protected org.openide.util.Task postCreationTask (Runnable run) { 167 return RP.post (run); 168 } 169 170 public void waitFinished () { 171 org.openide.util.Task t; 172 synchronized (this) { 173 t = creation; 174 } 175 176 if (t != null) { 177 t.waitFinished (); 178 } 179 180 super.waitFinished (); 181 } 182 183 } 184 | Popular Tags |