migrating to cdmlib-plugin 2.0.0.20 including new term loading
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / EmptyTextViewerPrompt.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.DocumentEvent;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IDocumentListener;
16 import org.eclipse.jface.text.TextViewer;
17 import org.eclipse.swt.custom.StyledText;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.FocusEvent;
21 import org.eclipse.swt.events.FocusListener;
22 import org.eclipse.swt.graphics.Font;
23
24 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
25 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
26
27 /**
28 * Displays a message such as "Click here to start entering
29 * data" when <code>document</code> is empty.
30 *
31 * @author p.ciardelli
32 * @created 15.09.2008
33 * @version 1.0
34 */
35 public class EmptyTextViewerPrompt implements IDocumentListener {
36 private static final Logger logger = Logger
37 .getLogger(EmptyTextViewerPrompt.class);
38
39 private IDocument document;
40 private StyledText textControl;
41 private String prompt;
42 private FocusListener focusListener = null;
43
44 private Font textFont;
45 private Font promptFont = TaxEditorPlugin.getDefault()
46 .getFont(ITaxEditorConstants.DEFAULT_PROMPT_FONT);
47
48 public EmptyTextViewerPrompt(TextViewer textViewer, String prompt) {
49
50 this.document = textViewer.getDocument();
51 this.textControl = textViewer.getTextWidget();
52 this.prompt = prompt;
53
54 this.textFont = textControl.getFont();
55
56 documentChanged(null);
57 }
58
59 /**
60 * Override <code>prompt</code>'s default font.
61 *
62 * @param font
63 */
64 public void setPromptFont(Font font) {
65 this.promptFont = font;
66 }
67
68 /**
69 * Adds a focus listener to the <code>textControl</code>, which hides
70 * the <code>prompt</code> if the cursor is in the <code>textControl</code>,
71 * and shows it if the cursor is elsewhere.
72 */
73 private void createPrompt() {
74
75 textControl.setFont(promptFont);
76 document.set(prompt);
77
78 focusListener = new FocusListener() {
79
80 public void focusGained(FocusEvent e) {
81 if (document.get().equals(prompt)) {
82 textControl.setFont(textFont);
83 document.set("");
84 }
85 }
86
87 public void focusLost(FocusEvent e) {
88 if (document.getLength() == 0) {
89 textControl.setFont(promptFont);
90 document.set(prompt);
91 }
92 }
93
94 };
95 textControl.addFocusListener(focusListener);
96 textControl.addDisposeListener(new DisposeListener() {
97
98
99 public void widgetDisposed(DisposeEvent e) {
100 focusListener = null;
101 }
102
103 });
104 }
105
106 /**
107 * Removes focus listener from <code>textControl</code>, turning off
108 * the showing of <code>prompt</code>.
109 */
110 private void removePrompt() {
111 if (focusListener != null) {
112 textControl.removeFocusListener(focusListener);
113 focusListener = null;
114 }
115 }
116
117 /* (non-Javadoc)
118 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
119 */
120 public void documentAboutToBeChanged(DocumentEvent event) {}
121
122 /* (non-Javadoc)
123 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
124 */
125 public void documentChanged(DocumentEvent event) {
126
127 if (document.get().equals(prompt)) {
128 return;
129 }
130
131 if (document.getLength() == 0) {
132 createPrompt();
133 } else {
134 removePrompt();
135 }
136 }
137 }