PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/css/CSSParsedData.java

#
Java | 145 lines | 94 code | 15 blank | 36 comment | 22 complexity | ae5a7964062fa86d3f403351f48b4268 MD5 | raw file
  1/*
  2* SideKickParsedData.java
  3*
  4* Copyright (C) 2003, 2004 Slava Pestov
  5*
  6* This program is free software; you can redistribute it and/or
  7* modify it under the terms of the GNU General Public License
  8* as published by the Free Software Foundation; either version 2
  9* of the License, or any later version.
 10*
 11* This program is distributed in the hope that it will be useful,
 12* but WITHOUT ANY WARRANTY; without even the implied warranty of
 13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14* GNU General Public License for more details.
 15*
 16* You should have received a copy of the GNU General Public License
 17* along with this program; if not, write to the Free Software
 18* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19*/
 20
 21package sidekick.css;
 22
 23// Imports
 24import sidekick.*;
 25import sidekick.util.*;
 26import javax.swing.tree.*;
 27import java.util.*;
 28
 29import sidekick.css.parser.CSSNode;
 30
 31
 32/**
 33 * Stores a buffer structure tree.
 34 *
 35 * Plugins can extend this class to persist plugin-specific information.
 36 * For example, the XML plugin stores code completion-related structures using
 37 * a subclass.
 38 */
 39public class CSSParsedData extends SideKickParsedData {
 40    /**
 41     * @param fileName The file name being parsed, used as the root of the
 42     * tree.
 43     */
 44    public CSSParsedData( String fileName ) {
 45        super( fileName );
 46    }
 47
 48    public TreePath getTreePathForPosition(int dot)
 49    {
 50        if(root.getChildCount() == 0) {
 51            return null;
 52        }
 53
 54        Object userObject = ( ( DefaultMutableTreeNode ) root ).getUserObject();
 55        if ( userObject == null) {
 56            return null;
 57        }
 58        if ( !( userObject instanceof SideKickAsset ) ) {
 59            return null;
 60        }
 61        SideKickAsset asset = (SideKickAsset)userObject;
 62        CSSNode css_node = (CSSNode)asset.getElement();
 63        DefaultMutableTreeNode node = root;
 64        if ( nodeContains(css_node, dot) ) {
 65            node = getLeafNode(root, dot);
 66        }
 67        List<TreeNode> nodeList = new ArrayList<TreeNode>();
 68        while (node != null)
 69        {
 70            nodeList.add(node);
 71            node = (DefaultMutableTreeNode)node.getParent();
 72        }
 73        Collections.reverse(nodeList);
 74        return new TreePath(nodeList.toArray());
 75
 76    }
 77
 78    private DefaultMutableTreeNode getLeafNode(DefaultMutableTreeNode node, int dot) {
 79        Enumeration en = node.children();
 80        while ( en.hasMoreElements() ) {
 81            DefaultMutableTreeNode child = (DefaultMutableTreeNode)en.nextElement();
 82            SideKickAsset asset = (SideKickAsset)child.getUserObject();
 83            CSSNode css_node = (CSSNode)asset.getElement();
 84            if (nodeContains(css_node, dot)) {
 85                return getLeafNode(child, dot);
 86            }
 87        }
 88        return node;
 89    }
 90
 91    private boolean nodeContains(CSSNode node, int dot) {
 92        int start = node.getStartPosition().getOffset();
 93        int end = node.getEndPosition().getOffset();
 94        return start <= dot && dot <= end ? true : false;
 95    }
 96
 97    // overriden to search CSSNodes rather than TreeNodes.  Not all tree nodes
 98    // may be showing, need the deepest asset at the cursor position for code
 99    // completion.
100    public IAsset getAssetAtOffset( int pos ) {
101        if ( pos < 0 ) {
102            return null;
103        }
104        Object userObject = ( ( DefaultMutableTreeNode ) root ).getUserObject();
105        if ( userObject == null) {
106            return null;
107        }
108        if ( !( userObject instanceof SideKickAsset ) ) {
109            return null;
110        }
111        SideKickAsset returnable = (SideKickAsset) userObject;
112        CSSNode rootNode = (CSSNode)returnable.getElement();
113        if ( !rootNode.hasChildren() ) {
114            return returnable;
115        }
116
117        for ( CSSNode child : rootNode.getChildren() ) {
118            if ( pos >= child.getStartPosition().getOffset() && pos <= child.getEndPosition().getOffset() ) {
119                CSSNode node = getCSSNodeAtOffset( child, pos );
120                SideKickAsset asset = new SideKickAsset(node);
121                asset.setStart(node.getStartPosition());
122                asset.setEnd(node.getEndPosition());
123                return asset;
124
125            }
126        }
127        return returnable;
128    }
129
130
131    private CSSNode getCSSNodeAtOffset( CSSNode tn, int pos ) {
132        for ( CSSNode child : tn.getChildren() ) {
133            try {
134                if ( pos >= child.getStartPosition().getOffset() && pos <= child.getEndPosition().getOffset() ) {
135                    return getCSSNodeAtOffset( child, pos );
136                }
137            }
138            catch ( NullPointerException e ) {
139                // I was getting an NPE here...
140                //e.printStackTrace();
141            }
142        }
143        return tn;
144    }
145}