001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.validation.ui;
043:
044: import java.io.IOException;
045: import java.util.List;
046: import javax.swing.text.StyledDocument;
047: import org.netbeans.modules.xml.validation.ShowCookie;
048: import org.netbeans.modules.xml.xam.Component;
049: import org.netbeans.modules.xml.xam.Model;
050: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
051: import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
052: import org.netbeans.modules.xml.xam.spi.Validator.ResultType;
053: import org.openide.ErrorManager;
054: import org.openide.filesystems.FileObject;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.text.CloneableEditorSupport;
058: import org.openide.text.NbDocument;
059: import org.openide.windows.IOProvider;
060: import org.openide.windows.InputOutput;
061: import org.openide.windows.OutputWriter;
062: import org.openide.util.NbBundle;
063: import org.openide.windows.OutputListener;
064:
065: /**
066: * Class to manage displaying validation information in the Output window.
067: * @author Praveen Savur
068: */
069: public class ValidationOutputWindow {
070:
071: OutputWriter normalWriter;
072: OutputWriter errorWriter;
073:
074: private String warningMsgType = null;
075:
076: private String errorMsgType = null;
077:
078: /**
079: * Creates a new instance of ValidationOutputWindow
080: */
081: public ValidationOutputWindow() {
082: initialise();
083: }
084:
085: /**
086: * Display Validation Results in the output window.
087: * @param validationInformation validation information that has to be displayed.
088: */
089: public void displayValidationInformation(
090: List<ResultItem> validationInformation) {
091:
092: for (ResultItem resultItem : validationInformation) {
093: ResultType resultType = resultItem.getType();
094:
095: Component component = resultItem.getComponents();
096:
097: try {
098: if (resultType.equals(ResultType.ERROR))
099: showError(resultItem);
100: else if (resultType.equals(ResultType.WARNING))
101: showWarning(resultItem);
102: else if (resultType.equals(ResultType.ADVICE))
103: showAdvice(resultItem);
104: } catch (IOException ex) {
105: ErrorManager.getDefault().notify(
106: ErrorManager.INFORMATIONAL, ex);
107: }
108: normalWriter.println("");
109:
110: }
111:
112: printCountSummary(validationInformation);
113: }
114:
115: /**
116: * Initialise streams.
117: */
118: private void initialise() {
119: InputOutput io = IOProvider.getDefault().getIO(
120: NbBundle.getMessage(ValidationOutputWindow.class,
121: "TITLE_XML_check_window"), false);
122:
123: normalWriter = io.getOut();
124: errorWriter = io.getErr();
125:
126: warningMsgType = NbBundle.getMessage(
127: ValidationOutputWindow.class, "MSG_WARNING");
128:
129: errorMsgType = NbBundle.getMessage(
130: ValidationOutputWindow.class, "MSG_ERROR");
131:
132: }
133:
134: /**
135: * Display Error information.
136: */
137: private void showError(ResultItem resultItem) throws IOException {
138: showMessage(errorMsgType, resultItem, true);
139: }
140:
141: /**
142: * Display warning information.
143: */
144: private void showWarning(ResultItem resultItem) throws IOException {
145: showMessage(warningMsgType, resultItem, false);
146: }
147:
148: private void showMessage(String errorTypeStr,
149: ResultItem resultItem, boolean importance)
150: throws IOException {
151: OutputListener listener = new ValidationOutputListener(
152: resultItem);
153: int lineNumber = getLineNumber(resultItem);
154: int columnNumber = getColumnNumber(resultItem);
155: if (lineNumber == -1 || columnNumber == -1) {
156: errorWriter.println(getFileName(resultItem)
157: + ": "
158: + NbBundle.getMessage(ValidationOutputWindow.class,
159: "MSG_Position_Unavailable"), listener,
160: importance);
161: } else {
162: errorWriter.println(getFileName(resultItem) + ":"
163: + lineNumber + "," + columnNumber, listener,
164: importance);
165: }
166: errorWriter.println(errorTypeStr + " "
167: + resultItem.getDescription());
168: }
169:
170: /**
171: * Display advice information.
172: */
173: private void showAdvice(ResultItem resultItem) throws IOException {
174: normalWriter.println(getFileName(resultItem) + " :"
175: + getLineNumber(resultItem) + ","
176: + getColumnNumber(resultItem));
177: normalWriter.println(resultItem.getDescription());
178: }
179:
180: /**
181: * Get filename from ResultItem.
182: */
183: private String getFileName(ResultItem resultItem) {
184: String fileName;
185:
186: assert resultItem.getModel() != null : "Model associated with ResultItem is null"; // NOI18N
187: fileName = ((FileObject) resultItem.getModel().getModelSource()
188: .getLookup().lookup(FileObject.class)).getPath();
189:
190: return fileName;
191: }
192:
193: /**
194: * Get styled document from Component.
195: */
196: private StyledDocument getStyledDocument(Component component) {
197: int position = 0;
198: DataObject dobj = null;
199:
200: try {
201: Model model = component.getModel();
202:
203: // Model can be null, if component has been deleted from model
204: // while validation was still in progress.
205: if (model == null)
206: return null;
207: dobj = DataObject.find((FileObject) model.getModelSource()
208: .getLookup().lookup(FileObject.class));
209: } catch (DataObjectNotFoundException ex) {
210: ErrorManager.getDefault().notify(ex);
211: }
212:
213: CloneableEditorSupport editor = (CloneableEditorSupport) dobj
214: .getCookie(org.openide.cookies.EditorCookie.class);
215: StyledDocument doc = editor.getDocument();
216:
217: return doc;
218: }
219:
220: /**
221: * Get line number from ResultItem.
222: */
223: private int getLineNumber(ResultItem resultItem) {
224: int lineNumber;
225:
226: if (resultItem.getComponents() != null) {
227: StyledDocument doc = getStyledDocument(resultItem
228: .getComponents());
229: // Happens if model is modified during validation.
230: if (doc == null)
231: return -1;
232: int position = getPosition(resultItem.getComponents());
233: lineNumber = NbDocument.findLineNumber(doc, position) + 1;
234: } else {
235: lineNumber = resultItem.getLineNumber();
236: }
237: return lineNumber;
238: }
239:
240: /**
241: * Get column number from ResultItem.
242: */
243: private int getColumnNumber(ResultItem resultItem) {
244: int columnNumber;
245:
246: if (resultItem.getComponents() != null) {
247: StyledDocument doc = getStyledDocument(resultItem
248: .getComponents());
249: // Happens if model is modified during validation.
250: if (doc == null)
251: return -1;
252: int position = getPosition(resultItem.getComponents());
253: columnNumber = NbDocument.findLineColumn(doc, position);
254: } else {
255: columnNumber = resultItem.getColumnNumber();
256: }
257: return columnNumber;
258: }
259:
260: /**
261: * Get Position from component.
262: */
263: private int getPosition(Component component) {
264: int position = 0;
265:
266: // TODO: Is this valid.
267: if (component instanceof DocumentComponent) {
268: position = ((DocumentComponent) component).findPosition();
269: }
270: return position;
271: }
272:
273: private void printCountSummary(
274: List<ResultItem> validationInformation) {
275: int warnings = 0;
276: int errors = 0;
277:
278: for (ResultItem resultItem : validationInformation) {
279: if (resultItem.getType().equals(ResultType.ERROR))
280: errors++;
281: else if (resultItem.getType().equals(ResultType.WARNING))
282: warnings++;
283: }
284:
285: normalWriter.println(errors + " Error(s), " + warnings
286: + " Warning(s).");
287: }
288:
289: /**
290: * Class to handle callbacks from URL's in the output window.
291: */
292: private final class ValidationOutputListener implements
293: org.openide.windows.OutputListener {
294:
295: FileObject fileObject;
296: ResultItem resultItem;
297:
298: public ValidationOutputListener(ResultItem resultItem) {
299: fileObject = (FileObject) resultItem.getModel()
300: .getModelSource().getLookup().lookup(
301: FileObject.class);
302: this .resultItem = resultItem;
303: }
304:
305: public void outputLineSelected(
306: org.openide.windows.OutputEvent ev) {
307: }
308:
309: public void outputLineAction(org.openide.windows.OutputEvent ev) {
310:
311: try {
312: DataObject dataObject = DataObject.find(fileObject);
313:
314: if (dataObject == null)
315: return;
316:
317: ShowCookie editorCookie = (ShowCookie) dataObject
318: .getCookie(ShowCookie.class);
319:
320: if (editorCookie != null)
321: editorCookie.show(resultItem);
322:
323: } catch (IOException ex) {
324: ErrorManager.getDefault().notify(
325: ErrorManager.INFORMATIONAL, ex);
326: }
327: }
328:
329: public void outputLineCleared(org.openide.windows.OutputEvent ev) {
330: }
331: }
332: }
|