1 19 20 21 package org.netbeans.modules.i18n.java; 22 23 24 import java.io.IOException ; 25 26 import org.netbeans.modules.i18n.ResourceHolder; 27 28 import org.netbeans.modules.properties.BundleStructure; 29 import org.netbeans.modules.properties.Element; 30 import org.netbeans.modules.properties.PropertiesDataObject; 31 import org.netbeans.modules.properties.PropertiesStructure; 32 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileSystem; 35 import org.openide.filesystems.Repository; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 39 44 public class JavaResourceHolder extends ResourceHolder { 45 46 private String selectedLocale; 47 48 49 public JavaResourceHolder() { 50 super(new Class [] {PropertiesDataObject.class}); 51 } 52 53 60 public void setLocalization(String locale) { 61 selectedLocale = locale; 62 } 63 64 private String getLocalizationFileName() { 65 return selectedLocale != null && !selectedLocale.equals("") ? resource.getName() + selectedLocale : resource.getName(); 67 } 68 69 71 public String [] getAllKeys() { 72 if(resource == null) 73 return new String [0]; 74 75 return ((PropertiesDataObject)resource).getBundleStructure().getKeys(); 76 } 77 78 81 public String findFreeKey(String keySpec) { 82 BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure(); 83 return bundleStructure != null ? bundleStructure.findFreeKey(keySpec) : null; 84 } 85 86 88 public String getValueForKey(String key) { 89 if(resource == null) 90 return null; 91 92 Element.ItemElem item = getItem(key); 93 return item == null ? null : item.getValue(); 94 } 95 96 98 public String getCommentForKey(String key) { 99 if(resource == null) 100 return null; 101 102 Element.ItemElem item = getItem(key); 103 return item == null ? null : item.getComment(); 104 } 105 106 107 private Element.ItemElem getItem(String key) { 108 BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure(); 109 if (bundleStructure == null) 110 return null; 111 112 return bundleStructure.getItem(getLocalizationFileName(), key); 113 } 114 115 118 public Object getAllData(String key) { 119 BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure(); 120 return bundleStructure != null ? bundleStructure.getAllData(key) : null; 121 } 122 123 126 public void setAllData(String key, Object data) { 127 BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure(); 128 if (bundleStructure != null) 129 bundleStructure.setAllData(key, (String [])data); 130 } 131 132 138 public void addProperty(Object key, Object value, String comment, boolean forceNewValue) { 139 if(resource == null || key == null) return; 140 141 String keyValue = key.toString(); 142 String valueValue = value == null ? "" : value.toString(); String commentValue = comment; 144 145 BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure(); 147 if (bundleStructure != null) { 148 bundleStructure.addItem(getLocalizationFileName(), 149 keyValue, valueValue, commentValue, 150 forceNewValue); 151 } 152 } 153 154 157 public void removeProperty(Object key) { 158 BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure(); 159 if (bundleStructure != null) 160 bundleStructure.removeItem(key.toString()); 161 } 162 163 165 protected DataObject createTemplate(Class clazz) throws IOException { 166 return getTemplate(); 167 } 168 169 172 public static DataObject getTemplate() throws IOException { 173 FileSystem defaultFS = Repository.getDefault().getDefaultFileSystem(); 174 175 FileObject fileObject = defaultFS.findResource("Templates/Other/properties.properties"); 177 if(fileObject == null) 178 throw new IOException (Util.getString("EXC_TemplateNotFound")); 179 180 try { 181 return DataObject.find(fileObject); 182 } catch(DataObjectNotFoundException e) { 183 throw new IOException (Util.getString("EXC_TemplateNotFound")); 184 } 185 } 186 } 187 | Popular Tags |