View Javadoc

1   /* ======================================
2    * Copyright (c) 2004-2005 Achim Nierbeck
3    * All rights reserved.
4    *
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.gnu.org/licenses/lgpl.html
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * ======================================
16   */
17  
18  /*
19   * TextObserver.java
20   * $Revision: 1.6 $
21   * Date 14.12.2005
22   * 
23   * $Author: nierbeck $ 
24   * $Date: 2006/02/02 21:33:07 $ 
25   * 
26   */
27  
28  package de.nierbeck.timeTrack.views;
29  
30  import java.util.Observable;
31  import java.util.Observer;
32  
33  import org.eclipse.swt.SWT;
34  import org.eclipse.swt.events.FocusListener;
35  import org.eclipse.swt.events.ModifyListener;
36  import org.eclipse.swt.events.SelectionListener;
37  import org.eclipse.swt.events.VerifyListener;
38  import org.eclipse.swt.graphics.Font;
39  import org.eclipse.swt.graphics.Point;
40  import org.eclipse.swt.graphics.Rectangle;
41  import org.eclipse.swt.layout.RowData;
42  import org.eclipse.swt.widgets.Composite;
43  import org.eclipse.swt.widgets.Text;
44  import org.eclipse.swt.widgets.TypedListener;
45  
46  import de.nierbeck.timeTrack.TimeTrackEntriesManager;
47  
48  /***
49   * @author Achim
50   * 
51   */
52  public class TextObserver implements Observer {
53  
54  	private Text text = null;
55  
56  	/***
57  	 * @param parent - the Parent of the Composit
58  	 * @param style - the Style of the Composit
59  	 */
60  	public TextObserver(Composite parent, int style) {
61  		text = new Text(parent, style);
62  	}
63  
64  	/***
65  	 * @param o - the Observable
66  	 * @param arg - the argument
67  	 * 
68  	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
69  	 */
70  	public final void update(Observable o, Object arg) {
71  		if (o instanceof TimeTrackEntriesManager) {
72  			TimeTrackEntriesManager entries = (TimeTrackEntriesManager) o;
73  			long duration = entries.getCurrentSelected().getDuration();
74  			this.text.setText(Long.toString(duration));
75  			this.text.redraw();
76  		}
77  	}
78  
79  	/***
80  	 * Adds listener to observer
81  	 * 
82  	 * @param listener - the listener to add
83  	 */
84  	public final void addModifyListener(ModifyListener listener) {
85  		text.addModifyListener(listener);
86  	}
87  
88  	/***
89  	 * Adds listener to observer
90  	 * 
91  	 * @param listener - the listener to add
92  	 */
93  	public final void addSelectionListener(SelectionListener listener) {
94  		text.addSelectionListener(listener);
95  	}
96  
97  	/***
98  	 * Adds listener to observer
99  	 * @param listener - the listener to add
100 	 */
101 	public final void addVerifyListener(VerifyListener listener) {
102 		text.addVerifyListener(listener);
103 	}
104 
105 	/***
106 	 * Delegate Method
107 	 * @param string - delegated to Text
108 	 */
109 	public final void append(String string) {
110 		text.append(string);
111 	}
112 
113 	/***
114 	 * Delegate Method
115 	 *
116 	 */
117 	public final void clearSelection() {
118 		text.clearSelection();
119 	}
120 
121 	/***
122 	 * Delegate Method
123 	 * 
124 	 * @param wHint - delegated to Text
125 	 * @param hHint - delegated to Text
126 	 * @param changed - delegated to Text
127 	 * @return the Point from Text
128 	 */
129 	public final Point computeSize(int wHint, int hHint, boolean changed) {
130 		return text.computeSize(wHint, hHint, changed);
131 	}
132 
133 	/***
134 	 * Delegate Method
135 	 * 
136 	 * @param x - delegated to Text
137 	 * @param y - delegated to Text
138 	 * @param width - delegated to Text
139 	 * @param height - delegated to Text
140 	 * @return the Rectangle from Text
141 	 */
142 	public final Rectangle computeTrim(int x, int y, int width, int height) {
143 		return text.computeTrim(x, y, width, height);
144 	}
145 
146 	/***
147 	 * Delegate Method
148 	 *
149 	 * @see Text#copy()
150 	 */
151 	public final void copy() {
152 		text.copy();
153 	}
154 
155 	/***
156 	 * Delegate Method
157 	 * 
158 	 * @see Text#cut()
159 	 */
160 	public final void cut() {
161 		text.cut();
162 	}
163 
164 	/***
165 	 * Delegate Method
166 	 * 
167 	 * @return the borderWidht from Text
168 	 * 
169 	 * @see Text#getBorderWidth()
170 	 */
171 	public final int getBorderWidth() {
172 		return text.getBorderWidth();
173 	}
174 
175 	/***
176 	 * Delegate Method
177 	 * 
178 	 * @return the linenumber from Text
179 	 * 
180 	 * @see Text#getCaretLineNumber()
181 	 *
182 	 */
183 	public final int getCaretLineNumber() {
184 		return text.getCaretLineNumber();
185 	}
186 
187 	/***
188 	 * Delegate Method
189 	 * 
190 	 * @return the point from Text
191 	 * 
192 	 * @see Text#getCaretLocation()
193 	 */
194 	public final Point getCaretLocation() {
195 		return text.getCaretLocation();
196 	}
197 
198 	/***
199 	 * Delegate Method
200 	 * 
201 	 * @return the position from Text
202 	 * 
203 	 * @see Text#getCaretPosition()
204 	 */
205 	public final int getCaretPosition() {
206 		return text.getCaretPosition();
207 	}
208 
209 	/***
210 	 * Delegate Method
211 	 * 
212 	 * @return the count from text
213 	 * 
214 	 * @see Text#getCharCount()
215 	 */
216 	public final int getCharCount() {
217 		return text.getCharCount();
218 	}
219 
220 	/***
221 	 * Delegate Method
222 	 * 
223 	 * @return the enabled flag from Text
224 	 * 
225 	 * @see Text#getDoubleClickEnabled()
226 	 */
227 	public final boolean getDoubleClickEnabled() {
228 		return text.getDoubleClickEnabled();
229 	}
230 
231 	/***
232 	 * Delegate Method
233 	 * 
234 	 * @return the echo char from Text
235 	 * 
236 	 * @see Text#getEchoChar()
237 	 */
238 	public final char getEchoChar() {
239 		return text.getEchoChar();
240 	}
241 
242 	/***
243 	 * Delegate Method
244 	 * 
245 	 * @return flag from Text
246 	 * 
247 	 * @see Text#getEditable()
248 	 */
249 	public final boolean getEditable() {
250 		return text.getEditable();
251 	}
252 
253 	/***
254 	 * Delegate Method
255 	 * 
256 	 * @return line count
257 	 * 
258 	 * @see Text#getLineCount()
259 	 */
260 	public final int getLineCount() {
261 		return text.getLineCount();
262 	}
263 
264 	/***
265 	 * Delegate Method
266 	 * 
267 	 * @return the line delimiter
268 	 * 
269 	 * @see Text#getLineDelimiter()
270 	 */
271 	public final String getLineDelimiter() {
272 		return text.getLineDelimiter();
273 	}
274 
275 	/***
276 	 * Delegate Method
277 	 * 
278 	 * @return line height
279 	 * 
280 	 * @see Text#getLineHeight()
281 	 */
282 	public final int getLineHeight() {
283 		return text.getLineHeight();
284 	}
285 
286 	/***
287 	 * Delegate Method
288 	 * 
289 	 * @return orientation
290 	 * 
291 	 * @see Text#getOrientation()
292 	 */
293 	public final int getOrientation() {
294 		return text.getOrientation();
295 	}
296 
297 	/***
298 	 * Delegate Method
299 	 * 
300 	 * @return point of selection
301 	 * 
302 	 * @see Text#getSelection()
303 	 */
304 	public final Point getSelection() {
305 		return text.getSelection();
306 	}
307 
308 	/***
309 	 * Delegate Method
310 	 * 
311 	 * @return selection count
312 	 * 
313 	 * @see Text#getSelectionCount()
314 	 */
315 	public final int getSelectionCount() {
316 		return text.getSelectionCount();
317 	}
318 
319 	/***
320 	 * Delegate Method
321 	 * 
322 	 * @return the selection text
323 	 * 
324 	 * @see Text#getSelectionText()
325 	 */
326 	public final String getSelectionText() {
327 		return text.getSelectionText();
328 	}
329 
330 	/***
331 	 * Delegate Method
332 	 * 
333 	 * @return tabsize
334 	 * 
335 	 * @see Text#getTabs()
336 	 */
337 	public final int getTabs() {
338 		return text.getTabs();
339 	}
340 
341 	/***
342 	 * Delegate Method
343 	 * 
344 	 * @return text
345 	 * 
346 	 * @see Text#getText()
347 	 */
348 	public final String getText() {
349 		return text.getText();
350 	}
351 
352 	/***
353 	 * Delegate Method
354 	 * 
355 	 * @param start - delegated to text
356 	 * @param end - delegated to text
357 	 * @return the Text on that position
358 	 * 
359 	 * @see Text#getText(int, int)
360 	 */
361 	public final String getText(int start, int end) {
362 		return text.getText(start, end);
363 	}
364 
365 	/***
366 	 * Delegate Method
367 	 * 
368 	 * @return textlimit
369 	 * 
370 	 * @see Text#getTextLimit()
371 	 */
372 	public final int getTextLimit() {
373 		return text.getTextLimit();
374 	}
375 
376 	/***
377 	 * Delegate Method
378 	 * 
379 	 * @return top Index
380 	 * 
381 	 * @see Text#getTopIndex()
382 	 */
383 	public final int getTopIndex() {
384 		return text.getTopIndex();
385 	}
386 
387 	/***
388 	 * Delegate Method
389 	 * 
390 	 * @return top Pixel
391 	 * 
392 	 * @see Text#getTopPixel()
393 	 */
394 	public final int getTopPixel() {
395 		return text.getTopPixel();
396 	}
397 
398 	/***
399 	 * Delegate Method
400 	 * 
401 	 * @param string - delegated to Text
402 	 * 
403 	 * @see Text#insert(java.lang.String)
404 	 */
405 	public final void insert(String string) {
406 		text.insert(string);
407 	}
408 
409 	/***
410 	 * Delegate Method
411 	 *
412 	 * @see Text#paste()
413 	 */
414 	public final void paste() {
415 		text.paste();
416 	}
417 
418 	/***
419 	 * removes the modyfy listener
420 	 * 
421 	 * @param listener - the listener which has to be removed from the listeners
422 	 * 
423 	 * @see Text#removeModifyListener(org.eclipse.swt.events.ModifyListener)
424 	 */
425 	public final void removeModifyListener(ModifyListener listener) {
426 		text.removeModifyListener(listener);
427 	}
428 
429 	/***
430 	 * removes the given selection listener
431 	 * 
432 	 * @param listener - the listener to be removed
433 	 * 
434 	 */
435 	public final void removeSelectionListener(SelectionListener listener) {
436 		text.removeSelectionListener(listener);
437 	}
438 
439 	/***
440 	 * removes the given verify listener
441 	 * 
442 	 * @param listener - the listener to be removed
443 	 */
444 	public final void removeVerifyListener(VerifyListener listener) {
445 		text.removeVerifyListener(listener);
446 	}
447 
448 	/***
449 	 * Delegate Method
450 	 * 
451 	 * @see Text#selectAll()
452 	 */
453 	public final void selectAll() {
454 		text.selectAll();
455 	}
456 
457 	/***
458 	 * Delegate Method
459 	 * 
460 	 * @param doubleClick - delegated to Text
461 	 * 
462 	 * @see Text#setDoubleClickEnabled(boolean)
463 	 */
464 	public final void setDoubleClickEnabled(boolean doubleClick) {
465 		text.setDoubleClickEnabled(doubleClick);
466 	}
467 
468 	/***
469 	 * Delegate Method
470 	 * 
471 	 * @param echo - delegated to Text
472 	 * 
473 	 * @see Text#setEchoChar(char)
474 	 */
475 	public final void setEchoChar(char echo) {
476 		text.setEchoChar(echo);
477 	}
478 
479 	/***
480 	 * Delegate Method
481 	 * 
482 	 * @param editable - delegated to Text
483 	 * 
484 	 * @see Text#setEditable(boolean)
485 	 */
486 	public final void setEditable(boolean editable) {
487 		text.setEditable(editable);
488 	}
489 
490 	/***
491 	 * Delegate Method
492 	 * 
493 	 * @param font - delegated to Text
494 	 * 
495 	 * @see Text#setFont(org.eclipse.swt.graphics.Font)
496 	 */
497 	public final void setFont(Font font) {
498 		text.setFont(font);
499 	}
500 
501 	/***
502 	 * Delegate Method
503 	 * 
504 	 * @param orientation - delegated to Text
505 	 * 
506 	 * @see Text#setOrientation(int)
507 	 */
508 	public final void setOrientation(int orientation) {
509 		text.setOrientation(orientation);
510 	}
511 
512 	/***
513 	 * Delegate Method
514 	 * 
515 	 * @param redraw - delegated to Text
516 	 * 
517 	 * @see Text#setRedraw(boolean)
518 	 */
519 	public final void setRedraw(boolean redraw) {
520 		text.setRedraw(redraw);
521 	}
522 	
523 	/***
524 	 * Delegate Method
525 	 * 
526 	 * @param start - delegated to Text
527 	 * 
528 	 * @see Text#setSelection(int)
529 	 */
530 	public final void setSelection(int start) {
531 		text.setSelection(start);
532 	}
533 
534 	/***
535 	 * Delegate Method
536 	 * 
537 	 * @param start - delegated to Text
538 	 * @param end - delegated to Text
539 	 * 
540 	 * @see Text#setSelection(int, int)
541 	 */
542 	public final void setSelection(int start, int end) {
543 		text.setSelection(start, end);
544 	}
545 
546 	/***
547 	 * Delegate Method
548 	 * 
549 	 * @param selection - delegated to Text
550 	 * 
551 	 * @see Text#setSelection(org.eclipse.swt.graphics.Point)
552 	 */
553 	public final void setSelection(Point selection) {
554 		text.setSelection(selection);
555 	}
556 
557 	/***
558 	 * Delegate Method
559 	 * 
560 	 * @param tabs - delegated to Text
561 	 * 
562 	 * @see Text#setTabs(int)
563 	 */
564 	public final void setTabs(int tabs) {
565 		text.setTabs(tabs);
566 	}
567 
568 	/***
569 	 * Delegate Method
570 	 * 
571 	 * @param string - delegated to Text
572 	 * 
573 	 * @see Text#setText(java.lang.String)
574 	 */
575 	public final void setText(String string) {
576 		text.setText(string);
577 	}
578 
579 	/***
580 	 * Delegate Method
581 	 * 
582 	 * @param limit - delegated to Text
583 	 * 
584 	 * @see Text#setTextLimit(int)
585 	 */
586 	public final void setTextLimit(int limit) {
587 		text.setTextLimit(limit);
588 	}
589 
590 	/***
591 	 * Delegate Method
592 	 * 
593 	 * @param index - delegated to Text
594 	 * 
595 	 * @see Text#setTopIndex(int)
596 	 */
597 	public final void setTopIndex(int index) {
598 		text.setTopIndex(index);
599 	}
600 
601 	/***
602 	 * Delegate Method
603 	 *
604 	 * @see Text#showSelection()
605 	 */
606 	public final void showSelection() {
607 		text.showSelection();
608 	}
609 
610 	/***
611 	 * Delegate Method
612 	 * 
613 	 * @param durationData - delegated to Text
614 	 * 
615 	 * @see org.eclipse.swt.widgets.Control#setLayoutData(java.lang.Object)
616 	 */	
617 	public final void setLayoutData(RowData durationData) {
618 		text.setLayoutData(durationData);
619 	}
620 
621 	/***
622 	 * Adds a listener to the focus event
623 	 * 
624 	 * @param listener - the listener to be added
625 	 */
626 	public final void addFocusListener(FocusListener listener) {
627 		TypedListener typedListener = new TypedListener(listener);
628 		text.addListener(SWT.FocusIn, typedListener);
629 		text.addListener(SWT.FocusOut, typedListener);
630 	}
631 
632 	/***
633 	 * Delegate Method
634 	 * 
635 	 * @param rect - delegated to Text
636 	 * 
637 	 * @see org.eclipse.swt.widgets.Control#setBounds(org.eclipse.swt.graphics.Rectangle)
638 	 */
639 	public final void setBounds(Rectangle rect) {
640 		text.setBounds(rect);
641 	}
642 
643 }