001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.apache.tools.ant.module.loader;
043:
044: import java.io.IOException;
045: import javax.swing.event.ChangeEvent;
046: import javax.swing.event.ChangeListener;
047: import org.apache.tools.ant.module.api.AntProjectCookie;
048: import org.openide.cookies.EditCookie;
049: import org.openide.cookies.EditorCookie;
050: import org.openide.cookies.OpenCookie;
051: import org.openide.cookies.PrintCookie;
052: import org.openide.cookies.SaveCookie;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileLock;
055: import org.openide.loaders.DataObject;
056: import org.openide.nodes.FilterNode;
057: import org.openide.nodes.Node;
058: import org.openide.text.CloneableEditor;
059: import org.openide.text.DataEditorSupport;
060: import org.openide.util.NbBundle;
061: import org.openide.util.WeakListeners;
062: import org.openide.windows.CloneableOpenSupport;
063: import org.w3c.dom.Element;
064:
065: final class AntProjectDataEditor extends DataEditorSupport implements
066: OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie,
067: ChangeListener {
068:
069: private boolean addedChangeListener = false;
070:
071: public AntProjectDataEditor(AntProjectDataObject obj) {
072: super (obj, new AntEnv(obj));
073: setMIMEType(AntProjectDataLoader.REQUIRED_MIME);
074: }
075:
076: @Override
077: protected boolean notifyModified() {
078: if (!super .notifyModified()) {
079: return false;
080: } else {
081: AntEnv e = (AntEnv) env;
082: e.getAntProjectDataObject().addSaveCookie(e);
083: return true;
084: }
085: }
086:
087: @Override
088: protected void notifyUnmodified() {
089: super .notifyUnmodified();
090: AntEnv e = (AntEnv) env;
091: e.getAntProjectDataObject().removeSaveCookie(e);
092: }
093:
094: @Override
095: protected String messageName() {
096: String name = super .messageName();
097: return annotateWithProjectName(name);
098: }
099:
100: @Override
101: protected String messageHtmlName() {
102: String name = super .messageHtmlName();
103: return name != null ? annotateWithProjectName(name) : null;
104: }
105:
106: /** #25793 fix - adds project name to given ant script name if needed.
107: * @return ant script name annotated with project name or ant script name unchanged
108: */
109: private String annotateWithProjectName(String name) {
110: DataObject d = getDataObject();
111: if (d.getPrimaryFile().getNameExt().equals("build.xml")) { // NOI18N
112: // #25793: show project name in case the script name does not suffice
113: AntProjectCookie cookie = d
114: .getCookie(AntProjectCookie.class);
115: Element pel = cookie.getProjectElement();
116: if (pel != null) {
117: String projectName = pel.getAttribute("name"); // NOI18N
118: if (!projectName.equals("")) { // NOI18N
119: name = NbBundle.getMessage(
120: AntProjectDataEditor.class,
121: "LBL_editor_tab", name, projectName);
122: }
123: }
124: if (!addedChangeListener) {
125: cookie.addChangeListener(WeakListeners.change(this ,
126: cookie));
127: addedChangeListener = true;
128: }
129: }
130: return name;
131: }
132:
133: /**
134: * Overridden to ensure that the displayName of the node in the editor has
135: * the right annotation for build.xml files, so that the Navigator will display it.
136: */
137: @Override
138: protected void initializeCloneableEditor(CloneableEditor editor) {
139: super .initializeCloneableEditor(editor);
140: editor.setActivatedNodes(new Node[] { new FilterNode(
141: getDataObject().getNodeDelegate()) {
142: @Override
143: public String getDisplayName() {
144: return messageName();
145: }
146: } });
147: }
148:
149: public void stateChanged(ChangeEvent e) {
150: // Project name might have changed. See messageName().
151: updateTitles();
152: }
153:
154: private static class AntEnv extends DataEditorSupport.Env implements
155: SaveCookie {
156:
157: private static final long serialVersionUID = 6610627377311504616L;
158:
159: public AntEnv(AntProjectDataObject obj) {
160: super (obj);
161: }
162:
163: AntProjectDataObject getAntProjectDataObject() {
164: return (AntProjectDataObject) getDataObject();
165: }
166:
167: @Override
168: protected FileObject getFile() {
169: return getDataObject().getPrimaryFile();
170: }
171:
172: @Override
173: protected FileLock takeLock() throws IOException {
174: return ((AntProjectDataObject) getDataObject())
175: .getPrimaryEntry().takeLock();
176: }
177:
178: public void save() throws IOException {
179: ((AntProjectDataEditor) findCloneableOpenSupport())
180: .saveDocument();
181: getDataObject().setModified(false);
182: }
183:
184: @Override
185: public CloneableOpenSupport findCloneableOpenSupport() {
186: return (CloneableOpenSupport) getDataObject().getCookie(
187: EditCookie.class);
188: }
189:
190: }
191:
192: }
|