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); }
public String getColumnText(Object element, int columnIndex) { TemplatePersistenceData data = (TemplatePersistenceData) element; Template template= data.getTemplate(); switch (columnIndex) { case 0: return template.getName(); case 1: TemplateContextType type= fContextTypeRegistry.getContextType(template.getContextTypeId()); if (type != null) return type.getName(); return template.getContextTypeId(); case 2: return template.getDescription(); case 3: return template.isAutoInsertable() ? TemplatesMessages.TemplatePreferencePage_on : ""; //$NON-NLS-1$ default: return ""; //$NON-NLS-1$ } }
/** * 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); }
private void add() { Iterator it= fContextTypeRegistry.contextTypes(); if (it.hasNext()) { Template template= new Template("", "", ((TemplateContextType) it.next()).getId(), "", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Template newTemplate= editTemplate(template, false, true); if (newTemplate != null) { TemplatePersistenceData data= new TemplatePersistenceData(newTemplate, true); fTemplateStore.add(data); fTableViewer.refresh(); fTableViewer.setChecked(data, true); fTableViewer.setSelection(new StructuredSelection(data)); } } }
@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); } }
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); } }
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); } }
/** * 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]); } } }
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); } } } }
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; }
@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); }
/** * 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); }
/** * Saves the templates to the preferences. * * @throws IOException if the templates cannot be written */ public void save() throws IOException { ArrayList custom= new ArrayList(); List<TemplatePersistenceData> fTemplates=Arrays.asList(this.getTemplateData(true)); for (Iterator it= fTemplates.iterator(); it.hasNext();) { TemplatePersistenceData data= (TemplatePersistenceData) it.next(); if (data.isCustom() && !(data.isUserAdded() && data.isDeleted())) // don't save deleted user-added templates custom.add(data); } StringWriter output= new StringWriter(); TemplateReaderWriter writer= new SilverStripeTemplateReaderWriter(); writer.save((TemplatePersistenceData[]) custom.toArray(new TemplatePersistenceData[custom.size()]), output); this.stopListeningForPreferenceChanges(); try { fPreferenceStore.setValue(fKey, output.toString()); if (fPreferenceStore instanceof IPersistentPreferenceStore) ((IPersistentPreferenceStore)fPreferenceStore).save(); } finally { this.startListeningForPreferenceChanges(); } }
/** * Creates the edit dialog. Subclasses may override this method to provide a * custom dialog. * * @param template the template being edited * @param edit whether the dialog should be editable * @param isNameModifiable whether the template name may be modified * @return the created or modified template, or <code>null</code> if the edition failed * @since 3.1 */ protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) { IStructuredSelection selection= (IStructuredSelection) this.getTableViewer().getSelection(); Object[] objects= selection.toArray(); if ((objects != null) || (objects.length == 1)) { TemplatePersistenceData data=(TemplatePersistenceData) selection.getFirstElement(); if(data.getTemplate() instanceof SilverStripeTemplate) { template=new SilverStripeTemplate((SilverStripeTemplate) data.getTemplate()); } } EditTemplateDialog dialog= new SSEditTemplateDialog(getShell(), template, edit, isNameModifiable, this.getContextTypeRegistry()); if (dialog.open() == Window.OK) { return dialog.getTemplate(); } return null; }
/** * 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); } } }
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= JSDTTypeScriptUIPlugin.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$ } }
public Object getParent(TreeListDialogField 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; }
protected TemplatePersistenceData[] getTemplateOfCategory(boolean isComment) { ArrayList res = new ArrayList(); 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 (TemplatePersistenceData[]) res.toArray(new TemplatePersistenceData[res.size()]); }
protected void updateSourceViewerInput(List 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 = JavaScriptPlugin.getDefault().getCodeTemplateContextRegistry() .getContextType(template.getContextTypeId()); fTemplateProcessor.setContextType(type); fPatternViewer.getDocument().set(template.getPattern()); } else { fPatternViewer.getDocument().set(""); //$NON-NLS-1$ } }
protected void doButtonPressed(int buttonIndex, List 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_(); } }
private void edit(TemplatePersistenceData data) { Template newTemplate = new Template(data.getTemplate()); EditTemplateDialog dialog = new EditTemplateDialog(getShell(), newTemplate, true, false, JavaScriptPlugin.getDefault().getCodeTemplateContextRegistry()); if (dialog.open() == Window.OK) { // changed data.setTemplate(dialog.getTemplate()); fCodeTemplateTree.refresh(data); fCodeTemplateTree.selectElements(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; } } }
private void export(List selected) { Set datas = new HashSet(); 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((TemplatePersistenceData[]) datas.toArray(new TemplatePersistenceData[datas.size()])); }
public boolean performOk(boolean enabled) { 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) { JavaScriptPlugin.log(e); openWriteErrorDialog(e); } return true; }
private TemplatePersistenceData[] getEnabledTemplates() { List enabled= new ArrayList(); TemplatePersistenceData[] datas= fTemplateStore.getTemplateData(false); for (int i= 0; i < datas.length; i++) { if (datas[i].isEnabled()) enabled.add(datas[i]); } return (TemplatePersistenceData[]) enabled.toArray(new TemplatePersistenceData[enabled.size()]); }
/** * Updates the pattern viewer. */ protected void updateViewerInput() { IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); if (selection.size() == 1) { TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement(); Template template= data.getTemplate(); fPatternViewer.getDocument().set(template.getPattern()); } else { fPatternViewer.getDocument().set(""); //$NON-NLS-1$ } }
private void edit() { IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); Object[] objects= selection.toArray(); if ((objects == null) || (objects.length != 1)) return; TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement(); edit(data); }
private void export() { IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); Object[] templates= selection.toArray(); TemplatePersistenceData[] datas= new TemplatePersistenceData[templates.length]; for (int i= 0; i != templates.length; i++) datas[i]= (TemplatePersistenceData) templates[i]; export(datas); }
private void remove() { IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); Iterator elements= selection.iterator(); while (elements.hasNext()) { TemplatePersistenceData data= (TemplatePersistenceData) elements.next(); fTemplateStore.delete(data); } fTableViewer.refresh(); }
private void revert() { IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); Iterator elements= selection.iterator(); while (elements.hasNext()) { TemplatePersistenceData data= (TemplatePersistenceData) elements.next(); data.revert(); fTableViewer.setChecked(data, data.isEnabled()); } selectionChanged1(); fTableViewer.refresh(); }
private void addNamedTemplates(String inlineContextId, String namedContextId, String key) { Template[] schemaTemplates = templateStore.getTemplates(inlineContextId); for (int i = 0; i < schemaTemplates.length; i++) { Template schemaTemplate = schemaTemplates[i]; Template template = createNamedTemplate(schemaTemplate, namedContextId, key); templateStore.add(new TemplatePersistenceData(template, true)); } }
/** * Adds a template to the internal store. The added templates must have a * unique id. * * @param data * the template data to add */ @Override protected void internalAdd(final TemplatePersistenceData data) { if (!data.isCustom()) { // give the data a new unique id final String id = getNewIdFromId(data.getId()); final TemplatePersistenceData d2 = new TemplatePersistenceData(data.getTemplate(), true, id); super.internalAdd(d2); } }
public GamlEditTemplateDialog createDialog(final TemplatePersistenceData template, final boolean edit, final Shell shell) { GamlEditTemplateDialog dialog = new GamlEditTemplateDialog(shell, template, edit, contextTypeRegistry, configurationProvider.get(), resourceProvider, languageName); return dialog; }
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; String title = edit ? TemplateDialogMessages.EditTemplateDialog_title_edit : TemplateDialogMessages.EditTemplateDialog_title_new; setTitle(title); // this.fTemplate = data.getTemplate(); // fIsNameModifiable = isNameModifiable; List<String[]> contexts = Lists.newArrayList(); for ( Iterator<TemplateContextType> it = Iterators.filter(registry.contextTypes(), TemplateContextType.class); it .hasNext(); ) { TemplateContextType type = it.next(); contexts.add(new String[] { type.getId(), type.getName() }); } // fContextTypes = contexts.toArray(new String[contexts.size()][]); // fContextTypeRegistry = registry; }
/** * @see msi.gama.lang.gaml.ui.editor.IGamlEditor#openEditTemplateDialog() */ public boolean openEditTemplateDialog(final TemplatePersistenceData data, final boolean edit) { final GamlEditTemplateDialog d = getTemplateFactory().createDialog(data, edit, getEditorSite().getShell()); if (d.open() == Window.OK) { getTemplateStore().directAdd(d.getData(), edit); return true; } return false; }