KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > WorkingSetsFilter


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.launchConfigurations;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.debug.core.ILaunchConfigurationType;
20 import org.eclipse.debug.internal.ui.DebugUIPlugin;
21 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerFilter;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.IWorkingSet;
27
28 /**
29  * creates a filter for the current working sets in use on the workbench to be applied in the launch configuration
30  * dialog and the launch history/last launched
31  *
32  * @since 3.2
33  */

34 public class WorkingSetsFilter extends ViewerFilter {
35     
36     /* (non-Javadoc)
37      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
38      */

39     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
40         if(element instanceof ILaunchConfigurationType) {
41             return true;
42         }
43         if(element instanceof ILaunchConfiguration) {
44             ILaunchConfiguration config = (ILaunchConfiguration)element;
45             try {
46                 IResource[] resources = config.getMappedResources();
47                 if(resources == null) {
48                     return true;
49                 }
50                 IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
51                 if(window == null) {
52                     return true;
53                 }
54                 IWorkbenchPage page = window.getActivePage();
55                 if(page == null) {
56                     return true;
57                 }
58                 IWorkingSet[] wsets = page.getWorkingSets();
59                 if(wsets.length < 1) {
60                     return true;
61                 }
62                 //remove breakpoint working sets
63
ArrayList JavaDoc ws = new ArrayList JavaDoc();
64                 for (int i = 0; i < wsets.length; i++) {
65                     if(!IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(wsets[i].getId())) {
66                         ws.add(wsets[i]);
67                     }
68                 }
69                 if(ws.isEmpty()) {
70                     return true;
71                 }
72                 for (int i = 0; i < resources.length; i++) {
73                     if(workingSetContains((IWorkingSet[]) ws.toArray(new IWorkingSet[ws.size()]), resources[i])) {
74                         return true;
75                     }
76                 }
77             }
78             catch (CoreException e) {}
79         }
80         return false;
81     }
82
83     /**
84      * Determines if the specified group of working sets contains the specified resource.
85      * @param wsets the set of working sets to examine
86      * @param res the resource to check for containment
87      * @return true iff any one of the specified working sets contains the specified resource
88      * @since 3.2
89      */

90     public static boolean workingSetContains(IWorkingSet[] wsets, IResource res) {
91         ArrayList JavaDoc parents = new ArrayList JavaDoc();
92         parents.add(res);
93         while(res != null) {
94             res = res.getParent();
95             if(res != null) {
96                 parents.add(res);
97             }
98         }
99         IResource lres = null;
100         for(int i = 0; i < wsets.length; i++) {
101             IAdaptable[] elements = wsets[i].getElements();
102             for(int j = 0; j < elements.length; j++) {
103                 lres = (IResource)elements[j].getAdapter(IResource.class);
104                 if(lres != null) {
105                     if(parents.contains(lres)) {
106                         return true;
107                     }
108                 }
109             }
110         }
111         return false;
112     }
113     
114 }
115
Popular Tags