KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.Collections JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.ant.internal.ui.AntUtil;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.IPath;
20
21
22 public class AntProjectNodeProxy extends AntProjectNode {
23     
24     private String JavaDoc fBuildFileName;
25     private String JavaDoc fDefaultTargetName;
26     private boolean fParsed= false;
27
28     /**
29      * Creates a new project node with the given name and the given build file
30      * name.
31      *
32      * @param name the project's name or <code>null</code> if the project's
33      * name is not known. If this value is <code>null</code>, the file will be
34      * parsed the first time a value is requested that requires it.
35      * @param buildFileName
36      */

37     public AntProjectNodeProxy(String JavaDoc name, String JavaDoc buildFileName) {
38         super(null, null);
39         fName= name;
40         fBuildFileName= buildFileName;
41     }
42     
43     /**
44      * Creates a new project node on the given build file.
45      */

46     public AntProjectNodeProxy(String JavaDoc buildFileName) {
47         this(null, buildFileName);
48     }
49     
50     public void parseBuildFile(boolean force) {
51         if (fParsed && !force) {
52             return;
53         }
54         fChildNodes= null;
55         fParsed= true;
56         AntTargetNode[] nodes = null;
57         IPath buildFilePath= AntUtil.getFile(getBuildFileName()).getLocation();
58         if (buildFilePath == null) {
59             setProblemSeverity(AntModelProblem.SEVERITY_ERROR);
60             setProblemMessage(AntModelMessages.AntProjectNodeProxy_0);
61             return;
62         }
63         nodes = AntUtil.getTargets(buildFilePath.toString());
64         
65         if (nodes == null || nodes.length < 1) {
66             setProblemSeverity(AntModelProblem.SEVERITY_ERROR);
67             setProblemMessage(AntModelMessages.AntProjectNodeProxy_1);
68             return;
69         }
70         
71         AntProjectNode projectNode = nodes[0].getProjectNode();
72         if (nodes[0].getTargetName().length() != 0) {
73             //not just the implicit target
74
for (int i = 0; i < nodes.length; i++) {
75                 addChildNode(nodes[i]);
76             }
77         }
78         
79         fModel= projectNode.getAntModel();
80         fProject= (AntModelProject)projectNode.getProject();
81         fLabel= null;
82         fName= null;
83     }
84     
85     public void parseBuildFile() {
86         parseBuildFile(false);
87     }
88     
89     /* (non-Javadoc)
90      * @see org.eclipse.ant.internal.ui.model.AntProjectNode#getDescription()
91      */

92     public String JavaDoc getDescription() {
93         if (fProject != null) {
94             return super.getDescription();
95         }
96         return null;
97     }
98     /* (non-Javadoc)
99      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getLabel()
100      */

101     public String JavaDoc getLabel() {
102         if (fName != null) {
103             return fName;
104         }
105         if (fProject == null) {
106             parseBuildFile();
107         }
108         StringBuffer JavaDoc name= new StringBuffer JavaDoc(super.getLabel());
109         AntProjectNode realNode= getRealNode();
110         if (realNode != null && realNode.getProblemMessage() != null) {
111             name.append(" <"); //$NON-NLS-1$
112
name.append(getRealNode().getProblemMessage());
113             name.append('>');
114         }
115         fName= name.toString();
116         return fName;
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getChildNodes()
121      */

122     public List JavaDoc getChildNodes() {
123         if (fProject == null) {
124             parseBuildFile();
125         }
126         List JavaDoc children= super.getChildNodes();
127         if (children == null) {
128             return Collections.EMPTY_LIST;
129         }
130         return children;
131     }
132     
133     public String JavaDoc getBuildFileName() {
134         return fBuildFileName;
135     }
136
137     public void setDefaultTargetName(String JavaDoc defaultTarget) {
138         fDefaultTargetName= defaultTarget;
139     }
140     
141     public String JavaDoc getDefaultTargetName() {
142         if (fProject == null) {
143             return fDefaultTargetName;
144         }
145         return super.getDefaultTargetName();
146     }
147     
148     /* (non-Javadoc)
149      * @see org.eclipse.ant.internal.ui.model.AntElementNode#hasChildren()
150      */

151     public boolean hasChildren() {
152         return true;
153     }
154     
155     /* (non-Javadoc)
156      * @see org.eclipse.ant.internal.ui.model.AntElementNode#dispose()
157      */

158     public void dispose() {
159         if (fProject != null) {
160             super.dispose();
161         }
162     }
163     
164     /* (non-Javadoc)
165      * @see org.eclipse.ant.internal.ui.model.AntElementNode#isErrorNode()
166      */

167     public boolean isErrorNode() {
168         if (fProject == null) {
169             return super.isErrorNode();
170         }
171         return getRealNode().isErrorNode();
172     }
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getProblemMessage()
176      */

177     public String JavaDoc getProblemMessage() {
178         if (isErrorNode()) {
179             return getBuildFileName();
180         }
181         return null;
182     }
183     
184     /* (non-Javadoc)
185      * @see org.eclipse.ant.internal.ui.model.AntElementNode#isWarningNode()
186      */

187     public boolean isWarningNode() {
188         if (fProject == null) {
189             return super.isWarningNode();
190         }
191         return getRealNode().isWarningNode();
192     }
193     
194     private AntProjectNode getRealNode() {
195         if (fModel != null) {
196             return fModel.getProjectNode();
197         }
198         return null;
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getAntModel()
203      */

204     protected IAntModel getAntModel() {
205         if (fProject == null) {
206             parseBuildFile();
207         }
208         return super.getAntModel();
209     }
210     
211     /* (non-Javadoc)
212      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getLength()
213      */

214     public int getLength() {
215         if (fProject == null) {
216             parseBuildFile();
217         }
218         AntProjectNode realNode= getRealNode();
219         if (realNode == null) {
220             return -1;
221         }
222         return realNode.getLength();
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getOffset()
227      */

228     public int getOffset() {
229         if (fProject == null) {
230             parseBuildFile();
231         }
232         AntProjectNode realNode= getRealNode();
233         if (realNode == null) {
234             return -1;
235         }
236         return realNode.getOffset();
237     }
238     
239     /* (non-Javadoc)
240      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getSelectionLength()
241      */

242     public int getSelectionLength() {
243         if (fProject == null) {
244             parseBuildFile();
245         }
246         AntProjectNode realNode= getRealNode();
247         if (realNode == null) {
248             return -1;
249         }
250         return realNode.getSelectionLength();
251     }
252     
253     /* (non-Javadoc)
254      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getBuildFileResource()
255      */

256     public IFile getBuildFileResource() {
257         if (fProject == null) {
258             if (fBuildFileName != null) {
259                 return AntUtil.getFile(fBuildFileName);
260             }
261         }
262         return super.getBuildFileResource();
263     }
264     
265     /* (non-Javadoc)
266      * @see java.lang.Object#toString()
267      */

268     public String JavaDoc toString() {
269         return getLabel();
270     }
271 }
272
Popular Tags