/plugins/SideKick/tags/release-1-0/sidekick/util/ElementUtil.java

# · Java · 154 lines · 73 code · 12 blank · 69 comment · 6 complexity · eda53f7a7824554bed21b5ebaeda3925 MD5 · raw file

  1. /*
  2. Copyright (c) 2006, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the <ORGANIZATION> nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package sidekick.util;
  26. import java.util.Enumeration;
  27. import javax.swing.SwingUtilities;
  28. import javax.swing.text.Position;
  29. import javax.swing.tree.DefaultMutableTreeNode;
  30. import org.gjt.sp.jedit.Buffer;
  31. public class ElementUtil {
  32. /**
  33. * Convert the start of a Location to a Position.
  34. * Need to create Positions for each node. The javacc parser finds line and
  35. * column location, need to convert this to a Position in the buffer. The
  36. * SideKickElement contains a column offset based on the current tab size as
  37. * set in the Buffer, need to use getOffsetOfVirtualColumn to account for
  38. * soft and hard tab handling.
  39. *
  40. * Note that this method will also set the start position in the given SideKickElement.
  41. *
  42. * @param buffer the buffer containing the child element/text in question
  43. * @param element the SideKickElement representing some text in the buffer
  44. * @return a Position representing the offset from the start of the buffer
  45. * to the start of the element
  46. */
  47. public static Position createStartPosition( Buffer buffer, SideKickElement element ) {
  48. int line_offset = buffer.getLineStartOffset(
  49. Math.max( element.getStartLocation().line - 1, 0 ) );
  50. int[] totalVirtualWidth = new int[ 1 ];
  51. int column_offset = buffer.getOffsetOfVirtualColumn(
  52. Math.max( element.getStartLocation().line - 1, 0 ),
  53. Math.max( element.getStartLocation().column - 1, 0 ),
  54. totalVirtualWidth );
  55. if ( column_offset == -1 ) {
  56. column_offset = totalVirtualWidth[ 0 ];
  57. }
  58. Position p = createPosition( line_offset, column_offset );
  59. element.setStartPosition( p );
  60. return p;
  61. }
  62. /**
  63. * Convert the end of a Location to a Position.
  64. * Need to create Positions for each node. The javacc parser finds line and
  65. * column location, need to convert this to a Position in the buffer. The
  66. * SideKickElement contains a column offset based on the current tab size as
  67. * set in the Buffer, need to use getOffsetOfVirtualColumn to account for
  68. * soft and hard tab handling.
  69. *
  70. * Note that this method will also set the end position in the given SideKickElement.
  71. *
  72. * @param buffer the buffer containing the child element/text in question
  73. * @param element the SideKickElement representing some text in the buffer
  74. * @return a Position representing the offset from the start of the buffer
  75. * to the end of the element
  76. */
  77. public static Position createEndPosition( Buffer buffer, SideKickElement element ) {
  78. int line_offset = buffer.getLineStartOffset(
  79. Math.max( element.getEndLocation().line - 1, 0 ) );
  80. int[] totalVirtualWidth = new int[ 1 ];
  81. int column_offset = buffer.getOffsetOfVirtualColumn(
  82. Math.max( element.getEndLocation().line - 1, 0 ),
  83. Math.max( element.getEndLocation().column - 1, 0 ),
  84. totalVirtualWidth );
  85. if ( column_offset == -1 ) {
  86. column_offset = totalVirtualWidth[ 0 ];
  87. }
  88. Position p = createPosition( line_offset, column_offset );
  89. element.setEndPosition( p );
  90. return p;
  91. }
  92. public static Position createPosition( int line_offset, int column_offset ) {
  93. final int lo = line_offset;
  94. final int co = column_offset;
  95. return new Position() {
  96. public int getOffset() {
  97. return lo + co;
  98. }
  99. };
  100. }
  101. /**
  102. * Assumes the user objects in the given node, and child nodes, are
  103. * objects that implement SideKickElement. This method removes the
  104. * SideKickElement from each node and replaces it with a SideKickAsset
  105. * that wraps the original SideKickElement.
  106. * @param buffer the Buffer representing the text that is to be displayed
  107. * in SideKick. This is used to calculate positions for the individual
  108. * tree nodes.
  109. * @param node the root node of the tree to convert.
  110. */
  111. public static void convert( Buffer buffer, DefaultMutableTreeNode node ) {
  112. final Buffer _buffer = buffer;
  113. final DefaultMutableTreeNode _node = node;
  114. SwingUtilities.invokeLater(
  115. new Runnable() {
  116. public void run() {
  117. _convert( _buffer, _node );
  118. }
  119. }
  120. );
  121. }
  122. private static void _convert( Buffer buffer, DefaultMutableTreeNode node ) {
  123. // convert the children of the node
  124. Enumeration children = node.children();
  125. while ( children.hasMoreElements() ) {
  126. _convert( buffer, ( DefaultMutableTreeNode ) children.nextElement() );
  127. }
  128. // convert the node itself
  129. if ( ( node.getUserObject() instanceof SideKickElement ) ) {
  130. SideKickElement userObject = ( SideKickElement ) node.getUserObject();
  131. Position start_position = createStartPosition( buffer, userObject );
  132. Position end_position = createEndPosition( buffer, userObject );
  133. SideKickAsset asset = new SideKickAsset( userObject );
  134. asset.setStart( start_position );
  135. asset.setEnd( end_position );
  136. node.setUserObject( asset );
  137. }
  138. }
  139. }