KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.preferences;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.viewers.IStructuredContentProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerComparator;
24
25 /**
26  * Content provider that maintains a generic list of objects which are shown in
27  * a table viewer.
28  */

29 public class AntContentProvider implements IStructuredContentProvider {
30     protected List JavaDoc elements = new ArrayList JavaDoc();
31     protected TableViewer tableViewer;
32     private ViewerComparator fComparator= null;
33
34     public void add(Object JavaDoc o) {
35         if (elements.contains(o)) {
36             return;
37         }
38         elements.add(o);
39         tableViewer.add(o);
40         tableViewer.setSelection(new StructuredSelection(o), true);
41     }
42
43     public void dispose() {
44     }
45
46     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
47         return elements.toArray();
48     }
49
50     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
51         tableViewer = (TableViewer) viewer;
52         elements.clear();
53         if (newInput != null) {
54             tableViewer.setComparator(getComparator());
55             List JavaDoc list;
56             if (newInput instanceof List JavaDoc) {
57                 list= (List JavaDoc) newInput;
58             } else {
59                 list= Arrays.asList((Object JavaDoc[]) newInput);
60             }
61             elements.addAll(list);
62         }
63     }
64
65     public void remove(Object JavaDoc o) {
66         elements.remove(o);
67         tableViewer.remove(o);
68     }
69     
70     public void remove(IStructuredSelection selection) {
71         Object JavaDoc[] array= selection.toArray();
72         elements.removeAll(Arrays.asList(array));
73         tableViewer.remove(array);
74     }
75     
76     protected ViewerComparator getComparator() {
77         if (fComparator == null) {
78             fComparator= new ViewerComparator() {
79                 /* (non-Javadoc)
80                  * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
81                  */

82                 public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
83                     return e1.toString().compareToIgnoreCase(e2.toString());
84                 }
85             };
86         }
87         return fComparator;
88     }
89 }
90
Popular Tags