78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 Matthew Hall and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * Matthew Hall - initial API and implementation (bug 226765)
|
|
10 ******************************************************************************/
|
|
11
|
|
12 module org.eclipse.jface.internal.databinding.viewers.ViewerUpdater;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import org.eclipse.jface.util.Util;
|
|
17 import org.eclipse.jface.viewers.IElementComparer;
|
|
18 import org.eclipse.jface.viewers.IStructuredSelection;
|
|
19 import org.eclipse.jface.viewers.StructuredViewer;
|
|
20
|
|
21 /**
|
|
22 * NON-API - An interface for updating a viewer's elements.
|
|
23 *
|
|
24 * @since 1.2
|
|
25 */
|
|
26 public abstract class ViewerUpdater {
|
|
27 private final StructuredViewer viewer;
|
|
28
|
|
29 /**
|
|
30 * Constructs a ViewerUpdater for updating the specified viewer.
|
|
31 *
|
|
32 * @param viewer
|
|
33 * the viewer which will be updated through this instance.
|
|
34 */
|
|
35 protected this(StructuredViewer viewer) {
|
|
36 this.viewer = viewer;
|
|
37 }
|
|
38
|
|
39 /**
|
|
40 * Insert the element into the viewer at the specified position.
|
|
41 *
|
|
42 * @param element
|
|
43 * the element to add
|
|
44 * @param position
|
|
45 * the position of the element
|
|
46 */
|
|
47 public abstract void insert(Object element, int position);
|
|
48
|
|
49 /**
|
|
50 * Remove the element from the viewer
|
|
51 *
|
|
52 * @param element
|
|
53 * the element to remove
|
|
54 * @param position
|
|
55 * the position of the element
|
|
56 */
|
|
57 public abstract void remove(Object element, int position);
|
|
58
|
|
59 /**
|
|
60 * Replace the specified element at the given position with the new element.
|
|
61 *
|
|
62 * @param oldElement
|
|
63 * the element being replaced
|
|
64 * @param newElement
|
|
65 * the element that replaces <code>oldElement</code>
|
|
66 * @param position
|
|
67 * the position of the element being replaced.
|
|
68 */
|
|
69 public void replace(Object oldElement, Object newElement, int position) {
|
|
70 remove(oldElement, position);
|
|
71 insert(newElement, position);
|
|
72 }
|
|
73
|
|
74 /**
|
|
75 * Moves the specified element from the specified old position to the
|
|
76 * specified new position. No action is taken if the viewer has a sorter or
|
|
77 * filter(s).
|
|
78 *
|
|
79 * @param element
|
|
80 * the element being moved
|
|
81 * @param oldPosition
|
|
82 * the position of the element before it is moved
|
|
83 * @param newPosition
|
|
84 * the position of the element after it is moved
|
|
85 */
|
|
86 public void move(Object element, int oldPosition, int newPosition) {
|
|
87 if (viewer.getComparator() is null && viewer.getFilters().length is 0) {
|
|
88 IStructuredSelection selection = cast(IStructuredSelection) viewer
|
|
89 .getSelection();
|
|
90
|
|
91 remove(element, oldPosition);
|
|
92 insert(element, newPosition);
|
|
93
|
|
94 // Preserve selection
|
|
95 if (!selection.isEmpty()) {
|
|
96 IElementComparer comparer = viewer.getComparer();
|
|
97 Object[] selectedElements = selection.toArray();
|
|
98 for (int i = 0; i < selectedElements.length; i++) {
|
|
99 if (comparer is null ? Util.equals(element,
|
|
100 selectedElements[i]) : comparer.equals(element,
|
|
101 selectedElements[i])) {
|
|
102 viewer.setSelection(selection);
|
|
103 break;
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Adds the elements to the viewer.
|
|
112 *
|
|
113 * @param elements
|
|
114 * the elements to add
|
|
115 */
|
|
116 public abstract void add(Object[] elements);
|
|
117
|
|
118 /**
|
|
119 * Removes the elements from the viewer
|
|
120 *
|
|
121 * @param elements
|
|
122 * the elements to remove
|
|
123 */
|
|
124 public abstract void remove(Object[] elements);
|
|
125 }
|