KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > actions > XMLDisplayer


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 package org.netbeans.modules.css.actions;
20
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.*;
24 import java.net.*;
25
26 import org.openide.loaders.DataObject;
27 import org.openide.loaders.DataObjectNotFoundException;
28 import org.openide.text.Line;
29 import org.openide.*;
30 import org.openide.nodes.*;
31 import org.openide.cookies.*;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.URLMapper;
34 import org.openide.windows.*;
35
36 import org.xml.sax.SAXParseException JavaDoc;
37
38
39 /**
40  * Handles output window for XML parser, see display.
41  *
42  * @author Petr Kuzel
43  * @version 1.0
44  */

45 public class XMLDisplayer {
46
47     //0 extends message, 1 line number, 2 url of external entity
48
private final String JavaDoc FORMAT = "{0} [{1}] {2}"; // NOI18N
49

50     /** output tab */
51     private InputOutput xmlIO;
52
53     /** writer to that tab */
54     private OutputWriter ow = null;
55
56     
57     /** Creates new XMLDisplayer */
58     public XMLDisplayer() {
59         this(Util.THIS.getString("TITLE_XML_check_window"));
60     }
61
62     protected XMLDisplayer(String JavaDoc tab) {
63         initInputOutput(tab);
64     }
65
66     /**
67      * Display plain message in output window.
68      */

69     public void display(String JavaDoc msg) {
70         ow.println(msg);
71     }
72     
73     /**
74      * Displayed message may also take focus the window. Sutable for the last message.
75      */

76     public void display(String JavaDoc msg, boolean takeFocus) {
77         if (takeFocus) {
78             boolean wasFocusTaken = xmlIO.isFocusTaken();
79             xmlIO.select();
80             xmlIO.setFocusTaken(true);
81             ow.println(msg);
82             xmlIO.setFocusTaken(wasFocusTaken);
83         } else {
84             ow.println(msg);
85         }
86     }
87
88     /**
89      * Try to move InputOutput to front. Suitable for last message.
90      */

91     public final void moveToFront() {
92         boolean wasFocusTaken = xmlIO.isFocusTaken();
93         xmlIO.select();
94         xmlIO.setFocusTaken(true);
95         ow.write("\r");
96         xmlIO.setFocusTaken(wasFocusTaken);
97     }
98     
99     /** Show using SAX parser error format */
100     public void display(DataObject dobj, SAXParseException JavaDoc sex) {
101         
102         // resolve actual data object that caused exception
103
// it may differ from XML document for external entities
104

105         DataObject actualDataObject = null;
106         try {
107             FileObject[] fos = URLMapper.findFileObjects(new URL(sex.getSystemId()));
108             if (fos.length > 0) {
109                 actualDataObject = DataObject.find(fos[0]);
110             }
111         } catch (MalformedURLException ex) {
112             // we test for null
113
} catch (DataObjectNotFoundException ex) {
114             // we test for null
115
}
116
117         // external should contain systemID for unresolned external entities
118

119         String JavaDoc external = ""; // NOI18N
120

121         if (actualDataObject == null) {
122             external = sex.getSystemId();
123         }
124         
125         
126         display (
127             actualDataObject, sex.getMessage(), external,
128             new Integer JavaDoc( sex.getLineNumber() ),
129             new Integer JavaDoc( sex.getColumnNumber() )
130         );
131     }
132
133
134     /** Show it in output tab formatted and with attached controller. */
135     protected void display(DataObject dobj, String JavaDoc message, String JavaDoc ext, Integer JavaDoc line, Integer JavaDoc col) {
136
137         
138         Object JavaDoc[] args = new Object JavaDoc[] {
139                             message,
140                             line,
141                             ext
142                         };
143
144         String JavaDoc text = MessageFormat.format(FORMAT, args);
145
146         try {
147             if (dobj == null) throw new IOException JavaDoc("catchIt"); // NOI18N
148
IOCtl ec = new IOCtl (
149                            dobj,
150                            Math.max(line.intValue() - 1, 0),
151                            Math.max(col.intValue() - 1, 0)
152                        );
153             ow.println(text, ec);
154         } catch (IOException JavaDoc catchIt) {
155             ow.println(text); // print without controller
156
}
157
158     }
159
160     /** Set output writer used by this displayer.
161     * Share existing, clear content on reuse.
162     */

163     private void initInputOutput (String JavaDoc name) {
164         if (ow != null) return;
165         xmlIO = IOProvider.getDefault().getIO(name, false);
166         xmlIO.setFocusTaken (false);
167         ow = xmlIO.getOut();
168         try {
169             ow.reset();
170         } catch (java.io.IOException JavaDoc ex) {
171             //bad luck
172
}
173     }
174
175     final class IOCtl implements OutputListener {
176         /** line we check */
177         Line xline;
178
179         /** column with the err */
180         int column;
181
182         /**
183         * @param fo is a FileObject with an error
184         * @param line is a line with the error
185         * @param column is a column with the error
186         * @param text text to display to status line
187         * @exception FileNotFoundException
188         */

189         public IOCtl (DataObject data, int line, int column)
190         throws java.io.IOException JavaDoc {
191             this.column = column;
192             LineCookie cookie = (LineCookie)data.getCookie(LineCookie.class);
193             if (cookie == null) {
194                 throw new java.io.FileNotFoundException JavaDoc ();
195             }
196             xline = cookie.getLineSet ().getOriginal (line);
197         }
198
199         public void outputLineSelected (OutputEvent ev) {
200             try {
201                 xline.markError();
202                 xline.show(Line.SHOW_TRY_SHOW, column);
203             } catch (IndexOutOfBoundsException JavaDoc ex) {
204             } catch (ClassCastException JavaDoc ex) {
205                 // This is hack because of CloneableEditorSupport error -- see CloneableEditorSupport:1193
206
}
207         }
208
209         public void outputLineAction (OutputEvent ev) {
210             try {
211                 xline.markError();
212                 xline.show(Line.SHOW_GOTO, column);
213             } catch (IndexOutOfBoundsException JavaDoc ex) {
214             } catch (ClassCastException JavaDoc ex) {
215                 // This is hack because of CloneableEditorSupport error -- see CloneableEditorSupport:1193
216
}
217         }
218
219         public void outputLineCleared (OutputEvent ev) {
220             try {
221                 xline.unmarkError();
222             } catch (IndexOutOfBoundsException JavaDoc ex) {
223             }
224         }
225     }
226
227 }
228
Popular Tags