KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > validation > ui > ValidationOutputWindow


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.validation.ui;
21
22 import java.io.IOException JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.StyledDocument JavaDoc;
25 import org.netbeans.modules.xml.validation.ShowCookie;
26 import org.netbeans.modules.xml.xam.Component;
27 import org.netbeans.modules.xml.xam.Model;
28 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
29 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
30 import org.netbeans.modules.xml.xam.spi.Validator.ResultType;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileObject;
33 import org.openide.loaders.DataObject;
34 import org.openide.loaders.DataObjectNotFoundException;
35 import org.openide.text.CloneableEditorSupport;
36 import org.openide.text.NbDocument;
37 import org.openide.windows.IOProvider;
38 import org.openide.windows.InputOutput;
39 import org.openide.windows.OutputWriter;
40 import org.openide.util.NbBundle;
41 import org.openide.windows.OutputListener;
42
43 /**
44  * Class to manage displaying validation information in the Output window.
45  * @author Praveen Savur
46  */

47 public class ValidationOutputWindow {
48     
49     OutputWriter normalWriter;
50     OutputWriter errorWriter;
51     
52     private String JavaDoc warningMsgType = null;
53     
54     private String JavaDoc errorMsgType = null;
55     
56     /**
57      * Creates a new instance of ValidationOutputWindow
58      */

59     public ValidationOutputWindow() {
60         initialise();
61     }
62     
63     
64     /**
65      * Display Validation Results in the output window.
66      * @param validationInformation validation information that has to be displayed.
67      */

68     public void displayValidationInformation(List JavaDoc<ResultItem> validationInformation) {
69         
70         for(ResultItem resultItem: validationInformation) {
71             ResultType resultType = resultItem.getType();
72             
73             Component component = resultItem.getComponents();
74             
75             try {
76                 if(resultType.equals(ResultType.ERROR))
77                     showError(resultItem);
78                 else if(resultType.equals(ResultType.WARNING))
79                     showWarning(resultItem);
80                 else if(resultType.equals(ResultType.ADVICE))
81                     showAdvice(resultItem);
82             } catch (IOException JavaDoc ex) {
83                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
84             }
85             normalWriter.println("");
86             
87         }
88         
89         printCountSummary(validationInformation);
90     }
91     
92     
93     
94     
95     /**
96      * Initialise streams.
97      */

98     private void initialise() {
99         InputOutput io = IOProvider.getDefault().getIO(NbBundle.
100                 getMessage(ValidationOutputWindow.class, "TITLE_XML_check_window"), false);
101         
102         normalWriter = io.getOut();
103         errorWriter = io.getErr();
104         
105         warningMsgType = NbBundle.
106                 getMessage(ValidationOutputWindow.class, "MSG_WARNING");
107         
108         errorMsgType = NbBundle.
109                 getMessage(ValidationOutputWindow.class, "MSG_ERROR");
110                 
111     }
112     
113     
114     /**
115      * Display Error information.
116      */

117     private void showError(ResultItem resultItem)
118     throws IOException JavaDoc {
119         showMessage(errorMsgType, resultItem, true);
120     }
121     
122     
123     /**
124      * Display warning information.
125      */

126     private void showWarning(ResultItem resultItem)
127     throws IOException JavaDoc {
128         showMessage(warningMsgType, resultItem, false);
129     }
130     
131     private void showMessage(String JavaDoc errorTypeStr, ResultItem resultItem, boolean importance)
132     throws IOException JavaDoc {
133         OutputListener listener = new ValidationOutputListener(resultItem);
134         int lineNumber = getLineNumber(resultItem);
135         int columnNumber = getColumnNumber(resultItem);
136         if(lineNumber == -1 || columnNumber == -1) {
137             errorWriter.println(getFileName(resultItem) + ": " + NbBundle.getMessage(ValidationOutputWindow.class,
138                     "MSG_Position_Unavailable"),
139                     listener, importance);
140         } else {
141             errorWriter.println(getFileName(resultItem) + ":" + lineNumber
142                     + "," + columnNumber,
143                     listener, importance);
144         }
145         errorWriter.println(errorTypeStr + " " + resultItem.getDescription());
146     }
147     
148     
149     
150     /**
151      * Display advice information.
152      */

153     private void showAdvice(ResultItem resultItem)
154     throws IOException JavaDoc {
155         normalWriter.println(getFileName(resultItem) + " :" +
156                 getLineNumber(resultItem) + "," + getColumnNumber(resultItem));
157         normalWriter.println(resultItem.getDescription());
158     }
159     
160     
161     /**
162      * Get filename from ResultItem.
163      */

164     private String JavaDoc getFileName(ResultItem resultItem) {
165         String JavaDoc fileName;
166         
167         assert resultItem.getModel() != null: "Model associated with ResultItem is null"; // NOI18N
168
fileName = ((FileObject) resultItem.getModel().getModelSource().
169                 getLookup().lookup(FileObject.class)).getPath();
170         
171         return fileName;
172     }
173     
174     
175     /**
176      * Get styled document from Component.
177      */

178     private StyledDocument JavaDoc getStyledDocument(Component component) {
179         int position = 0;
180         DataObject dobj = null;
181         
182         try {
183             Model model = component.getModel();
184             
185             // Model can be null, if component has been deleted from model
186
// while validation was still in progress.
187
if(model == null)
188                 return null;
189             dobj = DataObject.find((FileObject) model.
190                     getModelSource().getLookup().lookup(FileObject.class));
191         } catch (DataObjectNotFoundException ex) {
192             ErrorManager.getDefault().notify(ex);
193         }
194         
195         CloneableEditorSupport editor = (CloneableEditorSupport)dobj.
196                 getCookie(org.openide.cookies.EditorCookie.class);
197         StyledDocument JavaDoc doc = editor.getDocument();
198         
199         return doc;
200     }
201     
202     
203     /**
204      * Get line number from ResultItem.
205      */

206     private int getLineNumber(ResultItem resultItem) {
207         int lineNumber;
208         
209         if(resultItem.getComponents() != null) {
210             StyledDocument JavaDoc doc = getStyledDocument(resultItem.getComponents());
211             // Happens if model is modified during validation.
212
if(doc==null)
213                 return -1;
214             int position = getPosition(resultItem.getComponents());
215             lineNumber = NbDocument.findLineNumber(doc, position) + 1;
216         } else {
217             lineNumber = resultItem.getLineNumber();
218         }
219         return lineNumber;
220     }
221     
222     
223     /**
224      * Get column number from ResultItem.
225      */

226     private int getColumnNumber(ResultItem resultItem) {
227         int columnNumber;
228         
229         if(resultItem.getComponents() != null) {
230             StyledDocument JavaDoc doc = getStyledDocument(resultItem.getComponents());
231             // Happens if model is modified during validation.
232
if(doc==null)
233                 return -1;
234             int position = getPosition(resultItem.getComponents());
235             columnNumber = NbDocument.findLineColumn(doc, position);
236         } else {
237             columnNumber = resultItem.getColumnNumber();
238         }
239         return columnNumber;
240     }
241     
242     
243     /**
244      * Get Position from component.
245      */

246     private int getPosition(Component component) {
247         int position = 0;
248         
249         // TODO: Is this valid.
250
if(component instanceof DocumentComponent) {
251             position = ((DocumentComponent)component).findPosition();
252         }
253         return position;
254     }
255     
256     
257     private void printCountSummary(List JavaDoc<ResultItem> validationInformation) {
258         int warnings = 0;
259         int errors = 0;
260         
261         for(ResultItem resultItem: validationInformation) {
262             if(resultItem.getType().equals(ResultType.ERROR))
263                 errors ++;
264             else if(resultItem.getType().equals(ResultType.WARNING))
265                 warnings++;
266         }
267         
268         normalWriter.println(errors + " Error(s), " + warnings +" Warning(s).");
269     }
270     
271     
272     
273     /**
274      * Class to handle callbacks from URL's in the output window.
275      */

276     private final class ValidationOutputListener
277             implements org.openide.windows.OutputListener {
278         
279         FileObject fileObject;
280         ResultItem resultItem;
281         
282         public ValidationOutputListener(ResultItem resultItem) {
283             fileObject = (FileObject) resultItem.getModel().
284                     getModelSource().getLookup().lookup(FileObject.class);
285             this.resultItem = resultItem;
286         }
287         
288         public void outputLineSelected(org.openide.windows.OutputEvent ev) {
289         }
290         
291         public void outputLineAction(org.openide.windows.OutputEvent ev) {
292             
293             try {
294                 DataObject dataObject = DataObject.find(fileObject);
295                 
296                 if(dataObject == null)
297                     return;
298                 
299                 ShowCookie editorCookie = (ShowCookie)
300                 dataObject.getCookie(ShowCookie.class);
301                 
302                 if(editorCookie != null)
303                     editorCookie.show(resultItem);
304                 
305             } catch (IOException JavaDoc ex){
306                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
307             }
308         }
309         
310         public void outputLineCleared(org.openide.windows.OutputEvent ev) {
311         }
312     }
313 }
314
Popular Tags