KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > WsdlEditorSupport


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.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 /** Support for editing a WsdlDataObject as text.
41  *
42  * @author mkuchtiak
43  */

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     /** Create a new editor support.
52      * @param obj the data object whose primary file will be edited as text
53      */

54     public WsdlEditorSupport(WsdlDataObject obj) {
55         super (obj, new XmlEnv (obj));
56         dataObject=obj;
57         setMIMEType ("text/xml"); // NOI18N
58
}
59
60     /**
61      * Overridden method from CloneableEditorSupport.
62      */

63     protected void saveFromKitToStream (StyledDocument doc, EditorKit kit,
64                                             OutputStream stream)
65                     throws IOException, BadLocationException {
66         // kit and kit() are not accessible so we pretend
67
// to create the kit; actually this should just return kit.
68
EditorKit k = this.createEditorKit();
69         OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF8"); // NOI18N
70
Writer writer = new BufferedWriter(osw);
71         k.write(writer, doc, 0, doc.getLength());
72         writer.close();
73     }
74
75     /**
76      * Overridden method from CloneableEditorSupport.
77      */

78     protected void loadFromStreamToKit (StyledDocument doc, InputStream stream,
79                                             EditorKit kit)
80                     throws IOException, BadLocationException {
81         // kit and kit() are not accessible so we pretend
82
// to create the kit; actually this should just return kit.
83
EditorKit k = this.createEditorKit();
84         InputStreamReader isr = new InputStreamReader(stream, "UTF8"); // NOI18N
85
Reader reader = new BufferedReader(isr);
86         k.read(reader, doc, 0);
87         reader.close();
88     }
89
90     /** Called when the document is modified.
91      * Here, adding a save cookie to the object and marking it modified.
92      * @return true if the modification is acceptable
93      */

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     /** Called when the document becomes unmodified.
107      * Here, removing the save cookie from the object and marking it unmodified.
108      */

109     protected void notifyUnmodified () {
110         super.notifyUnmodified ();
111         WsdlDataObject obj = (WsdlDataObject) getDataObject ();
112         obj.removeSaveCookie();
113     }
114
115     /** A save cookie to use for the editor support.
116      * When saved, saves the document to disk and marks the object unmodified.
117      */

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     /*
128      * Save document using encoding declared in XML prolog if possible otherwise
129      * at UTF-8 (in such case it updates the prolog).
130      */

131     public void saveDocument () throws java.io.IOException JavaDoc {
132         final javax.swing.text.StyledDocument JavaDoc doc = getDocument();
133         // dependency on xml/core
134
String JavaDoc enc = EncodingUtil.detectEncoding(doc);
135         if (enc == null) enc = "UTF8"; //!!! // NOI18N
136

137         try {
138             //test encoding on dummy stream
139
new java.io.OutputStreamWriter JavaDoc(new java.io.ByteArrayOutputStream JavaDoc(1), enc);
140             super.saveDocument();
141             //moved from Env.save()
142
getDataObject().setModified (false);
143         } catch (java.io.UnsupportedEncodingException JavaDoc ex) {
144             // ask user what next?
145
String JavaDoc message = java.text.MessageFormat.format(NbBundle.getMessage(WsdlEditorSupport.class,"TEXT_SAVE_AS_UTF"),
146                                                             new Object JavaDoc[] {enc});
147             NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(message);
148             Object JavaDoc res = DialogDisplayer.getDefault().notify(descriptor);
149
150             if (res.equals(NotifyDescriptor.YES_OPTION)) {
151
152                 // update prolog to new valid encoding
153

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; // actual prolog length
159

160                     //parse prolog and get prolog end
161
if (prolog[0] == '<' && prolog[1] == '?' && prolog[2] == 'x') {
162
163                         // look for delimitting ?>
164
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 JavaDoc edit = new Runnable JavaDoc() {
175                          public void run() {
176                              try {
177
178                                 doc.remove(0, passPrologLen + 1); // +1 it removes exclusive
179
doc.insertString(0, "<?xml version='1.0' encoding='UTF-8' ?> \n<!-- was: " + new String JavaDoc(prolog, 0, passPrologLen + 1) + " -->", null); // NOI18N
180

181                              } catch (BadLocationException e) {
182                                  if (System.getProperty("netbeans.debug.exceptions") != null) // NOI18N
183
e.printStackTrace();
184                              }
185                          }
186                     };
187
188                     NbDocument.runAtomic(doc, edit);
189
190                     super.saveDocument();
191                     //moved from Env.save()
192
getDataObject().setModified (false);
193
194                 } catch (BadLocationException lex) {
195                     org.openide.ErrorManager.getDefault().notify(lex);
196                 }
197
198             } else { // NotifyDescriptor != YES_OPTION
199
return;
200             }
201         }
202     }
203
204     public UndoRedo.Manager getUndo(){
205         return getUndoRedo();
206     }
207
208     /** A description of the binding between the editor support and the object.
209      * Note this may be serialized as part of the window system and so
210      * should be static, and use the transient modifier where needed.
211      */

212     private static class XmlEnv extends DataEditorSupport.Env {
213
214         private static final long serialVersionUID = -800036748848958489L;
215
216         //private static final long serialVersionUID = ...L;
217

218         /** Create a new environment based on the data object.
219          * @param obj the data object to edit
220          */

221         public XmlEnv (WsdlDataObject obj) {
222             super (obj);
223         }
224
225         /** Get the file to edit.
226          * @return the primary file normally
227          */

228         protected FileObject getFile () {
229             return getDataObject ().getPrimaryFile ();
230         }
231
232         /** Lock the file to edit.
233          * Should be taken from the file entry if possible, helpful during
234          * e.g. deletion of the file.
235          * @return a lock on the primary file normally
236          * @throws IOException if the lock could not be taken
237          */

238         protected FileLock takeLock () throws IOException {
239             return ((WsdlDataObject) getDataObject ()).getPrimaryEntry ().takeLock ();
240         }
241
242         /** Find the editor support this environment represents.
243          * Note that we have to look it up, as keeping a direct
244          * reference would not permit this environment to be serialized.
245          * @return the editor support
246          */

247         public CloneableOpenSupport findCloneableOpenSupport () {
248             return (WsdlEditorSupport) getDataObject ().getCookie (WsdlEditorSupport.class);
249         }
250     }
251 }
252
Popular Tags