1 11 package org.eclipse.debug.internal.ui.sourcelookup; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.swt.events.SelectionAdapter; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.widgets.Button; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.ui.actions.SelectionListenerAction; 24 25 30 public abstract class SourceContainerAction extends SelectionListenerAction { 31 private SourceContainerViewer fViewer; 33 private Button fButton; 35 private Shell fShell; 37 38 42 public SourceContainerAction(String label) { 43 super(label); 44 } 45 46 51 public void setViewer(SourceContainerViewer 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 SourceContainerViewer getViewer() { 68 return fViewer; 69 } 70 71 77 protected List getOrderedSelection() { 78 List targets = new ArrayList (); 79 List selection = 80 ((IStructuredSelection) getViewer().getSelection()).toList(); 81 ISourceContainer[] entries = getViewer().getEntries(); 82 for (int i = 0; i < entries.length; i++) { 83 ISourceContainer target = entries[i]; 84 if (selection.contains(target)) { 85 targets.add(target); 86 } 87 } 88 return targets; 89 } 90 91 94 protected List getEntriesAsList() { 95 ISourceContainer[] entries = getViewer().getEntries(); 96 List list = new ArrayList (entries.length); 97 for (int i = 0; i < entries.length; i++) { 98 list.add(entries[i]); 99 } 100 return list; 101 } 102 103 106 protected void setEntries(List list) { 107 getViewer().setEntries( 108 (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()])); 109 getViewer().setSelection(getViewer().getSelection()); 111 } 112 113 117 protected boolean isIndexSelected( 118 IStructuredSelection selection, 119 int index) { 120 if (selection.isEmpty()) { 121 return false; 122 } 123 Iterator entries = selection.iterator(); 124 List list = getEntriesAsList(); 125 while (entries.hasNext()) { 126 Object next = entries.next(); 127 if (list.indexOf(next) == index) { 128 return true; 129 } 130 } 131 return false; 132 } 133 134 137 public void setButton(Button button) { 138 fButton = button; 139 button.addSelectionListener(new SelectionAdapter() { 140 public void widgetSelected(SelectionEvent evt) { 141 run(); 142 } 143 }); 144 } 145 146 149 public void setEnabled(boolean enabled) { 150 super.setEnabled(enabled); 151 if (fButton != null) { 152 fButton.setEnabled(enabled); 153 } 154 } 155 156 159 protected void update() { 160 selectionChanged((IStructuredSelection) getViewer().getSelection()); 161 } 162 163 166 protected Shell getShell() { 167 if (fShell == null) { 168 fShell = getViewer().getControl().getShell(); 169 } 170 return fShell; 171 } 172 173 176 public void setShell(Shell shell) { 177 fShell = shell; 178 } 179 180 } 181 | Popular Tags |