001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.model.api.support;
020:
021: import javax.swing.text.StyledDocument;
022: import org.openide.cookies.LineCookie;
023: import org.openide.filesystems.FileObject;
024: import org.openide.loaders.DataObject;
025: import org.openide.loaders.DataObjectNotFoundException;
026: import org.openide.text.Line;
027: import org.openide.text.NbDocument;
028: import org.openide.util.Lookup;
029: import org.netbeans.modules.xml.xam.Model;
030: import org.netbeans.modules.xml.xam.ModelSource;
031: import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
032: import org.netbeans.modules.xml.xam.Component;
033: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
034:
035: /**
036: * @author Vladimir Yaroslavskiy
037: * @version 2007.12.10
038: */
039: public class Util {
040:
041: private Util() {
042: }
043:
044: public static Line getLine(ResultItem item) {
045: int number;
046: Component component = item.getComponents();
047:
048: if (component != null) {
049: number = -1;
050:
051: if (component instanceof DocumentComponent) {
052: number = getLineNumber((DocumentComponent) item
053: .getComponents());
054: }
055: } else {
056: number = item.getLineNumber() - 1;
057: }
058: //System.out.println(" number: " + number);
059:
060: if (number < 1) {
061: return null;
062: }
063: FileObject file = getFileObjectByModel(component == null ? item
064: .getModel() : component.getModel());
065:
066: if (file == null) {
067: return null;
068: }
069: LineCookie cookie = null;
070:
071: try {
072: DataObject data = DataObject.find(file);
073: cookie = (LineCookie) data.getCookie(LineCookie.class);
074: } catch (DataObjectNotFoundException e) {
075: e.printStackTrace();
076: }
077: if (cookie == null) {
078: return null;
079: }
080: return cookie.getLineSet().getCurrent(number);
081: }
082:
083: private static int getLineNumber(DocumentComponent entity) {
084: if (entity == null) {
085: return -1;
086: }
087: Model model = entity.getModel();
088:
089: if (model == null) {
090: return -1;
091: }
092: ModelSource source = model.getModelSource();
093:
094: if (source == null) {
095: return -1;
096: }
097: Lookup lookup = source.getLookup();
098:
099: if (lookup == null) {
100: return -1;
101: }
102: StyledDocument document = (StyledDocument) lookup
103: .lookup(StyledDocument.class);
104:
105: if (document == null) {
106: return -1;
107: }
108: return NbDocument.findLineNumber(document, entity
109: .findPosition());
110: }
111:
112: private static FileObject getFileObjectByModel(Model model) {
113: if (model == null) {
114: return null;
115: }
116: ModelSource src = model.getModelSource();
117:
118: if (src == null) {
119: return null;
120: }
121: Lookup lookup = src.getLookup();
122:
123: if (lookup == null) {
124: return null;
125: }
126: return lookup.lookup(FileObject.class);
127: }
128: }
|