1 19 package org.netbeans.modules.gsf; 20 21 import java.io.IOException ; 22 import java.text.DateFormat ; 23 import java.util.Date ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import org.netbeans.api.java.classpath.ClassPath; 28 29 import org.netbeans.modules.gsf.Language; 30 import org.openide.ErrorManager; 31 import org.openide.actions.CopyAction; 32 import org.openide.actions.CutAction; 33 import org.openide.actions.DeleteAction; 34 import org.openide.actions.OpenAction; 35 import org.openide.actions.PasteAction; 36 import org.openide.actions.PropertiesAction; 37 import org.openide.actions.RenameAction; 38 import org.openide.actions.SaveAsTemplateAction; 39 import org.openide.actions.ToolsAction; 40 import org.openide.filesystems.FileObject; 41 import org.openide.loaders.DataObjectExistsException; 42 import org.openide.loaders.ExtensionList; 43 import org.openide.loaders.FileEntry; 44 import org.openide.loaders.MultiDataObject; 45 import org.openide.loaders.UniFileLoader; 46 import org.openide.util.Lookup; 47 import org.openide.util.MapFormat; 48 import org.openide.util.NbBundle; 49 import org.openide.util.actions.SystemAction; 50 51 52 57 public class GsfDataLoader extends UniFileLoader { 58 boolean initialized; 59 60 64 public GsfDataLoader() { 65 super(GsfDataObject.class.getName()); 66 } 67 68 @Override 69 protected void initialize() { 70 super.initialize(); 71 72 ExtensionList list = getExtensions(); 73 74 for (Language language : LanguageRegistry.getInstance()) { 75 list.addMimeType(language.getMimeType()); 76 77 String [] extensions = language.getExtensions(); 78 79 for (int i = 0; i < extensions.length; i++) { 80 assert extensions[i].charAt(0) != '.' : "Extensions should exclude the ."; 81 list.addExtension(extensions[i]); 82 } 83 } 84 85 initialized = true; 86 } 87 88 @Override 89 protected MultiDataObject createMultiObject(FileObject primaryFile) 90 throws DataObjectExistsException, IOException { 91 Language language = 92 LanguageRegistry.getInstance().getLanguageByMimeType(primaryFile.getMIMEType()); 93 94 return new GsfDataObject(primaryFile, this, language); 95 } 96 97 @Override 98 protected String defaultDisplayName() { 99 StringBuilder sb = new StringBuilder (); 101 102 for (Language language : LanguageRegistry.getInstance()) { 103 if (sb.length() > 0) { 104 sb.append(", "); 105 } 106 107 sb.append(language.getDisplayName()); 108 } 109 110 return NbBundle.getMessage(GsfDataLoader.class, "GenericLoaderName", sb.toString()); 111 } 112 113 @Override 114 protected SystemAction[] defaultActions() { 115 return new SystemAction[] { 116 SystemAction.get(OpenAction.class), 117 null, 118 SystemAction.get(CutAction.class), 119 SystemAction.get(CopyAction.class), 120 SystemAction.get(PasteAction.class), 121 null, 122 SystemAction.get(DeleteAction.class), 123 SystemAction.get(RenameAction.class), 124 null, 125 SystemAction.get(SaveAsTemplateAction.class), 126 null, 127 SystemAction.get(ToolsAction.class), 128 SystemAction.get(PropertiesAction.class), 129 }; 130 } 131 132 @Override 133 protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) { 134 FileEntry.Format entry = new FileEntry.Format(obj, primaryFile) { 135 protected java.text.Format createFormat (FileObject target, String n, String e) { 136 ClassPath cp = ClassPath.getClassPath(target, ClassPath.SOURCE); 137 String resourcePath = ""; 138 if (cp != null) { 139 resourcePath = cp.getResourceName(target); 140 } else { 141 ErrorManager.getDefault().log(ErrorManager.WARNING, "No classpath was found for folder: "+target); 142 } 143 Map m = new HashMap (); 144 m.put("NAME", n ); String capitalizedName; 146 if (n.length() > 1) { 147 capitalizedName = Character.toUpperCase(n.charAt(0))+n.substring(1); 148 } else { 149 capitalizedName = ""+Character.toUpperCase(n.charAt(0)); 150 } 151 m.put("CAPITALIZEDNAME", capitalizedName); m.put("LOWERNAME", n.toLowerCase()); m.put("UPPERNAME", n.toUpperCase()); String packageName = resourcePath.replace('/', '.'); 156 m.put("PACKAGE", packageName); String capitalizedPkgName; 158 if (packageName == null || packageName.length() == 0) { 159 packageName = ""; 160 capitalizedPkgName = ""; 161 } else if (packageName.length() > 1) { 162 capitalizedPkgName = Character.toUpperCase(packageName.charAt(0))+packageName.substring(1); 163 } else { 164 capitalizedPkgName = ""+Character.toUpperCase(packageName.charAt(0)); 165 } 166 m.put("CAPITALIZEDPACKAGE", capitalizedPkgName); m.put("PACKAGE_SLASHES", resourcePath); if (target.isRoot ()) { 170 m.put ("PACKAGE_AND_NAME", n); m.put ("PACKAGE_AND_NAME_SLASHES", n); } else { 173 m.put ("PACKAGE_AND_NAME", resourcePath.replace('/', '.') + '.' + n); m.put ("PACKAGE_AND_NAME_SLASHES", resourcePath + '/' + n); } 176 m.put("DATE", DateFormat.getDateInstance(DateFormat.LONG).format(new Date ())); m.put("TIME", DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date ())); MapFormat f = new MapFormat(m); 179 f.setLeftBrace( "__" ); f.setRightBrace( "__" ); 182 return f; 183 } 184 }; 185 return entry; 186 } 187 } 188 | Popular Tags |