1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.StringTokenizer ; 29 import javax.swing.SwingUtilities ; 30 import org.netbeans.editor.BaseKit; 31 import org.netbeans.editor.Settings; 32 import org.openide.NotifyDescriptor; 33 import org.openide.cookies.InstanceCookie; 34 import org.openide.filesystems.FileChangeAdapter; 35 import org.openide.filesystems.FileChangeListener; 36 import org.openide.filesystems.FileEvent; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.Repository; 39 import org.openide.loaders.DataFolder; 40 import org.openide.loaders.DataObject; 41 import org.openide.loaders.DataObjectNotFoundException; 42 import org.openide.options.SystemOption; 43 import org.openide.util.NbBundle; 44 45 56 public class AllOptionsFolder{ 57 58 59 public static final String FOLDER = "Editors"; public static final String OPTION_FILE_NAME = "Settings.settings"; 62 63 private static AllOptionsFolder settingsFolder; 64 65 private static boolean baseInitialized = false; 66 67 private static DataFolder folder; 68 private static MIMEOptionFolder mimeFolder; 69 70 private static Map installedOptions = new Hashtable (); 72 73 74 private static FileChangeListener moduleRegListener; 75 76 77 78 private AllOptionsFolder(DataFolder fld) { 79 folder = fld; 80 } 81 82 83 public MIMEOptionFolder getMIMEFolder(){ 84 synchronized (Settings.class){ 85 if (mimeFolder!=null) return mimeFolder; 86 87 FileObject f = Repository.getDefault().getDefaultFileSystem(). 88 findResource(FOLDER+"/text/"+BaseOptions.BASE); 90 if (f==null){ 92 FileObject fo = Repository.getDefault().getDefaultFileSystem(). 93 findResource(AllOptionsFolder.FOLDER); 94 String fName = "text/"+BaseOptions.BASE; 96 if (fo != null){ 97 try{ 98 StringTokenizer stok = new StringTokenizer (fName,"/"); while (stok.hasMoreElements()) { 100 String newFolder = stok.nextToken(); 101 if (fo.getFileObject(newFolder) == null) 102 fo = fo.createFolder(newFolder); 103 else 104 fo = fo.getFileObject(newFolder); 105 } 106 }catch(IOException ioe){ 107 ioe.printStackTrace(); 108 } 109 110 f = Repository.getDefault().getDefaultFileSystem(). 111 findResource(AllOptionsFolder.FOLDER+"/text/"+BaseOptions.BASE); } 113 } 114 115 if (f != null) { 116 try { 117 DataObject d = DataObject.find(f); 118 DataFolder df = (DataFolder)d.getCookie(DataFolder.class); 119 if (df != null) { 120 mimeFolder = new MIMEOptionFolder(df, getBase()); 121 return mimeFolder; 122 } 123 } catch (org.openide.loaders.DataObjectNotFoundException ex) { 124 ex.printStackTrace(); 125 } 126 } 127 128 return null; 129 } 130 } 131 132 133 public List getInstalledOptions(){ 134 135 137 List retList = new ArrayList (); 138 String [] MIMES = new String [] {"text", "application"}; for (int in = 0; in<MIMES.length; in++) { 140 FileObject mainFolderFO = Repository.getDefault().getDefaultFileSystem(). 141 findResource(AllOptionsFolder.FOLDER+"/" + MIMES[in]); if (mainFolderFO != null){ 143 DataFolder mainFolder = DataFolder.findFolder(mainFolderFO); 144 if (mainFolder != null){ 145 DataObject subFolders[] = mainFolder.getChildren(); 146 for (int i=0; i<subFolders.length; i++){ 147 if (!(subFolders[i] instanceof DataFolder)) continue; 148 DataFolder subFolder = (DataFolder) subFolders[i]; 149 FileObject optionInstance = Repository.getDefault().getDefaultFileSystem(). 150 findResource(subFolder.getPrimaryFile().getPath()+"/"+AllOptionsFolder.OPTION_FILE_NAME); if (optionInstance == null) continue; 152 try{ 153 DataObject optionDO = DataObject.find(optionInstance); 154 if (optionDO == null) continue; 155 InstanceCookie ic = (InstanceCookie)optionDO.getCookie(InstanceCookie.class); 156 if (ic == null) continue; 157 BaseOptions bo = AllOptionsFolder.getDefault().getBO(ic); 158 if (bo == null) continue; 159 retList.add(bo.getClass()); 160 }catch(DataObjectNotFoundException donf){ 161 donf.printStackTrace(); 162 } 163 } 164 } 165 } 166 } 167 168 AllOptions allOptions 170 = (AllOptions)AllOptions.findObject(AllOptions.class, true); 171 172 if (allOptions == null) return retList; 173 174 SystemOption[] sos = allOptions.getOptions(); 175 if (sos == null) return retList; 176 177 for (int i=0; i<sos.length; i++){ 178 179 if (!(sos[i] instanceof BaseOptions)) continue; 180 181 BaseOptions bo = (BaseOptions) sos[i]; 182 if (retList.contains(bo.getClass())) retList.remove(bo.getClass()); 183 if (BaseKit.getKit(bo.getKitClass()).getContentType() != null){ 184 retList.add(bo.getClass()); 185 processInitializers(bo, false); 186 }else{ 187 final String kitClazz = bo.getKitClass().toString(); 188 SwingUtilities.invokeLater( 189 new Runnable () { 190 public void run() { 191 NotifyDescriptor msg = new NotifyDescriptor.Message( 192 193 NbBundle.getMessage( AllOptionsFolder.class, "ERR_NoContentTypeDefined", kitClazz), 194 NotifyDescriptor.WARNING_MESSAGE 195 ); 196 197 org.openide.DialogDisplayer.getDefault().notify(msg); 198 } 199 } 200 ); 201 } 202 } 203 204 return retList; 205 } 206 207 public static void unregisterModuleRegListener(){ 208 FileObject moduleRegistry = Repository.getDefault().getDefaultFileSystem().findResource("Modules"); 210 if (moduleRegistry !=null){ if (moduleRegListener!=null) 212 moduleRegistry.removeFileChangeListener(moduleRegListener); 213 } 214 } 215 216 217 public static AllOptionsFolder getDefault(){ 218 synchronized (Settings.class) { 219 if (settingsFolder!=null) return settingsFolder; 221 org.openide.filesystems.FileObject f = Repository.getDefault().getDefaultFileSystem(). 222 findResource(FOLDER); 223 if (f==null) return null; 224 225 DataFolder df = DataFolder.findFolder(f); 226 if (df == null) { 227 } else { 228 if (settingsFolder == null){ 229 settingsFolder = new AllOptionsFolder(df); 230 231 if(moduleRegListener == null) { 233 moduleRegListener = new FileChangeAdapter() { 234 public void fileChanged(FileEvent fe){ 235 updateOptions(); 236 } 237 }; 238 239 FileObject moduleRegistry = Repository.getDefault().getDefaultFileSystem().findResource("Modules"); 241 if (moduleRegistry !=null){ moduleRegistry.addFileChangeListener(moduleRegListener); 243 } 244 } 245 246 return settingsFolder; 247 } 248 } 249 return null; 250 } 251 } 252 253 254 public List getKeyBindingList() { 255 return getBase().getKeyBindingList(); 256 } 257 258 259 public void setKeyBindingList(List list) { 260 getBase().setKeyBindingList(list); 261 } 262 263 public boolean isToolbarVisible() { 264 return getBase().isToolbarVisible(); 265 } 266 267 public void setToolbarVisible(boolean toolbarVisible) { 268 getBase().setToolbarVisible(toolbarVisible); 269 } 270 271 public boolean getLineNumberVisible(){ 272 return getBase().getLineNumberVisible(); 273 } 274 275 public void setLineNumberVisible(boolean lineVisible) { 276 getBase().setLineNumberVisible(lineVisible); 277 } 278 279 public boolean isTextAntialiasing() { 280 return getBase().isTextAntialiasing(); 281 } 282 283 public void setTextAntialiasing(boolean textAntialiasing) { 284 getBase().setTextAntialiasing(textAntialiasing); 285 } 286 287 289 protected void loadDefaultKeyBindings(){ 290 getBase().getKeyBindingList(); 291 } 292 293 294 private static Class uninstallOption(){ 295 List updatedInstalledOptions = AllOptionsFolder.getDefault().getInstalledOptions(); 296 synchronized (Settings.class){ 297 Iterator i = installedOptions.keySet().iterator(); 298 while (i.hasNext()){ 299 Object obj = i.next(); 300 if(obj instanceof Class ){ 301 if (!updatedInstalledOptions.contains(obj)){ 302 installedOptions.remove(obj); 303 return (Class )obj; 304 } 305 } 306 } 307 return null; 308 } 309 } 310 311 private static void updateOptions(){ 312 uninstallOption(); 313 List installedOpts = new ArrayList (installedOptions.values()); 314 Iterator i = installedOpts.iterator(); 315 while (i.hasNext()){ 316 Object obj = i.next(); 317 if (obj instanceof BaseOptions){ 318 BaseOptions bo = (BaseOptions)obj; 319 if (bo != null){ 320 bo.initPopupMenuItems(); 321 } 322 } 323 } 324 } 325 326 327 public boolean baseInitialized(){ 328 return baseInitialized; 329 } 330 331 333 private BaseOptions getBase(){ 334 335 BaseOptions ret = (BaseOptions)BaseOptions.findObject(BaseOptions.class, true); 336 337 synchronized (Settings.class){ 338 if (baseInitialized == false){ 339 Settings.addInitializer(ret.getSettingsInitializer(), 341 Settings.OPTION_LEVEL); 342 baseInitialized = true; 343 Settings.reset(); 344 } 345 } 346 347 return ret; 348 } 349 350 351 protected BaseOptions getBO(InstanceCookie ic){ 352 initInstance(ic); 353 BaseOptions ret = null; 354 try{ 355 synchronized (Settings.class){ 356 ret = (installedOptions.get(ic.instanceClass()) instanceof BaseOptions) ? (BaseOptions) installedOptions.get(ic.instanceClass()) 357 : null; 358 } 359 }catch(ClassNotFoundException cnfe){ 360 cnfe.printStackTrace(); 361 362 }catch(IOException ioex){ 363 ioex.printStackTrace(); 364 } 365 return ret; 366 } 367 368 369 private void initInstance(InstanceCookie ic){ 370 try{ 371 Object optionObj; 372 synchronized (Settings.class){ 373 if (installedOptions.containsKey(ic.instanceClass())) { 374 return; 375 } 376 optionObj = ic.instanceCreate(); 377 if (!(optionObj instanceof BaseOptions)) return; 378 installedOptions.put(ic.instanceClass(), (BaseOptions)optionObj); 379 } 380 processInitializers((BaseOptions)optionObj, false); 381 }catch(ClassNotFoundException cnfe){ 382 cnfe.printStackTrace(); 383 }catch(IOException ioex){ 384 ioex.printStackTrace(); 385 } 386 } 387 388 395 public void loadMIMEOption(Class kitClass){ 396 loadMIMEOption(kitClass, true); 397 } 398 399 411 public void loadMIMEOption(Class kitClass, boolean processOldTypeOption){ 412 String contentType = BaseKit.getKit(kitClass).getContentType(); 413 if (contentType == null) return; 414 FileObject optionFO = Repository.getDefault().getDefaultFileSystem(). 415 findResource(FOLDER+"/"+contentType+"/"+OPTION_FILE_NAME); if (optionFO == null) { 417 if (processOldTypeOption){ 421 BaseOptions oldBO = BaseOptions.getOptions(kitClass); 422 if (oldBO != null){ 423 boolean process = false; 424 synchronized (Settings.class){ 425 if (!installedOptions.containsKey(kitClass)){ 426 installedOptions.put(kitClass, oldBO); 427 process = true; 428 } 429 } 430 if (process){ 431 processInitializers(oldBO, false); 432 } 433 } 434 } 435 return; 436 } 437 438 try{ 439 DataObject optionDO = DataObject.find(optionFO); 440 if (optionDO == null) return; 441 442 InstanceCookie ic = (InstanceCookie)optionDO.getCookie(InstanceCookie.class); 443 if (ic == null) return; 444 445 initInstance(ic); 446 447 }catch(DataObjectNotFoundException donf){ 448 donf.printStackTrace(); 449 } 450 } 451 452 454 private void processInitializers(BaseOptions bo, boolean remove) { 455 synchronized (Settings.class){ 457 Settings.Initializer si = bo.getSettingsInitializer(); 458 Settings.removeInitializer(si.getName()); 460 if (!remove) { Settings.addInitializer(si, Settings.OPTION_LEVEL); 462 } 463 464 bo.loadXMLSettings(); 466 467 bo.initPopupMenuItems(); 469 470 472 Settings.reset(); 473 } 474 } 476 477 } 478 | Popular Tags |