KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > nodes > AntProjectNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.nodes;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.apache.tools.ant.module.AntModule;
27 import org.apache.tools.ant.module.api.AntProjectCookie;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileStateInvalidException;
30 import org.openide.loaders.DataNode;
31 import org.openide.loaders.DataObject;
32 import org.openide.nodes.Node;
33 import org.openide.nodes.Sheet;
34 import org.openide.util.Mutex;
35 import org.openide.util.NbBundle;
36 import org.openide.util.Utilities;
37 import org.openide.util.WeakListeners;
38 import org.w3c.dom.Element JavaDoc;
39
40 public final class AntProjectNode extends DataNode implements ChangeListener JavaDoc {
41     
42     public AntProjectNode (DataObject obj) {
43         this(obj, obj.getCookie(AntProjectCookie.class));
44     }
45     
46     private AntProjectNode(DataObject obj, AntProjectCookie cookie) {
47         super(obj, new AntProjectChildren(cookie));
48         cookie.addChangeListener(WeakListeners.change(this, cookie));
49     }
50     
51     @Override JavaDoc
52     public Image JavaDoc getIcon(int type) {
53         Image JavaDoc i = getBasicIcon();
54         try {
55             // #25248: annotate the build script icon
56
i = getDataObject().getPrimaryFile().getFileSystem().getStatus().
57                 annotateIcon(i, type, getDataObject().files());
58         } catch (FileStateInvalidException fsie) {
59             AntModule.err.notify(ErrorManager.INFORMATIONAL, fsie);
60         }
61         return i;
62     }
63     private Image JavaDoc getBasicIcon() {
64         AntProjectCookie.ParseStatus cookie = getCookie(AntProjectCookie.ParseStatus.class);
65         if (cookie.getFile() == null && cookie.getFileObject() == null) {
66             // Script has been invalidated perhaps? Don't continue, we would
67
// just get an NPE from the getParseException.
68
return Utilities.loadImage("org/apache/tools/ant/module/resources/AntIconError.gif"); // NOI18N
69
}
70         if (!cookie.isParsed()) {
71             // Assume for now it is not erroneous.
72
return Utilities.loadImage("org/apache/tools/ant/module/resources/AntIcon.gif"); // NOI18N
73
}
74         Throwable JavaDoc exc = cookie.getParseException();
75         if (exc != null) {
76             return Utilities.loadImage("org/apache/tools/ant/module/resources/AntIconError.gif"); // NOI18N
77
} else {
78             return Utilities.loadImage("org/apache/tools/ant/module/resources/AntIcon.gif"); // NOI18N
79
}
80     }
81     @Override JavaDoc
82     public Image JavaDoc getOpenedIcon(int type) {
83         return getIcon(type);
84     }
85     
86     @Override JavaDoc
87     public String JavaDoc getShortDescription() {
88         AntProjectCookie cookie = getCookie(AntProjectCookie.class);
89         if (cookie.getFile() == null && cookie.getFileObject() == null) {
90             // Script has been invalidated perhaps? Don't continue, we would
91
// just get an NPE from the getParseException.
92
return super.getShortDescription();
93         }
94         Throwable JavaDoc exc = cookie.getParseException();
95         if (exc != null) {
96             String JavaDoc m = exc.getLocalizedMessage();
97             if (m != null) {
98                  return m;
99             } else {
100                 return exc.toString();
101             }
102         } else {
103             Element JavaDoc pel = cookie.getProjectElement();
104             if (pel != null) {
105                 String JavaDoc projectName = pel.getAttribute("name"); // NOI18N
106
if (!projectName.equals("")) { // NOI18N
107
// Set the node description in the IDE to the name of the project
108
return NbBundle.getMessage(AntProjectNode.class, "LBL_named_script_description", projectName);
109                 } else {
110                     // No name specified, OK.
111
return NbBundle.getMessage(AntProjectNode.class, "LBL_anon_script_description");
112                 }
113             } else {
114                 // ???
115
return super.getShortDescription();
116             }
117         }
118     }
119
120     @Override JavaDoc
121     protected Sheet createSheet() {
122         Sheet sheet = super.createSheet();
123
124         Sheet.Set props = new Sheet.Set();
125         props.setName("project"); // NOI18N
126
props.setDisplayName(NbBundle.getMessage(AntProjectNode.class, "LBL_proj_sheet"));
127         props.setShortDescription(NbBundle.getMessage(AntProjectNode.class, "HINT_proj_sheet"));
128         add2Sheet (props);
129         sheet.put(props);
130
131         return sheet;
132     }
133
134     private class ProjectNameProperty extends AntProperty {
135         public ProjectNameProperty(String JavaDoc name) {
136             super(name);
137         }
138         @Override JavaDoc
139         protected Element JavaDoc getElement () {
140             return getCookie(AntProjectCookie.class).getProjectElement();
141         }
142     }
143
144     private class ProjectTargetProperty extends AntProperty {
145         public ProjectTargetProperty(String JavaDoc name) {
146             super(name);
147         }
148         @Override JavaDoc
149         protected Element JavaDoc getElement () {
150             return getCookie(AntProjectCookie.class).getProjectElement();
151         }
152     }
153
154     private void add2Sheet (Sheet.Set props) {
155         ResourceBundle JavaDoc bundle = NbBundle.getBundle (AntProjectNode.class);
156         
157         // Create the required properties (XML attributes) of the Ant project
158
Node.Property<?> prop = new ProjectNameProperty("name"); // NOI18N
159
// Cannot reuse 'name' because it conflicts with the DataObject.PROP_NAME:
160
prop.setName ("projectName"); // NOI18N
161
prop.setDisplayName (bundle.getString ("PROP_projectName"));
162         prop.setShortDescription (bundle.getString ("HINT_projectName"));
163         props.put (prop);
164         prop = new ProjectTargetProperty("default"); // NOI18N
165
prop.setDisplayName (bundle.getString ("PROP_default"));
166         prop.setShortDescription (bundle.getString ("HINT_default"));
167         props.put (prop);
168     }
169
170     public void stateChanged (ChangeEvent JavaDoc ev) {
171         Mutex.EVENT.writeAccess(new Runnable JavaDoc() {
172             public void run() {
173                 fireIconChange();
174                 fireOpenedIconChange();
175                 fireShortDescriptionChange(null, null);
176                 fireCookieChange();
177                 firePropertyChange(null, null, null);
178             }
179         });
180     }
181
182 }
183
Popular Tags