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-2007 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:
042: package org.netbeans.modules.visualweb.extension.openide.loaders;
043:
044: import java.awt.Component;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.List;
048: import java.util.Map;
049: import java.util.WeakHashMap;
050: import javax.swing.Action;
051: import javax.swing.JSeparator;
052: import org.openide.ErrorManager;
053: import org.openide.cookies.InstanceCookie;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.Repository;
056: import org.openide.loaders.DataFolder;
057: import org.openide.loaders.FolderInstance;
058:
059: /**
060: * Provides support for retrieving instances from the system filesystem (SFS) folder.
061: * Currently it supports retrieving <code>Action</code> instances.
062: * <p>
063: * Note: It doesn't support the retrieving of instances from the subfolders.
064: * </p>
065: *
066: * @author Peter Zavadsky
067: */
068: public final class SystemFileSystemSupport {
069:
070: /** Inteface defining the action provider. */
071: interface ActionsProvider {
072: Action[] getActions();
073: }
074:
075: /** Dummy impl of <code>ActionsProvider</code> used for the cases
076: * the folder in the system filesystem doesn't exist. */
077: private static ActionsProvider DUMMY_ACTIONS_PROVIDER = new ActionsProvider() {
078: public Action[] getActions() {
079: return new Action[0];
080: }
081: };
082:
083: /** Maps <code>DataFolder</code> to <code>ActionsProvider</code>. */
084: private static final Map dataFolder2actionsProvider = new WeakHashMap();
085:
086: private SystemFileSystemSupport() {
087: }
088:
089: /** Provides the actions retrieved from the specified folder in SFS.
090: * If the specified folder doesn't exist, an empty array is returned.
091: * The <code>null</code> values in the array represent separators.
092: * <p>
093: * Note: It doesn't retrieve the actions from the subfolders.
094: * </p>
095: * @param folderPath specifies the path to the folder in SFS */
096: public static Action[] getActions(String folderPath) {
097: return getActionProvider(folderPath).getActions();
098: }
099:
100: /** Gets <code>ActionProvider</code> for specified folder in SFS.
101: * @param folderPath specifies the path to the folder in SFS */
102: private static ActionsProvider getActionProvider(String folderPath) {
103: DataFolder dataFolder = getDataFolder(folderPath);
104: if (dataFolder == null) {
105: return DUMMY_ACTIONS_PROVIDER;
106: }
107:
108: synchronized (dataFolder2actionsProvider) {
109: ActionsProvider actionsProvider = (ActionsProvider) dataFolder2actionsProvider
110: .get(dataFolder);
111: if (actionsProvider == null) {
112: actionsProvider = new DefaultActionsProvider(dataFolder);
113: dataFolder2actionsProvider.put(dataFolder,
114: actionsProvider);
115: }
116: return actionsProvider;
117: }
118: }
119:
120: private static DataFolder getDataFolder(String folderPath) {
121: FileObject fileObject = Repository.getDefault()
122: .getDefaultFileSystem().findResource(folderPath);
123: if (fileObject == null) {
124: return null;
125: }
126:
127: return DataFolder.findFolder(fileObject);
128: }
129:
130: private static class DefaultActionsProvider extends FolderInstance
131: implements ActionsProvider {
132:
133: public DefaultActionsProvider(DataFolder dataFolder) {
134: super (dataFolder);
135: }
136:
137: /** Gets the action array. */
138: public Action[] getActions() {
139: try {
140: return (Action[]) instanceCreate();
141: } catch (IOException e) {
142: ErrorManager.getDefault().notify(
143: ErrorManager.INFORMATIONAL, e);
144: } catch (ClassNotFoundException e) {
145: ErrorManager.getDefault().notify(
146: ErrorManager.INFORMATIONAL, e);
147: }
148:
149: return new Action[0];
150: }
151:
152: /** Creates the actions. */
153: protected Object createInstance(InstanceCookie[] cookies)
154: throws IOException, ClassNotFoundException {
155: List actions = new ArrayList();
156: for (int i = 0; i < cookies.length; i++) {
157: Class clazz = cookies[i].instanceClass();
158: if (JSeparator.class.isAssignableFrom(clazz)) {
159: // XXX <code>null</code> is interpreted as a separator.
160: actions.add(null);
161: continue;
162: }
163:
164: Object object;
165: try {
166: object = cookies[i].instanceCreate();
167: } catch (IOException e) {
168: ErrorManager.getDefault().notify(
169: ErrorManager.INFORMATIONAL, e);
170: continue;
171: } catch (ClassNotFoundException e) {
172: ErrorManager.getDefault().notify(
173: ErrorManager.INFORMATIONAL, e);
174: continue;
175: }
176:
177: if (object instanceof Action) {
178: actions.add(object);
179: continue;
180: } else {
181: ErrorManager.getDefault().notify(
182: ErrorManager.INFORMATIONAL,
183: new IllegalStateException(
184: "There is an unexpected object="
185: + object
186: + // NOI18N
187: ", in the folder instance="
188: + this )); // NOI18N
189: continue;
190: }
191: }
192:
193: return actions.toArray(new Action[0]);
194: }
195:
196: /** Currently not recursive. */
197: protected InstanceCookie acceptFolder(DataFolder df) {
198: return null;
199: }
200: } // End of DefaultActionsProvider class.
201:
202: }
|