Search in sources :

Example 6 with TemplatePersistenceData

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

the class AbstractTemplatesPage method setupPasteOperation.

/**
 * Setup the paste operation
 *
 * We get the editors Paste operation and sets up a new operation that
 * checks for the clipboard contents for {@link TemplatesTransfer} data.
 */
private void setupPasteOperation() {
    fEditorOldPasteAction = fTextEditor.getAction(ITextEditorActionConstants.PASTE);
    fEditorPasteAction = new Action(TemplatesMessages.TemplatesPage_paste) {

        @Override
        public void run() {
            Clipboard clipboard = new Clipboard(getShell().getDisplay());
            try {
                Template template = getTemplateFromClipboard(clipboard);
                if (template != null)
                    insertTemplate(template);
                else
                    fEditorOldPasteAction.run();
            } finally {
                clipboard.dispose();
            }
        }

        @Override
        public void runWithEvent(Event event) {
            run();
        }

        /**
         * Convert the clipboard contents into a template
         *
         * @param clipboard the clipboard
         * @return the template or null if contents are not valid
         */
        private Template getTemplateFromClipboard(Clipboard clipboard) {
            TemplatePersistenceData[] contents = (TemplatePersistenceData[]) clipboard.getContents(TemplatesTransfer.getInstance());
            if (contents != null && contents.length == 1)
                return contents[0].getTemplate();
            return null;
        }
    };
    fEditorPasteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);
    fTextEditor.setAction(ITextEditorActionConstants.PASTE, fEditorPasteAction);
}
Also used : IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Event(org.eclipse.swt.widgets.Event) ControlEvent(org.eclipse.swt.events.ControlEvent) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) Clipboard(org.eclipse.swt.dnd.Clipboard) Template(org.eclipse.jface.text.templates.Template)

Example 7 with TemplatePersistenceData

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

the class AbstractTemplatesPage method editTemplate.

/**
 * Edits the selected template.
 */
private void editTemplate() {
    TemplatePersistenceData selectedTemplate = getSelectedTemplates()[0];
    Template oldTemplate = selectedTemplate.getTemplate();
    Template newTemplate = editTemplate(new Template(oldTemplate), true, true);
    if (newTemplate != null) {
        if (!newTemplate.getName().equals(oldTemplate.getName()) && MessageDialog.openQuestion(getShell(), TemplatesMessages.TemplatesPage_question_create_new_title, TemplatesMessages.TemplatesPage_question_create_new_message)) {
            TemplatePersistenceData templateData = new TemplatePersistenceData(newTemplate, true);
            getTemplateStore().add(templateData);
            refresh();
            fTreeViewer.setSelection(new StructuredSelection(templateData), true);
        } else {
            selectedTemplate.setTemplate(newTemplate);
            updatePatternViewer(newTemplate);
        }
    }
    saveTemplateStore();
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Template(org.eclipse.jface.text.templates.Template)

Example 8 with TemplatePersistenceData

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

the class TemplatePreferencePage method createContents.

@Override
protected Control createContents(Composite ancestor) {
    Composite parent = new Composite(ancestor, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    parent.setLayout(layout);
    Composite innerParent = new Composite(parent, SWT.NONE);
    GridLayout innerLayout = new GridLayout();
    innerLayout.numColumns = 2;
    innerLayout.marginHeight = 0;
    innerLayout.marginWidth = 0;
    innerParent.setLayout(innerLayout);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;
    innerParent.setLayoutData(gd);
    Composite tableComposite = new Composite(innerParent, SWT.NONE);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.widthHint = 360;
    data.heightHint = convertHeightInCharsToPixels(10);
    tableComposite.setLayoutData(data);
    ColumnLayout columnLayout = new ColumnLayout();
    tableComposite.setLayout(columnLayout);
    Table table = new Table(tableComposite, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    GC gc = new GC(getShell());
    gc.setFont(JFaceResources.getDialogFont());
    TemplateViewerComparator viewerComparator = new TemplateViewerComparator();
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText(TemplatesMessages.TemplatePreferencePage_column_name);
    int minWidth = computeMinimumColumnWidth(gc, TemplatesMessages.TemplatePreferencePage_column_name);
    columnLayout.addColumnData(new ColumnWeightData(2, minWidth, true));
    column1.addSelectionListener(new TemplateColumnSelectionAdapter(column1, 0, viewerComparator));
    TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText(TemplatesMessages.TemplatePreferencePage_column_context);
    minWidth = computeMinimumContextColumnWidth(gc);
    columnLayout.addColumnData(new ColumnWeightData(1, minWidth, true));
    column2.addSelectionListener(new TemplateColumnSelectionAdapter(column2, 1, viewerComparator));
    TableColumn column3 = new TableColumn(table, SWT.NONE);
    column3.setText(TemplatesMessages.TemplatePreferencePage_column_description);
    minWidth = computeMinimumColumnWidth(gc, TemplatesMessages.TemplatePreferencePage_column_description);
    columnLayout.addColumnData(new ColumnWeightData(3, minWidth, true));
    column3.addSelectionListener(new TemplateColumnSelectionAdapter(column3, 2, viewerComparator));
    TableColumn column4 = new TableColumn(table, SWT.NONE);
    column4.setAlignment(SWT.CENTER);
    column4.setText(TemplatesMessages.TemplatePreferencePage_column_autoinsert);
    minWidth = computeMinimumColumnWidth(gc, TemplatesMessages.TemplatePreferencePage_column_autoinsert);
    minWidth = Math.max(minWidth, computeMinimumColumnWidth(gc, TemplatesMessages.TemplatePreferencePage_on));
    columnLayout.addColumnData(new ColumnPixelData(minWidth, false, false));
    column4.addSelectionListener(new TemplateColumnSelectionAdapter(column4, 3, viewerComparator));
    gc.dispose();
    fTableViewer = new CheckboxTableViewer(table);
    fTableViewer.setLabelProvider(new TemplateLabelProvider());
    fTableViewer.setContentProvider(new TemplateContentProvider());
    fTableViewer.setComparator(viewerComparator);
    // Specify default sorting
    table.setSortColumn(column1);
    table.setSortDirection(viewerComparator.getDirection());
    fTableViewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent e) {
            edit();
        }
    });
    fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent e) {
            selectionChanged1();
        }
    });
    fTableViewer.addCheckStateListener(new ICheckStateListener() {

        @Override
        public void checkStateChanged(CheckStateChangedEvent event) {
            TemplatePersistenceData d = (TemplatePersistenceData) event.getElement();
            d.setEnabled(event.getChecked());
        }
    });
    BidiUtils.applyTextDirection(fTableViewer.getControl(), BidiUtils.BTD_DEFAULT);
    Composite buttons = new Composite(innerParent, SWT.NONE);
    buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    buttons.setLayout(layout);
    fAddButton = new Button(buttons, SWT.PUSH);
    fAddButton.setText(TemplatesMessages.TemplatePreferencePage_new);
    fAddButton.setLayoutData(getButtonGridData(fAddButton));
    fAddButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            add();
        }
    });
    fEditButton = new Button(buttons, SWT.PUSH);
    fEditButton.setText(TemplatesMessages.TemplatePreferencePage_edit);
    fEditButton.setLayoutData(getButtonGridData(fEditButton));
    fEditButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            edit();
        }
    });
    fRemoveButton = new Button(buttons, SWT.PUSH);
    fRemoveButton.setText(TemplatesMessages.TemplatePreferencePage_remove);
    fRemoveButton.setLayoutData(getButtonGridData(fRemoveButton));
    fRemoveButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            remove();
        }
    });
    createSeparator(buttons);
    fRestoreButton = new Button(buttons, SWT.PUSH);
    fRestoreButton.setText(TemplatesMessages.TemplatePreferencePage_restore);
    fRestoreButton.setLayoutData(getButtonGridData(fRestoreButton));
    fRestoreButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            restoreDeleted();
        }
    });
    fRevertButton = new Button(buttons, SWT.PUSH);
    fRevertButton.setText(TemplatesMessages.TemplatePreferencePage_revert);
    fRevertButton.setLayoutData(getButtonGridData(fRevertButton));
    fRevertButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            revert();
        }
    });
    createSeparator(buttons);
    fImportButton = new Button(buttons, SWT.PUSH);
    fImportButton.setText(TemplatesMessages.TemplatePreferencePage_import);
    fImportButton.setLayoutData(getButtonGridData(fImportButton));
    fImportButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            import_();
        }
    });
    fExportButton = new Button(buttons, SWT.PUSH);
    fExportButton.setText(TemplatesMessages.TemplatePreferencePage_export);
    fExportButton.setLayoutData(getButtonGridData(fExportButton));
    fExportButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            export();
        }
    });
    fPatternViewer = doCreateViewer(parent);
    if (isShowFormatterSetting()) {
        fFormatButton = new Button(parent, SWT.CHECK);
        fFormatButton.setText(TemplatesMessages.TemplatePreferencePage_use_code_formatter);
        GridData gd1 = new GridData();
        gd1.horizontalSpan = 2;
        fFormatButton.setLayoutData(gd1);
        fFormatButton.setSelection(getPreferenceStore().getBoolean(getFormatterPreferenceKey()));
    }
    fTableViewer.setInput(fTemplateStore);
    fTableViewer.setAllChecked(false);
    fTableViewer.setCheckedElements(getEnabledTemplates());
    updateButtons();
    Dialog.applyDialogFont(parent);
    innerParent.layout();
    return parent;
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) SelectionListener(org.eclipse.swt.events.SelectionListener) ITextListener(org.eclipse.jface.text.ITextListener) Listener(org.eclipse.swt.widgets.Listener) ModifyListener(org.eclipse.swt.events.ModifyListener) DisposeListener(org.eclipse.swt.events.DisposeListener) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) IMenuListener(org.eclipse.jface.action.IMenuListener) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) ICheckStateListener(org.eclipse.jface.viewers.ICheckStateListener) FocusListener(org.eclipse.swt.events.FocusListener) CheckboxTableViewer(org.eclipse.jface.viewers.CheckboxTableViewer) ICheckStateListener(org.eclipse.jface.viewers.ICheckStateListener) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) GC(org.eclipse.swt.graphics.GC) TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) ColumnPixelData(org.eclipse.jface.viewers.ColumnPixelData) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) TableColumn(org.eclipse.swt.widgets.TableColumn) GridData(org.eclipse.swt.layout.GridData) FocusEvent(org.eclipse.swt.events.FocusEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) CheckStateChangedEvent(org.eclipse.jface.viewers.CheckStateChangedEvent) Event(org.eclipse.swt.widgets.Event) TextEvent(org.eclipse.jface.text.TextEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) CheckStateChangedEvent(org.eclipse.jface.viewers.CheckStateChangedEvent)

Example 9 with TemplatePersistenceData

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

the class TemplatePreferencePage method edit.

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);
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 10 with TemplatePersistenceData

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

the class TemplatePreferencePage method import_.

private void import_() {
    FileDialog dialog = new FileDialog(getShell());
    dialog.setText(TemplatesMessages.TemplatePreferencePage_import_title);
    dialog.setFilterExtensions(new String[] { TemplatesMessages.TemplatePreferencePage_import_extension });
    String path = dialog.open();
    if (path == null)
        return;
    try {
        ArrayList<TemplatePersistenceData> selection = new ArrayList<>();
        TemplateReaderWriter reader = new TemplateReaderWriter();
        File file = new File(path);
        if (file.exists()) {
            try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
                TemplatePersistenceData[] datas = reader.read(input, null);
                for (int i = 0; i < datas.length; i++) {
                    TemplatePersistenceData data = datas[i];
                    fTemplateStore.add(data);
                    String id = data.getId();
                    if (id == null) {
                        selection.add(data);
                    } else {
                        data = fTemplateStore.getTemplateData(id);
                        if (data != null) {
                            selection.add(data);
                        }
                    }
                }
            }
        }
        fTableViewer.refresh();
        fTableViewer.setAllChecked(false);
        fTableViewer.setCheckedElements(getEnabledTemplates());
        fTableViewer.setSelection(new StructuredSelection(selection), true);
        selectionChanged1();
    } catch (FileNotFoundException e) {
        openReadErrorDialog(e);
    } catch (IOException e) {
        openReadErrorDialog(e);
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) FileNotFoundException(java.io.FileNotFoundException) TemplateReaderWriter(org.eclipse.jface.text.templates.persistence.TemplateReaderWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

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