You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LinePosition.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. /**
  24. * Holds information about a range of text.
  25. */
  26. public class LinePosition {
  27. /** Starting line. */
  28. private int startLine;
  29. /** Ending line. */
  30. private int endLine;
  31. /** Starting position. */
  32. private int startPos;
  33. /** Ending position. */
  34. private int endPos;
  35. /**
  36. * Constructs a new line position.
  37. *
  38. * @param startLine Starting line
  39. * @param endLine Ending line
  40. * @param startPos Starting position
  41. * @param endPos Ending position
  42. */
  43. public LinePosition(final int startLine, final int startPos,
  44. final int endLine, final int endPos) {
  45. this.startLine = startLine;
  46. this.endLine = endLine;
  47. this.startPos = startPos;
  48. this.endPos = endPos;
  49. }
  50. /**
  51. * Constructs a new line position.
  52. *
  53. * @param position Position to create position from
  54. */
  55. public LinePosition(final LinePosition position) {
  56. this.startLine = position.getStartLine();
  57. this.endLine = position.getEndLine();
  58. this.startPos = position.getStartPos();
  59. this.endPos = position.getEndPos();
  60. }
  61. /**
  62. * Returns the end line for this position.
  63. *
  64. * @return End line
  65. */
  66. public int getEndLine() {
  67. return endLine;
  68. }
  69. /**
  70. * Returns the end position for this position.
  71. *
  72. * @return End position
  73. */
  74. public int getEndPos() {
  75. return endPos;
  76. }
  77. /**
  78. * Returns the start line for this position.
  79. *
  80. * @return Start line
  81. */
  82. public int getStartLine() {
  83. return startLine;
  84. }
  85. /**
  86. * Returns the start position for this position.
  87. *
  88. * @return Start position
  89. */
  90. public int getStartPos() {
  91. return startPos;
  92. }
  93. /**
  94. * Sets the positions end line.
  95. *
  96. * @param endLine new end line
  97. */
  98. public void setEndLine(final int endLine) {
  99. this.endLine = endLine;
  100. }
  101. /**
  102. * Sets the positions end line.
  103. *
  104. * @param endPos new end line
  105. */
  106. public void setEndPos(final int endPos) {
  107. this.endPos = endPos;
  108. }
  109. /**
  110. * Sets the positions start line.
  111. *
  112. * @param startLine new start line
  113. */
  114. public void setStartLine(final int startLine) {
  115. this.startLine = startLine;
  116. }
  117. /**
  118. * Sets the positions start position.
  119. *
  120. * @param startPos new start position
  121. */
  122. public void setStartPos(final int startPos) {
  123. this.startPos = startPos;
  124. }
  125. /**
  126. * Gets a new, normalised version of this position. Start and end lines and positions are
  127. * normalised such that the same range of lines and positions are included, but the start
  128. * line and position comes before the end line and position.
  129. *
  130. * @return A normalised copy of this position.
  131. */
  132. public LinePosition getNormalised() {
  133. if (startLine > endLine) {
  134. // Multi-line "backwards" selection; swap both lines and positions.
  135. return new LinePosition(endLine, endPos, startLine, startPos);
  136. } else if (startLine == endLine && startPos > endPos) {
  137. // Single-line "backwards" selection; just swap the positions.
  138. return new LinePosition(startLine, endPos, endLine, startPos);
  139. } else {
  140. // Single- or multi-line "forward" selection; swap nothing.
  141. return new LinePosition(startLine, startPos, endLine, endPos);
  142. }
  143. }
  144. @Override
  145. public String toString() {
  146. return "Position[" + startLine + ", " + startPos + ", " + endLine
  147. + ", " + endPos + ']';
  148. }
  149. }