Mercurial > 510Connectbot
comparison src/com/five_ten_sg/connectbot/bean/SelectionArea.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
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 com.five_ten_sg.connectbot.bean; | |
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 | |
37 public SelectionArea() { | |
38 reset(); | |
39 } | |
40 | |
41 public final void reset() { | |
42 top = left = bottom = right = 0; | |
43 selectingOrigin = true; | |
44 } | |
45 | |
46 /** | |
47 * @param columns | |
48 * @param rows | |
49 */ | |
50 public void setBounds(int columns, int rows) { | |
51 maxColumns = columns - 1; | |
52 maxRows = rows - 1; | |
53 } | |
54 | |
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 | |
64 public boolean isSelectingOrigin() { | |
65 return selectingOrigin; | |
66 } | |
67 | |
68 public void finishSelectingOrigin() { | |
69 selectingOrigin = false; | |
70 } | |
71 | |
72 public void decrementRow() { | |
73 if (selectingOrigin) | |
74 setTop(top - 1); | |
75 else | |
76 setBottom(bottom - 1); | |
77 } | |
78 | |
79 public void incrementRow() { | |
80 if (selectingOrigin) | |
81 setTop(top + 1); | |
82 else | |
83 setBottom(bottom + 1); | |
84 } | |
85 | |
86 public void setRow(int row) { | |
87 if (selectingOrigin) | |
88 setTop(row); | |
89 else | |
90 setBottom(row); | |
91 } | |
92 | |
93 private void setTop(int top) { | |
94 this.top = bottom = checkBounds(top, maxRows); | |
95 } | |
96 | |
97 public int getTop() { | |
98 return Math.min(top, bottom); | |
99 } | |
100 | |
101 private void setBottom(int bottom) { | |
102 this.bottom = checkBounds(bottom, maxRows); | |
103 } | |
104 | |
105 public int getBottom() { | |
106 return Math.max(top, bottom); | |
107 } | |
108 | |
109 public void decrementColumn() { | |
110 if (selectingOrigin) | |
111 setLeft(left - 1); | |
112 else | |
113 setRight(right - 1); | |
114 } | |
115 | |
116 public void incrementColumn() { | |
117 if (selectingOrigin) | |
118 setLeft(left + 1); | |
119 else | |
120 setRight(right + 1); | |
121 } | |
122 | |
123 public void setColumn(int column) { | |
124 if (selectingOrigin) | |
125 setLeft(column); | |
126 else | |
127 setRight(column); | |
128 } | |
129 | |
130 private void setLeft(int left) { | |
131 this.left = right = checkBounds(left, maxColumns); | |
132 } | |
133 | |
134 public int getLeft() { | |
135 return Math.min(left, right); | |
136 } | |
137 | |
138 private void setRight(int right) { | |
139 this.right = checkBounds(right, maxColumns); | |
140 } | |
141 | |
142 public int getRight() { | |
143 return Math.max(left, right); | |
144 } | |
145 | |
146 public String copyFrom(VDUBuffer vb) { | |
147 int size = (getRight() - getLeft() + 1) * (getBottom() - getTop() + 1); | |
148 StringBuffer buffer = new StringBuffer(size); | |
149 | |
150 for (int y = getTop(); y <= getBottom(); y++) { | |
151 int lastNonSpace = buffer.length(); | |
152 | |
153 for (int x = getLeft(); x <= getRight(); x++) { | |
154 // only copy printable chars | |
155 char c = vb.getChar(x, y); | |
156 | |
157 if (!Character.isDefined(c) || | |
158 (Character.isISOControl(c) && c != '\t')) | |
159 c = ' '; | |
160 | |
161 if (c != ' ') | |
162 lastNonSpace = buffer.length(); | |
163 | |
164 buffer.append(c); | |
165 } | |
166 | |
167 // Don't leave a bunch of spaces in our copy buffer. | |
168 if (buffer.length() > lastNonSpace) | |
169 buffer.delete(lastNonSpace + 1, buffer.length()); | |
170 | |
171 if (y != bottom) | |
172 buffer.append("\n"); | |
173 } | |
174 | |
175 return buffer.toString(); | |
176 } | |
177 | |
178 @Override | |
179 public String toString() { | |
180 StringBuilder buffer = new StringBuilder(); | |
181 buffer.append("SelectionArea[top="); | |
182 buffer.append(top); | |
183 buffer.append(", bottom="); | |
184 buffer.append(bottom); | |
185 buffer.append(", left="); | |
186 buffer.append(left); | |
187 buffer.append(", right="); | |
188 buffer.append(right); | |
189 buffer.append(", maxColumns="); | |
190 buffer.append(maxColumns); | |
191 buffer.append(", maxRows="); | |
192 buffer.append(maxRows); | |
193 buffer.append(", isSelectingOrigin="); | |
194 buffer.append(isSelectingOrigin()); | |
195 buffer.append("]"); | |
196 return buffer.toString(); | |
197 } | |
198 } |