1 11 12 package org.eclipse.debug.internal.ui.importexport.breakpoints; 13 14 import java.util.ArrayList ; 15 import java.util.Vector ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.debug.core.model.IBreakpoint; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 20 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointContainer; 21 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsContentProvider; 22 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsLabelProvider; 23 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsComparator; 24 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView; 25 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsViewer; 26 import org.eclipse.debug.internal.ui.views.breakpoints.IBreakpointOrganizer; 27 import org.eclipse.debug.ui.IDebugUIConstants; 28 import org.eclipse.jface.viewers.CheckStateChangedEvent; 29 import org.eclipse.jface.viewers.IBaseLabelProvider; 30 import org.eclipse.jface.viewers.ICheckStateListener; 31 import org.eclipse.jface.viewers.IStructuredSelection; 32 import org.eclipse.jface.viewers.StructuredSelection; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.graphics.Font; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Tree; 39 import org.eclipse.swt.widgets.TreeItem; 40 import org.eclipse.swt.widgets.Widget; 41 import org.eclipse.ui.IViewPart; 42 43 51 public class EmbeddedBreakpointsViewer { 52 53 private IStructuredSelection fSelection = null; 55 private BreakpointsView fView = null; 56 private BreakpointsContentProvider fProvider = null; 57 private Tree fTree = null; 58 private BreakpointsViewer fViewer = null; 59 private ICheckStateListener fCheckListener = new ICheckStateListener() { 60 public void checkStateChanged(CheckStateChangedEvent event) { 61 updateCheckedState(event.getElement(), event.getChecked()); 62 } 63 }; 64 65 private static final int HEIGHT_HINT = 150; 67 68 78 public EmbeddedBreakpointsViewer(Composite parent, Object input) { 79 Assert.isNotNull(parent); 80 Assert.isNotNull(input); 81 createControl(parent, input, null); 82 } 83 84 91 public EmbeddedBreakpointsViewer(Composite parent, Object input, IStructuredSelection selection) { 92 Assert.isNotNull(parent); 93 Assert.isNotNull(input); 94 createControl(parent, input, selection); 95 } 96 97 105 private void createControl(Composite parent, Object input, IStructuredSelection selection) { 106 fSelection = selection; 107 if(fSelection == null) { 108 IViewPart fViewpart = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().findView(IDebugUIConstants.ID_BREAKPOINT_VIEW); 109 if(fViewpart != null) { 110 fSelection = (IStructuredSelection)fViewpart.getViewSite().getSelectionProvider().getSelection(); 111 } 112 else { 113 fSelection = new StructuredSelection(); 114 } 115 } 116 Font font = parent.getFont(); 117 Composite composite = new Composite(parent, SWT.NULL); 118 composite.setLayout(new GridLayout(1, true)); 119 GridData grid = new GridData(GridData.FILL_BOTH); 120 grid.heightHint = HEIGHT_HINT; 121 composite.setLayoutData(grid); 122 composite.setFont(font); 123 124 fTree = new Tree(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CHECK); 126 fProvider = new BreakpointsContentProvider(); 127 fView = ((BreakpointsView)DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().findView(IDebugUIConstants.ID_BREAKPOINT_VIEW)); 128 fTree.setLayoutData(new GridData(GridData.FILL_BOTH)); 129 fViewer = new BreakpointsViewer(fTree); 130 IBaseLabelProvider labelProvider = null; 132 if (fView == null) { 133 labelProvider = new BreakpointsLabelProvider(); 134 } else { 135 labelProvider = fView.getCheckboxViewer().getLabelProvider(); 136 } 137 fViewer.setComparator(new BreakpointsComparator()); 138 fViewer.setLabelProvider(labelProvider); 139 fViewer.addCheckStateListener(fCheckListener); 140 IBreakpointOrganizer[] orgs = null; 141 if(fView != null) { 142 orgs = fView.getBreakpointOrganizers(); 143 } 144 fViewer.setContentProvider(fProvider); 145 fViewer.setInput(input); 146 fProvider.setOrganizers(orgs); 147 initViewerState(); 148 } 149 150 153 private void initViewerState() { 154 Object [] items = fSelection.toArray(); 155 fViewer.setGrayedElements(new Object [] {}); 156 fViewer.setCheckedElements(new Object [] {}); 157 ArrayList list = new ArrayList (); 158 for(int i = 0; i < items.length; i++) { 159 Object item = items[i]; 160 if(item instanceof IBreakpoint) { 161 list.add(item); 162 } 163 else if (item instanceof BreakpointContainer) { 164 getBreakpointsFromContainers((BreakpointContainer)item, list); 165 } 166 } 167 for(int i = 0; i < list.size(); i++) { 168 updateCheckedState(list.get(i), true); 169 } 170 } 171 172 177 private void getBreakpointsFromContainers(BreakpointContainer container, ArrayList list) { 178 Object [] elements = container.getChildren(); 179 for(int i = 0; i < elements.length; i++) { 180 if(elements[i] instanceof IBreakpoint) { 181 list.add(elements[i]); 182 } 183 else { 184 getBreakpointsFromContainers((BreakpointContainer)elements[i], list); 185 } 186 } 187 } 188 189 193 public IStructuredSelection getCheckedElements() { 194 Object [] list = fViewer.getCheckedElements(); 195 Vector selected = new Vector (); 196 for(int i = 0; i < list.length; i++) { 197 if(!selected.contains(list[i])) { 198 selected.addElement(list[i]); 199 } 200 } 201 return new StructuredSelection(selected); 202 } 203 204 208 public BreakpointsViewer getViewer() { 209 return fViewer; 210 } 211 212 217 private Widget[] searchItems(Object element) { 218 ArrayList list = new ArrayList (); 219 TreeItem[] items = fTree.getItems(); 220 for (int i = 0; i < items.length; i++) { 221 findAllOccurrences(items[i], element, list); 222 } 223 return (Widget[]) list.toArray(new Widget[0]); 224 } 225 226 232 private void findAllOccurrences(TreeItem item, Object element, ArrayList list) { 233 if (element.equals(item.getData())) { 234 list.add(item); 235 } 236 TreeItem[] items = item.getItems(); 237 for (int i = 0; i < items.length; i++) { 238 findAllOccurrences(items[i], element, list); 239 } 240 } 241 242 248 private void updateCheckedState(Object obj, boolean enable) { 249 if (obj instanceof IBreakpoint) { 250 Widget[] list = searchItems(obj); 251 TreeItem item = null; 252 for(int i = 0; i < list.length; i++) { 253 item = (TreeItem)list[i]; 254 item.setChecked(enable); 255 refreshParents(item); 256 } 257 } 258 else if (obj instanceof BreakpointContainer) { 259 ArrayList bps = new ArrayList (); 260 getBreakpointsFromContainers((BreakpointContainer)obj, bps); 261 for(int j = 0; j < bps.size(); j++) { 262 updateCheckedState(bps.get(j), enable); 263 } 264 } 265 } 266 267 271 private void refreshParents(TreeItem item) { 272 TreeItem parent = item.getParentItem(); 273 while (parent != null) { 274 int checked = getNumberChildrenChecked(parent); 275 if(checked == 0) { 276 parent.setGrayed(false); 277 parent.setChecked(false); 278 } 279 else if(checked == parent.getItemCount()) { 280 if(getNumberChildrenGrayed(parent) > 0) { 281 parent.setGrayed(true); 282 } 283 else { 284 parent.setGrayed(false); 285 } 286 parent.setChecked(true); 287 } 288 else { 289 parent.setGrayed(true); 290 parent.setChecked(true); 291 } 292 parent = parent.getParentItem(); 293 } 294 } 295 296 301 private int getNumberChildrenGrayed(TreeItem parent) { 302 TreeItem[] children = parent.getItems(); 303 int count = 0; 304 for(int i = 0; i < children.length; i++) { 305 if(children[i].getGrayed()) { 306 count++; 307 } 308 } 309 return count; 310 } 311 312 317 private int getNumberChildrenChecked(TreeItem parent) { 318 TreeItem[] children = parent.getItems(); 319 int count = 0; 320 for(int i = 0; i < children.length; i++) { 321 if(children[i].getChecked()) { 322 count++; 323 } 324 } 325 return count; 326 } 327 } 328 | Popular Tags |