diff options
author | teicher | 2004-12-17 15:12:50 +0000 |
---|---|---|
committer | teicher | 2004-12-17 15:12:50 +0000 |
commit | 7d60c2dba1a964e7fa48d5a1de875a4d20194685 (patch) | |
tree | c3d823cc339d9c94afd2c1b6e6b48c273002277a /org.eclipse.jface.text/src/org/eclipse/jface/text/templates | |
parent | 8c3080d6ef17a3d9dd0db5f71790e52dcbe33460 (diff) | |
download | eclipse.platform.text-7d60c2dba1a964e7fa48d5a1de875a4d20194685.tar.gz eclipse.platform.text-7d60c2dba1a964e7fa48d5a1de875a4d20194685.tar.xz eclipse.platform.text-7d60c2dba1a964e7fa48d5a1de875a4d20194685.zip |
- project specific templates
- template access by id (bug 75806)
Diffstat (limited to 'org.eclipse.jface.text/src/org/eclipse/jface/text/templates')
2 files changed, 50 insertions, 3 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateReaderWriter.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateReaderWriter.java index c8e15ce5f19..7564f54cd29 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateReaderWriter.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateReaderWriter.java @@ -81,6 +81,25 @@ public class TemplateReaderWriter { } /** + * Reads the template with identifier <code>id</code> from a reader and + * returns it. The reader must present a serialized form as produced by the + * <code>save</code> method. + * + * @param reader the reader to read templates from + * @param id the id of the template to return + * @return the read template, encapsulated in an instances of + * <code>TemplatePersistenceData</code> + * @throws IOException if reading from the stream fails + * @since 3.1 + */ + public TemplatePersistenceData readSingle(Reader reader, String id) throws IOException { + TemplatePersistenceData[] datas= read(new InputSource(reader), null, id); + if (datas.length > 0) + return datas[0]; + return null; + } + + /** * Reads templates from a stream and adds them to the templates. * * @param reader the reader to read templates from @@ -89,7 +108,7 @@ public class TemplateReaderWriter { * @throws IOException if reading from the stream fails */ public TemplatePersistenceData[] read(Reader reader, ResourceBundle bundle) throws IOException { - return read(new InputSource(reader), bundle); + return read(new InputSource(reader), bundle, null); } /** @@ -101,7 +120,7 @@ public class TemplateReaderWriter { * @throws IOException if reading from the stream fails */ public TemplatePersistenceData[] read(InputStream stream, ResourceBundle bundle) throws IOException { - return read(new InputSource(stream), bundle); + return read(new InputSource(stream), bundle, null); } /** @@ -109,10 +128,11 @@ public class TemplateReaderWriter { * * @param source the input source * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur + * @param singleId the template id to extract, or <code>null</code> to read in all templates * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> * @throws IOException if reading from the stream fails */ - private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle) throws IOException { + private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle, String singleId) throws IOException { try { Collection templates= new ArrayList(); Set ids= new HashSet(); @@ -135,6 +155,9 @@ public class TemplateReaderWriter { if (id != null && ids.contains(id)) throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.duplicate.id")); //$NON-NLS-1$ + if (singleId != null && !singleId.equals(id)) + continue; + boolean deleted = getBooleanValue(attributes, DELETED_ATTRIBUTE, false); String name= getStringValue(attributes, NAME_ATTRIBUTE); @@ -165,6 +188,9 @@ public class TemplateReaderWriter { data.setDeleted(deleted); templates.add(data); + + if (singleId != null && singleId.equals(id)) + break; } return (TemplatePersistenceData[]) templates.toArray(new TemplatePersistenceData[templates.size()]); diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java index c1d3dee22e6..619433f5302 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java @@ -268,6 +268,27 @@ public class TemplateStore { } /** + * Returns the first enabled template that matches the given template id. + * + * @param id the id of the template searched for + * @return the first enabled template that matches id, or <code>null</code> if none is found + * @since 3.1 + */ + public Template findTemplateById(String id) { + Assert.isNotNull(id); + + for (Iterator it= fTemplates.iterator(); it.hasNext();) { + TemplatePersistenceData data= (TemplatePersistenceData) it.next(); + if (data.isEnabled() && !data.isDeleted() + && id.equals(data.getId())) + return data.getTemplate(); + } + + return null; + + } + + /** * Returns all template datas. * * @param includeDeleted whether to include deleted datas |