KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > validator > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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

47 public class ValidationOutputWindow {
48     
49     OutputWriter normalWriter;
50     OutputWriter errorWriter;
51     
52     /**
53      * Creates a new instance of ValidationOutputWindow
54      */

55     public ValidationOutputWindow() {
56         initialize();
57     }
58     
59     
60     /**
61      * Display Validation Results in the output window.
62      * @param validationInformation validation information that has to be displayed.
63      */

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

94     private void initialize() {
95         InputOutput io = IOProvider.getDefault().getIO(NbBundle.
96                 getMessage(ValidationOutputWindow.class, "TITLE_XML_check_window"), false);
97         
98         normalWriter = io.getOut();
99         errorWriter = io.getErr();
100     }
101     
102     
103     /**
104      * Display Error information.
105      */

106     private void showError(ResultItem resultItem)
107     throws IOException JavaDoc {
108         
109         OutputListener listener = new ValidationOutputListener(resultItem);
110         errorWriter.println(getFileName(resultItem) + ":" +
111                 getLineNumber(resultItem) + "," + getColumnNumber(resultItem),
112                 listener, true);
113         errorWriter.println(resultItem.getDescription());
114     }
115     
116     
117     /**
118      * Display warning information.
119      */

120     private void showWarning(ResultItem resultItem)
121     throws IOException JavaDoc {
122         OutputListener listener = new ValidationOutputListener(resultItem);
123         errorWriter.println(getFileName(resultItem) + ":" +
124                 getLineNumber(resultItem) + "," + getColumnNumber(resultItem),
125                 listener, false);
126         errorWriter.println(resultItem.getDescription());
127     }
128     
129     
130     /**
131      * Display advice information.
132      */

133     private void showAdvice(ResultItem resultItem)
134     throws IOException JavaDoc {
135         normalWriter.println(getFileName(resultItem) + " :" +
136                 getLineNumber(resultItem) + "," + getColumnNumber(resultItem));
137         normalWriter.println(resultItem.getDescription());
138     }
139     
140     
141     /**
142      * Get filename from ResultItem.
143      */

144     private String JavaDoc getFileName(ResultItem resultItem) {
145         String JavaDoc fileName;
146         
147         assert resultItem.getModel() != null: "Model associated with ResultItem is null"; // NOI18N
148
fileName = ((FileObject) resultItem.getModel().getModelSource().
149                 getLookup().lookup(FileObject.class)).getPath();
150         
151         return fileName;
152     }
153     
154     
155     /**
156      * Get styled document from Component.
157      */

158     private StyledDocument JavaDoc getStyledDocument(Component component) {
159         int position = 0;
160         DataObject dobj = null;
161         
162         try {
163             dobj = DataObject.find((FileObject) component.getModel().
164                     getModelSource().getLookup().lookup(FileObject.class));
165         } catch (DataObjectNotFoundException ex) {
166             ErrorManager.getDefault().notify(ex);
167         }
168         
169         CloneableEditorSupport editor = (CloneableEditorSupport)dobj.
170                 getCookie(org.openide.cookies.EditorCookie.class);
171         StyledDocument JavaDoc doc = editor.getDocument();
172         
173         return doc;
174     }
175     
176     
177     /**
178      * Get line number from ResultItem.
179      */

180     private int getLineNumber(ResultItem resultItem) {
181         int lineNumber;
182         
183         if(resultItem.getComponents() != null) {
184             StyledDocument JavaDoc doc = getStyledDocument(resultItem.getComponents());
185             int position = getPosition(resultItem.getComponents());
186             lineNumber = NbDocument.findLineNumber(doc, position) + 1;
187         } else {
188             lineNumber = resultItem.getLineNumber();
189         }
190         return lineNumber;
191     }
192     
193     
194     /**
195      * Get column number from ResultItem.
196      */

197     private int getColumnNumber(ResultItem resultItem) {
198         int columnNumber;
199         
200         if(resultItem.getComponents() != null) {
201             StyledDocument JavaDoc doc = getStyledDocument(resultItem.getComponents());
202             int position = getPosition(resultItem.getComponents());
203             columnNumber = NbDocument.findLineColumn(doc, position);
204         } else {
205             columnNumber = resultItem.getColumnNumber();
206         }
207         return columnNumber;
208     }
209     
210     
211     /**
212      * Get Position from component.
213      */

214     private int getPosition(Component component) {
215         int position = 0;
216         
217         // TODO: Is this valid.
218
if(component instanceof DocumentComponent) {
219             position = ((DocumentComponent)component).findPosition();
220         }
221         return position;
222     }
223     
224     
225     private void printCountSummary(List JavaDoc<ResultItem> validationInformation) {
226         int warnings = 0;
227         int errors = 0;
228         
229         for(ResultItem resultItem: validationInformation) {
230             if(resultItem.getType().equals(ResultType.ERROR))
231                 errors ++;
232             else if(resultItem.getType().equals(ResultType.WARNING))
233                 warnings++;
234         }
235         
236         normalWriter.println(errors + " Error(s), " + warnings +" Warning(s).");
237     }
238     
239     
240     
241     /**
242      * Class to handle callbacks from URL's in the output window.
243      */

244     private final class ValidationOutputListener
245             implements org.openide.windows.OutputListener {
246         
247         FileObject fileObject;
248         ResultItem resultItem;
249         
250         public ValidationOutputListener(ResultItem resultItem) {
251             fileObject = (FileObject) resultItem.getModel().
252                     getModelSource().getLookup().lookup(FileObject.class);
253             this.resultItem = resultItem;
254         }
255         
256         public void outputLineSelected(org.openide.windows.OutputEvent ev) {
257         }
258         
259         public void outputLineAction(org.openide.windows.OutputEvent ev) {
260             
261             try {
262                 DataObject dataObject = DataObject.find(fileObject);
263                 
264                 if(dataObject == null)
265                     return;
266                 
267                 CloneableEditorSupport editor = (CloneableEditorSupport)dataObject.
268                         getCookie(org.openide.cookies.EditorCookie.class);
269                 JEditorPane JavaDoc[] panes = editor.getOpenedPanes();
270                 boolean focusFound = false;
271                 if(panes.length > 0){
272                     for(int i = 0; i < panes.length; i++){
273                         JEditorPane JavaDoc pane = panes[i];
274                         if(pane.hasFocus()){
275                             setCaretPosition(pane);
276                             focusFound = true;
277                             break;
278                         }
279                     }
280                     if(!focusFound){
281                         JEditorPane JavaDoc pane = panes[0];
282                         pane.requestFocusInWindow();
283                         setCaretPosition(pane);
284                     }
285                 }
286             } catch (IOException JavaDoc ex){
287                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
288             }
289             
290         }
291         
292         private void setCaretPosition(JEditorPane JavaDoc pane){
293             Component component = resultItem.getComponents();
294             if(component instanceof WSDLComponent){
295                 int position = ((WSDLComponent)component).findPosition();
296                 pane.setCaretPosition(position);
297             }else {
298                 int line = resultItem.getLineNumber();
299                 try {
300                     int position = NbDocument.findLineOffset(
301                             (StyledDocument JavaDoc)pane.getDocument(),line);
302                     pane.setCaretPosition(position);
303                 } catch (IndexOutOfBoundsException JavaDoc iob) {
304                     // ignore
305
}
306             }
307         }
308         
309         public void outputLineCleared(org.openide.windows.OutputEvent ev) {
310         }
311     }
312 }
Popular Tags