1 /* 2 * ConnectBot: simple, powerful, open-source SSH client for Android 3 * Copyright 2007 Kenny Root, Jeffrey Sharkey 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.connectbot.util; 19 20 import de.mud.terminal.VDUBuffer; 21 22 /** 23 * @author Kenny Root 24 * Keep track of a selection area for the terminal copying mechanism. 25 * If the orientation is flipped one way, swap the bottom and top or 26 * left and right to keep it in the correct orientation. 27 */ 28 public class SelectionArea { 29 private int top; 30 private int bottom; 31 private int left; 32 private int right; 33 private int maxColumns; 34 private int maxRows; 35 private boolean selectingOrigin; 36 SelectionArea()37 public SelectionArea() { 38 reset(); 39 } 40 reset()41 public final void reset() { 42 top = left = bottom = right = 0; 43 selectingOrigin = true; 44 } 45 46 /** 47 * @param columns 48 * @param rows 49 */ setBounds(int columns, int rows)50 public void setBounds(int columns, int rows) { 51 maxColumns = columns - 1; 52 maxRows = rows - 1; 53 } 54 checkBounds(int value, int max)55 private int checkBounds(int value, int max) { 56 if (value < 0) 57 return 0; 58 else if (value > max) 59 return max; 60 else 61 return value; 62 } 63 isSelectingOrigin()64 public boolean isSelectingOrigin() { 65 return selectingOrigin; 66 } 67 finishSelectingOrigin()68 public void finishSelectingOrigin() { 69 selectingOrigin = false; 70 } 71 decrementRow()72 public void decrementRow() { 73 if (selectingOrigin) 74 setTop(top - 1); 75 else 76 setBottom(bottom - 1); 77 } 78 incrementRow()79 public void incrementRow() { 80 if (selectingOrigin) 81 setTop(top + 1); 82 else 83 setBottom(bottom + 1); 84 } 85 setRow(int row)86 public void setRow(int row) { 87 if (selectingOrigin) 88 setTop(row); 89 else 90 setBottom(row); 91 } 92 setTop(int top)93 private void setTop(int top) { 94 this.top = bottom = checkBounds(top, maxRows); 95 } 96 getTop()97 public int getTop() { 98 return Math.min(top, bottom); 99 } 100 setBottom(int bottom)101 private void setBottom(int bottom) { 102 this.bottom = checkBounds(bottom, maxRows); 103 } 104 getBottom()105 public int getBottom() { 106 return Math.max(top, bottom); 107 } 108 decrementColumn()109 public void decrementColumn() { 110 if (selectingOrigin) 111 setLeft(left - 1); 112 else 113 setRight(right - 1); 114 } 115 incrementColumn()116 public void incrementColumn() { 117 if (selectingOrigin) 118 setLeft(left + 1); 119 else 120 setRight(right + 1); 121 } 122 setColumn(int column)123 public void setColumn(int column) { 124 if (selectingOrigin) 125 setLeft(column); 126 else 127 setRight(column); 128 } 129 setLeft(int left)130 private void setLeft(int left) { 131 this.left = right = checkBounds(left, maxColumns); 132 } 133 getLeft()134 public int getLeft() { 135 return Math.min(left, right); 136 } 137 setRight(int right)138 private void setRight(int right) { 139 this.right = checkBounds(right, maxColumns); 140 } 141 getRight()142 public int getRight() { 143 return Math.max(left, right); 144 } 145 copyFrom(VDUBuffer vb)146 public String copyFrom(VDUBuffer vb) { 147 int size = (getRight() - getLeft() + 1) * (getBottom() - getTop() + 1); 148 149 StringBuffer buffer = new StringBuffer(size); 150 151 for(int y = getTop(); y <= getBottom(); y++) { 152 int lastNonSpace = buffer.length(); 153 154 for (int x = getLeft(); x <= getRight(); x++) { 155 // only copy printable chars 156 char c = vb.getChar(x, y); 157 158 if (!Character.isDefined(c) || 159 (Character.isISOControl(c) && c != '\t')) 160 c = ' '; 161 162 if (c != ' ') 163 lastNonSpace = buffer.length(); 164 165 buffer.append(c); 166 } 167 168 // Don't leave a bunch of spaces in our copy buffer. 169 if (buffer.length() > lastNonSpace) 170 buffer.delete(lastNonSpace + 1, buffer.length()); 171 172 if (y != bottom) 173 buffer.append("\n"); 174 } 175 176 return buffer.toString(); 177 } 178 179 @Override toString()180 public String toString() { 181 StringBuilder buffer = new StringBuilder(); 182 183 buffer.append("SelectionArea[top="); 184 buffer.append(top); 185 buffer.append(", bottom="); 186 buffer.append(bottom); 187 buffer.append(", left="); 188 buffer.append(left); 189 buffer.append(", right="); 190 buffer.append(right); 191 buffer.append(", maxColumns="); 192 buffer.append(maxColumns); 193 buffer.append(", maxRows="); 194 buffer.append(maxRows); 195 buffer.append(", isSelectingOrigin="); 196 buffer.append(isSelectingOrigin()); 197 buffer.append("]"); 198 199 return buffer.toString(); 200 } 201 } 202