KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > UpdateNode


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.autoupdate;
21
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.Action JavaDoc;
26
27 import org.openide.nodes.Node;
28 import org.openide.nodes.AbstractNode;
29 import org.openide.util.NbBundle;
30 import org.openide.util.actions.CallableSystemAction;
31 import org.openide.util.HelpCtx;
32
33 /** The UpdateNode class holds static innerclasses ( Module, Group,
34  * Children and wait. This class serves only like a namespace for
35  * the innerclasses and defines some usefull constants.
36  *
37  * @author Petr Hrebejk
38  */

39 class UpdateNode extends Object JavaDoc {
40
41     /** Iconbase for module Updates with new modules */
42     private static final String JavaDoc NEW_MODULE_ICON_BASE = "org/netbeans/modules/autoupdate/resources/newModule.gif"; // NOI18N
43
/** Iconbase for module Updates with module updates */
44     private static final String JavaDoc UPDATE_MODULE_ICON_BASE = "org/netbeans/modules/autoupdate/resources/updateModule.png"; // NOI18N
45
/** Iconbase for module Updates with module updates */
46     private static final String JavaDoc MODULE_GROUP_ICON_BASE = "org/openide/loaders/defaultFolder.gif"; // NOI18N
47
/** Iconbase for wait node */
48     private static final String JavaDoc WAIT_ICON_BASE = "org/openide/src/resources/wait.gif"; // NOI18N
49
/** Iconbase for module purchased modules */
50     private static final String JavaDoc PURCHASED_MODULE_ICON_BASE = "org/netbeans/modules/autoupdate/resources/purchasedModule.gif"; // NOI18N
51
/** Iconbase for AU servers */
52     private static final String JavaDoc SERVER_ICON_BASE = "org/netbeans/modules/autoupdate/resources/updateAction.gif"; // NOI18N
53
/** Iconbase for local update */
54     private static final String JavaDoc LOCAL_ICON_BASE = "org/openide/resources/localFS.gif"; // NOI18N
55
/** Iconbase for module Updates with module updates */
56     private static final String JavaDoc LOCALE_MODULE_ICON_BASE = "org/netbeans/modules/autoupdate/resources/localeModule.gif"; // NOI18N
57

58     /** Private constructor, the class should have no instances */
59     private UpdateNode() {
60     }
61
62     private static String JavaDoc getBundle( String JavaDoc key ) {
63         return NbBundle.getMessage( UpdateNode.class, key );
64     }
65     
66     /** Class for representing module update in the tree */
67     static class Module extends AbstractNode {
68
69         private ModuleUpdate moduleUpdate;
70         static CallableSystemAction moduleAction = null;
71
72         Module ( ModuleUpdate moduleUpdate ) {
73             super( org.openide.nodes.Children.LEAF );
74
75             this.moduleUpdate = moduleUpdate;
76
77             setDisplayName( moduleUpdate.getName() );
78             if ( moduleUpdate.isPurchased() )
79                 setIconBaseWithExtension( PURCHASED_MODULE_ICON_BASE );
80             else if ( moduleUpdate instanceof L10NUpdate )
81                 setIconBaseWithExtension( LOCALE_MODULE_ICON_BASE );
82             else
83                 setIconBaseWithExtension( moduleUpdate.isNew() ? NEW_MODULE_ICON_BASE : UPDATE_MODULE_ICON_BASE );
84
85             getCookieSet().add( moduleUpdate );
86         }
87         
88         CallableSystemAction getModuleAction() {
89             if ( moduleAction == null )
90                 moduleAction = new ModuleAction();
91             return moduleAction;
92         }
93         
94         public Action JavaDoc getPreferredAction () {
95             return getModuleAction ();
96         }
97         
98         class ModuleAction extends CallableSystemAction {
99             public void performAction() {
100                 Settings.getShared().fireNodeDefaultAction();
101             }
102
103             public HelpCtx getHelpCtx() {
104                 return null;
105             }
106
107             public String JavaDoc getName() {
108                 return null;
109             }
110             
111             protected boolean asynchronous () {
112                 return false;
113             }
114             
115         }
116     }
117
118     /** Class for representing module wait node */
119     static class Wait extends AbstractNode {
120
121         Wait ( ) {
122             super( org.openide.nodes.Children.LEAF );
123
124             setDisplayName( getBundle( "CTL_WaitNode" ) );
125             setIconBaseWithExtension( WAIT_ICON_BASE );
126         }
127     }
128
129     abstract static class FolderContent extends org.openide.nodes.Children.Keys {
130         abstract void refreshContent(boolean recursive);
131
132         final void refreshSubFolders() {
133             Node[] nodes = getNodes();
134             for ( int i=0; i < nodes.length; i++ ) {
135                 if ( nodes[i] instanceof Folder )
136                     ( (Folder) nodes[i] ).refreshContent( true );
137             }
138         }
139     }
140     
141     static class Folder extends AbstractNode {
142         
143         protected FolderContent folderContent;
144         
145         public Folder( FolderContent fc ) {
146             super( fc );
147             folderContent = fc;
148         }
149         
150         final void refreshContent(boolean recursive) {
151             folderContent.refreshContent( recursive );
152         }
153
154     }
155     
156     /** Class for representing module group in the tree */
157     static class Group extends Folder {
158
159         private ModuleGroup group;
160
161         Group( ModuleGroup group ) {
162             super( new UpdateNode.Children( group ) );
163
164             this.group = group;
165
166             setDisplayName( group.getName() );
167             setIconBaseWithExtension( MODULE_GROUP_ICON_BASE );
168
169             getCookieSet().add( group );
170         }
171
172     }
173
174     /** Class for representing module group in the tree */
175     static class Server extends Folder {
176
177         Server( AutoupdateType at ) {
178             super( new UpdateNode.ATChildren( at ) );
179
180             setDisplayName( at.getName() );
181             this.setIconBaseWithExtension( SERVER_ICON_BASE );
182
183         }
184
185     }
186     
187     /** Class for representing module group in the tree */
188     static class LocalServer extends Folder {
189
190         LocalServer( Object JavaDoc obj ) {
191             super( new UpdateNode.ATChildren( obj ) );
192
193             setDisplayName( getBundle( "LBL_LocalUpdate" ) );
194             this.setIconBaseWithExtension( LOCAL_ICON_BASE );
195
196         }
197
198     }
199     
200     static AllServers getAllServers() {
201         return new AllServers();
202     }
203     
204     /** Class for representing module group in the tree */
205     static class AllServers extends Folder {
206
207         AllServers() {
208             super( new UpdateNode.ASChildren() );
209         }
210
211     }
212
213     /** Holds children nodes of the module group */
214     static class Children extends FolderContent {
215
216         protected ModuleGroup moduleGroup;
217         
218         // CONSTRUCTORS -----------------------------------------------------------------------
219

220         /** Creates module group children.
221          * @param group The group of modules
222          */

223
224         public Children ( ModuleGroup moduleGroup ) {
225             super();
226             this.moduleGroup = moduleGroup;
227             
228             refreshContent( false );
229         }
230         
231         /** Called when the preparation of nodes is needed
232          */

233         protected void addNotify() {
234             //refreshAllKeys ();
235
}
236
237         /** Called when all children are garbage collected */
238         protected void removeNotify() {
239             setKeys( java.util.Collections.EMPTY_SET );
240         }
241
242         // IMPLEMENTATION of Children.Keys ------------------------------------------
243

244         /** Creates nodes for given key.
245         */

246         protected Node[] createNodes( final Object JavaDoc key ) {
247
248             if (key instanceof ModuleUpdate) {
249                 return new Node[] { new UpdateNode.Module( (ModuleUpdate)key ) };
250             }
251             else if (key instanceof ModuleGroup ) {
252                 return new Node[] { new UpdateNode.Group( (ModuleGroup)key ) };
253             }
254             else {
255                 // Unknown pattern
256
return new Node[0];
257             }
258         }
259         
260         void refreshContent(boolean recursive) {
261             ArrayList JavaDoc list = new java.util.ArrayList JavaDoc();
262             Iterator JavaDoc it = moduleGroup.getItems().iterator();
263             while ( it.hasNext() ) {
264                 Object JavaDoc ob = it.next();
265                 if (ob instanceof ModuleUpdate) {
266                     ModuleUpdate mu = (ModuleUpdate) ob;
267                     if (! mu.isSelected () && DependencyChecker.checkPlatformDependency (mu.getRemoteModule ())) {
268                         list.add (mu);
269                     }
270                 } else {
271                     list.add (ob);
272                 }
273             }
274                     
275             setKeys( list );
276             if ( recursive )
277                 refreshSubFolders();
278         }
279         
280     }
281     
282     /** Holds children nodes of the module group */
283     static class ATChildren extends FolderContent {
284
285         private Object JavaDoc autoupdateType;
286         // CONSTRUCTORS -----------------------------------------------------------------------
287

288         /** Creates module group children.
289          * @param group The group of modules
290          */

291
292         public ATChildren ( Object JavaDoc obj ) {
293             super();
294             this.autoupdateType = obj;
295             refreshContent( false );
296         }
297
298         /** Called when the preparation of nodes is needed
299          */

300         protected void addNotify() {
301             //refreshAllKeys ();
302
}
303
304         /** Called when all children are garbage collected */
305         protected void removeNotify() {
306             setKeys( java.util.Collections.EMPTY_SET );
307         }
308
309         // IMPLEMENTATION of Children.Keys ------------------------------------------
310

311         /** Creates nodes for given key.
312         */

313         protected Node[] createNodes( final Object JavaDoc key ) {
314             Node[] result;
315             if (key instanceof ModuleUpdate) {
316                 result = new Node[] { new UpdateNode.Module( (ModuleUpdate)key ) };
317             }
318             else if (key instanceof ModuleGroup ) {
319                 result = new Node[] { new UpdateNode.Group( (ModuleGroup)key ) };
320                 // count the available modules in folder
321
int countOfChildren = ((ModuleGroup)key).getItems ().size ();
322                 // if none available module then don't add empty folder
323
if (countOfChildren == 0) result = new Node[0];
324             }
325             else {
326                 // Unknown pattern
327
result = new Node[0];
328             }
329             return result;
330         }
331         
332         void refreshContent(boolean recursive) {
333             ArrayList JavaDoc list = new java.util.ArrayList JavaDoc();
334             ModuleGroup group = ((Updates)(Wizard.getAllUpdates().get( autoupdateType ))).getRootGroup();
335             
336             if (group != null) {
337                 Iterator JavaDoc it = group.getItems().iterator();
338                 while ( it.hasNext() ) {
339                     Object JavaDoc ob = it.next();
340                     if (( ob instanceof ModuleGroup ) ||
341                             (ob instanceof ModuleUpdate && !((ModuleUpdate)ob).isSelected () &&
342                             DependencyChecker.checkPlatformDependency (((ModuleUpdate) ob).getRemoteModule ())) )
343                         list.add(ob);
344                 }
345             }
346                     
347             setKeys( list );
348             if ( recursive )
349                 refreshSubFolders();
350         }
351         
352     }
353     
354     /** Holds children nodes of the module group */
355     static class ASChildren extends FolderContent {
356
357         // CONSTRUCTORS -----------------------------------------------------------------------
358

359         /** Creates module group children.
360          * @param group The group of modules
361          */

362
363         public ASChildren () {
364             super();
365             ArrayList JavaDoc list = new java.util.ArrayList JavaDoc();
366             Iterator JavaDoc it = Wizard.getAllUpdates().keySet().iterator();
367             while ( it.hasNext() ) {
368                 Object JavaDoc ob = it.next();
369                 list.add(ob);
370             }
371                     
372             setKeys( list );
373         }
374
375         /** Called when the preparation of nodes is needed
376          */

377         protected void addNotify() {
378             //refreshAllKeys ();
379
}
380
381         /** Called when all children are garbage collected */
382         protected void removeNotify() {
383             setKeys( java.util.Collections.EMPTY_SET );
384         }
385
386         // IMPLEMENTATION of Children.Keys ------------------------------------------
387

388         /** Creates nodes for given key.
389         */

390         protected Node[] createNodes( final Object JavaDoc key ) {
391
392             if (key instanceof AutoupdateType) {
393                 return new Node[] { new UpdateNode.Server( (AutoupdateType)key ) };
394             }
395             else {
396                 return new Node[] { new UpdateNode.LocalServer( key ) };
397             }
398         }
399         
400         void refreshContent(boolean recursive) {
401             if ( recursive )
402                 refreshSubFolders();
403         }
404         
405     }
406 }
407
Popular Tags