Search in sources :

Example 21 with EditingSupport

use of org.eclipse.jface.viewers.EditingSupport in project egit by eclipse.

the class RefSpecPanel method createDstColumn.

private void createDstColumn(final TableColumnLayout columnLayout) {
    final TableViewerColumn column = createColumn(columnLayout, UIText.RefSpecPanel_columnDst, COLUMN_DST_WEIGHT, SWT.LEFT);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(final Object element) {
            return ((RefSpec) element).getDestination();
        }

        @Override
        public String getToolTipText(Object element) {
            if (isInvalidSpec(element))
                return errorMessage;
            if (isDeleteRefSpec(element))
                return UIText.RefSpecPanel_dstDeletionDescription;
            if (pushSpecs)
                return UIText.RefSpecPanel_dstPushDescription;
            return UIText.RefSpecPanel_dstFetchDescription;
        }

        @Override
        public Color getBackground(final Object element) {
            if (isInvalidSpec(element))
                return errorBackgroundColor;
            return null;
        }

        @Override
        public Color getToolTipForegroundColor(Object element) {
            if (isInvalidSpec(element))
                return errorTextColor;
            return null;
        }
    });
    column.setEditingSupport(new EditingSupport(tableViewer) {

        @Override
        protected boolean canEdit(final Object element) {
            return true;
        }

        @Override
        protected CellEditor getCellEditor(final Object element) {
            return (pushSpecs ? remoteRefCellEditor : localRefCellEditor);
        }

        @Override
        protected Object getValue(final Object element) {
            return ((RefSpec) element).getDestination();
        }

        @Override
        protected void setValue(final Object element, final Object value) {
            if (value == null || ((String) value).length() == 0) {
                // to the old value.
                return;
            }
            final RefSpec oldSpec = (RefSpec) element;
            final RefSpec newSpec = setRefSpecDestination(oldSpec, (String) value);
            setRefSpec(oldSpec, newSpec);
        }
    });
}
Also used : ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) RefSpec(org.eclipse.jgit.transport.RefSpec) CellEditor(org.eclipse.jface.viewers.CellEditor) CheckboxCellEditor(org.eclipse.jface.viewers.CheckboxCellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) Color(org.eclipse.swt.graphics.Color) EditingSupport(org.eclipse.jface.viewers.EditingSupport) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn)

Example 22 with EditingSupport

use of org.eclipse.jface.viewers.EditingSupport in project egit by eclipse.

the class ConfigurationEditorComponent method createContents.

/**
 * @return the control being created
 */
public Control createContents() {
    final Composite main = new Composite(parent, SWT.NONE);
    main.setLayout(new GridLayout(2, false));
    GridDataFactory.fillDefaults().grab(true, true).applyTo(main);
    if (editableConfig instanceof FileBasedConfig) {
        Composite locationPanel = new Composite(main, SWT.NONE);
        GridLayout locationLayout = new GridLayout(3, false);
        locationLayout.marginWidth = 0;
        locationPanel.setLayout(locationLayout);
        GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(locationPanel);
        Label locationLabel = new Label(locationPanel, SWT.NONE);
        locationLabel.setText(UIText.ConfigurationEditorComponent_ConfigLocationLabel);
        // GridDataFactory.fillDefaults().applyTo(locationLabel);
        int locationStyle = SWT.BORDER | SWT.READ_ONLY;
        location = new Text(locationPanel, locationStyle);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(location);
        Button openEditor = new Button(locationPanel, SWT.PUSH);
        openEditor.setText(UIText.ConfigurationEditorComponent_OpenEditorButton);
        openEditor.setToolTipText(UIText.ConfigurationEditorComponent_OpenEditorTooltip);
        openEditor.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                IFileStore store = EFS.getLocalFileSystem().getStore(new Path(((FileBasedConfig) editableConfig).getFile().getAbsolutePath()));
                try {
                    IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), new FileStoreEditorInput(store), EditorsUI.DEFAULT_TEXT_EDITOR_ID);
                } catch (PartInitException ex) {
                    Activator.handleError(ex.getMessage(), ex, true);
                }
            }
        });
        openEditor.setEnabled(((FileBasedConfig) editableConfig).getFile() != null);
    }
    tv = new TreeViewer(main, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
    Tree tree = tv.getTree();
    GridDataFactory.fillDefaults().hint(100, 60).grab(true, true).applyTo(tree);
    TreeColumn key = new TreeColumn(tree, SWT.NONE);
    key.setText(UIText.ConfigurationEditorComponent_KeyColumnHeader);
    key.setWidth(150);
    final TextCellEditor editor = new TextCellEditor(tree);
    editor.setValidator(new ICellEditorValidator() {

        @Override
        public String isValid(Object value) {
            String editedValue = value.toString();
            return editedValue.length() > 0 ? null : UIText.ConfigurationEditorComponent_EmptyStringNotAllowed;
        }
    });
    editor.addListener(new ICellEditorListener() {

        @Override
        public void editorValueChanged(boolean oldValidState, boolean newValidState) {
            setErrorMessage(editor.getErrorMessage());
        }

        @Override
        public void cancelEditor() {
            setErrorMessage(null);
        }

        @Override
        public void applyEditorValue() {
            setErrorMessage(null);
        }
    });
    TreeColumn value = new TreeColumn(tree, SWT.NONE);
    value.setText(UIText.ConfigurationEditorComponent_ValueColumnHeader);
    value.setWidth(250);
    new TreeViewerColumn(tv, value).setEditingSupport(new EditingSupport(tv) {

        @Override
        protected void setValue(Object element, Object newValue) {
            Entry entry = (Entry) element;
            if (!entry.value.equals(newValue)) {
                entry.changeValue(newValue.toString());
                markDirty();
            }
        }

        @Override
        protected Object getValue(Object element) {
            return ((Entry) element).value;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return editor;
        }

        @Override
        protected boolean canEdit(Object element) {
            return editable && element instanceof Entry;
        }
    });
    tv.setContentProvider(new WorkbenchContentProvider());
    Font defaultFont;
    if (useDialogFont)
        defaultFont = JFaceResources.getDialogFont();
    else
        defaultFont = JFaceResources.getDefaultFont();
    tv.setLabelProvider(new ConfigEditorLabelProvider(defaultFont));
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    Composite buttonPanel = new Composite(main, SWT.NONE);
    GridLayoutFactory.fillDefaults().applyTo(buttonPanel);
    GridDataFactory.fillDefaults().grab(false, false).applyTo(buttonPanel);
    newValue = new Button(buttonPanel, SWT.PUSH);
    GridDataFactory.fillDefaults().applyTo(newValue);
    newValue.setText(UIText.ConfigurationEditorComponent_AddButton);
    newValue.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String suggestedKey;
            IStructuredSelection sel = (IStructuredSelection) tv.getSelection();
            Object first = sel.getFirstElement();
            if (first instanceof Section)
                suggestedKey = ((Section) first).name + DOT;
            else if (first instanceof SubSection) {
                SubSection sub = (SubSection) first;
                suggestedKey = sub.parent.name + DOT + sub.name + DOT;
            } else if (first instanceof Entry) {
                Entry entry = (Entry) first;
                if (entry.sectionparent != null)
                    suggestedKey = entry.sectionparent.name + DOT;
                else
                    suggestedKey = entry.subsectionparent.parent.name + DOT + entry.subsectionparent.name + DOT;
            } else
                suggestedKey = null;
            AddConfigEntryDialog dlg = new AddConfigEntryDialog(getShell(), suggestedKey);
            if (dlg.open() == Window.OK) {
                String result = dlg.getKey();
                if (result == null) {
                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=472110
                    return;
                }
                StringTokenizer st = new StringTokenizer(result, DOT);
                if (st.countTokens() == 2) {
                    String sectionName = st.nextToken();
                    String entryName = st.nextToken();
                    Entry entry = ((GitConfig) tv.getInput()).getEntry(sectionName, null, entryName);
                    if (entry == null)
                        editableConfig.setString(sectionName, null, entryName, dlg.getValue());
                    else
                        entry.addValue(dlg.getValue());
                    markDirty();
                } else if (st.countTokens() > 2) {
                    int n = st.countTokens();
                    String sectionName = st.nextToken();
                    StringBuilder b = new StringBuilder(st.nextToken());
                    for (int i = 0; i < n - 3; i++) {
                        b.append(DOT);
                        b.append(st.nextToken());
                    }
                    String subSectionName = b.toString();
                    String entryName = st.nextToken();
                    Entry entry = ((GitConfig) tv.getInput()).getEntry(sectionName, subSectionName, entryName);
                    if (entry == null)
                        editableConfig.setString(sectionName, subSectionName, entryName, dlg.getValue());
                    else
                        entry.addValue(dlg.getValue());
                    markDirty();
                } else
                    Activator.handleError(UIText.ConfigurationEditorComponent_WrongNumberOfTokensMessage, null, true);
            }
        }
    });
    remove = new Button(buttonPanel, SWT.PUSH);
    GridDataFactory.fillDefaults().applyTo(remove);
    remove.setText(UIText.ConfigurationEditorComponent_RemoveButton);
    remove.setToolTipText(UIText.ConfigurationEditorComponent_RemoveTooltip);
    remove.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection sel = (IStructuredSelection) tv.getSelection();
            Object first = sel.getFirstElement();
            if (first instanceof Section) {
                Section section = (Section) first;
                if (MessageDialog.openConfirm(getShell(), UIText.ConfigurationEditorComponent_RemoveSectionTitle, NLS.bind(UIText.ConfigurationEditorComponent_RemoveSectionMessage, section.name))) {
                    editableConfig.unsetSection(section.name, null);
                    markDirty();
                }
            } else if (first instanceof SubSection) {
                SubSection section = (SubSection) first;
                if (MessageDialog.openConfirm(getShell(), UIText.ConfigurationEditorComponent_RemoveSubsectionTitle, NLS.bind(UIText.ConfigurationEditorComponent_RemoveSubsectionMessage, section.parent.name + DOT + section.name))) {
                    editableConfig.unsetSection(section.parent.name, section.name);
                    markDirty();
                }
            } else if (first instanceof Entry) {
                ((Entry) first).removeValue();
                markDirty();
            }
            super.widgetSelected(e);
        }
    });
    tv.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            updateEnablement();
        }
    });
    initControlsFromConfig();
    contents = main;
    return contents;
}
Also used : TreeViewer(org.eclipse.jface.viewers.TreeViewer) CellEditor(org.eclipse.jface.viewers.CellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) Label(org.eclipse.swt.widgets.Label) EditingSupport(org.eclipse.jface.viewers.EditingSupport) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Font(org.eclipse.swt.graphics.Font) GridLayout(org.eclipse.swt.layout.GridLayout) ICellEditorListener(org.eclipse.jface.viewers.ICellEditorListener) Button(org.eclipse.swt.widgets.Button) TreeColumn(org.eclipse.swt.widgets.TreeColumn) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Tree(org.eclipse.swt.widgets.Tree) PartInitException(org.eclipse.ui.PartInitException) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Path(org.eclipse.core.runtime.Path) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Text(org.eclipse.swt.widgets.Text) UIText(org.eclipse.egit.ui.internal.UIText) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) StringTokenizer(java.util.StringTokenizer) ICellEditorValidator(org.eclipse.jface.viewers.ICellEditorValidator) IFileStore(org.eclipse.core.filesystem.IFileStore) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput)

Aggregations

EditingSupport (org.eclipse.jface.viewers.EditingSupport)22 CellEditor (org.eclipse.jface.viewers.CellEditor)18 TableViewerColumn (org.eclipse.jface.viewers.TableViewerColumn)16 TextCellEditor (org.eclipse.jface.viewers.TextCellEditor)15 GridData (org.eclipse.swt.layout.GridData)13 TableViewer (org.eclipse.jface.viewers.TableViewer)9 Composite (org.eclipse.swt.widgets.Composite)9 SelectionEvent (org.eclipse.swt.events.SelectionEvent)8 GridLayout (org.eclipse.swt.layout.GridLayout)8 ColumnLabelProvider (org.eclipse.jface.viewers.ColumnLabelProvider)7 CheckboxCellEditor (org.eclipse.jface.viewers.CheckboxCellEditor)6 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)6 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)6 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)6 Button (org.eclipse.swt.widgets.Button)6 Label (org.eclipse.swt.widgets.Label)6 TreeViewerColumn (org.eclipse.jface.viewers.TreeViewerColumn)5 Viewer (org.eclipse.jface.viewers.Viewer)5 ViewerCell (org.eclipse.jface.viewers.ViewerCell)5