Search in sources :

Example 1 with ViewerColumn

use of org.eclipse.jface.viewers.ViewerColumn in project AGREE by loonwerks.

the class VariablesView method createView.

@PostConstruct
void createView(final Composite parent) {
    final Display display = parent.getDisplay();
    colorManager = new DeviceResourceManager(display);
    unchangedValueColor = colorManager.createColor(new RGB(192, 192, 192));
    constraintErrorColor = colorManager.createColor(red);
    greenImage = createColorIconImage(display, green);
    redImage = createColorIconImage(display, red);
    yellowImage = createColorIconImage(display, yellow);
    customColorImage = createColorIconImage(display, customRgb);
    // Create root composite
    final Composite root = new Composite(parent, SWT.NONE);
    final GridLayout rootLayout = new GridLayout(1, false);
    root.setLayout(rootLayout);
    // Create the variable tree viewer
    treeViewer = new VariablesTreeViewer(root, SWT.FULL_SELECTION);
    // Enable tooltip support
    ColumnViewerToolTipSupport.enableFor(treeViewer);
    treeSelectionTracker = new TreeSelectionTracker(treeViewer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
    treeViewer.setUseHashlookup(true);
    final VariableContentProvider contentProvider = new VariableContentProvider();
    treeViewer.setContentProvider(contentProvider);
    treeViewer.setInput(null);
    treeViewer.setComparator(new ViewerComparator() {

        @Override
        public int compare(final Viewer viewer, final Object e1, final Object e2) {
            if (contentProvider.currentState == null) {
                return 0;
            }
            // Sort component instances before other variables
            final InstanceObject io1 = contentProvider.currentState.getEngineState().getElementInstanceObject(e1);
            final InstanceObject io2 = contentProvider.currentState.getEngineState().getElementInstanceObject(e2);
            if (io1 instanceof ComponentInstance && !(io2 instanceof ComponentInstance)) {
                return -1;
            } else if (io2 instanceof ComponentInstance && !(io1 instanceof ComponentInstance)) {
                return 1;
            }
            // Sort feature instances before the remainder
            if (io1 instanceof FeatureInstance && !(io2 instanceof FeatureInstance)) {
                return -1;
            } else if (io2 instanceof FeatureInstance && !(io1 instanceof FeatureInstance)) {
                return 1;
            }
            final String n1 = contentProvider.currentState.getEngineState().getElementName(e1);
            final String n2 = contentProvider.currentState.getEngineState().getElementName(e2);
            return getComparator().compare(n1, n2);
        }
    });
    treeViewer.setFilters(new ViewerFilter[] { new ViewerFilter() {

        @Override
        public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
            return !contentProvider.currentState.getEngineState().isElementHidden(element);
        }
    } });
    // Create columns
    final TreeViewerColumn nameColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
    nameColumn.getColumn().setWidth(200);
    nameColumn.getColumn().setText("Name");
    nameColumn.getColumn().addControlListener(resizeListener);
    nameColumn.setLabelProvider(new StyledCellLabelProvider() {

        @Override
        public void update(final ViewerCell cell) {
            cell.setText(getText(cell.getElement()));
            cell.setImage(getImage(cell.getElement()));
        }

        private String getText(final Object element) {
            // Get the name of the frame state element
            final SimulatorState state = contentProvider.getCurrentState();
            if (state == null) {
                return "";
            }
            return state.getEngineState().getElementName(element);
        }

        private Image getImage(final Object element) {
            final SimulatorState state = contentProvider.getCurrentState();
            if (state == null) {
                return null;
            }
            final InstanceObject instanceObject = state.getEngineState().getElementInstanceObject(element);
            return instanceObject == null ? null : UiUtil.getModelElementLabelProvider().getImage(instanceObject);
        }
    });
    final TreeViewerColumn nextValueColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
    nextValueColumn.getColumn().setWidth(100);
    nextValueColumn.getColumn().setText("Next Value");
    nextValueColumn.setLabelProvider(new OwnerDrawLabelProvider() {

        @Override
        public void initialize(final ColumnViewer viewer, final ViewerColumn column) {
            super.initialize(viewer, column, true);
        }

        @Override
        public void update(final ViewerCell cell) {
            final String constraintTxt = getConstraintText(cell.getElement());
            final String txt;
            final Color foreground;
            if (constraintTxt == null) {
                // Attempt to retrieve the value for the next frame from from the engine state.
                final SimulatorState state = contentProvider.getCurrentState();
                if (state == null) {
                    txt = "";
                } else {
                    final SimulationEngineState engineState = state.getEngineState();
                    txt = ValueFormatter.formatValue(engineState.getElementValue(engineState.getNumberOfFrames(), cell.getElement()));
                }
                foreground = unchangedValueColor;
            } else {
                txt = constraintTxt;
                foreground = null;
            }
            cell.setText(txt);
            cell.setForeground(foreground);
            super.update(cell);
        }

        private String getConstraintText(final Object element) {
            // Return the invalid value if the input that was entered is not correct
            final ConstraintError constraintError = elementToConstraintErrorMap.get(element);
            if (constraintError != null) {
                return constraintError.constraint;
            }
            final SimulationEngineState engineState = simulationUiService.getCurrentState().getEngineState();
            if (engineState == null) {
                return null;
            }
            final InputConstraint ic = engineState.getElementInputConstraintForNextFrame(element);
            if (ic == null) {
                return null;
            }
            return inputConstraintHelper.unparse(ic);
        }

        @Override
        public String getToolTipText(final Object element) {
            final ConstraintError constraintError = elementToConstraintErrorMap.get(element);
            if (constraintError == null) {
                return super.getToolTipText(element);
            } else {
                return constraintError.errorMessage;
            }
        }

        @Override
        protected void measure(final Event event, final Object element) {
        }

        @Override
        protected void erase(final Event event, final Object element) {
            // Color cells which contain invalid values
            if (elementToConstraintErrorMap.containsKey(element)) {
                final GC gc = event.gc;
                final Color oldBackground = event.gc.getBackground();
                gc.setBackground(constraintErrorColor);
                final Rectangle bounds = event.getBounds();
                gc.fillRectangle(bounds);
                gc.setBackground(oldBackground);
                // Ensure that selection and hover indicators are not drawn for the highlighted cell
                event.detail &= ~SWT.SELECTED;
                event.detail &= ~SWT.HOT;
            }
            // Disable drawing foreground. paint() will draw the foreground text in order to fix coloring issues when the row is selected.
            event.detail &= ~SWT.FOREGROUND;
        }

        @Override
        protected void paint(final Event event, final Object element) {
            // Draw just the text with the appropriate background color
            final TreeItem item = (TreeItem) event.item;
            final Rectangle textBounds = item.getTextBounds(event.index);
            final GC gc = event.gc;
            final String txt = item.getText(event.index);
            gc.setForeground(item.getForeground(event.index));
            String drawTxt = txt;
            // Check the width of the text and truncate it as necessary
            final int maxWidth = Math.max(gc.getClipping().width - 6, textBounds.width);
            if (maxWidth > 0) {
                String truncatedTxt = txt;
                while (gc.textExtent(drawTxt).x > maxWidth && truncatedTxt.length() > 0) {
                    truncatedTxt = truncatedTxt.substring(0, truncatedTxt.length() - 1);
                    drawTxt = truncatedTxt + "...";
                }
                gc.drawString(drawTxt, textBounds.x, textBounds.y, true);
            }
        }
    });
    nextValueEditingSupport = new NextValueEditingSupport(simulationUiService, treeViewer);
    nextValueColumn.setEditingSupport(nextValueEditingSupport);
    nextValueColumn.getColumn().addControlListener(resizeListener);
    // Make headers and lines visible
    final Tree tree = treeViewer.getTree();
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    createContextMenu(tree);
    // Listen for tree resizing
    tree.addControlListener(resizeListener);
    // Create the slider
    frameSlider = new Slider(root, SWT.HORIZONTAL);
    frameSlider.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            if (!editingTreeViewer) {
                startEditingTreeViewer();
                frameScrollOffset = frameSlider.getMaximum() - frameSlider.getSelection() - frameSlider.getThumb();
                update();
                stopEditingTreeViewer();
            }
        }
    });
    GridDataFactory.fillDefaults().grab(true, false).applyTo(frameSlider);
    // Add listener and populate UI with current state
    simulationUiService.addStateChangeListener(stateListener);
    stateListener.onSimulatorStateChanged(simulationUiService.getCurrentState());
}
Also used : ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Slider(org.eclipse.swt.widgets.Slider) FeatureInstance(org.osate.aadl2.instance.FeatureInstance) SimulationEngineState(edu.uah.rsesc.aadlsimulator.SimulationEngineState) TreeItem(org.eclipse.swt.widgets.TreeItem) ColumnViewer(org.eclipse.jface.viewers.ColumnViewer) Rectangle(org.eclipse.swt.graphics.Rectangle) ColumnViewer(org.eclipse.jface.viewers.ColumnViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) Image(org.eclipse.swt.graphics.Image) OwnerDrawLabelProvider(org.eclipse.jface.viewers.OwnerDrawLabelProvider) InstanceObject(org.osate.aadl2.instance.InstanceObject) GridLayout(org.eclipse.swt.layout.GridLayout) ComponentInstance(org.osate.aadl2.instance.ComponentInstance) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Tree(org.eclipse.swt.widgets.Tree) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint) GC(org.eclipse.swt.graphics.GC) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) DeviceResourceManager(org.eclipse.jface.resource.DeviceResourceManager) Composite(org.eclipse.swt.widgets.Composite) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) Color(org.eclipse.swt.graphics.Color) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SimulatorState(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState) RGB(org.eclipse.swt.graphics.RGB) ViewerColumn(org.eclipse.jface.viewers.ViewerColumn) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) ViewerCell(org.eclipse.jface.viewers.ViewerCell) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) ControlEvent(org.eclipse.swt.events.ControlEvent) BeforeStepForwardEvent(edu.uah.rsesc.aadlsimulator.ui.events.BeforeStepForwardEvent) Event(org.eclipse.swt.widgets.Event) SelectionEvent(org.eclipse.swt.events.SelectionEvent) InstanceObject(org.osate.aadl2.instance.InstanceObject) Display(org.eclipse.swt.widgets.Display) PostConstruct(javax.annotation.PostConstruct)

Example 2 with ViewerColumn

use of org.eclipse.jface.viewers.ViewerColumn in project org.eclipse.rap by eclipse-rap.

the class RowTemplateExample method createViewerColumn.

private ViewerColumn createViewerColumn(final ColumnViewer viewer, String name, int width) {
    ViewerColumn viewerColumn = null;
    if (viewer instanceof TableViewer) {
        TableViewer tableViewer = (TableViewer) viewer;
        TableViewerColumn tableColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        tableColumn.getColumn().setWidth(width);
        tableColumn.getColumn().setText(name);
        viewerColumn = tableColumn;
    } else if (viewer instanceof TreeViewer) {
        TreeViewer treeViewer = (TreeViewer) viewer;
        TreeViewerColumn treeColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
        treeColumn.getColumn().setWidth(width);
        treeColumn.getColumn().setText(name);
        viewerColumn = treeColumn;
    }
    return viewerColumn;
}
Also used : TreeViewer(org.eclipse.jface.viewers.TreeViewer) ViewerColumn(org.eclipse.jface.viewers.ViewerColumn) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) TableViewer(org.eclipse.jface.viewers.TableViewer) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn)

Example 3 with ViewerColumn

use of org.eclipse.jface.viewers.ViewerColumn in project org.eclipse.rap by eclipse-rap.

the class TreeViewerColumnTest method getViewerColumn.

private ViewerColumn getViewerColumn(ColumnViewer viewer, int index) {
    Method method;
    try {
        method = ColumnViewer.class.getDeclaredMethod("getViewerColumn", new Class[] { int.class });
        method.setAccessible(true);
        return (ViewerColumn) method.invoke(viewer, new Object[] { new Integer(index) });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ColumnViewer(org.eclipse.jface.viewers.ColumnViewer) Method(java.lang.reflect.Method) ViewerColumn(org.eclipse.jface.viewers.ViewerColumn)

Example 4 with ViewerColumn

use of org.eclipse.jface.viewers.ViewerColumn in project org.eclipse.rap by eclipse-rap.

the class RowTemplateExample method addLastNameColumn.

private void addLastNameColumn(ColumnViewer viewer) {
    ViewerColumn lastNameColumn = createViewerColumn(viewer, "Last Name", 200);
    lastNameColumn.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            Person p = (Person) element;
            return p.getLastName();
        }
    });
}
Also used : ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) ViewerColumn(org.eclipse.jface.viewers.ViewerColumn) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) Person(org.eclipse.rap.examples.pages.internal.Person)

Example 5 with ViewerColumn

use of org.eclipse.jface.viewers.ViewerColumn in project org.eclipse.rap by eclipse-rap.

the class RowTemplateExample method addPhoneColumn.

private void addPhoneColumn(ColumnViewer viewer) {
    ViewerColumn pohoneColumn = createViewerColumn(viewer, "Phone", 130);
    pohoneColumn.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            Person p = (Person) element;
            return p.getPhone();
        }
    });
}
Also used : ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) ViewerColumn(org.eclipse.jface.viewers.ViewerColumn) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) Person(org.eclipse.rap.examples.pages.internal.Person)

Aggregations

ViewerColumn (org.eclipse.jface.viewers.ViewerColumn)8 TreeViewerColumn (org.eclipse.jface.viewers.TreeViewerColumn)6 TableViewerColumn (org.eclipse.jface.viewers.TableViewerColumn)5 ColumnLabelProvider (org.eclipse.jface.viewers.ColumnLabelProvider)4 Person (org.eclipse.rap.examples.pages.internal.Person)4 ColumnViewer (org.eclipse.jface.viewers.ColumnViewer)3 Method (java.lang.reflect.Method)2 TreeViewer (org.eclipse.jface.viewers.TreeViewer)2 Image (org.eclipse.swt.graphics.Image)2 SimulationEngineState (edu.uah.rsesc.aadlsimulator.SimulationEngineState)1 BeforeStepForwardEvent (edu.uah.rsesc.aadlsimulator.ui.events.BeforeStepForwardEvent)1 SimulatorState (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState)1 InputConstraint (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint)1 PostConstruct (javax.annotation.PostConstruct)1 DeviceResourceManager (org.eclipse.jface.resource.DeviceResourceManager)1 OwnerDrawLabelProvider (org.eclipse.jface.viewers.OwnerDrawLabelProvider)1 StyledCellLabelProvider (org.eclipse.jface.viewers.StyledCellLabelProvider)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 Viewer (org.eclipse.jface.viewers.Viewer)1 ViewerCell (org.eclipse.jface.viewers.ViewerCell)1