1 11 12 package org.eclipse.ant.internal.ui.model; 13 14 import java.util.ArrayList ; 15 import java.util.Enumeration ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.apache.tools.ant.Target; 20 import org.eclipse.ant.internal.ui.AntUIImages; 21 import org.eclipse.ant.internal.ui.AntUIPlugin; 22 import org.eclipse.ant.internal.ui.IAntUIConstants; 23 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants; 24 import org.eclipse.jface.preference.IPreferenceStore; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.text.IRegion; 27 28 public class AntTargetNode extends AntElementNode { 29 30 private Target fTarget= null; 31 private String fLabel= null; 32 33 public AntTargetNode(Target target) { 34 super("target"); fTarget= target; 36 } 37 38 41 public String getLabel() { 42 if (fLabel == null) { 43 StringBuffer displayName= new StringBuffer (getTargetName()); 44 if (isDefaultTarget()) { 45 displayName.append(AntModelMessages.AntTargetNode_2); 46 } 47 if (isExternal()) { 48 appendEntityName(displayName); 49 } 50 51 fLabel= displayName.toString(); 52 } 53 return fLabel; 54 } 55 56 public Target getTarget() { 57 return fTarget; 58 } 59 60 public boolean isDefaultTarget() { 61 String targetName= fTarget.getName(); 62 if (targetName == null) { 63 return false; 64 } 65 return targetName.equals(fTarget.getProject().getDefaultTarget()); 66 } 67 68 74 public boolean isInternal() { 75 Target target= getTarget(); 76 return target.getDescription() == null && !isDefaultTarget(); 77 } 78 79 82 protected ImageDescriptor getBaseImageDescriptor() { 83 ImageDescriptor base= null; 84 if (isDefaultTarget()) { 85 base = AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ANT_DEFAULT_TARGET); 86 } else if (getTarget().getDescription() == null) { 87 base = AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ANT_TARGET_INTERNAL); 88 } else { 89 base = AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ANT_TARGET); 90 } 91 return base; 92 } 93 94 97 public void reset() { 98 super.reset(); 99 Map currentTargets = fTarget.getProject().getTargets(); 100 if (currentTargets.get(fTarget.getName()) != null) { 101 currentTargets.remove(fTarget.getName()); 102 } 103 } 104 105 109 public String checkDependencies() { 110 Enumeration dependencies= fTarget.getDependencies(); 111 while (dependencies.hasMoreElements()) { 112 String dependency = (String ) dependencies.nextElement(); 113 if (fTarget.getProject().getTargets().get(dependency) == null) { 114 return dependency; 115 } 116 } 117 return null; 118 } 119 120 123 public boolean collapseProjection() { 124 IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore(); 125 if (store.getBoolean(AntEditorPreferenceConstants.EDITOR_FOLDING_TARGETS)) { 126 return true; 127 } 128 return false; 129 } 130 131 public String getTargetName() { 132 String targetName= fTarget.getName(); 133 if (targetName == null) { 134 targetName= "target"; setProblemSeverity(AntModelProblem.SEVERITY_ERROR); 136 } 137 return targetName; 138 } 139 140 143 public boolean containsOccurrence(String identifier) { 144 if (getTargetName().equals(identifier)) { 145 return true; 146 } 147 Enumeration dependencies= fTarget.getDependencies(); 148 while (dependencies.hasMoreElements()) { 149 String dependency = (String ) dependencies.nextElement(); 150 if (dependency.equals(identifier)) { 151 return true; 152 } 153 } 154 if (identifier.startsWith("${") && identifier.endsWith("}")) { String ifString= fTarget.getIf(); 157 if (ifString != null && ifString.endsWith(identifier.substring(2, identifier.length() - 1))) { 158 return true; 159 } 160 String unlessString= fTarget.getUnless(); 161 if (unlessString != null && unlessString.endsWith(identifier.substring(2, identifier.length() - 1))) { 162 return true; 163 } 164 } 165 return false; 166 } 167 168 171 public String getOccurrencesIdentifier() { 172 return getTargetName(); 173 } 174 175 public List computeIdentifierOffsets(String identifier) { 176 String textToSearch= getAntModel().getText(getOffset(), getLength()); 177 if (textToSearch == null || textToSearch.length() == 0 || identifier.length() == 0) { 178 return null; 179 } 180 List results= new ArrayList (); 181 if (getTargetName().equals(identifier)) { 182 int nameOffset= textToSearch.indexOf("name"); nameOffset= textToSearch.indexOf(identifier, nameOffset); 184 results.add(new Integer (getOffset() + nameOffset)); 185 } else { 186 String ifString= fTarget.getIf(); 187 if (ifString != null && ifString.endsWith(identifier)) { 188 int ifOffset= textToSearch.indexOf("if"); ifOffset= textToSearch.indexOf(identifier, ifOffset); 190 results.add(new Integer (getOffset() + ifOffset)); 191 } else { 192 String unlessString= fTarget.getUnless(); 193 if (unlessString != null && unlessString.endsWith(identifier)) { 194 int unlessOffset= textToSearch.indexOf("unless"); unlessOffset= textToSearch.indexOf(identifier, unlessOffset); 196 results.add(new Integer (getOffset() + unlessOffset)); 197 } else { 198 int dependsOffset= textToSearch.indexOf("depends"); while (dependsOffset > 0 && !Character.isWhitespace(textToSearch.charAt(dependsOffset - 1))) { 200 dependsOffset= textToSearch.indexOf("depends", dependsOffset + 1); } 202 if (dependsOffset != -1) { 203 dependsOffset+= 7; 204 int dependsOffsetEnd= textToSearch.indexOf('"', dependsOffset); 205 dependsOffsetEnd= textToSearch.indexOf('"', dependsOffsetEnd+1); 206 while (dependsOffset < dependsOffsetEnd) { 207 dependsOffset= textToSearch.indexOf(identifier, dependsOffset); 208 if (dependsOffset == -1 || dependsOffset > dependsOffsetEnd) { 209 break; 210 } 211 char delimiter= textToSearch.charAt(dependsOffset - 1); 212 if (delimiter == ',' || delimiter == '"' || delimiter == ' ') { 213 results.add(new Integer (getOffset() + dependsOffset)); 214 } 215 dependsOffset+= identifier.length(); 216 } 217 } 218 } 219 } 220 } 221 return results; 222 } 223 224 227 public boolean isRegionPotentialReference(IRegion region) { 228 boolean superOK= super.isRegionPotentialReference(region); 229 if (!superOK) { 230 return false; 231 } 232 233 String textToSearch= getAntModel().getText(getOffset(), getLength()); 234 if (textToSearch == null) { 235 return false; 236 } 237 if (checkReferenceRegion(region, textToSearch, "depends")) { return true; 239 } else if (checkReferenceRegion(region, textToSearch, "name")) { return true; 241 } else if (checkReferenceRegion(region, textToSearch, "if")) { return true; 243 } 244 return checkReferenceRegion(region, textToSearch, "unless"); } 246 247 250 public boolean isFromDeclaration(IRegion region) { 251 String textToSearch= getAntModel().getText(getOffset(), getLength()); 252 if (textToSearch == null || textToSearch.length() == 0) { 253 return false; 254 } 255 return checkReferenceRegion(region, textToSearch, "name"); } 257 } 258 | Popular Tags |