1 19 20 package org.netbeans.modules.websvc.wsdl.xmlutils; 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.RequestProcessor; 35 import org.openide.util.NbBundle; 36 import org.openide.DialogDisplayer; 37 import org.openide.NotifyDescriptor; 38 39 import org.netbeans.modules.xml.api.EncodingUtil; 40 41 45 46 public class XMLJ2eeEditorSupport extends DataEditorSupport 47 implements EditCookie, EditorCookie.Observable,LineCookie, CloseCookie, PrintCookie { 48 49 50 private static final int AUTO_PARSING_DELAY = 2000; 51 private org.openide.DialogDescriptor dialog; 52 private RequestProcessor.Task parsingDocumentTask; 53 XMLJ2eeDataObject dataObject; 54 55 58 public XMLJ2eeEditorSupport(XMLJ2eeDataObject obj) { 59 super (obj, new XmlEnv (obj)); 60 dataObject=obj; 61 62 setMIMEType ("text/xml"); 65 final DocumentListener docListener = new DocumentListener() { 66 public void changedUpdate(javax.swing.event.DocumentEvent e) { 67 doUpdate(); 68 } 69 70 public void insertUpdate(javax.swing.event.DocumentEvent e) { 71 doUpdate(); 72 } 73 74 public void removeUpdate(javax.swing.event.DocumentEvent e) { 75 doUpdate(); 76 } 77 78 private void doUpdate() { 79 if (!((XMLJ2eeDataObject)XMLJ2eeEditorSupport.this.getDataObject()).isNodeDirty()) 80 restartTimer(); 81 } 82 }; 83 84 addPropertyChangeListener(new java.beans.PropertyChangeListener () { 86 public void propertyChange(java.beans.PropertyChangeEvent evt) { 87 if (EditorCookie.Observable.PROP_DOCUMENT.equals(evt.getPropertyName()) 88 && isDocumentLoaded() && getDocument() != null) { 89 getDocument().addDocumentListener(docListener); 90 } 91 } 92 }); 93 } 94 95 98 protected void saveFromKitToStream (StyledDocument doc, EditorKit kit, 99 OutputStream stream) 100 throws IOException, BadLocationException { 101 EditorKit k = this.createEditorKit(); 104 OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF8"); Writer writer = new BufferedWriter(osw); 106 k.write(writer, doc, 0, doc.getLength()); 107 writer.close(); 108 } 109 110 113 protected void loadFromStreamToKit (StyledDocument doc, InputStream stream, 114 EditorKit kit) 115 throws IOException, BadLocationException { 116 EditorKit k = this.createEditorKit(); 119 InputStreamReader isr = new InputStreamReader(stream, "UTF8"); Reader reader = new BufferedReader(isr); 121 k.read(reader, doc, 0); 122 reader.close(); 123 } 124 125 128 public void restartTimer() { 129 dataObject.setDocumentDirty(true); 131 Runnable r = new Runnable () { 132 public void run() { 133 dataObject.parsingDocument(); 134 } 135 }; 136 if (parsingDocumentTask==null || parsingDocumentTask.isFinished() || 137 parsingDocumentTask.cancel()) { 138 parsingDocumentTask = RequestProcessor.getDefault().post(r,100); 139 } else { 140 parsingDocumentTask = RequestProcessor.getDefault().post(r,AUTO_PARSING_DELAY); 141 } 142 } 143 144 148 protected boolean notifyModified () { 149 boolean notif = super.notifyModified(); 150 if (!notif){ 151 return false; 152 } 153 XMLJ2eeDataObject obj = (XMLJ2eeDataObject) getDataObject (); 154 if (obj.getCookie (SaveCookie.class) == null) { 156 obj.addSaveCookie (new Save ()); 157 } 158 return true; 159 } 160 161 164 protected void notifyUnmodified () { 165 super.notifyUnmodified (); 166 XMLJ2eeDataObject obj = (XMLJ2eeDataObject) getDataObject (); 167 obj.removeSaveCookie(); 168 } 169 170 173 private class Save implements SaveCookie { 174 public Save () { 175 } 176 177 public void save () throws IOException { 178 XMLJ2eeDataObject obj = (XMLJ2eeDataObject) getDataObject (); 179 if (obj.isDocumentValid()) { 180 obj.setSavingDocument(true); 181 saveDocument(); 182 }else { 183 obj.displayErrorMessage(); 184 dialog = new org.openide.DialogDescriptor( 185 NbBundle.getMessage (XMLJ2eeEditorSupport.class, "MSG_invalidXmlWarning"), 186 NbBundle.getMessage (XMLJ2eeEditorSupport.class, "TTL_invalidXmlWarning")); 187 java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 188 d.show(); 189 if (dialog.getValue() == org.openide.DialogDescriptor.OK_OPTION) { 190 obj.setSavingDocument(true); 191 saveDocument(); 192 } 193 } 194 } 195 } 196 200 public void saveDocument () throws java.io.IOException { 201 final javax.swing.text.StyledDocument doc = getDocument(); 202 String enc = EncodingUtil.detectEncoding(doc); 204 if (enc == null) enc = "UTF8"; 206 try { 207 new java.io.OutputStreamWriter (new java.io.ByteArrayOutputStream (1), enc); 209 super.saveDocument(); 210 getDataObject().setModified (false); 212 } catch (java.io.UnsupportedEncodingException ex) { 213 String message = java.text.MessageFormat.format(NbBundle.getMessage(XMLJ2eeEditorSupport.class,"TEXT_SAVE_AS_UTF"), 215 new Object [] {enc}); 216 NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(message); 217 Object res = DialogDisplayer.getDefault().notify(descriptor); 218 219 if (res.equals(NotifyDescriptor.YES_OPTION)) { 220 221 223 try { 224 final int MAX_PROLOG = 1000; 225 int maxPrologLen = Math.min(MAX_PROLOG, doc.getLength()); 226 final char prolog[] = doc.getText(0, maxPrologLen).toCharArray(); 227 int prologLen = 0; 229 if (prolog[0] == '<' && prolog[1] == '?' && prolog[2] == 'x') { 231 232 for (int i = 3; i<maxPrologLen; i++) { 234 if (prolog[i] == '?' && prolog[i+1] == '>') { 235 prologLen = i + 1; 236 break; 237 } 238 } 239 } 240 241 final int passPrologLen = prologLen; 242 243 Runnable edit = new Runnable () { 244 public void run() { 245 try { 246 247 doc.remove(0, passPrologLen + 1); doc.insertString(0, "<?xml version='1.0' encoding='UTF-8' ?> \n<!-- was: " + new String (prolog, 0, passPrologLen + 1) + " -->", null); 250 } catch (BadLocationException e) { 251 if (System.getProperty("netbeans.debug.exceptions") != null) e.printStackTrace(); 253 } 254 } 255 }; 256 257 NbDocument.runAtomic(doc, edit); 258 259 super.saveDocument(); 260 getDataObject().setModified (false); 262 263 } catch (BadLocationException lex) { 264 org.openide.ErrorManager.getDefault().notify(lex); 265 } 266 267 } else { return; 269 } 270 } 271 } 272 273 public UndoRedo.Manager getUndo(){ 274 return getUndoRedo(); 275 } 276 277 281 private static class XmlEnv extends DataEditorSupport.Env { 282 283 private static final long serialVersionUID = -800036748848958489L; 284 285 287 290 public XmlEnv (XMLJ2eeDataObject obj) { 291 super (obj); 292 } 293 294 297 protected FileObject getFile () { 298 return getDataObject ().getPrimaryFile (); 299 } 300 301 307 protected FileLock takeLock () throws IOException { 308 return ((XMLJ2eeDataObject) getDataObject ()).getPrimaryEntry ().takeLock (); 309 } 310 311 316 public CloneableOpenSupport findCloneableOpenSupport () { 317 return (XMLJ2eeEditorSupport) getDataObject ().getCookie (XMLJ2eeEditorSupport.class); 318 } 319 } 320 } 321 | Popular Tags |