KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > model > AntTargetNode


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
12 package org.eclipse.ant.internal.ui.model;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 JavaDoc fLabel= null;
32     
33     public AntTargetNode(Target target) {
34         super("target"); //$NON-NLS-1$
35
fTarget= target;
36     }
37     
38     /* (non-Javadoc)
39      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getLabel()
40      */

41     public String JavaDoc getLabel() {
42         if (fLabel == null) {
43             StringBuffer JavaDoc displayName= new StringBuffer JavaDoc(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 JavaDoc targetName= fTarget.getName();
62         if (targetName == null) {
63             return false;
64         }
65         return targetName.equals(fTarget.getProject().getDefaultTarget());
66     }
67     
68     /**
69      * Returns whether this target is an internal target. Internal
70      * targets are targets which has no description. The default target
71      * is never considered internal.
72      * @return whether the given target is an internal target
73      */

74     public boolean isInternal() {
75         Target target= getTarget();
76         return target.getDescription() == null && !isDefaultTarget();
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.ant.internal.ui.editor.model.AntElementNode#getBaseImageDescriptor()
81      */

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     /* (non-Javadoc)
95      * @see org.eclipse.ant.internal.ui.editor.model.AntElementNode#reset()
96      */

97     public void reset() {
98         super.reset();
99          Map JavaDoc currentTargets = fTarget.getProject().getTargets();
100          if (currentTargets.get(fTarget.getName()) != null) {
101             currentTargets.remove(fTarget.getName());
102          }
103     }
104
105     /**
106      * Returns the name of a missing dependency or <code>null</code> if all
107      * dependencies exist in the project.
108      */

109     public String JavaDoc checkDependencies() {
110         Enumeration JavaDoc dependencies= fTarget.getDependencies();
111         while (dependencies.hasMoreElements()) {
112             String JavaDoc dependency = (String JavaDoc) dependencies.nextElement();
113              if (fTarget.getProject().getTargets().get(dependency) == null) {
114                 return dependency;
115              }
116         }
117         return null;
118     }
119     
120     /* (non-Javadoc)
121      * @see org.eclipse.ant.internal.ui.model.AntElementNode#collapseProjection()
122      */

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 JavaDoc getTargetName() {
132         String JavaDoc targetName= fTarget.getName();
133         if (targetName == null) {
134             targetName= "target"; //$NON-NLS-1$
135
setProblemSeverity(AntModelProblem.SEVERITY_ERROR);
136         }
137         return targetName;
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.ant.internal.ui.model.AntElementNode#containsOccurrence(java.lang.String)
142      */

143     public boolean containsOccurrence(String JavaDoc identifier) {
144         if (getTargetName().equals(identifier)) {
145             return true;
146         }
147         Enumeration JavaDoc dependencies= fTarget.getDependencies();
148         while (dependencies.hasMoreElements()) {
149             String JavaDoc dependency = (String JavaDoc) dependencies.nextElement();
150              if (dependency.equals(identifier)) {
151                 return true;
152              }
153         }
154         //looking for properties
155
if (identifier.startsWith("${") && identifier.endsWith("}")) { //$NON-NLS-1$ //$NON-NLS-2$
156
String JavaDoc ifString= fTarget.getIf();
157             if (ifString != null && ifString.endsWith(identifier.substring(2, identifier.length() - 1))) {
158                 return true;
159             }
160             String JavaDoc 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     /* (non-Javadoc)
169      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getOccurrencesIdentifier()
170      */

171     public String JavaDoc getOccurrencesIdentifier() {
172         return getTargetName();
173     }
174     
175     public List JavaDoc computeIdentifierOffsets(String JavaDoc identifier) {
176         String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
177         if (textToSearch == null || textToSearch.length() == 0 || identifier.length() == 0) {
178             return null;
179         }
180         List JavaDoc results= new ArrayList JavaDoc();
181         if (getTargetName().equals(identifier)) {
182             int nameOffset= textToSearch.indexOf("name"); //$NON-NLS-1$
183
nameOffset= textToSearch.indexOf(identifier, nameOffset);
184             results.add(new Integer JavaDoc(getOffset() + nameOffset));
185         } else {
186             String JavaDoc ifString= fTarget.getIf();
187             if (ifString != null && ifString.endsWith(identifier)) {
188                 int ifOffset= textToSearch.indexOf("if"); //$NON-NLS-1$
189
ifOffset= textToSearch.indexOf(identifier, ifOffset);
190                 results.add(new Integer JavaDoc(getOffset() + ifOffset));
191             } else {
192                 String JavaDoc unlessString= fTarget.getUnless();
193                 if (unlessString != null && unlessString.endsWith(identifier)) {
194                     int unlessOffset= textToSearch.indexOf("unless"); //$NON-NLS-1$
195
unlessOffset= textToSearch.indexOf(identifier, unlessOffset);
196                     results.add(new Integer JavaDoc(getOffset() + unlessOffset));
197                 } else {
198                     int dependsOffset= textToSearch.indexOf("depends"); //$NON-NLS-1$
199
while (dependsOffset > 0 && !Character.isWhitespace(textToSearch.charAt(dependsOffset - 1))) {
200                         dependsOffset= textToSearch.indexOf("depends", dependsOffset + 1); //$NON-NLS-1$
201
}
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 JavaDoc(getOffset() + dependsOffset));
214                             }
215                             dependsOffset+= identifier.length();
216                         }
217                     }
218                 }
219             }
220         }
221         return results;
222     }
223     
224     /* (non-Javadoc)
225      * @see org.eclipse.ant.internal.ui.model.AntElementNode#isRegionPotentialReference(org.eclipse.jface.text.IRegion)
226      */

227     public boolean isRegionPotentialReference(IRegion region) {
228         boolean superOK= super.isRegionPotentialReference(region);
229         if (!superOK) {
230             return false;
231         }
232         
233         String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
234         if (textToSearch == null) {
235             return false;
236         }
237         if (checkReferenceRegion(region, textToSearch, "depends")) { //$NON-NLS-1$
238
return true;
239         } else if (checkReferenceRegion(region, textToSearch, "name")) { //$NON-NLS-1$
240
return true;
241         } else if (checkReferenceRegion(region, textToSearch, "if")) { //$NON-NLS-1$
242
return true;
243         }
244         return checkReferenceRegion(region, textToSearch, "unless"); //$NON-NLS-1$
245
}
246     
247     /* (non-Javadoc)
248      * @see org.eclipse.ant.internal.ui.model.AntElementNode#isFromDeclaration(org.eclipse.jface.text.IRegion)
249      */

250     public boolean isFromDeclaration(IRegion region) {
251          String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
252          if (textToSearch == null || textToSearch.length() == 0) {
253             return false;
254          }
255          return checkReferenceRegion(region, textToSearch, "name"); //$NON-NLS-1$
256
}
257 }
258
Popular Tags