KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > DefaultJavaPlatformProvider


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 package org.netbeans.modules.java.platform;
20
21 import org.netbeans.api.java.platform.JavaPlatform;
22 import org.openide.filesystems.*;
23 import org.openide.cookies.InstanceCookie;
24 import org.openide.ErrorManager;
25 import org.openide.loaders.DataObject;
26
27 import java.util.*;
28 import java.io.IOException JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import org.openide.util.Lookup;
32
33 public class DefaultJavaPlatformProvider implements JavaPlatformProvider, FileChangeListener {
34
35     private static final String JavaDoc PLATFORM_STORAGE = "Services/Platforms/org-netbeans-api-java-Platform"; //NOI18N
36

37     private HashSet listeners;
38     private FileObject storage;
39     private JavaPlatform defaultPlatform;
40
41     public DefaultJavaPlatformProvider () {
42         storage = Repository.getDefault().getDefaultFileSystem().findResource(PLATFORM_STORAGE);
43         if (storage == null) {
44             // Turn this off since it can confuse unit tests running w/o layer merging.
45
//assert false : "Cannot find platforms storage";
46
}
47         else {
48             storage.addFileChangeListener (this);
49         }
50     }
51
52     public JavaPlatform[] getInstalledPlatforms() {
53         List platforms = new ArrayList ();
54         if (storage != null) {
55             try {
56                 FileObject[] platfomDefinitions = storage.getChildren();
57                 for (int i=0; i<platfomDefinitions.length;i++) {
58                     DataObject dobj = DataObject.find (platfomDefinitions[i]);
59                     InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
60                     if (ic == null) {
61                         ErrorManager.getDefault().log(ErrorManager.WARNING,"DefaultPlatformStorage: The file: "+ //NOI18N
62
platfomDefinitions[i].getNameExt()+" hasn't InstanceCookie"); //NOI18N
63
continue;
64                     }
65                     else if (ic instanceof InstanceCookie.Of) {
66                         if (((InstanceCookie.Of)ic).instanceOf(JavaPlatform.class)) {
67                             platforms.add (ic.instanceCreate());
68                         }
69                         else {
70                             ErrorManager.getDefault().log(ErrorManager.WARNING,"DefaultPlatformStorage: The file: "+ //NOI18N
71
platfomDefinitions[i].getNameExt()+" isn't instance of JavaPlatform"); //NOI18N
72
}
73                     }
74                     else {
75                         Object JavaDoc instance = ic.instanceCreate();
76                         if (instance instanceof JavaPlatform) {
77                             platforms.add (instance);
78                         }
79                         else {
80                             ErrorManager.getDefault().log(ErrorManager.WARNING,"DefaultPlatformStorage: The file: "+ //NOI18N
81
platfomDefinitions[i].getNameExt()+" isn't instance of JavaPlatform"); //NOI18N
82
}
83                     }
84                 }
85             }catch (ClassNotFoundException JavaDoc cnf) {
86                 ErrorManager.getDefault().notify (cnf);
87             }
88             catch (IOException JavaDoc ioe) {
89                 ErrorManager.getDefault().notify (ioe);
90             }
91         }
92         return (JavaPlatform[])platforms.toArray(new JavaPlatform[platforms.size()]);
93     }
94     
95     public JavaPlatform getDefaultPlatform() {
96         if (this.defaultPlatform == null) {
97             JavaPlatform[] allPlatforms = this.getInstalledPlatforms();
98             for (int i=0; i< allPlatforms.length; i++) {
99                 if ("default_platform".equals(allPlatforms[i].getProperties().get("platform.ant.name"))) { //NOI18N
100
defaultPlatform = allPlatforms[i];
101                     break;
102                 }
103             }
104         }
105         return this.defaultPlatform;
106     }
107     
108
109     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
110         if (this.listeners == null) {
111             this.listeners = new HashSet ();
112         }
113         this.listeners.add (listener);
114     }
115
116     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
117         if (this.listeners == null) {
118             return;
119         }
120         this.listeners.remove (listener);
121     }
122
123
124     public void fileFolderCreated(FileEvent fe) {
125     }
126
127     public void fileDataCreated(FileEvent fe) {
128         firePropertyChange ();
129     }
130
131     public void fileChanged(FileEvent fe) {
132         firePropertyChange ();
133     }
134
135     public void fileDeleted(FileEvent fe) {
136         firePropertyChange ();
137     }
138
139     public void fileRenamed(FileRenameEvent fe) {
140         firePropertyChange ();
141     }
142
143     public void fileAttributeChanged(FileAttributeEvent fe) {
144     }
145
146     private void firePropertyChange () {
147         Iterator it = null;
148         synchronized (this) {
149             if (this.listeners == null) {
150                 return;
151             }
152             it = ((Set)this.listeners.clone()).iterator();
153         }
154         PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc (this, PROP_INSTALLED_PLATFORMS, null, null);
155         while (it.hasNext()) {
156             ((PropertyChangeListener JavaDoc)it.next()).propertyChange(event);
157         }
158     }
159     
160 }
161
Popular Tags