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

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