1 11 package org.eclipse.jdt.internal.debug.ui.classpath; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.ListenerList; 19 import org.eclipse.debug.core.ILaunchConfiguration; 20 import org.eclipse.jdt.internal.debug.ui.actions.RuntimeClasspathAction; 21 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer; 22 import org.eclipse.jdt.internal.debug.ui.launcher.IEntriesChangedListener; 23 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.StructuredSelection; 28 import org.eclipse.jface.viewers.TreeViewer; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.events.KeyAdapter; 31 import org.eclipse.swt.events.KeyEvent; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Shell; 35 36 39 public class RuntimeClasspathViewer extends TreeViewer implements IClasspathViewer { 40 41 44 private ListenerList fListeners = new ListenerList(); 45 46 private IClasspathEntry fCurrentParent= null; 47 48 53 public RuntimeClasspathViewer(Composite parent) { 54 super(parent); 55 56 GridData data = new GridData(GridData.FILL_BOTH); 57 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 58 data.heightHint = getTree().getItemHeight(); 59 getTree().setLayoutData(data); 60 61 getTree().addKeyListener(new KeyAdapter() { 62 public void keyPressed(KeyEvent event) { 63 if (updateSelection(RuntimeClasspathAction.REMOVE, (IStructuredSelection)getSelection()) && event.character == SWT.DEL && event.stateMask == 0) { 64 List selection= getSelectionFromWidget(); 65 getClasspathContentProvider().removeAll(selection); 66 notifyChanged(); 67 } 68 } 69 }); 70 } 71 72 75 public void setEntries(IRuntimeClasspathEntry[] entries) { 76 getClasspathContentProvider().setRefreshEnabled(false); 77 resolveCurrentParent(getSelection()); 78 getClasspathContentProvider().removeAll(fCurrentParent); 79 getClasspathContentProvider().setEntries(entries); 80 getClasspathContentProvider().setRefreshEnabled(true); 81 notifyChanged(); 82 } 83 84 87 public IRuntimeClasspathEntry[] getEntries() { 88 return getClasspathContentProvider().getModel().getAllEntries(); 89 } 90 91 94 public void addEntries(IRuntimeClasspathEntry[] entries) { 95 getClasspathContentProvider().setRefreshEnabled(false); 96 IStructuredSelection sel = (IStructuredSelection) getSelection(); 97 Object beforeElement = sel.getFirstElement(); 98 resolveCurrentParent(getSelection()); 99 List existingEntries= Arrays.asList(fCurrentParent.getEntries()); 100 for (int i = 0; i < entries.length; i++) { 101 if (!existingEntries.contains(entries[i])) { 102 getClasspathContentProvider().add(fCurrentParent, entries[i], beforeElement); 103 } 104 } 105 getClasspathContentProvider().setRefreshEnabled(true); 106 notifyChanged(); 107 } 108 109 private boolean resolveCurrentParent(ISelection selection) { 110 fCurrentParent= null; 111 Iterator selected= ((IStructuredSelection)selection).iterator(); 112 113 while (selected.hasNext()) { 114 Object element = selected.next(); 115 if (element instanceof ClasspathEntry) { 116 IClasspathEntry parent= ((IClasspathEntry)element).getParent(); 117 if (fCurrentParent != null) { 118 if (!fCurrentParent.equals(parent)) { 119 return false; 120 } 121 } else { 122 fCurrentParent= parent; 123 } 124 } else { 125 if (fCurrentParent != null) { 126 if (!fCurrentParent.equals(element)) { 127 return false; 128 } 129 } else { 130 fCurrentParent= (IClasspathEntry)element; 131 } 132 } 133 } 134 return true; 135 } 136 137 140 public boolean isEnabled() { 141 return true; 142 } 143 144 147 public void setLaunchConfiguration(ILaunchConfiguration configuration) { 148 if (getLabelProvider() != null) { 149 ((ClasspathLabelProvider)getLabelProvider()).setLaunchConfiguration(configuration); 150 } 151 } 152 153 public void addEntriesChangedListener(IEntriesChangedListener listener) { 154 fListeners.add(listener); 155 } 156 157 public void removeEntriesChangedListener(IEntriesChangedListener listener) { 158 fListeners.remove(listener); 159 } 160 161 164 public void notifyChanged() { 165 Object [] listeners = fListeners.getListeners(); 166 for (int i = 0; i < listeners.length; i++) { 167 ((IEntriesChangedListener)listeners[i]).entriesChanged(this); 168 } 169 } 170 171 174 public int indexOf(IRuntimeClasspathEntry entry) { 175 IClasspathEntry[] entries= getClasspathContentProvider().getBootstrapClasspathEntries(); 176 for (int i = 0; i < entries.length; i++) { 177 IClasspathEntry existingEntry = entries[i]; 178 if (existingEntry.equals(entry)) { 179 return 1; 180 } 181 } 182 entries= getClasspathContentProvider().getUserClasspathEntries(); 183 for (int i = 0; i < entries.length; i++) { 184 IClasspathEntry existingEntry = entries[i]; 185 if (existingEntry.equals(entry)) { 186 return 1; 187 } 188 } 189 190 return -1; 191 192 } 193 194 197 public Shell getShell() { 198 return getControl().getShell(); 199 } 200 201 private ClasspathContentProvider getClasspathContentProvider() { 202 return (ClasspathContentProvider)super.getContentProvider(); 203 } 204 205 208 public boolean updateSelection(int actionType, IStructuredSelection selection) { 209 210 if (selection.isEmpty()) { 211 return false; 212 } 213 switch (actionType) { 214 case RuntimeClasspathAction.ADD : 215 Iterator selected= selection.iterator(); 216 while (selected.hasNext()) { 217 IClasspathEntry entry = (IClasspathEntry)selected.next(); 218 if (!entry.isEditable() && entry instanceof ClasspathEntry) { 219 return false; 220 } 221 } 222 return selection.size() > 0; 223 case RuntimeClasspathAction.REMOVE : 224 case RuntimeClasspathAction.MOVE : 225 selected= selection.iterator(); 226 while (selected.hasNext()) { 227 IClasspathEntry entry = (IClasspathEntry)selected.next(); 228 if (!entry.isEditable()) { 229 return false; 230 } 231 } 232 return selection.size() > 0; 233 default : 234 break; 235 } 236 237 return true; 238 } 239 240 243 public ISelection getSelectedEntries() { 244 IStructuredSelection selection= (IStructuredSelection)getSelection(); 245 List entries= new ArrayList (selection.size() * 2); 246 Iterator itr= selection.iterator(); 247 while (itr.hasNext()) { 248 IClasspathEntry element = (IClasspathEntry) itr.next(); 249 if (element.hasEntries()) { 250 entries.addAll(Arrays.asList(element.getEntries())); 251 } else { 252 entries.add(element); 253 } 254 } 255 256 return new StructuredSelection(entries); 257 } 258 } 259 | Popular Tags |