Search in sources :

Example 26 with TemplatePersistenceData

use of org.eclipse.jface.text.templates.persistence.TemplatePersistenceData in project eclipse.platform.text by eclipse.

the class TemplatePreferencePage method updateButtons.

/**
 * Updates the buttons.
 */
protected void updateButtons() {
    IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
    int selectionCount = selection.size();
    int itemCount = fTableViewer.getTable().getItemCount();
    boolean canRestore = fTemplateStore.getTemplateData(true).length != fTemplateStore.getTemplateData(false).length;
    boolean canRevert = false;
    for (Iterator<?> it = selection.iterator(); it.hasNext(); ) {
        TemplatePersistenceData data = (TemplatePersistenceData) it.next();
        if (data.isModified()) {
            canRevert = true;
            break;
        }
    }
    fEditButton.setEnabled(selectionCount == 1);
    fExportButton.setEnabled(selectionCount > 0);
    fRemoveButton.setEnabled(selectionCount > 0 && selectionCount <= itemCount);
    fRestoreButton.setEnabled(canRestore);
    fRevertButton.setEnabled(canRevert);
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 27 with TemplatePersistenceData

use of org.eclipse.jface.text.templates.persistence.TemplatePersistenceData in project eclipse.platform.text by eclipse.

the class TemplatePreferencePage method restoreDeleted.

private void restoreDeleted() {
    TemplatePersistenceData[] oldTemplates = fTemplateStore.getTemplateData(false);
    fTemplateStore.restoreDeleted();
    TemplatePersistenceData[] newTemplates = fTemplateStore.getTemplateData(false);
    fTableViewer.refresh();
    fTableViewer.setCheckedElements(getEnabledTemplates());
    ArrayList<TemplatePersistenceData> selection = new ArrayList<>();
    selection.addAll(Arrays.asList(newTemplates));
    selection.removeAll(Arrays.asList(oldTemplates));
    fTableViewer.setSelection(new StructuredSelection(selection), true);
    selectionChanged1();
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 28 with TemplatePersistenceData

use of org.eclipse.jface.text.templates.persistence.TemplatePersistenceData in project eclipse.platform.text by eclipse.

the class ContributionTemplateStore method readIncludedTemplates.

private void readIncludedTemplates(Collection<TemplatePersistenceData> templates, IConfigurationElement element) throws IOException {
    String file = element.getAttribute(FILE);
    if (file != null) {
        Bundle plugin = Platform.getBundle(element.getContributor().getName());
        URL url = FileLocator.find(plugin, Path.fromOSString(file), null);
        if (url != null) {
            ResourceBundle bundle = null;
            String translations = element.getAttribute(TRANSLATIONS);
            if (translations != null) {
                URL bundleURL = FileLocator.find(plugin, Path.fromOSString(translations), null);
                if (bundleURL != null) {
                    try (InputStream bundleStream = bundleURL.openStream()) {
                        bundle = new PropertyResourceBundle(bundleStream);
                    }
                }
            }
            try (InputStream stream = new BufferedInputStream(url.openStream())) {
                TemplateReaderWriter reader = new TemplateReaderWriter();
                TemplatePersistenceData[] datas = reader.read(stream, bundle);
                for (int i = 0; i < datas.length; i++) {
                    TemplatePersistenceData data = datas[i];
                    if (data.isCustom()) {
                        if (data.getId() == null)
                            EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_no_id, data.getTemplate().getName()));
                        else
                            EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_deleted, data.getTemplate().getName()));
                    } else if (validateTemplate(data.getTemplate())) {
                        templates.add(data);
                    }
                }
            }
        }
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) BufferedInputStream(java.io.BufferedInputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) TemplateReaderWriter(org.eclipse.jface.text.templates.persistence.TemplateReaderWriter) URL(java.net.URL) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 29 with TemplatePersistenceData

use of org.eclipse.jface.text.templates.persistence.TemplatePersistenceData in project erlide_eclipse by erlang.

the class ErlideContributionTemplateStore method loadContributedTemplates.

/**
 * Loads the templates contributed via the templates extension point.
 *
 * @throws IOException
 *             {@inheritDoc}
 */
@Override
protected void loadContributedTemplates() throws IOException {
    super.loadContributedTemplates();
    final String erlideTemplates = System.getProperty("erlide.templates");
    if (erlideTemplates != null) {
        final String[] l = erlideTemplates.split(";");
        final Collection<TemplatePersistenceData> templates = new ArrayList<>();
        readIncludedTemplates(templates, l);
        for (final TemplatePersistenceData data : templates) {
            add(data);
        }
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) ArrayList(java.util.ArrayList)

Example 30 with TemplatePersistenceData

use of org.eclipse.jface.text.templates.persistence.TemplatePersistenceData in project dsl-devkit by dsldevkit.

the class ConfigurableTemplateStore method addTemplatesFromFile.

/**
 * Contribute templates defined in file with the give URL.
 *
 * @param templates
 *          the URL of the file with templates
 */
private void addTemplatesFromFile(final URL templates) {
    if (templates != null) {
        TemplateReaderWriter reader = new TemplateReaderWriter();
        try {
            InputStream openStream = templates.openStream();
            try {
                TemplatePersistenceData[] datas = reader.read(openStream, null);
                int templateCounter = 0;
                for (TemplatePersistenceData data : datas) {
                    if (data.getId() == null) {
                        templateCounter++;
                        TemplatePersistenceData dataWithGenId = new TemplatePersistenceData(data.getTemplate(), data.isEnabled(), // $NON-NLS-1$
                        templates.getPath() + "." + templateCounter);
                        dataWithGenId.setDeleted(data.isDeleted());
                        internalAdd(dataWithGenId);
                    } else {
                        // if contributed template has an id
                        internalAdd(data);
                    }
                }
            } finally {
                openStream.close();
            }
        } catch (IOException e) {
            LOG.error(e);
        }
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) InputStream(java.io.InputStream) TemplateReaderWriter(org.eclipse.jface.text.templates.persistence.TemplateReaderWriter) IOException(java.io.IOException)

Aggregations

TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)36 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)17 Template (org.eclipse.jface.text.templates.Template)13 TemplateReaderWriter (org.eclipse.jface.text.templates.persistence.TemplateReaderWriter)8 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)8 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 URL (java.net.URL)4 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)4 BufferedInputStream (java.io.BufferedInputStream)3 PropertyResourceBundle (java.util.PropertyResourceBundle)3 ResourceBundle (java.util.ResourceBundle)3 Point (org.eclipse.swt.graphics.Point)3 Bundle (org.osgi.framework.Bundle)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Action (org.eclipse.jface.action.Action)2 IAction (org.eclipse.jface.action.IAction)2 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)2