Example usage for org.eclipse.jface.viewers StructuredViewer update

List of usage examples for org.eclipse.jface.viewers StructuredViewer update

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers StructuredViewer update.

Prototype

public void update(Object element, String[] properties) 

Source Link

Document

Updates the given element's presentation when one or more of its properties changes.

Usage

From source file:gov.nasa.ensemble.common.ui.StructuredViewerUtils.java

License:Open Source License

/**
 * Performs the update on the viewer's control's display thread. Properly handles disposed controls.
 * // w  w w  . java2 s .c  o  m
 * @param structuredViewer
 * @param element
 * @param properties
 * 
 * @see StructuredViewer#update(Object element, String[] properties)
 */
public static void update(final StructuredViewer structuredViewer, final Object element,
        final String[] properties) {
    final Control control = structuredViewer.getControl();
    if ((control != null) && (!control.isDisposed())) {
        control.getDisplay().asyncExec(new Runnable() {
            @Override
            public void run() {
                if (!control.isDisposed()) {
                    structuredViewer.update(element, properties);
                }
            }
        });
    }
}

From source file:org.eclipse.e4mf.edit.ui.provider.NotifyChangedToViewerRefresh.java

License:Open Source License

public void refreshStructuredViewer(StructuredViewer viewer, Object object, int eventType, Object feature,
        Object oldValue, Object newValue, int index) {
    switch (eventType) {
    case Notification.ADD:
    case Notification.ADD_MANY:
    case Notification.REMOVE:
    case Notification.REMOVE_MANY:
    case Notification.MOVE: {
        viewer.refresh(object);/*  w  ww . j av  a2 s  . com*/
        break;
    }
    case Notification.UNSET:
    case Notification.SET: {
        if (feature instanceof EReference) {
            viewer.refresh(object);
        } else {
            viewer.update(object,
                    feature instanceof ENamedElement ? new String[] { ((ENamedElement) feature).getName() }
                            : null);
        }
        break;
    }
    //    case Notification.TOUCH:
    default: {
        refreshViewer(viewer, object, eventType, feature, oldValue, newValue, index);
        break;
    }
    }
}

From source file:org.eclipse.emf.cdo.explorer.ui.ViewerUtil.java

License:Open Source License

public static void update(final StructuredViewer viewer, final Object element, boolean async) {
    if (viewer != null) {
        final Control control = viewer.getControl();
        if (!control.isDisposed()) {
            Runnable runnable = new Runnable() {
                public void run() {
                    if (!control.isDisposed()) {
                        if (element instanceof Object[]) {
                            Object[] array = (Object[]) element;
                            viewer.update(array, null);
                        } else if (element instanceof Collection) {
                            Collection<?> collection = (Collection<?>) element;
                            viewer.update(collection.toArray(), null);
                        } else {
                            viewer.update(element, null);
                        }/*w ww.j  a v a  2s  .co  m*/
                    }
                }
            };

            Display display = control.getDisplay();
            if (async) {
                display.asyncExec(runnable);
            } else {
                display.syncExec(runnable);
            }
        }
    }
}

From source file:org.eclipse.emf.ecp.internal.ui.view.emf.NotifyChangedToViewerRefresh.java

License:Open Source License

public void refreshStructuredViewer(StructuredViewer viewer, Object object, int eventType, Object feature,
        Object oldValue, Object newValue, int index) {
    switch (eventType) {
    case Notification.ADD:
    case Notification.ADD_MANY:
    case Notification.REMOVE:
    case Notification.REMOVE_MANY:
    case Notification.MOVE: {
        viewer.refresh(object);/*from  www . j a  v  a 2s.c om*/
        break;
    }
    case Notification.UNSET:
    case Notification.SET: {
        if (feature instanceof EReference) {
            viewer.refresh(object);
        } else {
            viewer.update(object,
                    feature instanceof ENamedElement ? new String[] { ((ENamedElement) feature).getName() }
                            : null);
        }
        break;
    }
    // case Notification.TOUCH:
    default: {
        refreshViewer(viewer, object, eventType, feature, oldValue, newValue, index);
        break;
    }
    }
}

From source file:org.eclipse.jubula.client.ui.rcp.controllers.dnd.LocalSelectionClipboardTransfer.java

License:Open Source License

/**
 * Sets the transfer and source data for local use.
 * /*from www  .ja  v a 2  s  .  c  om*/
 * @param sel
 *            The transfer data. A value of <code>null</code> clears the
 *            transfer data.
 * @param source
 *            The source to set. A value of <code>null</code> clears the
 *            source data.
 * @param otherViewersToRefresh
 *            Viewers that should be updated (in addition to the source 
 *            viewer) after the selection change. May be <code>null</code>
 *            or empty, in which case no additional viewers will be updated.
 * @param isItCut <code>true</code> when the action is cut.
 */
public void setSelection(IStructuredSelection sel, StructuredViewer source,
        StructuredViewer[] otherViewersToRefresh, boolean isItCut) {

    IStructuredSelection oldSelection = getSelection();
    StructuredViewer oldSource = getSource();
    StructuredViewer[] oldViewersToRefresh = getOtherViewersToRefresh();
    setSelection(sel, isItCut);
    setSource(source);
    setOtherViewersToRefresh(otherViewersToRefresh);

    if (oldSource != null && !oldSource.getControl().isDisposed() && oldSelection != null) {
        // Allows the other item previously marked as "cut" to 
        // now be marked as normal.
        oldSource.update(oldSelection.toArray(), null);
        for (StructuredViewer toRefresh : oldViewersToRefresh) {
            if (toRefresh != oldSource) {
                toRefresh.update(oldSelection.toArray(), null);
            }
        }
    }

    // Refresh the viewer so that it can show the selection as "cut".
    if (source != null) {
        source.update(sel.toArray(), null);
        for (StructuredViewer toRefresh : getOtherViewersToRefresh()) {
            if (toRefresh != source) {
                toRefresh.update(sel.toArray(), null);
            }
        }
    }
}

From source file:org.eclipse.jubula.client.ui.rcp.provider.contentprovider.objectmapping.OMEditorTreeContentProvider.java

License:Open Source License

/**
 * {@inheritDoc}/* ww  w. ja  va 2s.c  o m*/
 */
public void inputChanged(final Viewer viewer, Object oldInput, final Object newInput) {
    Validate.isTrue(viewer instanceof TreeViewer);
    m_childToParentMap.clear();
    DataEventDispatcher ded = DataEventDispatcher.getInstance();
    if (m_modelListener != null) {
        ded.removeDataChangedListener(m_modelListener);
        m_modelListener = null;
    }

    if (newInput != null) {
        m_modelListener = new IDataChangedListener() {
            /** {@inheritDoc} */
            public void handleDataChanged(DataChangedEvent... events) {
                for (DataChangedEvent e : events) {
                    handleDataChanged(e.getPo(), e.getDataState(), e.getUpdateState());
                }
            }

            public void handleDataChanged(IPersistentObject po, DataState dataState, UpdateState updateState) {

                if (updateState != UpdateState.notInEditor) {
                    StructuredViewer structuredViewer = (StructuredViewer) viewer;
                    if (dataState == DataState.StructureModified) {
                        boolean objectsAreEqual = structuredViewer.getComparer() != null
                                ? structuredViewer.getComparer().equals(newInput, po)
                                : newInput.equals(po);
                        if (objectsAreEqual || po instanceof IAUTMainPO || po instanceof IObjectMappingPO) {
                            structuredViewer.refresh();
                        } else {
                            structuredViewer.refresh(po);
                        }
                    } else if (dataState == DataState.Renamed) {
                        structuredViewer.update(po, null);
                    }
                }
            }
        };
        ded.addDataChangedListener(m_modelListener, false);
    }
}

From source file:org.eclipse.net4j.util.ui.UIUtil.java

License:Open Source License

/**
 * @since 3.5/* ww  w .j  av a 2s.  c o m*/
 */
public static void updateElements(final StructuredViewer viewer, final Object element) {
    try {
        Control control = viewer.getControl();
        if (!control.isDisposed()) {
            control.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    try {
                        if (element instanceof Object[]) {
                            Object[] elements = (Object[]) element;
                            viewer.update(elements, null);
                        } else {
                            viewer.update(element, null);
                        }
                    } catch (Exception ignore) {
                    }
                }
            });
        }
    } catch (Exception ignore) {
    }
}

From source file:org.eclipse.net4j.util.ui.views.MultiViewersView.java

License:Open Source License

public void updateLabels(final Object element) {
    try {//  w  w  w.j a  v  a2s .  c om
        final StructuredViewer viewer = getCurrentViewer();
        if (viewer != null) {
            getDisplay().asyncExec(new Runnable() {
                public void run() {
                    try {
                        viewer.update(element, null);
                    } catch (RuntimeException ignore) {
                    }
                }
            });
        }
    } catch (RuntimeException ignore) {
    }
}

From source file:org.eclipse.oomph.internal.ui.FindAndReplaceTarget.java

License:Open Source License

public void setScope(IRegion scope) {
    // When the properties view is activated, it's sometimes given focus.
    // In that case we restore the focus to the dialog, but we need to given the scope changes that are caused by the transient focus changes.
    if (suspendScopeChanges) {
        return;//from www  .  j av  a2s . c  om
    }

    StructuredViewer viewer = getViewer();
    if (scope == null) {
        // Remember the objects that need label updating.
        Object[] selectionScopeObjectsToUpdate = selectionScopeObjects == null ? null
                : selectionScopeObjects.toArray();
        Object selectionToUpdate = selectedItem != null && selectedItem.itemPropertyDescriptor == null
                ? selectedItem.data.object
                : null;

        // The scope is set to null when the dialog loses focus.
        // We should forget about most of our state at this point.
        selectionText = null;
        selectedItem = null;
        findReplaceable = false;
        selectionScopeObjects = null;

        propertiesCleanup();

        // Update update the selection scope objects.
        if (selectionScopeObjectsToUpdate != null) {
            viewer.update(selectionScopeObjectsToUpdate, null);
        }

        // Update the selection object.
        if (selectionToUpdate != null) {
            viewer.update(selectionToUpdate, null);
        }
    } else {
        // Record the objects in the selection scope.
        // These will be painted in a special way in the first to highlight them.
        selectionScopeObjects = new HashSet<Object>();

        int depth = -1;
        for (FindAndReplaceTarget.Data data : new TextData(viewer)) {
            // If we hit the next object at the same depend, reset the depth.
            if (data.depth == depth) {
                depth = -1;
            }

            // If the object is directly in scope.
            if (selectionScope.contains(data.object)) {
                // Remember the depth if we haven't remember one already.
                if (depth == -1) {
                    depth = data.depth;
                }
            }

            // If the object is a selected object or nested under a selected object, include it.
            if (depth != -1) {
                selectionScopeObjects.add(data.object);
            }
        }

        // Hook up our decorating label provider for the current viewer.
        hookLabelProvider();

        // Eliminate the selection.
        viewer.setSelection(StructuredSelection.EMPTY);
    }
}

From source file:org.eclipse.oomph.internal.ui.FindAndReplaceTarget.java

License:Open Source License

/**
 * This records a match either initially or as a result of a find.
 */// w  w w . ja  va  2  s  .  co m
protected void setSelection(boolean preserve, StructuredViewer viewer, Data.Item item, final int start,
        Pattern pattern) {
    Object selectedObjectToUpdate = selectedItem != null && selectedItem.itemPropertyDescriptor == null
            ? selectedItem.data.object
            : null;

    // Remember the information about the item, pattern, and offset within the item of the match.
    selectedItem = item;
    selectedItemPattern = pattern;
    selectedItemStart = start;

    if (selectedObjectToUpdate != null) {
        viewer.update(selectedObjectToUpdate, null);
    }

    // There is no special tree item anymore.
    specialTreeItem = null;

    // If we haven't already done so, hook up the special label provider for providing selection feedback.
    hookLabelProvider();

    // Clean any previous stuff we did to decorate the properties view.
    propertiesCleanup();

    // In replace all mode, we don't want to provide any further feedback.
    if (replaceAllCommand == null) {
        // Select the item in the viewer, unless we're preserving the selection, i.e., during the initial feedback.
        StructuredSelection selection = new StructuredSelection(new TreePath(item.data.getPath()));
        if (!preserve) {
            viewer.setSelection(selection, true);
        }

        // If there is an active property page, update it's selection immediately.
        PropertySheetPage activePropertySheetPage = getActivePropertySheetPage();
        if (activePropertySheetPage != null) {
            activePropertySheetPage.selectionChanged(workbenchPart, selection);
        }

        // Make the properties view visible, creating it if necessary.
        IWorkbenchPartSite site = workbenchPart.getSite();
        IWorkbenchPage page = site.getPage();
        IViewPart viewPart = page.findView("org.eclipse.ui.views.PropertySheet");

        // Sometimes showing the properties view gives it focus, e.g., when the editor is maximized.
        Display display = site.getShell().getDisplay();
        Control oldFocusControl = display.getFocusControl();
        try {
            // Ignore scope changes while showing the properties view.
            suspendScopeChanges = true;
            if (item.itemPropertyDescriptor != null) {
                viewPart = page.showView("org.eclipse.ui.views.PropertySheet", null,
                        IWorkbenchPage.VIEW_VISIBLE);
                if (viewPart == null) {
                    viewPart = page.showView("org.eclipse.ui.views.PropertySheet", null,
                            IWorkbenchPage.VIEW_CREATE);
                }
            }

            // Restore the focus.
            Control newFocusControl = display.getFocusControl();
            if (oldFocusControl != newFocusControl) {
                oldFocusControl.setFocus();
            }
        } catch (PartInitException ex) {
            UIPlugin.INSTANCE.log(ex);
        } finally {
            suspendScopeChanges = false;
        }

        // If it is a property sheet, as expected...
        if (viewPart instanceof PropertySheet) {
            // And the current page is a property sheet page as expected...
            final PropertySheet propertySheet = (PropertySheet) viewPart;
            IPage currentPage = propertySheet.getCurrentPage();
            if (currentPage instanceof PropertySheetPage) {
                // And the control is a tree...
                Control control = currentPage.getControl();
                if (control instanceof Tree) {
                    final Tree tree = (Tree) control;

                    if (item.itemPropertyDescriptor != null) {
                        // Remember the filter action that we needed to check it to be able to show an advanced property.
                        Action filterAction = null;

                        // If the property has filter flags...
                        String[] filterFlags = item.itemPropertyDescriptor.getFilterFlags(item.data.object);
                        if (filterFlags != null) {
                            for (String filterFlag : filterFlags) {
                                // If the filter is one for expert property...
                                if ("org.eclipse.ui.views.properties.expert".equals(filterFlag)) {
                                    Action action = ReflectUtil.getValue(FILTER_ACTION_FIELD, currentPage);
                                    if (!action.isChecked()) {
                                        // Run the action to show advanced properties, and remember that.
                                        action.setChecked(true);
                                        action.run();
                                        filterAction = action;
                                    }
                                }
                            }
                        }

                        // Walk the tree items.
                        for (final TreeItem treeItem : tree.getItems()) {
                            // If there is an EMF property descriptor with a feature for the selected item...
                            PropertyDescriptor propertyDescriptor = getPropertyDescriptor(treeItem);
                            if (propertyDescriptor != null
                                    && propertyDescriptor.getFeature() == item.getFeature()) {
                                // Consider the label shown in the tree verses the value of the selected item...
                                String treeItemText = treeItem.getText(1);
                                String itemValue = item.value;

                                // We might need to replace the tree item's text with a special representation...
                                specialStart = -1;

                                // If they are're identical....
                                if (!treeItemText.equals(itemValue)) {
                                    // Find the match, which really must be there, do we can determine the length of the match.
                                    Matcher matcher = pattern.matcher(itemValue);
                                    if (matcher.find(start)) {
                                        // Remember this special item, because we'll want to update it after we do a replace to show the replaced text.
                                        specialTreeItem = treeItem;

                                        // If the end of the match is after the end of the tree item's text, or the strings up until the end of the match are not
                                        // identical...
                                        int end = matcher.end();
                                        if (treeItemText.length() < end || !treeItemText.substring(0, end)
                                                .equals(itemValue.substring(0, end))) {
                                            // Consider the starting point of the match, and work our way backward for 20 characters or until the preceding control
                                            // character.
                                            int begin = matcher.start();
                                            specialStart = 2;
                                            while (begin >= 0 && specialStart < 20
                                                    && !Character.isISOControl(itemValue.charAt(begin))) {
                                                ++specialStart;
                                                --begin;
                                            }

                                            // Work our way forward until the end of the string or until we hit a control character.
                                            int itemValueLength = itemValue.length();
                                            while (end < itemValueLength
                                                    && !Character.isISOControl(itemValue.charAt(end))) {
                                                ++end;
                                            }

                                            // Create a special string with ellipses at both ends.
                                            String specialText = "..." + itemValue.substring(begin + 1, end)
                                                    + "...";

                                            // But that back into the item.
                                            treeItem.setText(1, specialText);

                                            // Get the tree to redraw itself.
                                            tree.redraw();
                                        }
                                    }
                                }

                                // Create a paint listener to select the match.
                                final Listener paintItemListener = new Listener() {
                                    private void paintItem(Event event, TreeItem item, int matchStart) {
                                        String text = item.getText(1);
                                        Matcher matcher = selectedItemPattern.matcher(text);
                                        if (matchStart < text.length() && matcher.find(matchStart)) {
                                            // Compute the offset of the start of the matching, relative to the start of the text.
                                            int start = matcher.start();
                                            int x = event.gc.textExtent(text.substring(0, start)).x
                                                    + item.getTextBounds(1).x - treeItem.getBounds(1).x;

                                            // Compute the offset at the end of the match, taking into account the width of the matching text.
                                            int width = event.gc.textExtent(matcher.group()).x;
                                            event.gc.drawRectangle(event.x + x + 1, event.y, width + 1,
                                                    event.height - 1);
                                        } else if (text.endsWith("...")) {
                                            int x = event.gc.textExtent(text.substring(0, text.length() - 3)).x
                                                    + treeItem.getTextBounds(1).x - treeItem.getBounds(1).x;
                                            int width = event.gc.textExtent("...").x;
                                            event.gc.drawRectangle(event.x + x + 1, event.y, width + 1,
                                                    event.height - 1);
                                        }
                                    }

                                    public void handleEvent(Event event) {
                                        // If we're painting or special item...
                                        TreeItem item = (TreeItem) event.item;
                                        if (item == treeItem && event.index == 1) {
                                            paintItem(event, item, specialStart == -1 ? start : specialStart);
                                        }
                                    }
                                };

                                // Add the listener.
                                tree.addListener(SWT.PaintItem, paintItemListener);

                                // Set up the runnable to clean up what we've done here.
                                final PropertySheetPage propertySheetPage = (PropertySheetPage) currentPage;
                                final Action finalFilterAction = filterAction;
                                propertiesCleanup = new Runnable() {
                                    public void run() {
                                        // Remove the listener.
                                        tree.removeListener(SWT.PaintItem, paintItemListener);

                                        // If there is a filter action we toggled...
                                        if (finalFilterAction != null) {
                                            // Toggle it back, which will refresh the view.
                                            finalFilterAction.setChecked(false);
                                            finalFilterAction.run();
                                        } else {
                                            // Otherwise refresh the view.
                                            propertySheetPage.refresh();
                                        }
                                    }
                                };

                                // Select the item, and force a repaint.
                                tree.setSelection(treeItem);
                                tree.redraw();

                                // We're done.
                                return;
                            }
                        }
                    }

                    // If we didn't find it at all, clear out the selection.
                    tree.setSelection(new TreeItem[0]);
                }
            }
        }
    }
}