ref #9359 upgrade TaxEditor to log4j2
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / container / 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 package eu.etaxonomy.taxeditor.editor.name.container;
10
11 import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
12 import org.eclipse.jface.text.TextViewer;
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.ui.forms.IManagedForm;
17 import org.eclipse.ui.forms.widgets.ScrolledForm;
18
19 /**
20 * Checks whether the ScrolledForm a TextViewer is on needs to be
21 * redrawn because of a new line break or the removal of an existing
22 * one ...
23 * <p>
24 * ... in theory, anyway. Until I figure out how to detect a word wrap,
25 * the ScrolledForm is redrawn with every modify event.
26 *
27 * @author p.ciardelli
28 * @created 19.05.2008
29 */
30 public class LineWrapSupport {
31 private static final Logger logger = LogManager.getLogger(LineWrapSupport.class);
32
33 ModifyListener listener = new LineWrapListener();
34 int lineCount;
35
36 private StyledText textWidget;
37 private ScrolledForm scrolledForm;
38
39 // private TextViewer viewer;
40
41 /**
42 * <p>Constructor for LineWrapSupport.</p>
43 *
44 * @param viewer a {@link org.eclipse.jface.text.TextViewer} object.
45 * @param form a {@link org.eclipse.ui.forms.IManagedForm} object.
46 */
47 // public TextViewer getViewer() {
48 // return viewer;
49 // }
50 public LineWrapSupport(TextViewer viewer, IManagedForm form) {
51
52 if (viewer == null) {
53 throw new IllegalArgumentException(
54 "The provided TextViewer object is null."); //$NON-NLS-1$
55 }
56
57 // this.viewer = viewer;
58
59 textWidget = viewer.getTextWidget();
60 textWidget.addModifyListener(listener);
61
62 this.lineCount = textWidget.getLinePixel(textWidget.getLineCount());
63 this.scrolledForm = form.getForm();
64
65 logger.trace(this.getClass().getSimpleName() + " created"); //$NON-NLS-1$
66 }
67
68 /**
69 * Redraws the scrolledForm if a line wrap is detected.
70 */
71 private void checkLineCount() {
72
73 boolean lineWrapDetected = true;
74 if (lineWrapDetected ) {
75 scrolledForm.getBody().layout();
76 }
77 }
78
79 /**
80 * Calls <code>checkLineCount()</code> on a modify event.
81 *
82 * @author p.ciardelli
83 * @created 21.05.2008
84 */
85 class LineWrapListener implements ModifyListener {
86
87 @Override
88 public void modifyText(ModifyEvent e) {
89 checkLineCount();
90 }
91 }
92 }