diff org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/viewers/TreeViewerUpdater.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/viewers/TreeViewerUpdater.d	Tue Apr 14 11:35:29 2009 +0200
@@ -0,0 +1,178 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Matthew Hall and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Matthew Hall - initial API and implementation (bug 207858)
+ *     Matthew Hall - bug 226765
+ ******************************************************************************/
+
+module org.eclipse.jface.internal.databinding.viewers.TreeViewerUpdater;
+
+import java.lang.all;
+
+import org.eclipse.jface.util.Util;
+import org.eclipse.jface.viewers.AbstractTreeViewer;
+import org.eclipse.jface.viewers.IElementComparer;
+import org.eclipse.jface.viewers.ITreeSelection;
+import org.eclipse.jface.viewers.TreePath;
+import org.eclipse.jface.viewers.TreeViewer;
+
+/**
+ * NON-API - An interface for sending updates to an {@link AbstractTreeViewer}.
+ * 
+ * @since 1.2
+ */
+public class TreeViewerUpdater {
+    private final AbstractTreeViewer viewer;
+    private final TreeViewer treeViewer;
+
+    /**
+     * Constructs an ITreeViewerUpdater for updating the given viewer.
+     * 
+     * @param viewer
+     *            the viewer that will be updated
+     */
+    public this(AbstractTreeViewer viewer) {
+        this.viewer = viewer;
+        if (null !is cast(TreeViewer)viewer)
+            treeViewer = cast(TreeViewer) viewer;
+        else
+            treeViewer = null;
+    }
+
+    /**
+     * Insert the element into the viewer as a child of the specified parent
+     * element, at the specified position.
+     * 
+     * @param parent
+     *            the parent of the element being inserted
+     * @param element
+     *            the element to insert
+     * @param position
+     *            the position where the element is inserted
+     */
+    public void insert(Object parent, Object element, int position) {
+        viewer.insert(parent, element, position);
+    }
+
+    /**
+     * Replaces the specified element whenever it appears as a child of the
+     * specified parent element, at the given position with the new element.
+     * 
+     * @param parent
+     *            the parent of the element being replaced
+     * @param oldElement
+     *            the element being replaced
+     * @param newElement
+     *            the element that replaces <code>oldElement</code>
+     * @param position
+     *            the position of the element being replaced.
+     */
+    public void replace(Object parent, Object oldElement, Object newElement,
+            int position) {
+        if (treeViewer !is null && viewer.getComparator() is null
+                && viewer.getFilters().length is 0) {
+            treeViewer.replace(parent, position, newElement);
+        } else {
+            remove(parent, oldElement, position);
+            insert(parent, newElement, position);
+        }
+    }
+
+    /**
+     * Moves the specified element from the specified old position to the
+     * specified new position, whenever it appears as a child of the specified
+     * parent element. No action is taken if the viewer has a sorter or
+     * filter(s).
+     * 
+     * @param parent
+     *            the parent of the element being moved
+     * @param element
+     *            the element being moved
+     * @param oldPosition
+     *            the position of the element before it is moved
+     * @param newPosition
+     *            the position of the element after it is moved
+     */
+    public void move(Object parent, Object element, int oldPosition,
+            int newPosition) {
+        if (viewer.getComparator() is null && viewer.getFilters().length is 0) {
+
+            ITreeSelection selection = cast(ITreeSelection) viewer.getSelection();
+
+            remove(parent, element, oldPosition);
+            insert(parent, element, newPosition);
+
+            // Preserve selection
+            if (!selection.isEmpty()) {
+                // If the moved element is selected (or an ancestor of a
+                // selected element), restore the selection.
+                IElementComparer comparer = viewer.getComparer();
+                TreePath[] paths = selection.getPaths();
+                outer: for (int i = 0; i < paths.length; i++) {
+                    TreePath path = paths[i];
+                    for (int j = 0; j < path.getSegmentCount(); j++) {
+                        Object pathElement = path.getSegment(j);
+                        if (comparer is null ? Util
+                                .equals(element, pathElement) : comparer
+                                .equals(element, pathElement)) {
+                            viewer.setSelection(selection);
+                            break outer;
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Removes the element from the from whenever it appears as a child of the
+     * specified parent element, at the specified position.
+     * 
+     * @param parent
+     *            the parent of the element being removed
+     * @param element
+     *            the element to remove
+     * @param position
+     *            the position where the element is located
+     */
+    public void remove(Object parent, Object element, int position) {
+        if (treeViewer !is null && viewer.getComparator() is null
+                && viewer.getFilters().length is 0) {
+            // Only TreeViewer has a remove-by-index method.  
+            treeViewer.remove(parent, position);
+        } else {
+            viewer.remove(parent, [ element ]);
+        }
+    }
+
+    /**
+     * Add the elements into the viewer as children of the specified parent
+     * element.
+     * 
+     * @param parent
+     *            the parent of the element being inserted
+     * @param elements
+     *            the elements to insert
+     */
+    public void add(Object parent, Object[] elements) {
+        viewer.add(parent, elements);
+    }
+
+    /**
+     * Remove the elements from the viewer wherever they appear as children of
+     * the specified parent element.
+     * 
+     * @param parent
+     *            the parent of the elements being removed
+     * @param elements
+     *            the elements to remove
+     */
+    public void remove(Object parent, Object[] elements) {
+        viewer.remove(parent, elements);
+    }
+}