/** * Adds a template encapsulated in its persistent form. * * @param data the template to add */ public void add(TemplatePersistenceData data) { if (!validateTemplate(data.getTemplate())) return; if (data.isUserAdded()) { fTemplates.add(data); } else { for (TemplatePersistenceData persistenceData : fTemplates) { if (persistenceData.getId() != null && persistenceData.getId().equals(data.getId())) { persistenceData.setTemplate(data.getTemplate()); persistenceData.setDeleted(data.isDeleted()); persistenceData.setEnabled(data.isEnabled()); return; } } // add an id which is not contributed as add-on if (data.getTemplate() != null) { TemplatePersistenceData newData= new TemplatePersistenceData(data.getTemplate(), data.isEnabled(), data.getId()); fTemplates.add(newData); } } }
/** * Adds a template to the internal store. The added templates must have * a unique id. * * @param data the template data to add */ protected void internalAdd(TemplatePersistenceData data) { if (!data.isCustom()) { // check if the added template is not a duplicate id String id= data.getId(); for (TemplatePersistenceData persistenceData : fTemplates) { if (persistenceData.getId() != null && persistenceData.getId().equals(id)) return; } fTemplates.add(data); } }
/** * Returns whether the receiver represents a modified template, i.e. a * contributed template that has been changed. * * @return <code>true</code> if the contained template is contributed but has been modified, <code>false</code> otherwise */ public boolean isModified() { return isCustom() && !isUserAdded(); }
/** * In some cases, we must continue to respect the deprecated TemplatePresistenceData * even though we are given {@link org.eclipse.text.templates.TemplatePersistenceData}. * * @param data The {@link org.eclipse.text.templates.TemplatePersistenceData} that will * underlie this object. * @since 3.14 */ public TemplatePersistenceData(org.eclipse.text.templates.TemplatePersistenceData data) { super(data.getTemplate(), data.isEnabled(), data.getId()); // these are ignored this.ref= data instanceof TemplatePersistenceData ? ((TemplatePersistenceData) data).ref : data; // no double wrapping }
/** * Restores all contributed templates that have been deleted. */ public void restoreDeleted() { for (TemplatePersistenceData data : fTemplates) { if (data.isDeleted()) data.setDeleted(false); } }
public void load() throws IOException { if (fProjectStore != null) { fProjectStore.load(); Set<String> datas= new HashSet<>(); TemplatePersistenceData[] data= fProjectStore.getTemplateData(false); for (int i= 0; i < data.length; i++) { datas.add(data[i].getId()); } data= fInstanceStore.getTemplateData(false); for (int i= 0; i < data.length; i++) { TemplatePersistenceData orig= data[i]; if (!datas.contains(orig.getId())) { TemplatePersistenceData copy= new TemplatePersistenceData(new Template(orig.getTemplate()), orig.isEnabled(), orig.getId()); fProjectStore.add(copy); copy.setDeleted(true); } } } }
private void edit(TemplatePersistenceData data) { Template oldTemplate= data.getTemplate(); Template newTemplate= editTemplate(new Template(oldTemplate), true, true); if (newTemplate != null) { if (!newTemplate.getName().equals(oldTemplate.getName()) && MessageDialog.openQuestion(getShell(), TemplatesMessages.TemplatePreferencePage_question_create_new_title, TemplatesMessages.TemplatePreferencePage_question_create_new_message)) { data= new TemplatePersistenceData(newTemplate, true); fTemplateStore.add(data); fTableViewer.refresh(); } else { data.setTemplate(newTemplate); fTableViewer.refresh(data); } selectionChanged1(); fTableViewer.setChecked(data, data.isEnabled()); fTableViewer.setSelection(new StructuredSelection(data)); } }
private void updateTemplate(TemplatePersistenceData data) { TemplatePersistenceData[] datas= fTemplateStore.getTemplateData(); for (int i= 0; i < datas.length; i++) { String id= datas[i].getId(); if (id != null && id.equals(data.getId())) { datas[i].setTemplate(data.getTemplate()); break; } } }
@Override public void load() throws IOException { super.load(); if (fLegacySet != null) { List<Template> legacyTemplates= new ArrayList<>(Arrays.asList(fLegacySet.getTemplates())); fLegacySet.clear(); TemplatePersistenceData[] datas= getTemplateData(true); for (Iterator<Template> it= legacyTemplates.listIterator(); it.hasNext();) { Template t= it.next(); TemplatePersistenceData orig= findSimilarTemplate(datas, t, isCodeTemplates()); if (orig == null) { // no contributed match for the old template found if (!isCodeTemplates()) add(new TemplatePersistenceData(t, true)); } else { // a contributed template seems to be the descendant of the non-id template t if (!orig.getTemplate().getPattern().equals(t.getPattern())) // add as modified contributed template if changed compared to the original orig.setTemplate(t); } } save(); fLegacySet= null; } }
Template template= data.getTemplate(); String id= data.getId(); if (id != null) { Attr idAttr= document.createAttribute(ID_ATTRIBUTE); enabled.setValue(data.isEnabled() ? Boolean.toString(true) : Boolean.toString(false)); attributes.setNamedItem(enabled); deleted.setValue(data.isDeleted() ? Boolean.toString(true) : Boolean.toString(false)); attributes.setNamedItem(deleted);
/** * Returns all enabled templates for the given context type. * * @param contextTypeId the id of the context type of the requested templates, or <code>null</code> if all templates should be returned * @return all enabled templates for the given context type */ public Template[] getTemplates(String contextTypeId) { List<Template> templates= new ArrayList<>(); for (TemplatePersistenceData data : fTemplates) { if (data.isEnabled() && !data.isDeleted() && (contextTypeId == null || contextTypeId.equals(data.getTemplate().getContextTypeId()))) templates.add(data.getTemplate()); } return templates.toArray(new Template[templates.size()]); }
for (int i= datas.length - 1; i >= 0; i--) { TemplatePersistenceData data= datas[i]; if (data.isUserAdded()) { TemplatePersistenceData similar= findSimilarTemplate(datas, data.getTemplate(), isCodeTemplates); if (similar != data && !similar.isUserAdded()) { similar.setTemplate(data.getTemplate()); store.delete(data); datas= store.getTemplateData(true); for (int i= datas.length - 1; i >= 0; i--) { if (datas[i].isUserAdded()) store.delete(datas[i]);
/** * Only to be used by tests * * @param templateId the template id * @param pattern the new pattern * @param project not used */ public static void setCodeTemplate(String templateId, String pattern, IJavaProject project) { TemplateStoreCore codeTemplateStore= JavaManipulation.getCodeTemplateStore(); TemplatePersistenceData data= codeTemplateStore.getTemplateData(templateId); Template orig= data.getTemplate(); Template copy= new Template(orig.getName(), orig.getDescription(), orig.getContextTypeId(), pattern, true); data.setTemplate(copy); }
@Override public Template getTemplate() { return ref.getTemplate(); }
@Override public String getId() { return ref.getId(); }
public TemplatePersistenceData(Template template, boolean enabled, String id) { this(new org.eclipse.text.templates.TemplatePersistenceData(template, enabled, id)); }
String id=data.getId(); if (CodeTemplateContextType.CATCHBLOCK_ID.equals(id)) { return PreferencesMessages.CodeTemplateBlock_catchblock_label; return PreferencesMessages.CodeTemplateBlock_settercomment_label; return data.getTemplate().getDescription();
/** * Saves the templates to the preferences. * * @throws IOException if the templates cannot be written */ public void save() throws IOException { ArrayList<TemplatePersistenceData> custom= new ArrayList<>(); for (TemplatePersistenceData data : fTemplates) { if (data.isCustom() && !(data.isUserAdded() && data.isDeleted())) // don't save deleted user-added templates custom.add(data); } StringWriter output= new StringWriter(); TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(custom.toArray(new TemplatePersistenceData[custom.size()]), output); fIgnorePreferenceStoreChanges= true; try { fPreferenceStore.put(fKey, output.toString()); fPreferenceStore.flush(); } catch (BackingStoreException e) { } finally { fIgnorePreferenceStoreChanges= false; } }
TemplatePersistenceData data= new TemplatePersistenceData(template, enabled, id); data.setDeleted(deleted);
/** * 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) { TemplatePersistenceData data= getTemplateData(id); if (data != null && !data.isDeleted()) return data.getTemplate(); return null; }