org.eclipse.jface.text.templates.persistence.TemplatePersistenceData Java Examples

The following examples show how to use org.eclipse.jface.text.templates.persistence.TemplatePersistenceData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XtextTemplateStore.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void loadContributedTemplates() throws IOException {
	if (res==null)
		return;
	TemplateReaderWriter reader = new TemplateReaderWriter();
	InputStream openStream = null;
	try {
		openStream = res.openStream();
		try {
			TemplatePersistenceData[] read = reader.read(openStream, null);
			for (TemplatePersistenceData templatePersistenceData : read) {
				internalAdd(templatePersistenceData);
			}
		} finally {
			openStream.close();
		}
	} catch (IOException e) {
		log.error(e.getMessage(), e);
	}
}
 
Example #2
Source File: GamlTemplateStore.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public void directAdd(final TemplatePersistenceData data, final boolean isEdited) {
	if (isEdited) {
		add(data);
	} else {
		// Trick the template store so that (1) the template is considered
		// as "user added" while (2) maintaining a proper id
		final String id = data.getId();
		final TemplatePersistenceData d2 = new TemplatePersistenceData(data.getTemplate(), true) {

			@Override
			public String getId() {
				return id;
			}

		};
		add(d2);
	}
}
 
Example #3
Source File: CodeTemplatePreferencePage.java    From typescript.java with MIT License 6 votes vote down vote up
public void applyData(Object data) {
	if (data instanceof Map) {
		Object id = ((Map) data).get(DATA_SELECT_TEMPLATE);
		if (id instanceof String) {
			final TemplatePersistenceData[] templates = fCodeTemplateConfigurationBlock.fTemplateStore
					.getTemplateData();
			TemplatePersistenceData template = null;
			for (int index = 0; index < templates.length; index++) {
				template = templates[index];
				if (template.getId().equals(id)) {
					fCodeTemplateConfigurationBlock.postSetSelection(template);
					break;
				}
			}
		}
	}
	super.applyData(data);
}
 
Example #4
Source File: ProjectTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void load() throws IOException {
	if (fProjectStore != null) {
		fProjectStore.load();

		Set<String> datas= new HashSet<String>();
		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);
			}
		}
	}
}
 
Example #5
Source File: GamlEditTemplateDialog.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public GamlEditTemplateDialog(final Shell parent, final TemplatePersistenceData data, final boolean edit,
		final ContextTypeRegistry registry, final TemplatesLanguageConfiguration configuration,
		final IEditedResourceProvider resourceProvider, final String languageName) {
	super(parent);
	this.data = data;
	this.configuration = configuration;
	this.resourceProvider = resourceProvider;
	this.languageName = languageName;

	final String title = edit ? TemplateDialogMessages.EditTemplateDialog_title_edit
			: TemplateDialogMessages.EditTemplateDialog_title_new;
	setTitle(title);

	// this.fTemplate = data.getTemplate();
	// fIsNameModifiable = isNameModifiable;

	final List<String[]> contexts = Lists.newArrayList();
	for (final Iterator<TemplateContextType> it =
			Iterators.filter(registry.contextTypes(), TemplateContextType.class); it.hasNext();) {
		final TemplateContextType type = it.next();
		contexts.add(new String[] { type.getId(), type.getName() });
	}
	// fContextTypes = contexts.toArray(new String[contexts.size()][]);
	// fContextTypeRegistry = registry;

}
 
Example #6
Source File: CompatibilityTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Removes any duplicates from a template store. Duplicate user added templates
 * are copied over their contributed siblings. If isCodeTemplates is true,
 * any user added templates are then removed.
 *
 * @param store
 * @param isCodeTemplates
 */
public static void pruneDuplicates(TemplateStore store, boolean isCodeTemplates) {
	TemplatePersistenceData[] datas= store.getTemplateData(true);
	for (int i= datas.length - 1; i >= 0; i--) {
		TemplatePersistenceData data= datas[i];
		if (data.isUserAdded()) {
			// find a contributed template that is similar and check it
			TemplatePersistenceData similar= findSimilarTemplate(datas, data.getTemplate(), isCodeTemplates);
			if (similar != data && !similar.isUserAdded()) {
				similar.setTemplate(data.getTemplate());
				store.delete(data);
			}
		}
	}

	if (isCodeTemplates) {
		datas= store.getTemplateData(true);
		for (int i= datas.length - 1; i >= 0; i--) {
			if (datas[i].isUserAdded())
				store.delete(datas[i]);
		}
	}
}
 
Example #7
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean performOk(boolean enabled) {
	boolean res= super.performOk();
	if (!res)
		return false;

	if (fProject != null) {
		TemplatePersistenceData[] templateData= fTemplateStore.getTemplateData();
		for (int i= 0; i < templateData.length; i++) {
			fTemplateStore.setProjectSpecific(templateData[i].getId(), enabled);
		}
	}
	try {
		fTemplateStore.save();
	} catch (IOException e) {
		JavaPlugin.log(e);
		openWriteErrorDialog();
	}
	return true;
}
 
Example #8
Source File: CodeTemplatePreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void applyData(Object data) {
	if (data instanceof Map) {
		Object id= ((Map<?, ?>) data).get(DATA_SELECT_TEMPLATE);
		if (id instanceof String) {
			final TemplatePersistenceData[] templates= fCodeTemplateConfigurationBlock.fTemplateStore.getTemplateData();
			TemplatePersistenceData template= null;
			for (int index= 0; index < templates.length; index++) {
				template= templates[index];
				if (template.getId().equals(id)) {
					fCodeTemplateConfigurationBlock.postSetSelection(template);
					break;
				}
			}
		}
	}
	super.applyData(data);
}
 
Example #9
Source File: TemplateSelectDialog.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Sets the template options in the passed list (swt)
 */
private void fillTemplateOptions(TemplatePersistenceData[] templateData, Table list) {
    nameToTemplateData = new TreeMap<String, TemplatePersistenceData>();

    for (TemplatePersistenceData data : templateData) {
        if (PyContextType.PY_MODULES_CONTEXT_TYPE.equals(data.getTemplate().getContextTypeId())) {
            String name = data.getTemplate().getName();
            nameToTemplateData.put(name, data);
        }
    }
    ArrayList<String> lst = new ArrayList<String>(nameToTemplateData.keySet());
    for (String string : lst) {
        new TableItem(list, SWT.NONE).setText(string);
    }
    list.setSelection(0);
}
 
Example #10
Source File: TemplateReferenceMenu.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
void add(final TemplatePersistenceData t, final List<String> path) {
	if (path.size() == 0) {
		children.add(new TemplateNode(this, t, 1));
	} else if (path.size() == 1 && Strings.isGamaNumber(path.get(0))) {
		children.add(new TemplateNode(this, t, Integer.decode(path.get(0))));
	} else {
		final String name = path.remove(0);
		Node node = childWithName(name);
		if (node == null) {
			node = new TemplateTree(this, name);
			children.add(node);
		}
		((TemplateTree) node).add(t, path);
	}

}
 
Example #11
Source File: AdvancedTemplatesPreferencePage.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	if (selection.size() == 1) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String name = template.getName();
		TemplateContextType contextType = getContextTypeRegistry().getContextType(template.getContextTypeId());
		if (contextType != null) {
			String prefix = 
					"templates for " + grammarAccess.getGrammar().getName() +
					"'" + name + "'" + " for " + getContextTypeForGrammar(contextType) + ">>";
			String editablePart = template.getPattern();
			String suffix = "";
			partialEditor.updateModel(prefix, editablePart, suffix);
		} else {
			partialEditor.updateModel("", template.getPattern(), "");
		}
	} else {
		partialEditor.updateModel("", "", "");
	}
}
 
Example #12
Source File: CompatibilityTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void load() throws IOException {
	super.load();

	if (fLegacySet != null) {

		List<Template> legacyTemplates= new ArrayList<Template>(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;
	}
}
 
Example #13
Source File: CompatibilityTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static TemplatePersistenceData findSimilarTemplate(TemplatePersistenceData[] datas, Template template, boolean isCodeTemplates) {
	 for (int i= 0; i < datas.length; i++) {
		TemplatePersistenceData data= datas[i];
		Template orig= data.getTemplate();
		if (isSimilar(template, orig, isCodeTemplates))
			return data;
	 }

	 return null;
}
 
Example #14
Source File: ProjectTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean hasProjectSpecificTempates(IProject project) {
	String pref= new ProjectScope(project).getNode(JavaUI.ID_PLUGIN).get(KEY, null);
	if (pref != null && pref.trim().length() > 0) {
		Reader input= new StringReader(pref);
		TemplateReaderWriter reader= new TemplateReaderWriter();
		TemplatePersistenceData[] datas;
		try {
			datas= reader.read(input);
			return datas.length > 0;
		} catch (IOException e) {
			// ignore
		}
	}
	return false;
}
 
Example #15
Source File: ProjectTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public TemplatePersistenceData[] getTemplateData() {
	if (fProjectStore != null) {
		return fProjectStore.getTemplateData(true);
	} else {
		return fInstanceStore.getTemplateData(true);
	}
}
 
Example #16
Source File: LangTemplatePreferencePage.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	
	if(selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		getViewer().getDocument().set(template.getPattern());
	} else {
		getViewer().getDocument().set("");
	}
}
 
Example #17
Source File: ProjectTemplateStore.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void setProjectSpecific(String id, boolean projectSpecific) {
	Assert.isNotNull(fProjectStore);

	TemplatePersistenceData data= fProjectStore.getTemplateData(id);
	if (data == null) {
		return; // does not exist
	} else {
		data.setDeleted(!projectSpecific);
	}
}
 
Example #18
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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) {
	TemplateStore codeTemplateStore= JavaPlugin.getDefault().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);
}
 
Example #19
Source File: JavaTemplatePreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();

	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);

		IDocument doc= viewer.getDocument();

		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$

		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}
}
 
Example #20
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public Object getParent(TreeListDialogField<String> field, Object element) {
	if (element instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) element;
		if (data.getTemplate().getName().endsWith(CodeTemplateContextType.COMMENT_SUFFIX)) {
			return COMMENT_NODE;
		}
		return CODE_NODE;
	}
	return null;
}
 
Example #21
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected TemplatePersistenceData[] getTemplateOfCategory(boolean isComment) {
	ArrayList<TemplatePersistenceData> res=  new ArrayList<TemplatePersistenceData>();
	TemplatePersistenceData[] templates= fTemplateStore.getTemplateData();
	for (int i= 0; i < templates.length; i++) {
		TemplatePersistenceData curr= templates[i];
		if (isComment == curr.getTemplate().getName().endsWith(CodeTemplateContextType.COMMENT_SUFFIX)) {
			res.add(curr);
		}
	}
	return res.toArray(new TemplatePersistenceData[res.size()]);
}
 
Example #22
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void updateSourceViewerInput(List<Object> selection) {
	if (fPatternViewer == null || fPatternViewer.getTextWidget().isDisposed()) {
		return;
	}
	if (selection.size() == 1 && selection.get(0) instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.get(0);
		Template template= data.getTemplate();
		TemplateContextType type= JavaPlugin.getDefault().getCodeTemplateContextRegistry().getContextType(template.getContextTypeId());
		fTemplateProcessor.setContextType(type);
		fPatternViewer.getDocument().set(template.getPattern());
	} else {
		fPatternViewer.getDocument().set(""); //$NON-NLS-1$
	}
}
 
Example #23
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void doButtonPressed(int buttonIndex, List<Object> selected) {
	if (buttonIndex == IDX_EDIT) {
		edit((TemplatePersistenceData) selected.get(0));
	} else if (buttonIndex == IDX_EXPORT) {
		export(selected);
	} else if (buttonIndex == IDX_EXPORTALL) {
		exportAll();
	} else if (buttonIndex == IDX_IMPORT) {
		import_();
	}
}
 
Example #24
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void edit(TemplatePersistenceData data) {
	Template newTemplate= new Template(data.getTemplate());
	EditTemplateDialog dialog= new EditTemplateDialog(getShell(), newTemplate, true, false, JavaPlugin.getDefault().getCodeTemplateContextRegistry());
	if (dialog.open() == Window.OK) {
		// changed
		data.setTemplate(dialog.getTemplate());
		fCodeTemplateTree.refresh(data);
		fCodeTemplateTree.selectElements(new StructuredSelection(data));
	}
}
 
Example #25
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
		}
	}
}
 
Example #26
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void export(List<Object> selected) {
	Set<Object> datas= new HashSet<Object>();
	for (int i= 0; i < selected.size(); i++) {
		Object curr= selected.get(i);
		if (curr instanceof TemplatePersistenceData) {
			datas.add(curr);
		} else {
			TemplatePersistenceData[] cat= getTemplateOfCategory(curr == COMMENT_NODE);
			datas.addAll(Arrays.asList(cat));
		}
	}
	export(datas.toArray(new TemplatePersistenceData[datas.size()]));
}
 
Example #27
Source File: PythonModuleWizard.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Applies the template if one was specified.
 */
@Override
protected void afterEditorCreated(final IEditorPart openEditor) {
    if (!(openEditor instanceof PyEdit)) {
        return; //only works for PyEdit...
    }

    RunInUiThread.async(new Runnable() {

        @Override
        public void run() {
            PyEdit pyEdit = (PyEdit) openEditor;
            if (pyEdit.isDisposed()) {
                return;
            }
            TemplateSelectDialog dialog = new TemplateSelectDialog(Display.getCurrent().getActiveShell());
            dialog.open();
            TemplatePersistenceData selectedTemplate = dialog.getSelectedTemplate();
            if (selectedTemplate == null) {
                return; //no template selected, nothing to apply!
            }

            Template template = selectedTemplate.getTemplate();

            Region region = new Region(0, 0);
            PyDocumentTemplateContext context = PyDocumentTemplateContext.createContext(new PyContextType(),
                    pyEdit.getPySourceViewer(), region);

            TemplateProposal templateProposal = new TemplateProposal(template, context, region, null);
            templateProposal.apply(pyEdit.getPySourceViewer(), '\n', 0, 0);
        }
    });
}
 
Example #28
Source File: TemplateSelectDialog.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return the data for the selected template or null if not available.
 */
private TemplatePersistenceData getCurr() {
    if (templateList != null && nameToTemplateData != null) {
        TableItem[] sel = templateList.getSelection();
        if (sel != null && sel.length > 0) {
            return nameToTemplateData.get(sel[0].getText(0));
        }
    }
    return null;
}
 
Example #29
Source File: TemplateSelectDialog.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public TemplatePersistenceData getSelectedTemplate() {
    Object[] r = this.getResult();
    if (r != null && r.length > 0) {
        return (TemplatePersistenceData) r[0];
    }
    return null;
}
 
Example #30
Source File: ConfigurableTemplateStore.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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(), templates.getPath() + "." //$NON-NLS-1$
                + 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);
    }
  }
}