View Javadoc
1   package org.vaadin.aceeditor.client;
2   
3   import org.vaadin.aceeditor.client.TransportDoc.TransportMarker;
4   
5   
6   
7   /**
8    * 
9    * Ace marker.
10   * 
11   * The cssClass must be defined in some css file. Example:
12   * 
13   * .ace_marker-layer .mymarker1 {
14   *		background: red;
15   *  	border-bottom: 2px solid black;
16   *  	position: absolute;
17   *  }
18   *
19   */
20  public class AceMarker {
21  	
22  	/**
23  	 * 
24  	 * Ace Marker type.
25  	 *
26  	 */
27  	public enum Type {
28  		line,
29  		text
30  	}
31  	
32  	/**
33  	 * What to do with the marker when the text changes.
34  	 * 
35  	 * By default, Ace just keeps the marker in its place (DEFAULT).
36  	 * Alternatively, you can set the marker to ADJUST to text insertion/deletion.
37  	 * Or, you can set the marker to be REMOVE'd on text change.
38  	 *
39  	 */
40  	public enum OnTextChange {
41  		/**
42  		 * Keep the marker in its place.
43  		 */
44  		DEFAULT,
45  		/**
46  		 * Adjust the marker based on text insertion/deletion.
47  		 */
48  		ADJUST,
49  		/**
50  		 * Remove the marker when text changes.
51  		 */
52  		REMOVE
53  	}
54  	
55  	private final String markerId;
56  	private final AceRange range;
57  	private final OnTextChange onChange;
58  	private final String cssClass;
59  	private final Type type;
60  	private final boolean inFront;
61  	
62  	
63  	
64  	
65  	
66  	public AceMarker(String markerId, AceRange range, String cssClass, Type type, boolean inFront, OnTextChange onChange) {
67  		this.markerId = markerId;
68  		this.range = range.isBackwards() ? range.reversed() : range;
69  		this.cssClass = cssClass;
70  		this.type = type;
71  		this.inFront = inFront;
72  		this.onChange = onChange;
73  	}
74  
75  	@Override
76  	public boolean equals(Object o) {
77  		if (o instanceof AceMarker) {
78  			AceMarker om = (AceMarker)o;
79  			return markerId.equals(om.markerId) && range.equals(om.range) && onChange.equals(om.onChange) &&
80  					cssClass.equals(om.cssClass) && type.equals(om.type) && inFront==om.inFront;
81  		}
82  		return false;
83  	}
84  	@Override
85  	public int hashCode() {
86  		return range.hashCode(); // ?
87  	}
88  
89  	@Override
90  	public String toString() {
91  		return "("+range+";"+cssClass+";"+type+";"+inFront+")";
92  	}
93  
94  
95  
96  //	@Override
97  //	public int compareTo(AceMarker other) {
98  //		if (range.row1 < other.range.row1) {
99  //			return -1;
100 //		}
101 //		if (range.row1 > other.range.row1) {
102 //			return 1;
103 //		}
104 //		if (range.col1 < other.range.col1) {
105 //			return -1;
106 //		}
107 //		if (range.col1 > other.range.col1) {
108 //			return 1;
109 //		}
110 //		return 0;
111 //	}
112 	
113 	public TransportMarker asTransport() {
114 		TransportMarker tm = new TransportMarker();
115 		tm.markerId = markerId;
116 		tm.range = range.asTransport();
117 		tm.cssClass = cssClass;
118 		tm.type = type.toString();
119 		tm.inFront = inFront;
120 		tm.onChange = onChange.toString();
121 		return tm;
122 	}
123 
124 
125 
126 	public String getMarkerId() {
127 		return markerId;
128 	}
129 
130 	public static AceMarker fromTransport(TransportMarker im) {
131 		return new AceMarker(im.markerId, AceRange.fromTransport(im.range),
132 				im.cssClass, Type.valueOf(im.type), im.inFront, OnTextChange.valueOf(im.onChange));
133 	}
134 
135 	public AceRange getRange() {
136 		return range;
137 	}
138 	
139 	public OnTextChange getOnChange() {
140 		return onChange;
141 	}
142 
143 	public String getCssClass() {
144 		return cssClass;
145 	}
146 
147 	public Type getType() {
148 		return type;
149 	}
150 
151 	public boolean isInFront() {
152 		return inFront;
153 	}
154 
155 	public AceMarker withNewPosition(AceRange newRange) {
156 		return new AceMarker(markerId, newRange, cssClass, type, inFront, onChange);
157 	}
158 }