~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/sourcelookup/SourceContainerViewer.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     QNX Software Systems - Initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.cdt.debug.internal.ui.sourcelookup; 
 
12
 
 
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;
 
23
 
 
24
/**
 
25
 * The viewer containing the source containers.
 
26
 * It is a tree viewer since the containers are represented in tree form.
 
27
 */
 
28
public class SourceContainerViewer extends TreeViewer {
 
29
        /**
 
30
         * Whether enabled/editable.
 
31
         */
 
32
        private boolean fEnabled = true;
 
33
        /**
 
34
         * The source container entries displayed in this viewer
 
35
         */
 
36
        protected List<ISourceContainer> fEntries = new ArrayList<ISourceContainer>();
 
37
        
 
38
        class ContentProvider implements ITreeContentProvider {
 
39
                /**
 
40
                 * @see IStructuredContentProvider#getElements(Object)
 
41
                 */
 
42
                public Object[] getElements(Object inputElement) {
 
43
                        return getEntries();
 
44
                }
 
45
                
 
46
                /**
 
47
                 * @see IContentProvider#dispose()
 
48
                 */
 
49
                public void dispose() {
 
50
                }
 
51
                
 
52
                /**
 
53
                 * @see IContentProvider#inputChanged(Viewer, Object, Object)
 
54
                 */
 
55
                public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
 
56
                }
 
57
                
 
58
                /** 
 
59
                 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
 
60
                 */
 
61
                public Object[] getChildren(Object parentElement) {
 
62
                        try {
 
63
                                return ((ISourceContainer)parentElement).getSourceContainers();
 
64
                        } catch (CoreException e) {
 
65
                                return new Object[0];
 
66
                        }
 
67
                }
 
68
                
 
69
                /**
 
70
                 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
 
71
                 */
 
72
                public Object getParent(Object element) {
 
73
                        return null;
 
74
                }
 
75
                
 
76
                /**
 
77
                 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
 
78
                 */
 
79
                public boolean hasChildren(Object element) {
 
80
                        return ((ISourceContainer)element).isComposite();                               
 
81
                }
 
82
        }
 
83
        
 
84
        /**
 
85
         * Creates a runtime classpath viewer with the given parent.
 
86
         *
 
87
         * @param parent the parent control
 
88
         * @param panel the panel hosting this viewer
 
89
         */
 
90
        public SourceContainerViewer(Composite parent) {
 
91
                super(parent);
 
92
                setContentProvider(new ContentProvider());
 
93
                SourceContainerLabelProvider lp = new SourceContainerLabelProvider();
 
94
                setLabelProvider(lp);           
 
95
        }       
 
96
        
 
97
        /**
 
98
         * Sets the entries in this viewer 
 
99
         * 
 
100
         * @param entries source container entries
 
101
         */
 
102
        public void setEntries(ISourceContainer[] entries) {
 
103
                fEntries.clear();
 
104
                for (int i = 0; i < entries.length; i++) {
 
105
                        if (entries[i] != null)
 
106
                                fEntries.add(entries[i]);
 
107
                }
 
108
                if (getInput() == null) {
 
109
                        setInput(fEntries);
 
110
                        //select first item in list
 
111
                        if (!fEntries.isEmpty() && fEntries.get(0)!=null)
 
112
                                setSelection(new StructuredSelection(fEntries.get(0)));                 
 
113
                } else {
 
114
                        refresh();
 
115
                }
 
116
        }
 
117
        
 
118
        /**
 
119
         * Returns the entries in this viewer
 
120
         * 
 
121
         * @return the entries in this viewer
 
122
         */
 
123
        public ISourceContainer[] getEntries() {
 
124
                return fEntries.toArray(new ISourceContainer[fEntries.size()]);
 
125
        }
 
126
        
 
127
        /**
 
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.
 
132
         * 
 
133
         * @param entries additions
 
134
         */
 
135
        public void addEntries(ISourceContainer[] entries) {
 
136
                IStructuredSelection sel = (IStructuredSelection)getSelection();
 
137
                if (sel.isEmpty()) {
 
138
                        for (int i = 0; i < entries.length; i++) {
 
139
                                if (!fEntries.contains(entries[i])) {
 
140
                                        fEntries.add(entries[i]);
 
141
                                }
 
142
                        }
 
143
                }  else { 
 
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]);
 
148
                                        index++;
 
149
                                }
 
150
                        }
 
151
                }               
 
152
                
 
153
                if (!fEntries.isEmpty() && fEntries.get(0)!=null)
 
154
                        setSelection(new StructuredSelection(fEntries.get(0)));
 
155
                refresh();
 
156
        }       
 
157
        
 
158
        /**
 
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.
 
162
         */
 
163
        public void setEnabled(boolean enabled) {
 
164
                fEnabled = enabled;
 
165
                // fire selection change to update actions
 
166
                setSelection(getSelection());
 
167
        }       
 
168
        
 
169
        /**
 
170
         * Returns whether this viewer is enabled
 
171
         */
 
172
        public boolean isEnabled() {
 
173
                return fEnabled;
 
174
        }       
 
175
                
 
176
        /**
 
177
         * Returns the index of an equivalent entry, or -1 if none.
 
178
         * 
 
179
         * @return the index of an equivalent entry, or -1 if none
 
180
         */
 
181
        public int indexOf(ISourceContainer entry) {
 
182
                return fEntries.indexOf(entry);
 
183
        }
 
184
}