KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > variables > BuildFilesResolver


1 /*******************************************************************************
2  * Copyright (c) 2007 Matthew Conway 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  * Matthew Conway - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.externaltools.internal.variables;
12
13 import java.util.LinkedHashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IResourceDelta;
18 import org.eclipse.core.resources.IResourceDeltaVisitor;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.variables.IDynamicVariable;
21 import org.eclipse.core.variables.IDynamicVariableResolver;
22 import org.eclipse.ui.externaltools.internal.model.ExternalToolBuilder;
23
24 public class BuildFilesResolver implements IDynamicVariableResolver
25 {
26     private static final char ARG_REMOVED = 'r';
27     private static final char ARG_CHANGED = 'c';
28     private static final char ARG_ADDED = 'a';
29     private static final char ARG_DIRS = 'd';
30     private static final char ARG_FILES = 'f';
31
32     // Use a space as a separator as this is a more natural fit for sending a
33
// list of files to a unix command
34
private static final String JavaDoc FILE_LIST_SEPARATOR = " "; //$NON-NLS-1$
35

36     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException
37     {
38         String JavaDoc result = null;
39         
40         IResourceDelta buildDelta = ExternalToolBuilder.getBuildDelta();
41         if (buildDelta != null)
42         {
43             final StringBuffer JavaDoc fileList = new StringBuffer JavaDoc();
44             final Set JavaDoc changedResources = new LinkedHashSet JavaDoc();
45             
46             // Use the argument to determine which deltas to visit - if none,
47
// then defaults to all
48
int deltas = 0;
49             boolean dirs = false, files = false;
50             if (argument != null)
51             {
52                 // Check delta kinds
53
if (argument.indexOf(ARG_ADDED) > -1)
54                 {
55                     deltas |= IResourceDelta.ADDED;
56                 }
57                 if (argument.indexOf(ARG_CHANGED) > -1)
58                 {
59                     deltas |= IResourceDelta.CHANGED;
60                 }
61                 if (argument.indexOf(ARG_REMOVED) > -1)
62                 {
63                     deltas |= IResourceDelta.REMOVED;
64                 }
65                 
66                 // Check wether to include files and/or directories
67
if (argument.indexOf(ARG_DIRS) > -1)
68                 {
69                     dirs = true;
70                 }
71                 if (argument.indexOf(ARG_FILES) > -1)
72                 {
73                     files = true;
74                 }
75
76             }
77             if (deltas == 0)
78             {
79                 deltas = IResourceDelta.ADDED | IResourceDelta.CHANGED | IResourceDelta.REMOVED;
80             }
81             if (!dirs && !files)
82             {
83                 dirs = true;
84                 files = true;
85             }
86             final int trackDeltas = deltas;
87             final boolean trackDirs = dirs;
88             final boolean trackFiles = files;
89             
90             
91             buildDelta.accept(new IResourceDeltaVisitor()
92             {
93                 public boolean visit(IResourceDelta delta) throws CoreException
94                 {
95                     IResource resource = delta.getResource();
96                     
97                     // Only track files with the right kind of delta
98
boolean isTracked = (delta.getKind() & trackDeltas) > 0;
99                     if (isTracked)
100                     {
101                         // Only track dirs if desired
102
isTracked = trackDirs && resource.getType() != IResource.FILE;
103                         // Only track files if desired
104
isTracked |= trackFiles && resource.getType() == IResource.FILE;
105                     }
106                     
107                     // If tracking a change, then add it to the change set for inclusion in the variable's output
108
if (isTracked)
109                     {
110                         String JavaDoc osPath = resource.getLocation().toOSString();
111                         if (changedResources.add(osPath))
112                         {
113                             if (fileList.length() > 0)
114                             {
115                                 fileList.append(FILE_LIST_SEPARATOR);
116                             }
117                             
118                             // Since space is our separator, we need to add quotes
119
// around each file to handle filenames with embedded
120
// spaces. We also need to escape out embedded quotes in
121
// the filename so they don't conflict with these
122
// special quotes.
123
//
124
osPath = osPath.replaceAll("\"", "\\\\\""); //$NON-NLS-1$ //$NON-NLS-2$
125
fileList.append("\"" + osPath + "\""); //$NON-NLS-1$ //$NON-NLS-2$
126
}
127                     }
128                     return true;
129                 }
130             }, deltas);
131             result = fileList.toString();
132         }
133        
134         return result;
135     }
136 }
Popular Tags