1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer; 19 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.swt.events.SelectionAdapter; 22 import org.eclipse.swt.events.SelectionEvent; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.actions.SelectionListenerAction; 26 27 30 public abstract class RuntimeClasspathAction extends SelectionListenerAction { 31 32 public static final int DEFAULT= 0; 33 public static final int ADD= 1; 34 public static final int REMOVE= 2; 35 public static final int MOVE= 3; 36 37 private IClasspathViewer fViewer; 38 private Button fButton; 39 private Shell fShell; 40 41 public RuntimeClasspathAction(String label, IClasspathViewer viewer) { 42 super(label); 43 setViewer(viewer); 44 } 45 46 51 public void setViewer(IClasspathViewer viewer) { 52 if (fViewer != null) { 53 fViewer.removeSelectionChangedListener(this); 54 } 55 fViewer = viewer; 56 if (fViewer != null) { 57 fViewer.addSelectionChangedListener(this); 58 update(); 59 } 60 } 61 62 67 protected IClasspathViewer getViewer() { 68 return fViewer; 69 } 70 71 77 protected List getOrderedSelection() { 78 List targets = new ArrayList (); 79 List selection = ((IStructuredSelection)getViewer().getSelection()).toList(); 80 IRuntimeClasspathEntry[] entries = getViewer().getEntries(); 81 for (int i = 0; i < entries.length; i++) { 82 IRuntimeClasspathEntry target = entries[i]; 83 if (selection.contains(target)) { 84 targets.add(target); 85 } 86 } 87 return targets; 88 } 89 90 93 protected List getEntriesAsList() { 94 IRuntimeClasspathEntry[] entries = getViewer().getEntries(); 95 List list = new ArrayList (entries.length); 96 for (int i = 0; i < entries.length; i++) { 97 list.add(entries[i]); 98 } 99 return list; 100 } 101 102 105 protected void setEntries(List list) { 106 getViewer().setEntries((IRuntimeClasspathEntry[])list.toArray(new IRuntimeClasspathEntry[list.size()])); 107 getViewer().setSelection(getViewer().getSelection()); 109 } 110 111 115 protected boolean isIndexSelected(IStructuredSelection selection, int index) { 116 if (selection.isEmpty()) { 117 return false; 118 } 119 Iterator entries = selection.iterator(); 120 List list = getEntriesAsList(); 121 while (entries.hasNext()) { 122 Object next = entries.next(); 123 if (list.indexOf(next) == index) { 124 return true; 125 } 126 } 127 return false; 128 } 129 130 133 public void setButton(Button button) { 134 fButton = button; 135 button.addSelectionListener(new SelectionAdapter() { 136 public void widgetSelected(SelectionEvent evt) { 137 run(); 138 } 139 }); 140 fButton.setEnabled(false); 141 } 142 145 public void setEnabled(boolean enabled) { 146 super.setEnabled(enabled); 147 if (fButton != null) { 148 fButton.setEnabled(enabled); 149 } 150 } 151 152 155 protected void update() { 156 selectionChanged((IStructuredSelection)getViewer().getSelection()); 157 } 158 159 162 protected Shell getShell() { 163 if (fShell == null) { 164 fShell= getViewer().getShell(); 165 } 166 return fShell; 167 } 168 169 172 public void setShell(Shell shell) { 173 fShell= shell; 174 } 175 176 177 180 protected boolean updateSelection(IStructuredSelection selection) { 181 return getViewer().updateSelection(getActionType(), selection); 182 } 183 184 185 protected int getActionType() { 186 return DEFAULT; 187 } 188 } 189 | Popular Tags |