KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > RuntimeClasspathViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.debug.ui.launcher;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.ListenerList;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
20 import org.eclipse.jface.viewers.IStructuredContentProvider;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.KeyAdapter;
27 import org.eclipse.swt.events.KeyEvent;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Shell;
30
31 /**
32  * A viewer that displays and manipulates runtime classpath entries.
33  */

34 public class RuntimeClasspathViewer extends TableViewer implements IClasspathViewer {
35     
36     /**
37      * Whether enabled/editable.
38      */

39     private boolean fEnabled = true;
40     
41     /**
42      * Entry changed listeners
43      */

44     private ListenerList fListeners = new ListenerList();
45     
46     /**
47      * The runtime classpath entries displayed in this viewer
48      */

49     protected List JavaDoc fEntries = new ArrayList JavaDoc();
50     
51     class ContentProvider implements IStructuredContentProvider {
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         
74     /**
75      * Creates a runtime classpath viewer with the given parent.
76      *
77      * @param parent the parent control
78      */

79     public RuntimeClasspathViewer(Composite parent) {
80         super(parent);
81         setContentProvider(new ContentProvider());
82         RuntimeClasspathEntryLabelProvider lp = new RuntimeClasspathEntryLabelProvider();
83         setLabelProvider(lp);
84         setInput(fEntries);
85         
86         getTable().addKeyListener(new KeyAdapter() {
87             public void keyPressed(KeyEvent event) {
88                 if (isEnabled() && event.character == SWT.DEL && event.stateMask == 0) {
89                     List JavaDoc selection= getSelectionFromWidget();
90                     fEntries.removeAll(selection);
91                     setInput(fEntries);
92                     notifyChanged();
93                 }
94             }
95         });
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#setEntries(org.eclipse.jdt.launching.IRuntimeClasspathEntry[])
100      */

101     public void setEntries(IRuntimeClasspathEntry[] entries) {
102         fEntries.clear();
103         for (int i = 0; i < entries.length; i++) {
104             fEntries.add(entries[i]);
105         }
106         setInput(fEntries);
107         notifyChanged();
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#getEntries()
112      */

113     public IRuntimeClasspathEntry[] getEntries() {
114         return (IRuntimeClasspathEntry[])fEntries.toArray(new IRuntimeClasspathEntry[fEntries.size()]);
115     }
116     
117     /* (non-Javadoc)
118      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#addEntries(org.eclipse.jdt.launching.IRuntimeClasspathEntry[])
119      */

120     public void addEntries(IRuntimeClasspathEntry[] entries) {
121         IStructuredSelection sel = (IStructuredSelection)getSelection();
122         if (sel.isEmpty()) {
123             for (int i = 0; i < entries.length; i++) {
124                 if (!fEntries.contains(entries[i])) {
125                     fEntries.add(entries[i]);
126                 }
127             }
128         } else {
129             int index = fEntries.indexOf(sel.getFirstElement());
130             for (int i = 0; i < entries.length; i++) {
131                 if (!fEntries.contains(entries[i])) {
132                     fEntries.add(index, entries[i]);
133                     index++;
134                 }
135             }
136         }
137         setSelection(new StructuredSelection(entries));
138         refresh();
139         notifyChanged();
140     }
141     
142     /**
143      * Enables/disables this viewer. Note the control is not disabled, since
144      * we still want the user to be able to scroll if required to see the
145      * existing entries. Just actions should be disabled.
146      */

147     public void setEnabled(boolean enabled) {
148         fEnabled = enabled;
149         // fire selection change to update actions
150
setSelection(getSelection());
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#isEnabled()
155      */

156     public boolean isEnabled() {
157         return fEnabled;
158     }
159     
160     /**
161      * Sets the launch configuration context for this viewer, if any
162      */

163     public void setLaunchConfiguration(ILaunchConfiguration configuration) {
164         if (getLabelProvider() != null) {
165             ((RuntimeClasspathEntryLabelProvider)getLabelProvider()).setLaunchConfiguration(configuration);
166         }
167     }
168     
169     public void addEntriesChangedListener(IEntriesChangedListener listener) {
170         fListeners.add(listener);
171     }
172     
173     public void removeEntriesChangedListener(IEntriesChangedListener listener) {
174         fListeners.remove(listener);
175     }
176     
177     /* (non-Javadoc)
178      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#notifyChanged()
179      */

180     public void notifyChanged() {
181         Object JavaDoc[] listeners = fListeners.getListeners();
182         for (int i = 0; i < listeners.length; i++) {
183             ((IEntriesChangedListener)listeners[i]).entriesChanged(this);
184         }
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#indexOf(org.eclipse.jdt.launching.IRuntimeClasspathEntry)
189      */

190     public int indexOf(IRuntimeClasspathEntry entry) {
191         return fEntries.indexOf(entry);
192     }
193
194     /* (non-Javadoc)
195      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#getShell()
196      */

197     public Shell getShell() {
198         return getControl().getShell();
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#updateSelection(int, org.eclipse.jface.viewers.IStructuredSelection)
203      */

204     public boolean updateSelection(int actionType, IStructuredSelection selection) {
205         return isEnabled();
206     }
207 }
208
Popular Tags