1 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 ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeEvent ; 31 import org.openide.util.Lookup; 32 33 public class DefaultJavaPlatformProvider implements JavaPlatformProvider, FileChangeListener { 34 35 private static final String PLATFORM_STORAGE = "Services/Platforms/org-netbeans-api-java-Platform"; 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 } 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: "+ platfomDefinitions[i].getNameExt()+" hasn't InstanceCookie"); 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: "+ platfomDefinitions[i].getNameExt()+" isn't instance of JavaPlatform"); } 73 } 74 else { 75 Object instance = ic.instanceCreate(); 76 if (instance instanceof JavaPlatform) { 77 platforms.add (instance); 78 } 79 else { 80 ErrorManager.getDefault().log(ErrorManager.WARNING,"DefaultPlatformStorage: The file: "+ platfomDefinitions[i].getNameExt()+" isn't instance of JavaPlatform"); } 83 } 84 } 85 }catch (ClassNotFoundException cnf) { 86 ErrorManager.getDefault().notify (cnf); 87 } 88 catch (IOException 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"))) { defaultPlatform = allPlatforms[i]; 101 break; 102 } 103 } 104 } 105 return this.defaultPlatform; 106 } 107 108 109 public synchronized void addPropertyChangeListener(PropertyChangeListener 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 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 event = new PropertyChangeEvent (this, PROP_INSTALLED_PLATFORMS, null, null); 155 while (it.hasNext()) { 156 ((PropertyChangeListener )it.next()).propertyChange(event); 157 } 158 } 159 160 } 161 | Popular Tags |