KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ViewerComparator;
24 /**
25  * @since 3.3
26  */

27 public class BreakpointsComparator extends ViewerComparator {
28         /**
29          * @see ViewerSorter#isSorterProperty(Object, String)
30          */

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

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

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