KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > breakpoints > BreakpointsSorter


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.debug.internal.ui.views.breakpoints;
12
13
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.model.IBreakpoint;
17 import org.eclipse.debug.core.model.ILineBreakpoint;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.jface.viewers.IBasicPropertyConstants;
20 import org.eclipse.jface.viewers.ILabelProvider;
21 import org.eclipse.jface.viewers.StructuredViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerSorter;
24
25 public class BreakpointsSorter extends ViewerSorter {
26         /**
27          * @see ViewerSorter#isSorterProperty(Object, String)
28          */

29         public boolean isSorterProperty(Object JavaDoc element,String JavaDoc propertyId) {
30             return propertyId.equals(IBasicPropertyConstants.P_TEXT);
31         }
32         
33         /**
34          * Returns a negative, zero, or positive number depending on whether
35          * the first element is less than, equal to, or greater than
36          * the second element.
37          * <p>
38          * Group breakpoints by debug model
39          * within debug model, group breakpoints by type
40          * within type groups, sort by line number (if applicable) and then
41          * alphabetically by label
42          *
43          * @param viewer the viewer
44          * @param e1 the first element
45          * @param e2 the second element
46          * @return a negative number if the first element is less than the
47          * second element; the value <code>0</code> if the first element is
48          * equal to the second element; and a positive number if the first
49          * element is greater than the second element
50          */

51         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
52             if (!(e1 instanceof IBreakpoint)) {
53                 return super.compare(viewer, e1, e2);
54             }
55     
56             IBreakpoint b1= (IBreakpoint)e1;
57             IBreakpoint b2= (IBreakpoint)e2;
58             String JavaDoc modelId1= b1.getModelIdentifier();
59             String JavaDoc modelId2= b2.getModelIdentifier();
60             int result= modelId1.compareTo(modelId2);
61             if (result != 0) {
62                 return result;
63             }
64             String JavaDoc type1= ""; //$NON-NLS-1$
65
String JavaDoc type2= ""; //$NON-NLS-1$
66
IMarker marker1= b1.getMarker();
67             if (!marker1.exists()) {
68                 return 0;
69             }
70             try {
71                 type1= marker1.getType();
72             } catch (CoreException ce) {
73                 DebugUIPlugin.log(ce);
74             }
75             try {
76                 IMarker marker2= b2.getMarker();
77                 if (!marker2.exists()) {
78                     return 0;
79                 }
80                 type2= marker2.getType();
81             } catch (CoreException e) {
82                 DebugUIPlugin.log(e);
83             }
84         
85             result= type1.compareTo(type2);
86             if (result != 0) {
87                 return result;
88             }
89             // model and type are the same
90

91             ILabelProvider lprov = (ILabelProvider) ((StructuredViewer)viewer).getLabelProvider();
92             String JavaDoc name1= lprov.getText(e1);
93             String JavaDoc name2= lprov.getText(e2);
94     
95             boolean lineBreakpoint= false;
96             try {
97                 lineBreakpoint= marker1.isSubtypeOf(IBreakpoint.LINE_BREAKPOINT_MARKER);
98             } catch (CoreException ce) {
99             }
100             if (lineBreakpoint) {
101                 return compareLineBreakpoints(b1, b2, name1,name2);
102             }
103             
104             return name1.compareTo(name2);
105         }
106         
107         protected int compareLineBreakpoints(IBreakpoint b1, IBreakpoint b2, String JavaDoc name1, String JavaDoc name2) {
108             int colon1= name1.indexOf(':');
109             if (colon1 != -1) {
110                 int colon2= name2.indexOf(':');
111                 if (colon2 != -1) {
112                     String JavaDoc upToColon1= name1.substring(0, colon1);
113                     if (name2.startsWith(upToColon1)) {
114                         int l1= 0;
115                         int l2= 0;
116                         try {
117                             l1= ((ILineBreakpoint)b1).getLineNumber();
118                         } catch (CoreException e) {
119                             DebugUIPlugin.log(e);
120                         }
121                         try {
122                             l2= ((ILineBreakpoint)b2).getLineNumber();
123                         } catch (CoreException e) {
124                             DebugUIPlugin.log(e);
125                         }
126                         return l1 - l2;
127                     }
128                 }
129             }
130             return name1.compareTo(name2);
131         }
132     }
133
Popular Tags