Home | History | Annotate | Download | only in util
      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  * Keep track of a selection area for the terminal copying mechanism.
     24  * If the orientation is flipped one way, swap the bottom and top or
     25  * left and right to keep it in the correct orientation.
     26  */
     27 public class SelectionArea {
     28 	private int top;
     29 	private int bottom;
     30 	private int left;
     31 	private int right;
     32 	private int maxColumns;
     33 	private int maxRows;
     34 	private boolean selectingOrigin;
     35 
     36 	public SelectionArea() {
     37 		reset();
     38 	}
     39 
     40 	public final void reset() {
     41 		top = left = bottom = right = 0;
     42 		selectingOrigin = true;
     43 	}
     44 
     45 	/**
     46 	 * @param columns
     47 	 * @param rows
     48 	 */
     49 	public void setBounds(int columns, int rows) {
     50 		maxColumns = columns - 1;
     51 		maxRows = rows - 1;
     52 	}
     53 
     54 	private int checkBounds(int value, int max) {
     55 		if (value < 0)
     56 			return 0;
     57 		else if (value > max)
     58 			return max;
     59 		else
     60 			return value;
     61 	}
     62 
     63 	public boolean isSelectingOrigin() {
     64 		return selectingOrigin;
     65 	}
     66 
     67 	public void finishSelectingOrigin() {
     68 		selectingOrigin = false;
     69 	}
     70 
     71 	public void decrementRow() {
     72 		if (selectingOrigin)
     73 			setTop(top - 1);
     74 		else
     75 			setBottom(bottom - 1);
     76 	}
     77 
     78 	public void incrementRow() {
     79 		if (selectingOrigin)
     80 			setTop(top + 1);
     81 		else
     82 			setBottom(bottom + 1);
     83 	}
     84 
     85 	public void setRow(int row) {
     86 		if (selectingOrigin)
     87 			setTop(row);
     88 		else
     89 			setBottom(row);
     90 	}
     91 
     92 	private void setTop(int top) {
     93 		this.top = bottom = checkBounds(top, maxRows);
     94 	}
     95 
     96 	public int getTop() {
     97 		return Math.min(top, bottom);
     98 	}
     99 
    100 	private void setBottom(int bottom) {
    101 		this.bottom = checkBounds(bottom, maxRows);
    102 	}
    103 
    104 	public int getBottom() {
    105 		return Math.max(top, bottom);
    106 	}
    107 
    108 	public void decrementColumn() {
    109 		if (selectingOrigin)
    110 			setLeft(left - 1);
    111 		else
    112 			setRight(right - 1);
    113 	}
    114 
    115 	public void incrementColumn() {
    116 		if (selectingOrigin)
    117 			setLeft(left + 1);
    118 		else
    119 			setRight(right + 1);
    120 	}
    121 
    122 	public void setColumn(int column) {
    123 		if (selectingOrigin)
    124 			setLeft(column);
    125 		else
    126 			setRight(column);
    127 	}
    128 
    129 	private void setLeft(int left) {
    130 		this.left = right = checkBounds(left, maxColumns);
    131 	}
    132 
    133 	public int getLeft() {
    134 		return Math.min(left, right);
    135 	}
    136 
    137 	private void setRight(int right) {
    138 		this.right = checkBounds(right, maxColumns);
    139 	}
    140 
    141 	public int getRight() {
    142 		return Math.max(left, right);
    143 	}
    144 
    145 	public String copyFrom(VDUBuffer vb) {
    146 		int size = (getRight() - getLeft() + 1) * (getBottom() - getTop() + 1);
    147 
    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 
    182 		buffer.append("SelectionArea[top=");
    183 		buffer.append(top);
    184 		buffer.append(", bottom=");
    185 		buffer.append(bottom);
    186 		buffer.append(", left=");
    187 		buffer.append(left);
    188 		buffer.append(", right=");
    189 		buffer.append(right);
    190 		buffer.append(", maxColumns=");
    191 		buffer.append(maxColumns);
    192 		buffer.append(", maxRows=");
    193 		buffer.append(maxRows);
    194 		buffer.append(", isSelectingOrigin=");
    195 		buffer.append(isSelectingOrigin());
    196 		buffer.append("]");
    197 
    198 		return buffer.toString();
    199 	}
    200 }
    201