made the editor java 5 compliant (was java 6, unnecessarily)
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / CompositeBorderDecorator.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.swt.events.FocusEvent;
14 import org.eclipse.swt.events.FocusListener;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.ui.forms.IManagedForm;
18 import org.eclipse.ui.forms.widgets.FormToolkit;
19 import org.eclipse.ui.forms.widgets.ScrolledForm;
20
21 /**
22 * Draws a border on a Composite on an IManagedForm when it gets focus,
23 * undraws it when the Composite loses focus.
24 *
25 * @author p.ciardelli
26 *
27 */
28 public class CompositeBorderDecorator implements FocusListener {
29 private static final Logger logger = Logger.getLogger(CompositeBorderDecorator.class);
30
31 private FormToolkit toolkit;
32 private ScrolledForm scrolledForm;
33 private Composite borderedComposite;
34
35 /**
36 * If true, the <code>borderedComposite</code>'s border is erased when
37 * it loses focus.
38 */
39 private boolean doLoseFocus = true;
40
41 public CompositeBorderDecorator(Composite composite, IManagedForm form) {
42
43 this.borderedComposite = composite;
44 this.toolkit = form.getToolkit();
45 this.scrolledForm = form.getForm();
46
47 // Make sure composite retains its color scheme after being adapted to toolkit
48 Color backgroundColor = composite.getBackground();
49
50 toolkit.adapt(composite);
51
52 composite.setBackground(backgroundColor);
53
54 }
55
56
57 public void focusGained(FocusEvent e) {
58 paintBorder();
59 }
60
61
62 public void focusLost(FocusEvent e) {
63 if (doLoseFocus) {
64 unpaintBorder();
65 }
66 }
67
68 public void setBorderedComposite(Composite borderedComposite) {
69 this.borderedComposite = borderedComposite;
70 }
71
72 /**
73 * If this is set to true, border will disappear when focus is lost.
74 * Otherwise, it will remain until explicitly erased.
75 *
76 * @param doLoseFocus
77 */
78 public void setLoseFocus(boolean doLoseFocus) {
79 this.doLoseFocus = doLoseFocus;
80 }
81
82 /**
83 * Removes the border from <code>borderedComposite</code>.
84 */
85 public void unpaintBorder() {
86 paintBorder(null);
87 }
88
89 /**
90 * Adds a border to <code>borderedComposite</code>.
91 */
92 public void paintBorder() {
93 paintBorder(FormToolkit.TEXT_BORDER);
94 }
95
96 private void paintBorder(String border) {
97 borderedComposite.setData(FormToolkit.KEY_DRAW_BORDER, border);
98 toolkit.paintBordersFor(borderedComposite.getParent());
99 scrolledForm.reflow(false);
100 }
101 }