KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > SourceContainerViewer


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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  * Pawel Piech - Bug 173306: When editing source lookup, new source
11  * containers should be added at the top of the list
12  *******************************************************************************/

13 package org.eclipse.debug.internal.ui.sourcelookup;
14
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
21 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.swt.widgets.Composite;
28
29 /**
30  * The viewer containing the source containers in the
31  * SourceContainerLookupTab and the EditSourceLookupPathDialog.
32  * It is a tree viewer since the containers are represented in tree form.
33  *
34  * @since 3.0
35  */

36 public class SourceContainerViewer extends TreeViewer {
37     
38     /**
39      * Whether enabled/editable.
40      */

41     private boolean fEnabled = true;
42     /**
43      * The parent panel
44      */

45     private SourceLookupPanel fPanel;
46     /**
47      * The source container entries displayed in this viewer
48      */

49     protected List JavaDoc fEntries = new ArrayList JavaDoc();
50     
51     class ContentProvider implements ITreeContentProvider {
52         
53         /**
54          * @see IStructuredContentProvider#getElements(Object)
55          */

56         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
57             return getEntries();
58         }
59         
60         /**
61          * @see IContentProvider#dispose()
62          */

63         public void dispose() {
64         }
65         
66         /**
67          * @see IContentProvider#inputChanged(Viewer, Object, Object)
68          */

69         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
70         }
71         
72         /**
73          * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
74          */

75         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
76             try {
77                 return ((ISourceContainer)parentElement).getSourceContainers();
78             } catch (CoreException e) {
79                 return new Object JavaDoc[0];
80             }
81         }
82         
83         /**
84          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
85          */

86         public Object JavaDoc getParent(Object JavaDoc element) {
87             return null;
88         }
89         
90         /**
91          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
92          */

93         public boolean hasChildren(Object JavaDoc element) {
94             return ((ISourceContainer)element).isComposite();
95         }
96         
97     }
98     
99     /**
100      * Creates a runtime classpath viewer with the given parent.
101      *
102      * @param parent the parent control
103      * @param panel the panel hosting this viewer
104      */

105     public SourceContainerViewer(Composite parent, SourceLookupPanel panel) {
106         super(parent);
107         setContentProvider(new ContentProvider());
108         SourceContainerLabelProvider lp = new SourceContainerLabelProvider();
109         setLabelProvider(lp);
110         fPanel = panel;
111     }
112     
113     /**
114      * Sets the entries in this viewer
115      *
116      * @param entries source container entries
117      */

118     public void setEntries(ISourceContainer[] entries) {
119         fEntries.clear();
120         for (int i = 0; i < entries.length; i++) {
121             if(entries[i] != null)
122                 fEntries.add(entries[i]);
123         }
124         if (getInput() == null) {
125             setInput(fEntries);
126             //select first item in list
127
if(!fEntries.isEmpty() && fEntries.get(0)!=null)
128                 setSelection(new StructuredSelection(fEntries.get(0)));
129         } else {
130             refresh();
131         }
132         fPanel.setDirty(true);
133         fPanel.updateLaunchConfigurationDialog();
134     }
135     
136     /**
137      * Returns the entries in this viewer
138      *
139      * @return the entries in this viewer
140      */

141     public ISourceContainer[] getEntries() {
142         return (ISourceContainer[])fEntries.toArray(new ISourceContainer[fEntries.size()]);
143     }
144     
145     /**
146      * Adds the given entries to the list. If there is no selection
147      * in the list, the entries are added at the end of the list,
148      * otherwise the new entries are added before the (first) selected
149      * entry. The new entries are selected.
150      *
151      * @param entries additions
152      */

153     public void addEntries(ISourceContainer[] entries) {
154         int index = 0;
155         IStructuredSelection sel = (IStructuredSelection)getSelection();
156         if (!sel.isEmpty()) {
157             index = fEntries.indexOf(sel.getFirstElement());
158         }
159         for (int i = 0; i < entries.length; i++) {
160             if (!fEntries.contains(entries[i])) {
161                 fEntries.add(index, entries[i]);
162                 index++;
163             }
164         }
165         
166         refresh();
167         if(entries.length > 0)
168             setSelection(new StructuredSelection(entries));
169         fPanel.setDirty(true);
170         fPanel.updateLaunchConfigurationDialog();
171     }
172     
173     /**
174      * Enables/disables this viewer. Note the control is not disabled, since
175      * we still want the user to be able to scroll if required to see the
176      * existing entries. Just actions should be disabled.
177      */

178     public void setEnabled(boolean enabled) {
179         fEnabled = enabled;
180         // fire selection change to upate actions
181
setSelection(getSelection());
182     }
183     
184     /**
185      * Returns whether this viewer is enabled
186      */

187     public boolean isEnabled() {
188         return fEnabled;
189     }
190         
191     /**
192      * Returns the index of an equivalent entry, or -1 if none.
193      *
194      * @return the index of an equivalent entry, or -1 if none
195      */

196     public int indexOf(ISourceContainer entry) {
197         return fEntries.indexOf(entry);
198     }
199     
200     /**
201      * Returns the source locator associated with the parent panel.
202      *
203      * @return the source locator
204      */

205     public ISourceLookupDirector getSourceLocator()
206     {
207         return fPanel.fLocator;
208     }
209     
210 }
211
Popular Tags