KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > ContentTypeSettings


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.content;
12
13 import java.util.List JavaDoc;
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 JavaDoc contentTypeId, String JavaDoc fileSpec, int type) throws CoreException {
28         Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentTypeId);
29         String JavaDoc key = ContentType.getPreferenceKey(type);
30         List JavaDoc existingValues = Util.parseItemsIntoList(contentTypeNode.get(key, null));
31         for (int i = 0; i < existingValues.size(); i++)
32             if (((String JavaDoc) existingValues.get(i)).equalsIgnoreCase(fileSpec))
33                 // don't do anything if already exists
34
return;
35         existingValues.add(fileSpec);
36         // set new preference value
37
String JavaDoc newValue = Util.toListString(existingValues.toArray());
38         ContentType.setPreference(contentTypeNode, key, newValue);
39         try {
40             contentTypeNode.flush();
41         } catch (BackingStoreException bse) {
42             String JavaDoc 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 JavaDoc[] getFileSpecs(IScopeContext context, String JavaDoc contentTypeId, int type) {
49         Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentTypeId);
50         return getFileSpecs(contentTypeNode, type);
51     }
52
53     static String JavaDoc[] getFileSpecs(Preferences contentTypeNode, int type) {
54         String JavaDoc key = ContentType.getPreferenceKey(type);
55         String JavaDoc existing = contentTypeNode.get(key, null);
56         return Util.parseItems(existing);
57     }
58
59     public static String JavaDoc internalGetDefaultProperty(ContentType current, final Preferences contentTypePrefs, final QualifiedName key) throws BackingStoreException {
60         String JavaDoc id = current.getId();
61         if (contentTypePrefs.nodeExists(id)) {
62             Preferences contentTypeNode = contentTypePrefs.node(id);
63             String JavaDoc propertyValue = contentTypeNode.get(key.getLocalName(), null);
64             if (propertyValue != null)
65                 return propertyValue;
66         }
67         // try built-in settings
68
String JavaDoc propertyValue = current.basicGetDefaultProperty(key);
69         if (propertyValue != null)
70             return propertyValue;
71         // try ancestor
72
ContentType baseType = (ContentType) current.getBaseType();
73         return baseType == null ? null : internalGetDefaultProperty(baseType, contentTypePrefs, key);
74     }
75
76     static void removeFileSpec(IScopeContext context, String JavaDoc contentTypeId, String JavaDoc fileSpec, int type) throws CoreException {
77         Preferences contentTypeNode = ContentTypeManager.getInstance().getPreferences(context).node(contentTypeId);
78         String JavaDoc key = ContentType.getPreferenceKey(type);
79         String JavaDoc existing = contentTypeNode.get(key, null);
80         if (existing == null)
81             // content type has no settings - nothing to do
82
return;
83         List JavaDoc 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 JavaDoc) existingValues.get(i)).equalsIgnoreCase(fileSpec))
88                 index = i;
89         if (index == -1)
90             // did not find the file spec to be removed - nothing to do
91
return;
92         existingValues.remove(index);
93         // set new preference value
94
String JavaDoc newValue = Util.toListString(existingValues.toArray());
95         ContentType.setPreference(contentTypeNode, key, newValue);
96         try {
97             contentTypeNode.flush();
98         } catch (BackingStoreException bse) {
99             String JavaDoc 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     /*
111      * @see IContentTypeSettings
112      */

113     public void addFileSpec(String JavaDoc 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 JavaDoc getDefaultCharset() {
122         return getDefaultProperty(IContentDescription.CHARSET);
123     }
124
125     public String JavaDoc getDefaultProperty(final QualifiedName key) {
126         final Preferences contentTypePrefs = ContentTypeManager.getInstance().getPreferences(context);
127         try {
128             String JavaDoc propertyValue = internalGetDefaultProperty(contentType, contentTypePrefs, key);
129             return "".equals(propertyValue) ? null : propertyValue; //$NON-NLS-1$
130
} catch (BackingStoreException e) {
131             return null;
132         }
133     }
134
135     public String JavaDoc[] getFileSpecs(int type) {
136         return getFileSpecs(context, contentType.getId(), type);
137     }
138
139     public String JavaDoc getId() {
140         return contentType.getId();
141     }
142
143     public void removeFileSpec(String JavaDoc fileSpec, int type) throws CoreException {
144         removeFileSpec(context, contentType.getId(), fileSpec, type);
145     }
146
147     public void setDefaultCharset(String JavaDoc 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 JavaDoc 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