1 11 package org.eclipse.core.internal.content; 12 13 import java.util.List ; 14 import org.eclipse.core.runtime.*; 15 import org.eclipse.core.runtime.content.IContentDescription; 16 import org.eclipse.core.runtime.content.IContentTypeSettings; 17 import org.eclipse.core.runtime.preferences.IScopeContext; 18 import org.eclipse.osgi.util.NLS; 19 import org.osgi.service.prefs.BackingStoreException; 20 import org.osgi.service.prefs.Preferences; 21 22 public class ContentTypeSettings implements IContentTypeSettings, IContentTypeInfo { 23 24 private ContentType contentType; 25 private IScopeContext context; 26 27 static void addFileSpec(IScopeContext context, String contentTypeId, String fileSpec, int type) throws CoreException { 28 Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentTypeId); 29 String key = ContentType.getPreferenceKey(type); 30 List existingValues = Util.parseItemsIntoList(contentTypeNode.get(key, null)); 31 for (int i = 0; i < existingValues.size(); i++) 32 if (((String ) existingValues.get(i)).equalsIgnoreCase(fileSpec)) 33 return; 35 existingValues.add(fileSpec); 36 String newValue = Util.toListString(existingValues.toArray()); 38 ContentType.setPreference(contentTypeNode, key, newValue); 39 try { 40 contentTypeNode.flush(); 41 } catch (BackingStoreException bse) { 42 String message = NLS.bind(ContentMessages.content_errorSavingSettings, contentTypeId); 43 IStatus status = new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, bse); 44 throw new CoreException(status); 45 } 46 } 47 48 static String [] getFileSpecs(IScopeContext context, String contentTypeId, int type) { 49 Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentTypeId); 50 return getFileSpecs(contentTypeNode, type); 51 } 52 53 static String [] getFileSpecs(Preferences contentTypeNode, int type) { 54 String key = ContentType.getPreferenceKey(type); 55 String existing = contentTypeNode.get(key, null); 56 return Util.parseItems(existing); 57 } 58 59 public static String internalGetDefaultProperty(ContentType current, final Preferences contentTypePrefs, final QualifiedName key) throws BackingStoreException { 60 String id = current.getId(); 61 if (contentTypePrefs.nodeExists(id)) { 62 Preferences contentTypeNode = contentTypePrefs.node(id); 63 String propertyValue = contentTypeNode.get(key.getLocalName(), null); 64 if (propertyValue != null) 65 return propertyValue; 66 } 67 String propertyValue = current.basicGetDefaultProperty(key); 69 if (propertyValue != null) 70 return propertyValue; 71 ContentType baseType = (ContentType) current.getBaseType(); 73 return baseType == null ? null : internalGetDefaultProperty(baseType, contentTypePrefs, key); 74 } 75 76 static void removeFileSpec(IScopeContext context, String contentTypeId, String fileSpec, int type) throws CoreException { 77 Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentTypeId); 78 String key = ContentType.getPreferenceKey(type); 79 String existing = contentTypeNode.get(key, null); 80 if (existing == null) 81 return; 83 List existingValues = Util.parseItemsIntoList(contentTypeNode.get(key, null)); 84 int index = -1; 85 int existingCount = existingValues.size(); 86 for (int i = 0; index == -1 && i < existingCount; i++) 87 if (((String ) existingValues.get(i)).equalsIgnoreCase(fileSpec)) 88 index = i; 89 if (index == -1) 90 return; 92 existingValues.remove(index); 93 String newValue = Util.toListString(existingValues.toArray()); 95 ContentType.setPreference(contentTypeNode, key, newValue); 96 try { 97 contentTypeNode.flush(); 98 } catch (BackingStoreException bse) { 99 String message = NLS.bind(ContentMessages.content_errorSavingSettings, contentTypeId); 100 IStatus status = new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, bse); 101 throw new CoreException(status); 102 } 103 } 104 105 public ContentTypeSettings(ContentType contentType, IScopeContext context) { 106 this.context = context; 107 this.contentType = contentType; 108 } 109 110 113 public void addFileSpec(String fileSpec, int type) throws CoreException { 114 addFileSpec(context, contentType.getId(), fileSpec, type); 115 } 116 117 public ContentType getContentType() { 118 return contentType; 119 } 120 121 public String getDefaultCharset() { 122 return getDefaultProperty(IContentDescription.CHARSET); 123 } 124 125 public String getDefaultProperty(final QualifiedName key) { 126 final Preferences contentTypePrefs = ContentTypeManager.getInstance().getPreferences(context); 127 try { 128 String propertyValue = internalGetDefaultProperty(contentType, contentTypePrefs, key); 129 return "".equals(propertyValue) ? null : propertyValue; } catch (BackingStoreException e) { 131 return null; 132 } 133 } 134 135 public String [] getFileSpecs(int type) { 136 return getFileSpecs(context, contentType.getId(), type); 137 } 138 139 public String getId() { 140 return contentType.getId(); 141 } 142 143 public void removeFileSpec(String fileSpec, int type) throws CoreException { 144 removeFileSpec(context, contentType.getId(), fileSpec, type); 145 } 146 147 public void setDefaultCharset(String userCharset) throws CoreException { 148 Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentType.getId()); 149 ContentType.setPreference(contentTypeNode, ContentType.PREF_DEFAULT_CHARSET, userCharset); 150 try { 151 contentTypeNode.flush(); 152 } catch (BackingStoreException bse) { 153 String message = NLS.bind(ContentMessages.content_errorSavingSettings, contentType.getId()); 154 IStatus status = new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, bse); 155 throw new CoreException(status); 156 } 157 } 158 159 } 160 | Popular Tags |