1
/*******************************************************************************
2
* Copyright (c) 2004, 2005 QNX Software Systems 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
9
* QNX Software Systems - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.debug.internal.ui.sourcelookup;
13
import java.util.ArrayList;
14
import java.util.List;
15
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
17
import org.eclipse.jface.viewers.IStructuredSelection;
18
import org.eclipse.jface.viewers.ITreeContentProvider;
19
import org.eclipse.jface.viewers.StructuredSelection;
20
import org.eclipse.jface.viewers.TreeViewer;
21
import org.eclipse.jface.viewers.Viewer;
22
import org.eclipse.swt.widgets.Composite;
25
* The viewer containing the source containers.
26
* It is a tree viewer since the containers are represented in tree form.
28
public class SourceContainerViewer extends TreeViewer {
30
* Whether enabled/editable.
32
private boolean fEnabled = true;
34
* The source container entries displayed in this viewer
36
protected List<ISourceContainer> fEntries = new ArrayList<ISourceContainer>();
38
class ContentProvider implements ITreeContentProvider {
40
* @see IStructuredContentProvider#getElements(Object)
42
public Object[] getElements(Object inputElement) {
47
* @see IContentProvider#dispose()
49
public void dispose() {
53
* @see IContentProvider#inputChanged(Viewer, Object, Object)
55
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
59
* @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
61
public Object[] getChildren(Object parentElement) {
63
return ((ISourceContainer)parentElement).getSourceContainers();
64
} catch (CoreException e) {
70
* @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
72
public Object getParent(Object element) {
77
* @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
79
public boolean hasChildren(Object element) {
80
return ((ISourceContainer)element).isComposite();
85
* Creates a runtime classpath viewer with the given parent.
87
* @param parent the parent control
88
* @param panel the panel hosting this viewer
90
public SourceContainerViewer(Composite parent) {
92
setContentProvider(new ContentProvider());
93
SourceContainerLabelProvider lp = new SourceContainerLabelProvider();
98
* Sets the entries in this viewer
100
* @param entries source container entries
102
public void setEntries(ISourceContainer[] entries) {
104
for (int i = 0; i < entries.length; i++) {
105
if (entries[i] != null)
106
fEntries.add(entries[i]);
108
if (getInput() == null) {
110
//select first item in list
111
if (!fEntries.isEmpty() && fEntries.get(0)!=null)
112
setSelection(new StructuredSelection(fEntries.get(0)));
119
* Returns the entries in this viewer
121
* @return the entries in this viewer
123
public ISourceContainer[] getEntries() {
124
return fEntries.toArray(new ISourceContainer[fEntries.size()]);
128
* Adds the given entries to the list. If there is no selection
129
* in the list, the entries are added at the end of the list,
130
* otherwise the new entries are added before the (first) selected
131
* entry. The new entries are selected.
133
* @param entries additions
135
public void addEntries(ISourceContainer[] entries) {
136
IStructuredSelection sel = (IStructuredSelection)getSelection();
138
for (int i = 0; i < entries.length; i++) {
139
if (!fEntries.contains(entries[i])) {
140
fEntries.add(entries[i]);
144
int index = fEntries.indexOf(sel.getFirstElement());
145
for (int i = 0; i < entries.length; i++) {
146
if (!fEntries.contains(entries[i])) {
147
fEntries.add(index, entries[i]);
153
if (!fEntries.isEmpty() && fEntries.get(0)!=null)
154
setSelection(new StructuredSelection(fEntries.get(0)));
159
* Enables/disables this viewer. Note the control is not disabled, since
160
* we still want the user to be able to scroll if required to see the
161
* existing entries. Just actions should be disabled.
163
public void setEnabled(boolean enabled) {
165
// fire selection change to update actions
166
setSelection(getSelection());
170
* Returns whether this viewer is enabled
172
public boolean isEnabled() {
177
* Returns the index of an equivalent entry, or -1 if none.
179
* @return the index of an equivalent entry, or -1 if none
181
public int indexOf(ISourceContainer entry) {
182
return fEntries.indexOf(entry);