Search in sources :

Example 1 with CellLabelProvider

use of org.eclipse.jface.viewers.CellLabelProvider in project sling by apache.

the class MVPEditor method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    // now add the node type dropbox-combo
    Composite header = new Composite(composite, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
    layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
    layout.numColumns = 3;
    header.setLayout(layout);
    Label label = new Label(header, SWT.WRAP);
    label.setText("Modify property " + property.getName() + ":");
    GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
    data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
    label.setLayoutData(data);
    label.setFont(parent.getFont());
    ToolBar buttonBar = new ToolBar(header, SWT.NONE);
    ToolItem invisible = new ToolItem(buttonBar, SWT.NONE);
    ToolItem plus = new ToolItem(buttonBar, SWT.NONE);
    plus.setImage(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_ADD).createImage());
    plus.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            plus();
        }
    });
    final ToolItem minus = new ToolItem(buttonBar, SWT.NONE);
    minus.setImage(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE).createImage());
    minus.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            minus();
        }
    });
    minus.setEnabled(false);
    Composite tableParent = new Composite(composite, SWT.NONE);
    final GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.heightHint = 150;
    tableParent.setLayoutData(layoutData);
    TableColumnLayout tableLayout = new TableColumnLayout();
    tableParent.setLayout(tableLayout);
    viewer = new TableViewer(tableParent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    viewer.getTable().setLinesVisible(true);
    viewer.getTable().setHeaderVisible(true);
    // accessing property here directly, instead of going via (JcrProperty)inputElement;
    String[] rawLines = property.getValuesAsString();
    // convert raw lines to Line objects for easier editing management
    for (int i = 0; i < rawLines.length; i++) {
        lines.add(new Line(rawLines[i]));
    }
    viewer.setContentProvider(new IStructuredContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @Override
        public void dispose() {
        }

        @Override
        public Object[] getElements(Object inputElement) {
            return lines.toArray();
        }
    });
    TableViewerColumn column0 = new TableViewerColumn(viewer, SWT.NONE);
    column0.getColumn().setText("Type");
    column0.getColumn().setResizable(true);
    column0.getColumn().setWidth(100);
    tableLayout.setColumnData(column0.getColumn(), new ColumnWeightData(20, 100));
    column0.setLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            try {
                cell.setText(property.getTypeAsString());
                cell.setForeground(greyColor);
            } catch (Exception e) {
                cell.setText("n/a");
                cell.setForeground(greyColor);
            }
        }
    });
    TableViewerColumn column1 = new TableViewerColumn(viewer, SWT.NONE);
    column1.getColumn().setText("Value");
    column1.getColumn().setResizable(true);
    column1.getColumn().setWidth(200);
    tableLayout.setColumnData(column1.getColumn(), new ColumnWeightData(80, 200));
    column1.setLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            Line line = (Line) cell.getElement();
            cell.setText(line.getValue());
        }
    });
    column1.setEditingSupport(new EditingSupport(viewer) {

        @Override
        protected void setValue(Object element, Object value) {
            Line line = (Line) element;
            line.setValue(String.valueOf(value));
            // trigger a refresh:
            viewer.setInput(property);
        }

        @Override
        protected Object getValue(Object element) {
            final Line line = (Line) element;
            final String value = line.getValue();
            System.out.println("Value=" + value);
            return value;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new TextCellEditor(viewer.getTable());
        }

        @Override
        protected boolean canEdit(Object element) {
            // all values are editable
            return true;
        }
    });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            final ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection iss = (IStructuredSelection) selection;
                if (iss.isEmpty()) {
                    minus.setEnabled(false);
                } else {
                    minus.setEnabled(true);
                }
            } else {
                minus.setEnabled(false);
            }
        }
    });
    viewer.setInput(property);
    return composite;
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) CellEditor(org.eclipse.jface.viewers.CellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) Label(org.eclipse.swt.widgets.Label) EditingSupport(org.eclipse.jface.viewers.EditingSupport) TableViewer(org.eclipse.jface.viewers.TableViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) GridLayout(org.eclipse.swt.layout.GridLayout) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ISelection(org.eclipse.jface.viewers.ISelection) ToolItem(org.eclipse.swt.widgets.ToolItem) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) CellLabelProvider(org.eclipse.jface.viewers.CellLabelProvider) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) ViewerCell(org.eclipse.jface.viewers.ViewerCell) GridData(org.eclipse.swt.layout.GridData) ToolBar(org.eclipse.swt.widgets.ToolBar) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 2 with CellLabelProvider

use of org.eclipse.jface.viewers.CellLabelProvider in project sling by apache.

the class JcrPropertiesView method createPartControl.

/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
public void createPartControl(Composite parent) {
    SyncDirManager.registerUpdateListener(new UpdateHandler() {

        @Override
        public void syncDirUpdated(SyncDir syncDir) {
            refreshContent();
        }
    });
    mainControl = new Composite(parent, SWT.NONE);
    final GridLayout gridLayout = new GridLayout(1, true);
    mainControl.setLayout(gridLayout);
    if (getViewSite() != null) {
        titleLabel = new Label(mainControl, SWT.WRAP);
        titleLabel.setText("");
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        titleLabel.setLayoutData(data);
        Label horizontalLine = new Label(mainControl, SWT.SEPARATOR | SWT.HORIZONTAL);
        data = new GridData(GridData.FILL_HORIZONTAL);
        horizontalLine.setLayoutData(data);
    }
    Font font;
    if (!JFaceResources.getFontRegistry().hasValueFor(TITLE_FONT)) {
        FontData[] fontData = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT).getFontData();
        /* title font is 2pt larger than that used in the tabs. */
        fontData[0].setHeight(fontData[0].getHeight() + 2);
        JFaceResources.getFontRegistry().put(TITLE_FONT, fontData);
    }
    font = JFaceResources.getFont(TITLE_FONT);
    if (titleLabel != null) {
        titleLabel.setFont(font);
    }
    Composite tableParent = new Composite(mainControl, SWT.NONE);
    //        tableParent.setBackground(new Color(Display.getDefault(), 100,20,180));
    GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    // shrink to min - table settings will resize to correct ratios
    tableLayoutData.widthHint = 1;
    tableLayoutData.heightHint = SWT.DEFAULT;
    tableParent.setLayoutData(tableLayoutData);
    TableColumnLayout tableLayout = new TableColumnLayout() {

        @Override
        protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
            Point p = super.computeSize(composite, wHint, hHint, flushCache);
            return new Point(p.x, p.y);
        }
    };
    tableParent.setLayout(tableLayout);
    viewer = new TableViewer(tableParent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.HIDE_SELECTION | SWT.FULL_SELECTION);
    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new FocusCellOwnerDrawHighlighter(viewer), new CellNavigationStrategy());
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(viewer) {

        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            resetLastValueEdited();
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR) || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
        }
    };
    int features = ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION | ColumnViewerEditor.KEEP_EDITOR_ON_DOUBLE_CLICK;
    TableViewerEditor.create(viewer, focusCellManager, actSupport, features);
    viewer.getTable().setLinesVisible(true);
    viewer.getTable().setHeaderVisible(true);
    viewer.setContentProvider(new ViewContentProvider());
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            final ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection iss = (IStructuredSelection) selection;
                if (iss.isEmpty()) {
                    deleteAction.setEnabled(false);
                } else {
                    deleteAction.setEnabled(true);
                }
            } else {
                deleteAction.setEnabled(false);
            }
        }
    });
    CellLabelProvider clp = new JcrCellLabelProvider(viewer);
    TableViewerColumn column0 = new TableViewerColumn(viewer, SWT.NONE);
    column0.getColumn().setText("Name");
    column0.getColumn().setResizable(true);
    column0.getColumn().setWidth(200);
    tableLayout.setColumnData(column0.getColumn(), new ColumnWeightData(30, 140));
    final TableViewerColumn column1 = new TableViewerColumn(viewer, SWT.NONE);
    column1.getColumn().setText("Type");
    column1.getColumn().setResizable(true);
    column1.getColumn().setWidth(300);
    column1.setLabelProvider(clp);
    tableLayout.setColumnData(column1.getColumn(), new ColumnWeightData(10, 80));
    final TableViewerColumn column2 = new TableViewerColumn(viewer, SWT.NONE);
    column2.getColumn().setText("Value");
    column2.getColumn().setResizable(true);
    column2.getColumn().setWidth(300);
    tableLayout.setColumnData(column2.getColumn(), new ColumnWeightData(70, 220));
    final TableViewerColumn column3 = new TableViewerColumn(viewer, SWT.NONE);
    column3.getColumn().setText("Protected");
    column3.getColumn().setResizable(true);
    column3.getColumn().setWidth(300);
    column3.setLabelProvider(clp);
    tableLayout.setColumnData(column3.getColumn(), new ColumnWeightData(5, 57));
    final TableViewerColumn column4 = new TableViewerColumn(viewer, SWT.NONE);
    column4.getColumn().setText("Mandatory");
    column4.getColumn().setResizable(true);
    column4.getColumn().setWidth(300);
    column4.setLabelProvider(clp);
    tableLayout.setColumnData(column4.getColumn(), new ColumnWeightData(5, 62));
    final TableViewerColumn column5 = new TableViewerColumn(viewer, SWT.NONE);
    column5.getColumn().setText("Multiple");
    column5.getColumn().setResizable(true);
    column5.getColumn().setWidth(300);
    column5.setLabelProvider(clp);
    tableLayout.setColumnData(column5.getColumn(), new ColumnWeightData(5, 82));
    final TableViewerColumn column6 = new TableViewerColumn(viewer, SWT.NONE);
    column6.getColumn().setText("Auto Created");
    column6.getColumn().setResizable(true);
    column6.getColumn().setWidth(300);
    column6.setLabelProvider(clp);
    tableLayout.setColumnData(column6.getColumn(), new ColumnWeightData(5, 77));
    column0.setLabelProvider(clp);
    column0.setEditingSupport(new JcrEditingSupport(this, viewer, ColumnId.NAME));
    column1.setLabelProvider(clp);
    column1.setEditingSupport(new JcrEditingSupport(this, viewer, ColumnId.TYPE));
    column2.setLabelProvider(clp);
    column2.setEditingSupport(new JcrEditingSupport(this, viewer, ColumnId.VALUE));
    column5.setEditingSupport(new JcrEditingSupport(this, viewer, ColumnId.MULTIPLE));
    // Create the help context id for the viewer's control
    PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "org.apache.sling.ide.eclipse-ui.viewer");
    makeActions();
    hookContextMenu();
    hookDoubleClickAction();
    contributeToActionBars();
    listener = new ISelectionListener() {

        @Override
        public void selectionChanged(IWorkbenchPart part, ISelection selection) {
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection iss = (IStructuredSelection) selection;
                Object firstElem = iss.getFirstElement();
                if (firstElem instanceof JcrNode) {
                    JcrNode jcrNode = (JcrNode) firstElem;
                    setInput(jcrNode);
                    return;
                }
            }
        }
    };
    if (getViewSite() != null) {
        getViewSite().getPage().addSelectionListener(listener);
        final ISelection selection = getViewSite().getPage().getSelection();
        Display.getCurrent().asyncExec(new Runnable() {

            @Override
            public void run() {
                listener.selectionChanged(null, selection);
            }
        });
    }
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) Label(org.eclipse.swt.widgets.Label) SyncDir(org.apache.sling.ide.eclipse.ui.nav.model.SyncDir) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Font(org.eclipse.swt.graphics.Font) ISelectionListener(org.eclipse.ui.ISelectionListener) GridLayout(org.eclipse.swt.layout.GridLayout) FocusCellOwnerDrawHighlighter(org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter) ColumnViewerEditorActivationStrategy(org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) CellLabelProvider(org.eclipse.jface.viewers.CellLabelProvider) UpdateHandler(org.apache.sling.ide.eclipse.ui.nav.model.UpdateHandler) Composite(org.eclipse.swt.widgets.Composite) FontData(org.eclipse.swt.graphics.FontData) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Point(org.eclipse.swt.graphics.Point) CellNavigationStrategy(org.eclipse.jface.viewers.CellNavigationStrategy) ColumnViewerEditorActivationEvent(org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent) Point(org.eclipse.swt.graphics.Point) TableViewerFocusCellManager(org.eclipse.jface.viewers.TableViewerFocusCellManager) JcrNode(org.apache.sling.ide.eclipse.ui.nav.model.JcrNode) GridData(org.eclipse.swt.layout.GridData) TableViewer(org.eclipse.jface.viewers.TableViewer)

Aggregations

TableColumnLayout (org.eclipse.jface.layout.TableColumnLayout)2 CellLabelProvider (org.eclipse.jface.viewers.CellLabelProvider)2 ColumnWeightData (org.eclipse.jface.viewers.ColumnWeightData)2 ISelection (org.eclipse.jface.viewers.ISelection)2 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)2 TableViewer (org.eclipse.jface.viewers.TableViewer)2 TableViewerColumn (org.eclipse.jface.viewers.TableViewerColumn)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Composite (org.eclipse.swt.widgets.Composite)2 Label (org.eclipse.swt.widgets.Label)2 JcrNode (org.apache.sling.ide.eclipse.ui.nav.model.JcrNode)1 SyncDir (org.apache.sling.ide.eclipse.ui.nav.model.SyncDir)1 UpdateHandler (org.apache.sling.ide.eclipse.ui.nav.model.UpdateHandler)1 CellEditor (org.eclipse.jface.viewers.CellEditor)1 CellNavigationStrategy (org.eclipse.jface.viewers.CellNavigationStrategy)1 ColumnViewerEditorActivationEvent (org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent)1 ColumnViewerEditorActivationStrategy (org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy)1