001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.gsfpath.platform;
042:
043: import org.netbeans.modules.gsfpath.api.platform.JavaPlatform;
044: import org.openide.filesystems.*;
045: import org.openide.cookies.InstanceCookie;
046: import org.openide.ErrorManager;
047: import org.openide.loaders.DataObject;
048:
049: import java.util.*;
050: import java.io.IOException;
051: import java.beans.PropertyChangeListener;
052: import java.beans.PropertyChangeEvent;
053: import java.beans.PropertyChangeSupport;
054: import org.openide.util.Lookup;
055:
056: public class DefaultJavaPlatformProvider implements
057: JavaPlatformProvider, FileChangeListener {
058:
059: private static final String PLATFORM_STORAGE = "Services/Platforms/org-netbeans-modules-gsfpath-api-Platform"; //NOI18N
060:
061: private final PropertyChangeSupport pcs = new PropertyChangeSupport(
062: this );
063: private FileObject storage;
064: private JavaPlatform defaultPlatform;
065:
066: public DefaultJavaPlatformProvider() {
067: storage = Repository.getDefault().getDefaultFileSystem()
068: .findResource(PLATFORM_STORAGE);
069: if (storage == null) {
070: // Turn this off since it can confuse unit tests running w/o layer merging.
071: //assert false : "Cannot find platforms storage";
072: } else {
073: storage.addFileChangeListener(this );
074: }
075: }
076:
077: public JavaPlatform[] getInstalledPlatforms() {
078: List<JavaPlatform> platforms = new ArrayList<JavaPlatform>();
079: if (storage != null) {
080: try {
081: for (FileObject platformDefinition : storage
082: .getChildren()) {
083: DataObject dobj = DataObject
084: .find(platformDefinition);
085: InstanceCookie ic = dobj
086: .getCookie(InstanceCookie.class);
087: if (ic == null) {
088: ErrorManager.getDefault().log(
089: ErrorManager.WARNING,
090: "DefaultPlatformStorage: The file: "
091: + //NOI18N
092: platformDefinition.getNameExt()
093: + " has no InstanceCookie"); //NOI18N
094: continue;
095: } else if (ic instanceof InstanceCookie.Of) {
096: if (((InstanceCookie.Of) ic)
097: .instanceOf(JavaPlatform.class)) {
098: platforms.add((JavaPlatform) ic
099: .instanceCreate());
100: } else {
101: ErrorManager
102: .getDefault()
103: .log(
104: ErrorManager.WARNING,
105: "DefaultPlatformStorage: The file: "
106: + //NOI18N
107: platformDefinition
108: .getNameExt()
109: + " is not an instance of JavaPlatform"); //NOI18N
110: }
111: } else {
112: Object instance = ic.instanceCreate();
113: if (instance instanceof JavaPlatform) {
114: platforms.add((JavaPlatform) instance);
115: } else {
116: ErrorManager
117: .getDefault()
118: .log(
119: ErrorManager.WARNING,
120: "DefaultPlatformStorage: The file: "
121: + //NOI18N
122: platformDefinition
123: .getNameExt()
124: + " is not an instance of JavaPlatform"); //NOI18N
125: }
126: }
127: }
128: } catch (ClassNotFoundException cnf) {
129: ErrorManager.getDefault().notify(cnf);
130: } catch (IOException ioe) {
131: ErrorManager.getDefault().notify(ioe);
132: }
133: }
134: return platforms.toArray(new JavaPlatform[platforms.size()]);
135: }
136:
137: public JavaPlatform getDefaultPlatform() {
138: if (this .defaultPlatform == null) {
139: JavaPlatform[] allPlatforms = this .getInstalledPlatforms();
140: for (int i = 0; i < allPlatforms.length; i++) {
141: if ("default_platform".equals(allPlatforms[i]
142: .getProperties().get("platform.ant.name"))) { //NOI18N
143: defaultPlatform = allPlatforms[i];
144: break;
145: }
146: }
147: }
148: return this .defaultPlatform;
149: }
150:
151: public void addPropertyChangeListener(
152: PropertyChangeListener listener) {
153: pcs.addPropertyChangeListener(listener);
154: }
155:
156: public void removePropertyChangeListener(
157: PropertyChangeListener listener) {
158: pcs.removePropertyChangeListener(listener);
159: }
160:
161: public void fileFolderCreated(FileEvent fe) {
162: }
163:
164: public void fileDataCreated(FileEvent fe) {
165: firePropertyChange();
166: }
167:
168: public void fileChanged(FileEvent fe) {
169: firePropertyChange();
170: }
171:
172: public void fileDeleted(FileEvent fe) {
173: firePropertyChange();
174: }
175:
176: public void fileRenamed(FileRenameEvent fe) {
177: firePropertyChange();
178: }
179:
180: public void fileAttributeChanged(FileAttributeEvent fe) {
181: }
182:
183: private void firePropertyChange() {
184: pcs.firePropertyChange(PROP_INSTALLED_PLATFORMS, null, null);
185: }
186:
187: }
|