1 19 20 package org.netbeans.modules.websvc.wsdl; 21 22 import java.io.*; 23 24 import org.openide.cookies.*; 25 import org.openide.filesystems.FileLock; 26 import org.openide.filesystems.FileObject; 27 import org.openide.text.DataEditorSupport; 28 import org.openide.text.NbDocument; 29 import org.openide.windows.CloneableOpenSupport; 30 31 import javax.swing.event.*; 32 import javax.swing.text.*; 33 import org.openide.awt.UndoRedo; 34 import org.openide.util.NbBundle; 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 38 import org.netbeans.modules.xml.api.EncodingUtil; 39 40 44 45 public class WsdlEditorSupport extends DataEditorSupport 46 implements EditCookie, EditorCookie.Observable,LineCookie, CloseCookie, PrintCookie { 47 48 private org.openide.DialogDescriptor dialog; 49 WsdlDataObject dataObject; 50 51 54 public WsdlEditorSupport(WsdlDataObject obj) { 55 super (obj, new XmlEnv (obj)); 56 dataObject=obj; 57 setMIMEType ("text/xml"); } 59 60 63 protected void saveFromKitToStream (StyledDocument doc, EditorKit kit, 64 OutputStream stream) 65 throws IOException, BadLocationException { 66 EditorKit k = this.createEditorKit(); 69 OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF8"); Writer writer = new BufferedWriter(osw); 71 k.write(writer, doc, 0, doc.getLength()); 72 writer.close(); 73 } 74 75 78 protected void loadFromStreamToKit (StyledDocument doc, InputStream stream, 79 EditorKit kit) 80 throws IOException, BadLocationException { 81 EditorKit k = this.createEditorKit(); 84 InputStreamReader isr = new InputStreamReader(stream, "UTF8"); Reader reader = new BufferedReader(isr); 86 k.read(reader, doc, 0); 87 reader.close(); 88 } 89 90 94 protected boolean notifyModified () { 95 boolean notif = super.notifyModified(); 96 if (!notif){ 97 return false; 98 } 99 WsdlDataObject obj = (WsdlDataObject) getDataObject (); 100 if (obj.getCookie (SaveCookie.class) == null) { 101 obj.addSaveCookie (new Save ()); 102 } 103 return true; 104 } 105 106 109 protected void notifyUnmodified () { 110 super.notifyUnmodified (); 111 WsdlDataObject obj = (WsdlDataObject) getDataObject (); 112 obj.removeSaveCookie(); 113 } 114 115 118 private class Save implements SaveCookie { 119 public Save () { 120 } 121 122 public void save () throws IOException { 123 WsdlDataObject obj = (WsdlDataObject) getDataObject (); 124 saveDocument(); 125 } 126 } 127 131 public void saveDocument () throws java.io.IOException { 132 final javax.swing.text.StyledDocument doc = getDocument(); 133 String enc = EncodingUtil.detectEncoding(doc); 135 if (enc == null) enc = "UTF8"; 137 try { 138 new java.io.OutputStreamWriter (new java.io.ByteArrayOutputStream (1), enc); 140 super.saveDocument(); 141 getDataObject().setModified (false); 143 } catch (java.io.UnsupportedEncodingException ex) { 144 String message = java.text.MessageFormat.format(NbBundle.getMessage(WsdlEditorSupport.class,"TEXT_SAVE_AS_UTF"), 146 new Object [] {enc}); 147 NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(message); 148 Object res = DialogDisplayer.getDefault().notify(descriptor); 149 150 if (res.equals(NotifyDescriptor.YES_OPTION)) { 151 152 154 try { 155 final int MAX_PROLOG = 1000; 156 int maxPrologLen = Math.min(MAX_PROLOG, doc.getLength()); 157 final char prolog[] = doc.getText(0, maxPrologLen).toCharArray(); 158 int prologLen = 0; 160 if (prolog[0] == '<' && prolog[1] == '?' && prolog[2] == 'x') { 162 163 for (int i = 3; i<maxPrologLen; i++) { 165 if (prolog[i] == '?' && prolog[i+1] == '>') { 166 prologLen = i + 1; 167 break; 168 } 169 } 170 } 171 172 final int passPrologLen = prologLen; 173 174 Runnable edit = new Runnable () { 175 public void run() { 176 try { 177 178 doc.remove(0, passPrologLen + 1); doc.insertString(0, "<?xml version='1.0' encoding='UTF-8' ?> \n<!-- was: " + new String (prolog, 0, passPrologLen + 1) + " -->", null); 181 } catch (BadLocationException e) { 182 if (System.getProperty("netbeans.debug.exceptions") != null) e.printStackTrace(); 184 } 185 } 186 }; 187 188 NbDocument.runAtomic(doc, edit); 189 190 super.saveDocument(); 191 getDataObject().setModified (false); 193 194 } catch (BadLocationException lex) { 195 org.openide.ErrorManager.getDefault().notify(lex); 196 } 197 198 } else { return; 200 } 201 } 202 } 203 204 public UndoRedo.Manager getUndo(){ 205 return getUndoRedo(); 206 } 207 208 212 private static class XmlEnv extends DataEditorSupport.Env { 213 214 private static final long serialVersionUID = -800036748848958489L; 215 216 218 221 public XmlEnv (WsdlDataObject obj) { 222 super (obj); 223 } 224 225 228 protected FileObject getFile () { 229 return getDataObject ().getPrimaryFile (); 230 } 231 232 238 protected FileLock takeLock () throws IOException { 239 return ((WsdlDataObject) getDataObject ()).getPrimaryEntry ().takeLock (); 240 } 241 242 247 public CloneableOpenSupport findCloneableOpenSupport () { 248 return (WsdlEditorSupport) getDataObject ().getCookie (WsdlEditorSupport.class); 249 } 250 } 251 } 252 | Popular Tags |