PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package sidekick.css;
  21. // Imports
  22. import sidekick.*;
  23. import sidekick.util.*;
  24. import javax.swing.tree.*;
  25. import java.util.*;
  26. import sidekick.css.parser.CSSNode;
  27. /**
  28. * Stores a buffer structure tree.
  29. *
  30. * Plugins can extend this class to persist plugin-specific information.
  31. * For example, the XML plugin stores code completion-related structures using
  32. * a subclass.
  33. */
  34. public class CSSParsedData extends SideKickParsedData {
  35. /**
  36. * @param fileName The file name being parsed, used as the root of the
  37. * tree.
  38. */
  39. public CSSParsedData( String fileName ) {
  40. super( fileName );
  41. }
  42. public TreePath getTreePathForPosition(int dot)
  43. {
  44. if(root.getChildCount() == 0) {
  45. return null;
  46. }
  47. Object userObject = ( ( DefaultMutableTreeNode ) root ).getUserObject();
  48. if ( userObject == null) {
  49. return null;
  50. }
  51. if ( !( userObject instanceof SideKickAsset ) ) {
  52. return null;
  53. }
  54. SideKickAsset asset = (SideKickAsset)userObject;
  55. CSSNode css_node = (CSSNode)asset.getElement();
  56. DefaultMutableTreeNode node = root;
  57. if ( nodeContains(css_node, dot) ) {
  58. node = getLeafNode(root, dot);
  59. }
  60. List<TreeNode> nodeList = new ArrayList<TreeNode>();
  61. while (node != null)
  62. {
  63. nodeList.add(node);
  64. node = (DefaultMutableTreeNode)node.getParent();
  65. }
  66. Collections.reverse(nodeList);
  67. return new TreePath(nodeList.toArray());
  68. }
  69. private DefaultMutableTreeNode getLeafNode(DefaultMutableTreeNode node, int dot) {
  70. Enumeration en = node.children();
  71. while ( en.hasMoreElements() ) {
  72. DefaultMutableTreeNode child = (DefaultMutableTreeNode)en.nextElement();
  73. SideKickAsset asset = (SideKickAsset)child.getUserObject();
  74. CSSNode css_node = (CSSNode)asset.getElement();
  75. if (nodeContains(css_node, dot)) {
  76. return getLeafNode(child, dot);
  77. }
  78. }
  79. return node;
  80. }
  81. private boolean nodeContains(CSSNode node, int dot) {
  82. int start = node.getStartPosition().getOffset();
  83. int end = node.getEndPosition().getOffset();
  84. return start <= dot && dot <= end ? true : false;
  85. }
  86. // overriden to search CSSNodes rather than TreeNodes. Not all tree nodes
  87. // may be showing, need the deepest asset at the cursor position for code
  88. // completion.
  89. public IAsset getAssetAtOffset( int pos ) {
  90. if ( pos < 0 ) {
  91. return null;
  92. }
  93. Object userObject = ( ( DefaultMutableTreeNode ) root ).getUserObject();
  94. if ( userObject == null) {
  95. return null;
  96. }
  97. if ( !( userObject instanceof SideKickAsset ) ) {
  98. return null;
  99. }
  100. SideKickAsset returnable = (SideKickAsset) userObject;
  101. CSSNode rootNode = (CSSNode)returnable.getElement();
  102. if ( !rootNode.hasChildren() ) {
  103. return returnable;
  104. }
  105. for ( CSSNode child : rootNode.getChildren() ) {
  106. if ( pos >= child.getStartPosition().getOffset() && pos <= child.getEndPosition().getOffset() ) {
  107. CSSNode node = getCSSNodeAtOffset( child, pos );
  108. SideKickAsset asset = new SideKickAsset(node);
  109. asset.setStart(node.getStartPosition());
  110. asset.setEnd(node.getEndPosition());
  111. return asset;
  112. }
  113. }
  114. return returnable;
  115. }
  116. private CSSNode getCSSNodeAtOffset( CSSNode tn, int pos ) {
  117. for ( CSSNode child : tn.getChildren() ) {
  118. try {
  119. if ( pos >= child.getStartPosition().getOffset() && pos <= child.getEndPosition().getOffset() ) {
  120. return getCSSNodeAtOffset( child, pos );
  121. }
  122. }
  123. catch ( NullPointerException e ) {
  124. // I was getting an NPE here...
  125. //e.printStackTrace();
  126. }
  127. }
  128. return tn;
  129. }
  130. }