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