Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / LineWrapSupport.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.jface.text.TextViewer;
14 import org.eclipse.swt.custom.StyledText;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.ui.forms.IManagedForm;
19 import org.eclipse.ui.forms.widgets.ScrolledForm;
20
21 /**
22 * Checks whether the ScrolledForm a TextViewer is on needs to be
23 * redrawn because of a new line break or the removal of an existing
24 * one ...
25 * <p>
26 * ... in theory, anyway. Until I figure out how to detect a word wrap,
27 * the ScrolledForm is redrawn with every modify event.
28 *
29 * @author p.ciardelli
30 * @created 19.05.2008
31 * @version 1.0
32 */
33 public class LineWrapSupport {
34 private static final Logger logger = Logger
35 .getLogger(LineWrapSupport.class);
36
37 ModifyListener listener = new LineWrapListener();
38 int lineCount;
39
40 private StyledText textWidget;
41 private ScrolledForm scrolledForm;
42
43 private TextViewer viewer;
44
45 public LineWrapSupport(TextViewer viewer, IManagedForm form) {
46
47 if (viewer == null) {
48 throw new IllegalArgumentException(
49 "The provided TextViewer object is null.");
50 }
51
52 this.viewer = viewer;
53
54 textWidget = viewer.getTextWidget();
55 textWidget.addModifyListener(listener);
56
57 this.lineCount = textWidget.getLinePixel(textWidget.getLineCount());
58 this.scrolledForm = form.getForm();
59 }
60
61 /**
62 * Redraws the scrolledForm if a line wrap is detected.
63 */
64 private void checkLineCount() {
65
66 boolean lineWrapDetected = true;
67 if (lineWrapDetected ) {
68 scrolledForm.getBody().layout();
69 }
70 }
71
72 /**
73 * Calls <code>checkLineCount()</code> on a modify event.
74 *
75 * @author p.ciardelli
76 * @created 21.05.2008
77 * @version 1.0
78 */
79 class LineWrapListener implements ModifyListener {
80
81 public void modifyText(ModifyEvent e) {
82 checkLineCount();
83 }
84 }
85 }