KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > actions > XMLUpdateDocumentAction


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.xml.core.actions;
21
22 import java.util.Iterator JavaDoc;
23 import org.netbeans.modules.xml.core.cookies.UpdateDocumentCookie;
24 import org.openide.nodes.Node;
25 import org.openide.util.HelpCtx;
26 import org.openide.util.Lookup;
27 import org.openide.util.RequestProcessor;
28 import org.openide.util.actions.CookieAction;
29
30 /**
31  * Reinitialize internal structures. User has strong feeling that he changed
32  * external state that is not authomatically monitored by given data object.
33  *
34  */

35 public final class XMLUpdateDocumentAction extends CookieAction {
36     
37     /** generated Serialized Version UID */
38     private static final long serialVersionUID = -235822666875674523L;
39
40     /* @return the mode of action. */
41     protected int mode() {
42         return MODE_ALL;
43     }
44
45     /* Human presentable name of the action. This should be
46      * presented as an item in a menu.
47      * @return the name of the action
48      */

49     public String JavaDoc getName () {
50         return Util.THIS.getString ("PROP_UpdateDocument");
51     }
52
53     protected Class JavaDoc[] cookieClasses () {
54         return new Class JavaDoc[] { UpdateDocumentCookie.class };
55     }
56
57     /* Help context where to find more about the action.
58      * @return the help context for this action
59      */

60     public HelpCtx getHelpCtx () {
61         return new HelpCtx (XMLUpdateDocumentAction.class);
62     }
63
64     // is called from a ModuleActions thread
65
protected void performAction (final Node[] activatedNodes) {
66         
67         Lookup lookup = Lookup.getDefault();
68         Lookup.Template template = new Lookup.Template(Performer.class);
69         final Lookup.Result result = lookup.lookup(template);
70         
71         RequestProcessor.getDefault().postRequest(new Runnable JavaDoc() {
72             public void run() {
73                 for (int i = 0; i < activatedNodes.length; i++) {
74                     UpdateDocumentCookie rc = (UpdateDocumentCookie)activatedNodes[i].getCookie
75                                                    (UpdateDocumentCookie.class);
76                     if (rc != null) {
77                         rc.updateDocumentRoot();
78                     }
79
80                     //??? unfortunatelly there can be only one cookie per node
81
// use lookup to emulate N-cookies - performers
82

83                     // delegate to all registered performers
84
Iterator JavaDoc it = result.allInstances().iterator();
85                     while (it.hasNext()) {
86                         Performer next = (Performer) it.next();
87                         next.perform(activatedNodes[i]);
88                     }
89                 }
90             }
91         });
92     }
93     
94     protected boolean asynchronous() {
95         return false;
96     }
97
98     /**
99      * To be somehow exposed via API, now XML module specifics.
100      * It is registered at module layer. All registered performes are invoked.
101      */

102     public static interface Performer {
103         
104         /**
105          * Update internal state after possible change of external resources
106          * (flush caches etc.).
107          */

108         void perform(Node node);
109     }
110 }
111
Popular Tags