1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.File ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.logging.Logger ; 30 import org.netbeans.modules.projectimport.LoggerFactory; 31 import org.openide.ErrorManager; 32 33 38 public final class Workspace { 39 40 41 private static final Logger logger = 42 LoggerFactory.getDefault().createLogger(Workspace.class); 43 44 45 static class Variable { 46 private String name; 47 private String location; 48 49 String getName() { 50 return name; 51 } 52 53 void setName(String name) { 54 this.name = name; 55 } 56 57 String getLocation() { 58 return location; 59 } 60 61 void setLocation(String location) { 62 this.location = location; 63 } 64 65 public String toString() { 66 return name + " = " + location; 67 } 68 69 public boolean equals(Object obj) { 70 if (this == obj) return true; 71 if (!(obj instanceof Variable)) return false; 72 final Variable var = (Variable) obj; 73 if (name != null ? !name.equals(var.name) :var.name != null) 74 return false; 75 if (location != null ? !location.equals(var.location) : var.location != null) 76 return false; 77 return true; 78 } 79 80 public int hashCode() { 81 int result = 17; 82 result = 37 * result + System.identityHashCode(name); 83 result = 37 * result + System.identityHashCode(location); 84 return result; 85 } 86 } 87 88 private static final String RUNTIME_SETTINGS = 89 ".metadata/.plugins/org.eclipse.core.runtime/.settings/"; 90 static final String CORE_PREFERENCE = 91 RUNTIME_SETTINGS + "org.eclipse.jdt.core.prefs"; 92 static final String LAUNCHING_PREFERENCES = 93 RUNTIME_SETTINGS + "org.eclipse.jdt.launching.prefs"; 94 95 static final String RESOURCE_PROJECTS_DIR = 96 ".metadata/.plugins/org.eclipse.core.resources/.projects"; 97 98 static final String DEFAULT_JRE_CONTAINER = 99 "org.eclipse.jdt.launching.JRE_CONTAINER"; 100 101 private File corePrefFile; 102 private File launchingPrefsFile; 103 private File resourceProjectsDir; 104 private File workspaceDir; 105 106 private Set variables; 107 private Set projects = new HashSet (); 108 private Map jreContainers; 109 private Map userLibraries; 110 111 119 static Workspace createWorkspace(File workspaceDir) { 120 if (!EclipseUtils.isRegularWorkSpace(workspaceDir)) { 121 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, 122 "There is not a regular workspace in " + workspaceDir); 123 return null; 124 } 125 Workspace workspace = new Workspace(workspaceDir); 126 return workspace; 127 } 128 129 130 private Workspace(File workspaceDir) { 131 this.workspaceDir = workspaceDir; 132 corePrefFile = new File (workspaceDir, CORE_PREFERENCE); 133 launchingPrefsFile = new File (workspaceDir, LAUNCHING_PREFERENCES); 134 resourceProjectsDir = new File (workspaceDir, RESOURCE_PROJECTS_DIR); 135 } 136 137 File getDirectory() { 138 return workspaceDir; 139 } 140 141 File getCorePreferenceFile() { 142 return corePrefFile; 143 } 144 145 File getLaunchingPrefsFile() { 146 return launchingPrefsFile; 147 } 148 149 File getResourceProjectsDir() { 150 return resourceProjectsDir; 151 } 152 153 void addVariable(Variable var) { 154 if (variables == null) { 155 variables = new HashSet (); 156 } 157 variables.add(var); 158 } 159 160 Set getVariables() { 161 return variables; 162 } 163 164 void setJREContainers(Map jreContainers) { 165 this.jreContainers = jreContainers; 166 } 167 168 void addProject(EclipseProject project) { 169 projects.add(project); 170 } 171 172 void addUserLibrary(String libName, Collection jars) { 173 if (userLibraries == null) { 174 userLibraries = new HashMap (); 175 } 176 userLibraries.put(libName, jars); 177 } 178 179 Collection getJarsForUserLibrary(String libRawPath) { 180 return (Collection ) userLibraries.get(libRawPath); 181 } 182 183 187 EclipseProject getProjectByRawPath(String rawPath) { 188 EclipseProject project = null; 189 for (Iterator it = projects.iterator(); it.hasNext(); ) { 190 EclipseProject prj = (EclipseProject) it.next(); 191 if (prj.getName().equals(rawPath.substring(1))) { 193 project = prj; 194 } 195 } 196 if (project == null) { 197 logger.info("Project with raw path \"" + rawPath + "\" cannot" + " be found in project list: " + projects); } 200 return project; 201 } 202 203 public Set getProjects() { 204 return projects; 205 } 206 207 String getProjectAbsolutePath(String projectName) { 208 for (Iterator it = projects.iterator(); it.hasNext(); ) { 209 EclipseProject project = ((EclipseProject) it.next()); 210 if (project.getName().equals(projectName)) { 211 return project.getDirectory().getAbsolutePath(); 212 } 213 } 214 return null; 215 } 216 217 221 String getJDKDirectory(String jreContainer) { 222 if (jreContainer != null) { 223 if (!DEFAULT_JRE_CONTAINER.equals(jreContainer)) { 224 jreContainer = jreContainer.substring(jreContainer.lastIndexOf('/') + 1); 226 } 227 for (Iterator it = jreContainers.entrySet().iterator(); it.hasNext(); ) { 228 Map.Entry entry = (Map.Entry ) it.next(); 229 if (entry.getKey().equals(jreContainer)) { 230 return (String ) entry.getValue(); 231 } 232 } 233 } 234 return null; 235 } 236 } 237 | Popular Tags |