1 19 20 21 package org.netbeans.modules.web.jsf.navigation.graph.actions; 22 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.WeakHashMap ; 28 import javax.swing.Action ; 29 import javax.swing.JSeparator ; 30 import org.openide.ErrorManager; 31 import org.openide.cookies.InstanceCookie; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.Repository; 34 import org.openide.loaders.DataFolder; 35 import org.openide.loaders.FolderInstance; 36 37 38 47 public final class SystemFileSystemSupport { 48 49 50 interface ActionsProvider { 51 Action [] getActions(); 52 } 53 54 56 private static ActionsProvider DUMMY_ACTIONS_PROVIDER = new ActionsProvider() { 57 public Action [] getActions() { 58 return new Action [0]; 59 } 60 }; 61 62 63 private static final Map dataFolder2actionsProvider = new WeakHashMap (); 64 65 66 private SystemFileSystemSupport() { 67 } 68 69 70 78 public static Action [] getActions(String folderPath) { 79 return getActionProvider(folderPath).getActions(); 80 } 81 82 84 private static ActionsProvider getActionProvider(String folderPath) { 85 DataFolder dataFolder = getDataFolder(folderPath); 86 if (dataFolder == null) { 87 return DUMMY_ACTIONS_PROVIDER; 88 } 89 90 synchronized (dataFolder2actionsProvider) { 91 ActionsProvider actionsProvider = (ActionsProvider)dataFolder2actionsProvider.get(dataFolder); 92 if (actionsProvider == null) { 93 actionsProvider = new DefaultActionsProvider(dataFolder); 94 dataFolder2actionsProvider.put(dataFolder, actionsProvider); 95 } 96 return actionsProvider; 97 } 98 } 99 100 101 private static DataFolder getDataFolder(String folderPath) { 102 FileObject fileObject = Repository.getDefault().getDefaultFileSystem().findResource(folderPath); 103 if (fileObject == null) { 104 return null; 105 } 106 107 return DataFolder.findFolder(fileObject); 108 } 109 110 111 private static class DefaultActionsProvider extends FolderInstance implements ActionsProvider { 112 113 public DefaultActionsProvider(DataFolder dataFolder) { 114 super(dataFolder); 115 } 116 117 118 public Action [] getActions() { 119 try { 120 return (Action [])instanceCreate(); 121 } catch (IOException e) { 122 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 123 } catch (ClassNotFoundException e) { 124 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 125 } 126 127 return new Action [0]; 128 } 129 130 131 protected Object createInstance(InstanceCookie[] cookies) 132 throws IOException , ClassNotFoundException { 133 List actions = new ArrayList (); 134 for (int i = 0; i < cookies.length; i++) { 135 Class clazz = cookies[i].instanceClass(); 136 if (JSeparator .class.isAssignableFrom(clazz)) { 137 actions.add(null); 139 continue; 140 } 141 142 Object object; 143 try { 144 object = cookies[i].instanceCreate(); 145 } catch (IOException e) { 146 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 147 continue; 148 } catch (ClassNotFoundException e) { 149 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 150 continue; 151 } 152 153 if (object instanceof Action ) { 154 actions.add(object); 155 continue; 156 } else { 157 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 158 new IllegalStateException ("There is an unexpected object=" + object + ", in the folder instance=" + this)); continue; 161 } 162 } 163 164 return actions.toArray(new Action [0]); 165 } 166 167 168 protected InstanceCookie acceptFolder(DataFolder df) { 169 return null; 170 } 171 } 173 } 174 175 | Popular Tags |