KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > core > sourcelookup > containers > CompositeSourceContainer


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.core.sourcelookup.containers;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.MultiStatus;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
20 import org.eclipse.debug.internal.core.sourcelookup.SourceLookupMessages;
21
22 /**
23  * A source container of source containers.
24  * <p>
25  * Clients implementing composite source containers should subclass
26  * this class.
27  * </p>
28  * @since 3.0
29  */

30 public abstract class CompositeSourceContainer extends AbstractSourceContainer {
31     
32     private ISourceContainer[] fContainers;
33
34     /* (non-Javadoc)
35      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#isComposite()
36      */

37     public boolean isComposite() {
38         return true;
39     }
40     
41     /* (non-Javadoc)
42      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
43      */

44     public Object JavaDoc[] findSourceElements(String JavaDoc name) throws CoreException {
45         return findSourceElements(name, getSourceContainers());
46     }
47     
48     /**
49      * Returns a collection of source elements in the given containers corresponding to
50      * the given name. Returns an empty collection if no source elements are found.
51      * This source container's source lookup director specifies if duplicate
52      * source elements should be searched for, via <code>isFindDuplicates()</code>.
53      * When <code>false</code> the returned collection should contain at most one
54      * source element. If this is a composite container, the containers contained
55      * by this container are also searched.
56      * <p>
57      * The format of the given name is implementation specific but generally conforms
58      * to the format of a file name. If a source container does not recognize the
59      * name format provided, an empty collection should be returned. A source container
60      * may or may not require names to be fully qualified (i.e. be qualified with directory
61      * names).
62      * </p>
63      * @param name the name of the source element to search for
64      * @param containers the containers to search
65      * @return a collection of source elements corresponding to the given name
66      * @exception CoreException if an exception occurs while searching for source elements
67      */

68     protected Object JavaDoc[] findSourceElements(String JavaDoc name, ISourceContainer[] containers) throws CoreException {
69         List JavaDoc results = null;
70         CoreException single = null;
71         MultiStatus multiStatus = null;
72         if (isFindDuplicates()) {
73             results = new ArrayList JavaDoc();
74         }
75         for (int i = 0; i < containers.length; i++) {
76             ISourceContainer container = containers[i];
77             try {
78                 Object JavaDoc[] objects = container.findSourceElements(name);
79                 if (objects.length > 0) {
80                     if (isFindDuplicates()) {
81                         for (int j = 0; j < objects.length; j++) {
82                             results.add(objects[j]);
83                         }
84                     } else {
85                         if (objects.length == 1) {
86                             return objects;
87                         }
88                         return new Object JavaDoc[]{objects[0]};
89                     }
90                 }
91             } catch (CoreException e) {
92                 if (single == null) {
93                     single = e;
94                 } else if (multiStatus == null) {
95                     multiStatus = new MultiStatus(DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, new IStatus[]{single.getStatus()}, SourceLookupMessages.CompositeSourceContainer_0, null);
96                     multiStatus.add(e.getStatus());
97                 } else {
98                     multiStatus.add(e.getStatus());
99                 }
100             }
101         }
102         if (results == null) {
103             if (multiStatus != null) {
104                 throw new CoreException(multiStatus);
105             } else if (single != null) {
106                 throw single;
107             }
108             return EMPTY;
109         }
110         return results.toArray();
111     }
112     
113     /**
114      * Creates the source containers in this composite container.
115      * Subclasses should override this methods.
116      *
117      * @throws CoreException if unable to create the containers
118      */

119     protected abstract ISourceContainer[] createSourceContainers() throws CoreException;
120     
121     /* (non-Javadoc)
122      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getSourceContainers()
123      */

124     public synchronized ISourceContainer[] getSourceContainers() throws CoreException {
125         if (fContainers == null) {
126             fContainers = createSourceContainers();
127             for (int i = 0; i < fContainers.length; i++) {
128                 ISourceContainer container = fContainers[i];
129                 container.init(getDirector());
130             }
131         }
132         return fContainers;
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#dispose()
137      */

138     public void dispose() {
139         super.dispose();
140         if (fContainers != null) {
141             for (int i = 0; i < fContainers.length; i++) {
142                 ISourceContainer container = fContainers[i];
143                 container.dispose();
144             }
145         }
146         fContainers = null;
147     }
148 }
149
Popular Tags