1 package org.vaadin.hene.expandingtextarea;
2
3 import java.io.Serializable;
4 import java.lang.reflect.Method;
5
6 import org.vaadin.hene.expandingtextarea.widgetset.client.ui.ExpandingTextAreaServerRpc;
7
8 import com.vaadin.v7.data.Property;
9 import com.vaadin.server.PaintException;
10 import com.vaadin.server.PaintTarget;
11 import com.vaadin.v7.ui.TextArea;
12 import com.vaadin.util.ReflectTools;
13
14
15
16
17
18
19
20
21 @SuppressWarnings("serial")
22 public class ExpandingTextArea extends TextArea {
23
24 private static final Method ROWS_CHANGE_METHOD = ReflectTools.findMethod(
25 RowsChangeListener.class, "rowsChange", RowsChangeEvent.class);
26
27 private int rows = 2;
28 private Integer maxRows = null;
29
30 private ExpandingTextAreaServerRpc rpc = new ExpandingTextAreaServerRpc() {
31 public void setRows(int rows) {
32 ExpandingTextArea.this.rows = rows;
33 fireRowsChangeEvent();
34 }
35 };
36
37
38
39
40 public ExpandingTextArea() {
41 registerRpc(rpc);
42 setRows(2);
43 setValue("");
44 }
45
46
47
48
49
50
51
52 public ExpandingTextArea(String caption) {
53 registerRpc(rpc);
54 setRows(2);
55 setCaption(caption);
56 }
57
58
59
60
61
62
63
64
65 public ExpandingTextArea(Property dataSource) {
66 registerRpc(rpc);
67 setRows(2);
68 setPropertyDataSource(dataSource);
69 }
70
71
72
73
74
75
76
77
78
79
80
81 public ExpandingTextArea(String caption, Property dataSource) {
82 this(dataSource);
83 setCaption(caption);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public ExpandingTextArea(String caption, String value) {
99 registerRpc(rpc);
100 setRows(2);
101 setValue(value);
102 setCaption(caption);
103 }
104
105 @Override
106 public void paintContent(PaintTarget target) throws PaintException {
107 target.addVariable(this, "rows", getRows());
108 if (maxRows != null) {
109 target.addAttribute("maxRows", maxRows);
110 }
111 super.paintContent(target);
112 }
113
114 @Override
115 public void setRows(int rows) {
116 if (rows < 2) {
117 throw new IllegalArgumentException("rows must be >= 2");
118 }
119 if (this.rows != rows) {
120 this.rows = rows;
121 super.setRows(rows);
122 fireRowsChangeEvent();
123 }
124 }
125
126 @Override
127 public int getRows() {
128 return rows;
129 }
130
131 @Override
132 public void setHeight(float height, Unit unit) {
133 if (height == -1) {
134 super.setHeight(height, unit);
135 } else {
136 throw new IllegalArgumentException(
137 "ExpandingTextArea supports only undefined height");
138 }
139 }
140
141
142
143
144
145
146
147
148
149 public void setMaxRows(Integer maxRows) {
150 if (maxRows != null && maxRows < 2) {
151 throw new IllegalArgumentException("maxRows must be >= 2");
152 }
153 if ((this.maxRows == null && maxRows != null)
154 || !this.maxRows.equals(maxRows)) {
155 this.maxRows = maxRows;
156 requestRepaint();
157 }
158 }
159
160
161
162
163
164
165
166 public int getMaxRows() {
167 return maxRows;
168 }
169
170
171
172
173 public void addRowsChangeListener(RowsChangeListener listener) {
174 addListener(RowsChangeEvent.class, listener, ROWS_CHANGE_METHOD);
175 }
176
177
178
179
180 public void removeRowsChangeListener(RowsChangeListener listener) {
181 removeListener(RowsChangeEvent.class, listener, ROWS_CHANGE_METHOD);
182 }
183
184 private void fireRowsChangeEvent() {
185 fireEvent(new RowsChangeEvent(this));
186 }
187
188 public class RowsChangeEvent extends Event {
189
190 public RowsChangeEvent(ExpandingTextArea source) {
191 super(source);
192 }
193
194 public ExpandingTextArea getExpandingTextArea() {
195 return (ExpandingTextArea) getSource();
196 }
197
198 public int getRows() {
199 return getExpandingTextArea().getRows();
200 }
201 }
202
203
204
205
206
207
208
209
210
211
212
213 public interface RowsChangeListener extends Serializable {
214
215 void rowsChange(RowsChangeEvent event);
216 }
217
218 }