View Javadoc
1   package org.vaadin.aceeditor.client;
2   
3   import org.vaadin.aceeditor.client.TransportDoc.TransportRange;
4   
5   
6   /**
7    * 
8    * 
9    */
10  public class AceRange {
11  	
12  	private final int row1;
13  	private final int col1;
14  	private final int row2;
15  	private final int col2;
16  
17  
18  	public AceRange(int row1, int col1, int row2, int col2) {
19  		this.row1 = row1;
20  		this.col1 = col1;
21  		this.row2 = row2;
22  		this.col2 = col2;
23  	}
24  	
25  	public static AceRange fromPositions(int start, int end, String text) {
26  		return fromPositions(start, end, text.split("\n", -1));
27  	}
28  	
29  	public static AceRange fromPositions(int start, int end, String[] lines) {
30  		int[] rc1 = Util.lineColFromCursorPos(lines, start, 0);
31  		int[] rc2 = start==end ? rc1 : Util.lineColFromCursorPos(lines, end, 0);
32  		return new AceRange(rc1[0], rc1[1], rc2[0], rc2[1]);
33  	}
34  
35  	public int getStartRow() {
36  		return row1;
37  	}
38  	
39  	public int getStartCol() {
40  		return col1;
41  	}
42  	
43  	public int getEndRow() {
44  		return row2;
45  	}
46  	
47  	public int getEndCol() {
48  		return col2;
49  	}
50  	
51  	public int[] getPositions(String text) {
52  		return getPositions(text.split("\n", -1));
53  	}
54  	
55  	public int[] getPositions(String[] lines) {
56  		int start = Util.cursorPosFromLineCol(lines, row1, col1, 0);
57  		int end = isZeroLength() ? start : Util.cursorPosFromLineCol(lines, row2, col2, 0);
58  		return new int[]{start,end};
59  	}
60  	
61  	
62  
63  	public TransportRange asTransport() {
64  		TransportRange tr = new TransportRange();
65  		tr.row1 = getStartRow();
66  		tr.col1 = getStartCol();
67  		tr.row2 = getEndRow();
68  		tr.col2 = getEndCol();
69  		return tr;
70  	}
71  	
72  	@Override
73  	public boolean equals(Object o) {
74  		if (o instanceof AceRange) {
75  			AceRange or = (AceRange)o;
76  			return row1 == or.row1 && col1 == or.col1 && row2 == or.row2 && col2 == or.col2;
77  		}
78  		return false;
79  	}
80  	
81  	@Override
82  	public int hashCode() {
83  		return row1+col1+row2+col2; // ?
84  	}
85  	
86  	public boolean isBackwards() {
87  		return row1> row2 || (row1==row2 && col1>col2);
88  	}
89  	
90  	public AceRange reversed() {
91  		return new AceRange(row2, col2, row1, col1);
92  	}
93  	
94  	public boolean isZeroLength() {
95  		return row1==row2 && col1==col2;
96  	}
97  
98  	public static AceRange fromTransport(TransportRange tr) {
99  		return new AceRange(tr.row1, tr.col1, tr.row2, tr.col2);
100 	}
101 	
102 	@Override
103 	public String toString() {
104 		return "[("+row1+","+col1+")-("+row2+","+col2+")]";
105 	}
106 
107 }