summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorHendrik Still2013-09-08 15:07:05 (EDT)
committer Hendrik Still2013-09-10 04:43:44 (EDT)
commitb7edb96e40b286414c64b55fa55ca83159bc12ac (patch)
tree7fffd85a8222b0745059009946af2d7819737813
parentd8990d167015d654965be9e5ec764a47d9d62fd1 (diff)
downloadeclipse.platform.ui-b7edb96e40b286414c64b55fa55ca83159bc12ac.zip
eclipse.platform.ui-b7edb96e40b286414c64b55fa55ca83159bc12ac.tar.gz
eclipse.platform.ui-b7edb96e40b286414c64b55fa55ca83159bc12ac.tar.bz2
Bug 413975 - [Viewers] Add generics to the ViewerFilterrefs/changes/68/16268/1
Bug 413978 - [Viewers] Add generics to the ViewerComparator Added generics to the ViewerFilter and ViewerComparator to allow a typesafe use of the viewers framework. Includes also updates in the AbstractTableViewer and AbstractListViewer Change-Id: I4fd80e2866b27fab1a563ec17eb925b2e6b3bd3c Signed-off-by: Hendrik Still <hendrik.still@gammas.de>
-rw-r--r--bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractListViewer.java4
-rw-r--r--bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTableViewer.java17
-rw-r--r--bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredViewer.java32
-rw-r--r--bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerComparator.java27
-rw-r--r--bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerFilter.java30
5 files changed, 64 insertions, 46 deletions
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractListViewer.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractListViewer.java
index 1b84e7d..515f22f 100644
--- a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractListViewer.java
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractListViewer.java
@@ -300,7 +300,7 @@ public abstract class AbstractListViewer<E,I> extends StructuredViewer<E,I> {
* @return the index where the item should be inserted.
*/
protected int indexForElement(E element) {
- ViewerComparator comparator = getComparator();
+ ViewerComparator<E,I> comparator = getComparator();
if (comparator == null) {
return listGetItemCount();
}
@@ -308,7 +308,7 @@ public abstract class AbstractListViewer<E,I> extends StructuredViewer<E,I> {
int min = 0, max = count - 1;
while (min <= max) {
int mid = (min + max) / 2;
- Object data = listMap.get(mid);
+ E data = listMap.get(mid);
int compare = comparator.compare(this, data, element);
if (compare == 0) {
// find first item > element
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTableViewer.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTableViewer.java
index 04280ce..bf5a4cc 100644
--- a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTableViewer.java
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTableViewer.java
@@ -89,7 +89,7 @@ public abstract class AbstractTableViewer<E,I> extends ColumnViewer<E,I> {
IContentProvider<I> contentProvider = getContentProvider();
// If we are building lazily then request lookup now
if (contentProvider instanceof ILazyContentProvider) {
- ((ILazyContentProvider) contentProvider)
+ ((ILazyContentProvider<I>) contentProvider)
.updateElement(index);
return;
}
@@ -530,7 +530,7 @@ public abstract class AbstractTableViewer<E,I> extends ColumnViewer<E,I> {
List<E> result = new ArrayList<E>();
int[] selectionIndices = doGetSelectionIndices();
if (getContentProvider() instanceof ILazyContentProvider) {
- ILazyContentProvider lazy = (ILazyContentProvider) getContentProvider();
+ ILazyContentProvider<I> lazy = (ILazyContentProvider<I>) getContentProvider();
for (int i = 0; i < selectionIndices.length; i++) {
int selectionIndex = selectionIndices[i];
lazy.updateElement(selectionIndex);// Start the update
@@ -575,8 +575,8 @@ public abstract class AbstractTableViewer<E,I> extends ColumnViewer<E,I> {
* the element to insert
* @return the index where the item should be inserted.
*/
- protected int indexForElement(Object element) {
- ViewerComparator comparator = getComparator();
+ protected int indexForElement(E element) {
+ ViewerComparator<E,I> comparator = getComparator();
if (comparator == null) {
return doGetItemCount();
}
@@ -584,7 +584,8 @@ public abstract class AbstractTableViewer<E,I> extends ColumnViewer<E,I> {
int min = 0, max = count - 1;
while (min <= max) {
int mid = (min + max) / 2;
- Object data = doGetItem(mid).getData();
+ @SuppressWarnings("unchecked")
+ E data = (E)doGetItem(mid).getData();
int compare = comparator.compare(this, data, element);
if (compare == 0) {
// find first item > element
@@ -593,7 +594,9 @@ public abstract class AbstractTableViewer<E,I> extends ColumnViewer<E,I> {
if (mid >= count) {
break;
}
- data = doGetItem(mid).getData();
+ @SuppressWarnings("unchecked")
+ E itemElement = (E)doGetItem(mid).getData();
+ data = itemElement;
compare = comparator.compare(this, data, element);
}
return mid;
@@ -1002,7 +1005,7 @@ public abstract class AbstractTableViewer<E,I> extends ColumnViewer<E,I> {
}
if (getContentProvider() instanceof ILazyContentProvider) {
- ILazyContentProvider provider = (ILazyContentProvider) getContentProvider();
+ ILazyContentProvider<I> provider = (ILazyContentProvider<I>) getContentProvider();
// Now go through it again until all is done or we are no longer
// virtual
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredViewer.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredViewer.java
index b515481..90cf1bd 100644
--- a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredViewer.java
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredViewer.java
@@ -78,13 +78,13 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
/**
* This viewer's comparator used for sorting. <code>null</code> means there is no comparator.
*/
- private ViewerComparator sorter;
+ private ViewerComparator<E,I> sorter;
/**
* This viewer's filters (element type: <code>ViewerFilter</code>).
* <code>null</code> means there are no filters.
*/
- private List<ViewerFilter> filters;
+ private List<ViewerFilter<E,I>> filters;
/**
* Indicates whether the viewer should attempt to preserve the selection
@@ -587,9 +587,9 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
* a viewer filter
* @see StructuredViewer#setFilters(ViewerFilter[])
*/
- public void addFilter(ViewerFilter filter) {
+ public void addFilter(ViewerFilter<E,I> filter) {
if (filters == null) {
- filters = new ArrayList<ViewerFilter>();
+ filters = new ArrayList<ViewerFilter<E,I>>();
}
filters.add(filter);
refresh();
@@ -925,9 +925,9 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
protected E[] getFilteredChildren(I parent) {
E[] result = getRawChildren(parent);
if (filters != null) {
- for (Iterator<ViewerFilter> iter = filters.iterator(); iter.hasNext();) {
- ViewerFilter f = iter.next();
- E[] filteredResult = (E[]) f.filter(this, parent, result);
+ for (Iterator<ViewerFilter<E,I>> iter = filters.iterator(); iter.hasNext();) {
+ ViewerFilter<E,I> f = iter.next();
+ E[] filteredResult = f.filter(this, parent, result);
if (associateListener != null && filteredResult.length != result.length) {
notifyFilteredOut(result, filteredResult);
}
@@ -1117,7 +1117,7 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
*
* @since 3.2
*/
- public ViewerComparator getComparator(){
+ public ViewerComparator<E,I> getComparator(){
return sorter;
}
@@ -1371,14 +1371,14 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
* @return <code>true</code> if refiltering is required, and
* <code>false</code> otherwise
*/
- protected boolean needsRefilter(Object element, String property) {
+ protected boolean needsRefilter(E element, String property) {
if (sorter != null && sorter.isSorterProperty(element, property)) {
return true;
}
if (filters != null) {
for (int i = 0, n = filters.size(); i < n; ++i) {
- ViewerFilter filter = filters.get(i);
+ ViewerFilter<E,I> filter = filters.get(i);
if (filter.isFilterProperty(element, property)) {
return true;
}
@@ -1615,13 +1615,13 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
* a viewer filter
* @see StructuredViewer#setFilters(ViewerFilter[])
*/
- public void removeFilter(ViewerFilter filter) {
+ public void removeFilter(ViewerFilter<E,I> filter) {
Assert.isNotNull(filter);
if (filters != null) {
// Note: can't use List.remove(Object). Use identity comparison
// instead.
- for (Iterator<ViewerFilter> i = filters.iterator(); i.hasNext();) {
- ViewerFilter o = i.next();
+ for (Iterator<ViewerFilter<E,I>> i = filters.iterator(); i.hasNext();) {
+ ViewerFilter<E,I> o = i.next();
if (o == filter) {
i.remove();
refresh();
@@ -1646,11 +1646,11 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
* an array of viewer filters
* @since 3.3
*/
- public void setFilters(ViewerFilter[] filters) {
+ public void setFilters(ViewerFilter<E,I>[] filters) {
if (filters.length == 0) {
resetFilters();
} else {
- this.filters = new ArrayList<ViewerFilter>(Arrays.asList(filters));
+ this.filters = new ArrayList<ViewerFilter<E,I>>(Arrays.asList(filters));
refresh();
}
}
@@ -1827,7 +1827,7 @@ public abstract class StructuredViewer<E,I> extends ContentViewer<E,I> implement
*
* @since 3.2
*/
- public void setComparator(ViewerComparator comparator){
+ public void setComparator(ViewerComparator<E,I> comparator){
if (this.sorter != comparator){
this.sorter = comparator;
refresh();
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerComparator.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerComparator.java
index f6be9f5..ab5ba08 100644
--- a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerComparator.java
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerComparator.java
@@ -35,16 +35,18 @@ import org.eclipse.jface.util.Policy;
* categorization; and they may override the <code>compare</code> methods
* to provide a totally different way of sorting elements.
* </p>
+ * @param <E>
+ * @param <I>
* @see IStructuredContentProvider
* @see StructuredViewer
*
* @since 3.2
*/
-public class ViewerComparator {
+public class ViewerComparator<E,I> {
/**
* The comparator to use to sort a viewer's contents.
*/
- private Comparator comparator;
+ private Comparator<String> comparator;
/**
* Creates a new {@link ViewerComparator}, which uses the default comparator
@@ -61,7 +63,7 @@ public class ViewerComparator {
*
* @param comparator
*/
- public ViewerComparator(Comparator comparator){
+ public ViewerComparator(Comparator<String> comparator){
this.comparator = comparator;
}
@@ -70,7 +72,7 @@ public class ViewerComparator {
*
* @return the comparator used to sort strings
*/
- protected Comparator getComparator() {
+ protected Comparator<String> getComparator() {
if (comparator == null){
comparator = Policy.getComparator();
}
@@ -116,7 +118,7 @@ public class ViewerComparator {
* equal to the second element; and a positive number if the first
* element is greater than the second element
*/
- public int compare(Viewer viewer, Object e1, Object e2) {
+ public int compare(Viewer<I> viewer, E e1, E e2) {
int cat1 = category(e1);
int cat2 = category(e2);
@@ -131,15 +133,15 @@ public class ViewerComparator {
return getComparator().compare(name1, name2);
}
- private String getLabel(Viewer viewer, Object e1) {
+ private String getLabel(Viewer<I> viewer, E e1) {
String name1;
if (viewer == null || !(viewer instanceof ContentViewer)) {
name1 = e1.toString();
} else {
- IBaseLabelProvider prov = ((ContentViewer) viewer)
+ IBaseLabelProvider<E> prov = ((ContentViewer<E,I>) viewer)
.getLabelProvider();
if (prov instanceof ILabelProvider) {
- ILabelProvider lprov = (ILabelProvider) prov;
+ ILabelProvider<E> lprov = (ILabelProvider<E>) prov;
name1 = lprov.getText(e1);
} else {
name1 = e1.toString();
@@ -164,7 +166,7 @@ public class ViewerComparator {
* @return <code>true</code> if the sorting would be affected,
* and <code>false</code> if it would be unaffected
*/
- public boolean isSorterProperty(Object element, String property) {
+ public boolean isSorterProperty(E element, String property) {
return false;
}
@@ -182,10 +184,11 @@ public class ViewerComparator {
* @param viewer the viewer
* @param elements the elements to sort
*/
- public void sort(final Viewer viewer, Object[] elements) {
+ public void sort(final Viewer<I> viewer, E[] elements) {
try {
- Arrays.sort(elements, new Comparator() {
- public int compare(Object a, Object b) {
+ Arrays.sort(elements, new Comparator<E>() {
+
+ public int compare(E a, E b) {
return ViewerComparator.this.compare(viewer, a, b);
}
});
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerFilter.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerFilter.java
index 910a9d2..50b30d8 100644
--- a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerFilter.java
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerFilter.java
@@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.jface.viewers;
+import java.lang.reflect.Array;
import java.util.ArrayList;
/**
@@ -19,10 +20,12 @@ import java.util.ArrayList;
* Subclasses must implement the <code>select</code> method
* and may implement the <code>isFilterProperty</code> method.
* </p>
+ * @param <E>
+ * @param <I>
* @see IStructuredContentProvider
* @see StructuredViewer
*/
-public abstract class ViewerFilter {
+public abstract class ViewerFilter<E,I> {
/**
* Creates a new viewer filter.
*/
@@ -43,16 +46,25 @@ public abstract class ViewerFilter {
* @param elements the elements to filter
* @return the filtered elements
*/
- public Object[] filter(Viewer viewer, Object parent, Object[] elements) {
+ public E[] filter(Viewer<I> viewer, Object parent, E[] elements) {
int size = elements.length;
- ArrayList out = new ArrayList(size);
+ ArrayList<E> out = new ArrayList<E>(size);
+ E element = null;
for (int i = 0; i < size; ++i) {
- Object element = elements[i];
+ element = elements[i];
if (select(viewer, parent, element)) {
out.add(element);
}
}
- return out.toArray();
+ if (out.size() > 0) {
+ @SuppressWarnings("unchecked")
+ E[] newArrayInstance = (E[]) Array.newInstance(element.getClass(),
+ out.size());
+ return out.toArray(newArrayInstance);
+ }
+ @SuppressWarnings("unchecked")
+ E[] result = (E[]) out.toArray();
+ return result;
}
/**
@@ -69,7 +81,7 @@ public abstract class ViewerFilter {
* @return the filtered elements
* @since 3.2
*/
- public Object[] filter(Viewer viewer, TreePath parentPath, Object[] elements) {
+ public Object[] filter(Viewer<I> viewer, TreePath parentPath, E[] elements) {
return filter(viewer, parentPath.getLastSegment(), elements);
}
@@ -86,7 +98,7 @@ public abstract class ViewerFilter {
* @return <code>true</code> if the filtering would be affected,
* and <code>false</code> if it would be unaffected
*/
- public boolean isFilterProperty(Object element, String property) {
+ public boolean isFilterProperty(E element, String property) {
return false;
}
@@ -99,6 +111,6 @@ public abstract class ViewerFilter {
* @return <code>true</code> if element is included in the
* filtered set, and <code>false</code> if excluded
*/
- public abstract boolean select(Viewer viewer, Object parentElement,
- Object element);
+ public abstract boolean select(Viewer<I> viewer, Object parentElement,
+ E element);
}