KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataLdrActions


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.openide.loaders;
21
22
23 import java.io.IOException JavaDoc;
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 /** Manages actions read and write for a given loader.
30  *
31  * @author Jaroslav Tulach
32  */

33 final class DataLdrActions extends FolderInstance {
34     /** Reference<DataLoader> to know for what loader we work */
35     private java.lang.ref.Reference JavaDoc<DataLoader> ref;
36     /** last creating task */
37     private org.openide.util.Task creation;
38     /** processor to use */
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 JavaDoc<DataLoader> (l);
45     }
46     
47     /** Asks the manager to store these actions to disk. Provided for
48      * backward compatibility.
49      */

50     public synchronized void setActions (final SystemAction[] arr) {
51         class DoTheWork implements Runnable JavaDoc, FileSystem.AtomicAction {
52             private int state;
53             
54             /** The goal of this method is to make sure that all actions
55              * will really be stored on the disk.
56              */

57             private void work () throws IOException JavaDoc {
58                 DataObject[] now = folder.getChildren ();
59                 Map<Object JavaDoc, DataObject> nowToObj = new HashMap<Object JavaDoc, 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 JavaDoc instance = ic.instanceCreate();
67
68                             if (instance instanceof javax.swing.Action JavaDoc) {
69                                 nowToObj.put(instance, now[i]);
70                                 continue;
71                             }
72                             if (instance instanceof javax.swing.JSeparator JavaDoc) {
73                                 sepObjs.add(now[i]);
74                                 continue;
75                             }
76                         }
77                         catch (java.lang.ClassNotFoundException JavaDoc 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 JavaDoc.class);
95                             }
96                         }
97                     }
98                     order.add (obj);
99                 }
100                 
101                 // these were there but are not there anymore
102
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 JavaDoc ex) {
124                     Exceptions.printStackTrace(ex);
125                 }
126             }
127         }
128         
129         DoTheWork dtw = new DoTheWork ();
130         creation = RP.post (dtw);
131     }
132     
133     
134     /** Creates the actions and notifies the loader.
135      */

136     protected Object JavaDoc createInstance (org.openide.cookies.InstanceCookie[] cookies) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
137         ArrayList<javax.swing.Action JavaDoc> list = new ArrayList<javax.swing.Action JavaDoc> ();
138         for (int i = 0; i < cookies.length; i++) {
139             Class JavaDoc clazz = cookies[i].instanceClass ();
140             if (javax.swing.JSeparator JavaDoc.class.isAssignableFrom (clazz)) {
141                 list.add (null);
142                 continue;
143             }
144             
145             Object JavaDoc action = cookies[i].instanceCreate ();
146             if (action instanceof javax.swing.Action JavaDoc) {
147                 list.add ((javax.swing.Action JavaDoc)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 JavaDoc[0]);
158     }
159
160     /** Currently not recursive */
161     protected org.openide.cookies.InstanceCookie acceptFolder (DataFolder df) {
162         return null;
163     }
164
165     /** Creation in our own thread, so we can exclude storage modifications */
166     protected org.openide.util.Task postCreationTask (Runnable JavaDoc 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