1 19 20 package org.apache.tools.ant.module.loader; 21 22 import java.io.IOException ; 23 import javax.swing.event.ChangeEvent ; 24 import javax.swing.event.ChangeListener ; 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 ; 42 43 final class AntProjectDataEditor extends DataEditorSupport implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, ChangeListener { 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 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 64 protected void notifyUnmodified () { 65 super.notifyUnmodified (); 66 AntEnv e = (AntEnv) env; 67 e.getAntProjectDataObject ().removeSaveCookie (e); 68 } 69 70 @Override 71 protected String messageName() { 72 String name = super.messageName(); 73 return annotateWithProjectName(name); 74 } 75 76 @Override 77 protected String messageHtmlName () { 78 String name = super.messageHtmlName(); 79 return name != null ? annotateWithProjectName(name) : null; 80 } 81 82 85 private String annotateWithProjectName (String name) { 86 DataObject d = getDataObject(); 87 if (d.getPrimaryFile().getNameExt().equals("build.xml")) { AntProjectCookie cookie = d.getCookie(AntProjectCookie.class); 90 Element pel = cookie.getProjectElement(); 91 if (pel != null) { 92 String projectName = pel.getAttribute("name"); if (!projectName.equals("")) { 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 111 @Override 112 protected void initializeCloneableEditor(CloneableEditor editor) { 113 super.initializeCloneableEditor(editor); 114 editor.setActivatedNodes(new Node[] { 115 new FilterNode(getDataObject().getNodeDelegate()) { 116 @Override 117 public String getDisplayName() { 118 return messageName(); 119 } 120 } 121 }); 122 } 123 124 public void stateChanged(ChangeEvent e) { 125 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 142 protected FileObject getFile () { 143 return getDataObject ().getPrimaryFile (); 144 } 145 146 @Override 147 protected FileLock takeLock () throws IOException { 148 return ((AntProjectDataObject) getDataObject ()).getPrimaryEntry ().takeLock (); 149 } 150 151 public void save () throws IOException { 152 ((AntProjectDataEditor) findCloneableOpenSupport ()).saveDocument (); 153 getDataObject ().setModified (false); 154 } 155 156 @Override 157 public CloneableOpenSupport findCloneableOpenSupport () { 158 return (CloneableOpenSupport) getDataObject ().getCookie (EditCookie.class); 159 } 160 161 } 162 163 } 164 | Popular Tags |