KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.PlatformObject;
21 import org.eclipse.debug.core.model.IBreakpoint;
22
23 /**
24  * A container of breakpoints, based on a category.
25  */

26 public class BreakpointContainer extends PlatformObject {
27
28     private IAdaptable fCategory;
29     private IBreakpointOrganizer fOrganizer;
30     private List JavaDoc fBreakpoints;
31     private Map JavaDoc fCategoriesToContainers;
32     private IBreakpointOrganizer[] fNesting;
33     
34     /**
35      * Constructs a container of breakpoints for the given category,
36      * created by the given organizer.
37      *
38      * @param category breakpoint category
39      * @param organizer breakpoint organizer
40      * @param nesting nested organizers or <code>null</code> if none
41      */

42     public BreakpointContainer(IAdaptable category, IBreakpointOrganizer organizer, IBreakpointOrganizer[] nesting) {
43         fCategory = category;
44         fOrganizer = organizer;
45         fBreakpoints = new ArrayList JavaDoc();
46         fNesting = nesting;
47         fCategoriesToContainers = new HashMap JavaDoc();
48         // seed with all nested categories
49
if (nesting != null && nesting.length > 0) {
50             IAdaptable[] emptyCategories = nesting[0].getCategories();
51             if (emptyCategories != null) {
52                 for (int i = 0; i < emptyCategories.length; i++) {
53                     IAdaptable empty = emptyCategories[i];
54                     BreakpointContainer container = (BreakpointContainer) fCategoriesToContainers.get(empty);
55                     if (container == null) {
56                         container = new BreakpointContainer(empty, nesting[0], null);
57                         fCategoriesToContainers.put(empty, container);
58                     }
59                 }
60             }
61         }
62     }
63     
64     /**
65      * Adds a breakpoint to this container and its nested containers.
66      *
67      * @param breakpoint breakpoint to add
68      */

69     public void addBreakpoint(IBreakpoint breakpoint) {
70         fBreakpoints.add(breakpoint);
71         if (fNesting != null && fNesting.length > 0) {
72             IBreakpointOrganizer organizer = fNesting[0];
73             IAdaptable[] categories = organizer.getCategories(breakpoint);
74             if (categories== null || categories.length == 0) {
75                 categories = OtherBreakpointCategory.getCategories(organizer);
76             }
77             for (int i = 0; i < categories.length; i++) {
78                 IAdaptable category = categories[i];
79                 BreakpointContainer container = (BreakpointContainer) fCategoriesToContainers.get(category);
80                 if (container == null) {
81                     IBreakpointOrganizer[] nesting = null;
82                     if (fNesting.length > 1) {
83                         nesting = new IBreakpointOrganizer[fNesting.length - 1];
84                         System.arraycopy(fNesting, 1, nesting, 0, nesting.length);
85                     }
86                     container = new BreakpointContainer(category, organizer, nesting);
87                     fCategoriesToContainers.put(category, container);
88                 }
89                 container.addBreakpoint(breakpoint);
90             }
91         }
92     }
93     
94     /**
95      * Returns the breakpoints in this container
96      *
97      * @return the breakpoints in this container
98      */

99     public IBreakpoint[] getBreakpoints() {
100         return (IBreakpoint[]) fBreakpoints.toArray(new IBreakpoint[fBreakpoints.size()]);
101     }
102     
103     /**
104      * Returns this container's category.
105      *
106      * @return container category
107      */

108     public IAdaptable getCategory() {
109         return fCategory;
110     }
111     
112     /**
113      * Returns children as breakpoints or nested containers.
114      *
115      * @return children as breakpoints or nested containers
116      */

117     public Object JavaDoc[] getChildren() {
118         if (fCategoriesToContainers.isEmpty()) {
119             return getBreakpoints();
120         }
121         return getContainers();
122     }
123     
124     /**
125      * Returns the containers nested in this container, possibly empty.
126      *
127      * @return the containers nested in this container, possibly empty
128      */

129     public BreakpointContainer[] getContainers() {
130         Collection JavaDoc collection = fCategoriesToContainers.values();
131         return (BreakpointContainer[]) collection.toArray(new BreakpointContainer[collection.size()]);
132     }
133     
134     /**
135      * Returns this container's organizer.
136      *
137      * @return this container's organizer
138      */

139     public IBreakpointOrganizer getOrganizer() {
140         return fOrganizer;
141     }
142     
143     /* (non-Javadoc)
144      * @see java.lang.Object#equals(java.lang.Object)
145      */

146     public boolean equals(Object JavaDoc obj) {
147         if (obj instanceof BreakpointContainer) {
148             BreakpointContainer container = (BreakpointContainer) obj;
149             return getCategory().equals(container.getCategory());
150         }
151         return super.equals(obj);
152     }
153     
154     /* (non-Javadoc)
155      * @see java.lang.Object#hashCode()
156      */

157     public int hashCode() {
158         return getCategory().hashCode();
159     }
160     
161     /**
162      * Returns whether this container contains the given breakpoint.
163      *
164      * @param breakpoint
165      * @return whether this container contains the given breakpoint
166      */

167     public boolean contains(IBreakpoint breakpoint) {
168         return fBreakpoints.contains(breakpoint);
169     }
170     
171     /**
172      * Returns the leaf containers the given breakpoint is contained in, or <code>null</code>
173      * if none.
174      *
175      * @param breakpoint
176      * @return leaf containers the given breakpoint is contained in, or <code>null</code>
177      * if none
178      */

179     public BreakpointContainer[] getContainers(IBreakpoint breakpoint) {
180         if (contains(breakpoint)) {
181             BreakpointContainer[] containers = getContainers();
182             if (containers.length == 0) {
183                 return new BreakpointContainer[]{this};
184             }
185             List JavaDoc list = new ArrayList JavaDoc();
186             for (int i = 0; i < containers.length; i++) {
187                 BreakpointContainer container = containers[i];
188                 BreakpointContainer[] subcontainers = container.getContainers(breakpoint);
189                 if (subcontainers != null) {
190                     for (int j = 0; j < subcontainers.length; j++) {
191                         list.add(subcontainers[j]);
192                     }
193                 }
194             }
195             return (BreakpointContainer[]) list.toArray(new BreakpointContainer[list.size()]);
196         }
197         return null;
198     }
199 }
200
Popular Tags