KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > loader > AntProjectDataEditor


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.loader;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.apache.tools.ant.module.api.AntProjectCookie;
26 import org.openide.cookies.EditCookie;
27 import org.openide.cookies.EditorCookie;
28 import org.openide.cookies.OpenCookie;
29 import org.openide.cookies.PrintCookie;
30 import org.openide.cookies.SaveCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileLock;
33 import org.openide.loaders.DataObject;
34 import org.openide.nodes.FilterNode;
35 import org.openide.nodes.Node;
36 import org.openide.text.CloneableEditor;
37 import org.openide.text.DataEditorSupport;
38 import org.openide.util.NbBundle;
39 import org.openide.util.WeakListeners;
40 import org.openide.windows.CloneableOpenSupport;
41 import org.w3c.dom.Element JavaDoc;
42
43 final class AntProjectDataEditor extends DataEditorSupport implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, ChangeListener JavaDoc {
44     
45     private boolean addedChangeListener = false;
46
47     public AntProjectDataEditor (AntProjectDataObject obj) {
48         super (obj, new AntEnv (obj));
49         setMIMEType(AntProjectDataLoader.REQUIRED_MIME);
50     }
51
52     @Override JavaDoc
53     protected boolean notifyModified () {
54         if (!super.notifyModified ()) {
55             return false;
56         } else {
57             AntEnv e = (AntEnv) env;
58             e.getAntProjectDataObject ().addSaveCookie (e);
59             return true;
60         }
61     }
62
63     @Override JavaDoc
64     protected void notifyUnmodified () {
65         super.notifyUnmodified ();
66         AntEnv e = (AntEnv) env;
67         e.getAntProjectDataObject ().removeSaveCookie (e);
68     }
69     
70     @Override JavaDoc
71     protected String JavaDoc messageName() {
72         String JavaDoc name = super.messageName();
73         return annotateWithProjectName(name);
74     }
75     
76     @Override JavaDoc
77     protected String JavaDoc messageHtmlName () {
78         String JavaDoc name = super.messageHtmlName();
79         return name != null ? annotateWithProjectName(name) : null;
80     }
81     
82     /** #25793 fix - adds project name to given ant script name if needed.
83      * @return ant script name annotated with project name or ant script name unchanged
84      */

85     private String JavaDoc annotateWithProjectName (String JavaDoc name) {
86         DataObject d = getDataObject();
87         if (d.getPrimaryFile().getNameExt().equals("build.xml")) { // NOI18N
88
// #25793: show project name in case the script name does not suffice
89
AntProjectCookie cookie = d.getCookie(AntProjectCookie.class);
90             Element JavaDoc pel = cookie.getProjectElement();
91             if (pel != null) {
92                 String JavaDoc projectName = pel.getAttribute("name"); // NOI18N
93
if (!projectName.equals("")) { // NOI18N
94
name = NbBundle.getMessage(AntProjectDataEditor.class,
95                         "LBL_editor_tab", name, projectName);
96                 }
97             }
98             if (!addedChangeListener) {
99                 cookie.addChangeListener(WeakListeners.change(this, cookie));
100                 addedChangeListener = true;
101             }
102         }
103         return name;
104     }
105     
106     
107     /**
108      * Overridden to ensure that the displayName of the node in the editor has
109      * the right annotation for build.xml files, so that the Navigator will display it.
110      */

111     @Override JavaDoc
112     protected void initializeCloneableEditor(CloneableEditor editor) {
113         super.initializeCloneableEditor(editor);
114         editor.setActivatedNodes(new Node[] {
115             new FilterNode(getDataObject().getNodeDelegate()) {
116                 @Override JavaDoc
117                 public String JavaDoc getDisplayName() {
118                     return messageName();
119                 }
120             }
121         });
122     }
123
124     public void stateChanged(ChangeEvent JavaDoc e) {
125         // Project name might have changed. See messageName().
126
updateTitles();
127     }
128     
129     private static class AntEnv extends DataEditorSupport.Env implements SaveCookie {
130
131         private static final long serialVersionUID = 6610627377311504616L;
132         
133         public AntEnv (AntProjectDataObject obj) {
134             super (obj);
135         }
136         
137         AntProjectDataObject getAntProjectDataObject () {
138             return (AntProjectDataObject) getDataObject ();
139         }
140
141         @Override JavaDoc
142         protected FileObject getFile () {
143             return getDataObject ().getPrimaryFile ();
144         }
145
146         @Override JavaDoc
147         protected FileLock takeLock () throws IOException JavaDoc {
148             return ((AntProjectDataObject) getDataObject ()).getPrimaryEntry ().takeLock ();
149         }
150
151         public void save () throws IOException JavaDoc {
152             ((AntProjectDataEditor) findCloneableOpenSupport ()).saveDocument ();
153             getDataObject ().setModified (false);
154         }
155
156         @Override JavaDoc
157         public CloneableOpenSupport findCloneableOpenSupport () {
158             return (CloneableOpenSupport) getDataObject ().getCookie (EditCookie.class);
159         }
160
161     }
162
163 }
164
Popular Tags