+ /**
+ * Get the selection starting column number.
+ *
+ * @return the starting column number, or -1 if there is no selection.
+ * 0-based: column 0 is the first column.
+ */
+ public int getSelectionStartColumn() {
+ if (!inSelection) {
+ return -1;
+ }
+
+ int startCol = selectionColumn0;
+ int startRow = selectionLine0;
+ int endCol = selectionColumn1;
+ int endRow = selectionLine1;
+
+ if (((selectionColumn1 < selectionColumn0)
+ && (selectionLine1 == selectionLine0))
+ || (selectionLine1 < selectionLine0)
+ ) {
+ // The user selected from bottom-to-top and/or right-to-left.
+ // Reverse the coordinates for the inverted section.
+ startCol = selectionColumn1;
+ startRow = selectionLine1;
+ endCol = selectionColumn0;
+ endRow = selectionLine0;
+ }
+ return startCol;
+ }
+
+ /**
+ * Get the selection ending row number.
+ *
+ * @return the ending row number, or -1 if there is no selection.
+ * 0-based: row 0 is the first row.
+ */
+ public int getSelectionEndRow() {
+ if (!inSelection) {
+ return -1;
+ }
+
+ int startCol = selectionColumn0;
+ int startRow = selectionLine0;
+ int endCol = selectionColumn1;
+ int endRow = selectionLine1;
+
+ if (((selectionColumn1 < selectionColumn0)
+ && (selectionLine1 == selectionLine0))
+ || (selectionLine1 < selectionLine0)
+ ) {
+ // The user selected from bottom-to-top and/or right-to-left.
+ // Reverse the coordinates for the inverted section.
+ startCol = selectionColumn1;
+ startRow = selectionLine1;
+ endCol = selectionColumn0;
+ endRow = selectionLine0;
+ }
+ return endRow;
+ }
+
+ /**
+ * Get the selection ending column number.
+ *
+ * @return the ending column number, or -1 if there is no selection.
+ * 0-based: column 0 is the first column.
+ */
+ public int getSelectionEndColumn() {
+ if (!inSelection) {
+ return -1;
+ }
+
+ int startCol = selectionColumn0;
+ int startRow = selectionLine0;
+ int endCol = selectionColumn1;
+ int endRow = selectionLine1;
+
+ if (((selectionColumn1 < selectionColumn0)
+ && (selectionLine1 == selectionLine0))
+ || (selectionLine1 < selectionLine0)
+ ) {
+ // The user selected from bottom-to-top and/or right-to-left.
+ // Reverse the coordinates for the inverted section.
+ startCol = selectionColumn1;
+ startRow = selectionLine1;
+ endCol = selectionColumn0;
+ endRow = selectionLine0;
+ }
+ return endCol;
+ }
+
+ /**
+ * Unset the selection.
+ */
+ public void unsetSelection() {
+ inSelection = false;
+ }
+
+ /**
+ * Replace whatever is being selected with new text. If not in
+ * selection, nothing is replaced.
+ *
+ * @param text the new replacement text
+ */
+ public void replaceSelection(final String text) {
+ if (!inSelection) {
+ return;
+ }
+
+ // Delete selected text, then paste text from clipboard.
+ deleteSelection();
+
+ for (int i = 0; i < text.length(); ) {
+ int ch = text.codePointAt(i);
+ onKeypress(new TKeypressEvent(false, 0, ch, false, false,
+ false));
+ i += Character.charCount(ch);
+ }
+ }
+
+ /**
+ * Check if selection is available.
+ *
+ * @return true if a selection has been made
+ */
+ public boolean hasSelection() {
+ return inSelection;
+ }
+
+ /**
+ * Get the entire contents of the editor as one string.
+ *
+ * @return the editor contents
+ */
+ public String getText() {
+ return document.getText();
+ }
+
+ /**
+ * Set the entire contents of the editor from one string.
+ *
+ * @param text the new contents
+ */
+ public void setText(final String text) {
+ document = new Document(text, defaultColor);
+ unsetSelection();
+ topLine = 0;
+ leftColumn = 0;