2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
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
;
24 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
25 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
28 * Displays a message such as "Click here to start entering
29 * data" when <code>document</code> is empty.
35 public class EmptyTextViewerPrompt
implements IDocumentListener
{
36 private static final Logger logger
= Logger
37 .getLogger(EmptyTextViewerPrompt
.class);
39 private IDocument document
;
40 private StyledText textControl
;
41 private String prompt
;
42 private FocusListener focusListener
= null;
44 private Font textFont
;
45 private Font promptFont
= TaxEditorPlugin
.getDefault()
46 .getFont(ITaxEditorConstants
.DEFAULT_PROMPT_FONT
);
48 public EmptyTextViewerPrompt(TextViewer textViewer
, String prompt
) {
50 this.document
= textViewer
.getDocument();
51 this.textControl
= textViewer
.getTextWidget();
54 this.textFont
= textControl
.getFont();
56 documentChanged(null);
60 * Override <code>prompt</code>'s default font.
64 public void setPromptFont(Font font
) {
65 this.promptFont
= font
;
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.
73 private void createPrompt() {
75 textControl
.setFont(promptFont
);
78 focusListener
= new FocusListener() {
80 public void focusGained(FocusEvent e
) {
81 if (document
.get().equals(prompt
)) {
82 textControl
.setFont(textFont
);
87 public void focusLost(FocusEvent e
) {
88 if (document
.getLength() == 0) {
89 textControl
.setFont(promptFont
);
95 textControl
.addFocusListener(focusListener
);
96 textControl
.addDisposeListener(new DisposeListener() {
99 public void widgetDisposed(DisposeEvent e
) {
100 focusListener
= null;
107 * Removes focus listener from <code>textControl</code>, turning off
108 * the showing of <code>prompt</code>.
110 private void removePrompt() {
111 if (focusListener
!= null) {
112 textControl
.removeFocusListener(focusListener
);
113 focusListener
= null;
118 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
120 public void documentAboutToBeChanged(DocumentEvent event
) {}
123 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
125 public void documentChanged(DocumentEvent event
) {
127 if (document
.get().equals(prompt
)) {
131 if (document
.getLength() == 0) {