1 11 package org.eclipse.ant.internal.ui.launchConfigurations; 12 13 import java.io.File ; 14 import com.ibm.icu.text.MessageFormat; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.ant.core.AntCorePlugin; 19 import org.eclipse.ant.core.AntCorePreferences; 20 import org.eclipse.ant.core.IAntClasspathEntry; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.debug.core.ILaunchConfiguration; 25 import org.eclipse.jdt.internal.launching.AbstractRuntimeClasspathEntry; 26 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 27 import org.eclipse.jdt.launching.JavaRuntime; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 31 37 public class AntHomeClasspathEntry extends AbstractRuntimeClasspathEntry { 38 39 public static final String TYPE_ID = "org.eclipse.ant.ui.classpathentry.antHome"; 41 45 private String antHomeLocation = null; 46 47 50 public AntHomeClasspathEntry() { 51 antHomeLocation = null; 52 } 53 54 60 public AntHomeClasspathEntry(String antHome) { 61 antHomeLocation = antHome; 62 } 63 64 67 protected void buildMemento(Document document, Element memento) throws CoreException { 68 if (antHomeLocation == null) { 69 memento.setAttribute("default", "true"); } else { 71 memento.setAttribute("antHome", new Path(antHomeLocation).toString()); } 73 } 74 77 public void initializeFrom(Element memento) throws CoreException { 78 String antHome = memento.getAttribute("antHome"); if (antHome != null && (antHome.length() > 0)) { 80 IPath path = new Path(antHome); 81 antHomeLocation = path.toOSString(); 82 } else { 83 antHomeLocation = null; 84 } 85 } 86 89 public String getTypeId() { 90 return TYPE_ID; 91 } 92 95 public IRuntimeClasspathEntry[] getRuntimeClasspathEntries(ILaunchConfiguration configuration) throws CoreException { 96 List libs = new ArrayList (40); 97 AntCorePreferences preferences = AntCorePlugin.getPlugin().getPreferences(); 98 if (antHomeLocation == null) { 99 IAntClasspathEntry[] entries = preferences.getAntHomeClasspathEntries(); 100 for (int i = 0; i < entries.length; i++) { 101 IAntClasspathEntry entry = entries[i]; 102 libs.add(JavaRuntime.newStringVariableClasspathEntry(entry.getLabel())); 103 } 104 } else { 105 File lib= resolveAntHome(); 106 IPath libDir = new Path(antHomeLocation).append("lib"); String [] fileNames = lib.list(); 108 for (int i = 0; i < fileNames.length; i++) { 109 String name = fileNames[i]; 110 IPath path = new Path(name); 111 String fileExtension = path.getFileExtension(); 112 if ("jar".equalsIgnoreCase(fileExtension)) { libs.add(JavaRuntime.newArchiveRuntimeClasspathEntry(libDir.append(path))); 114 } 115 } 116 } 117 return (IRuntimeClasspathEntry[]) libs.toArray(new IRuntimeClasspathEntry[libs.size()]); 118 } 119 120 public File resolveAntHome() throws CoreException { 121 if (antHomeLocation == null) { return null; 123 } 124 IPath libDir= new Path(antHomeLocation).append("lib"); File lib= libDir.toFile(); 126 File parentDir= lib.getParentFile(); 127 if (parentDir == null || !parentDir.exists()) { 128 abort(MessageFormat.format(AntLaunchConfigurationMessages.AntHomeClasspathEntry_10, new String [] {antHomeLocation}), null); 129 } 130 if (!lib.exists() || !lib.isDirectory()) { 131 abort(MessageFormat.format(AntLaunchConfigurationMessages.AntHomeClasspathEntry_11, new String [] {antHomeLocation}), null); 132 } 133 return lib; 134 } 135 136 139 public String getName() { 140 if (antHomeLocation == null) { 141 return AntLaunchConfigurationMessages.AntHomeClasspathEntry_8; 142 } 143 return MessageFormat.format(AntLaunchConfigurationMessages.AntHomeClasspathEntry_9, new String []{antHomeLocation}); 144 } 145 146 149 public int getType() { 150 return IRuntimeClasspathEntry.OTHER; 151 } 152 153 156 public boolean isComposite() { 157 return true; 158 } 159 160 163 public boolean equals(Object obj) { 164 return obj instanceof AntHomeClasspathEntry && 165 equalsOrNull(antHomeLocation, ((AntHomeClasspathEntry)obj).antHomeLocation); 166 } 167 168 175 private boolean equalsOrNull(String s1, String s2) { 176 if (s1 == null || s2 == null) { 177 return s1 == s2; 178 } 179 return s1.equalsIgnoreCase(s2); 180 } 181 182 185 public int hashCode() { 186 return getClass().hashCode(); 187 } 188 189 194 protected void setAntHome(String path) { 195 antHomeLocation = path; 196 } 197 198 203 public String getAntHome() { 204 if (antHomeLocation == null) { 205 return AntCorePlugin.getPlugin().getPreferences().getAntHome(); 206 } 207 return antHomeLocation; 208 } 209 } 210 | Popular Tags |