在新的Composite的初始构造期间,字体不起作用

时间:2012-10-04 14:18:23

标签: java swt eclipse-rcp

我希望有一个Text小部件,当用户尚未在该字段中输入值时,该小部件可以在其中显示消息。我扩展了复合并基本上包含了一个文本字段。添加了焦点侦听器以删除焦点上的消息,并在焦点丢失时替换消息(如果该字段为空)。这一切都按预期工作。

我遇到的问题是,当文本字段放在文本字段中时,我希望提示的样式不同。该字体似乎最初没有被使用。一旦该场具有焦点并失去焦点,它看起来是正确的。

例如,这是它最初加载时的外观:
Text is not styled correctly

这就是它应该如何看待初始载荷以及它在失去焦点后的看法:
Text is styled correctly

它变得有点陌生,因为当我在一个简单的shell中运行它时,它应该如何工作。当我将它作为Eclipse应用程序运行时,它无法正确设置样式。

以下是我的复合材料的代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

/**
 * The <code>PromptingTextInput</code> component is a small enhancement to
 * standard <code>Text</code>. It adds the ability to specify a prompt value
 * that displays when the text is empty and the field does not have focus.
 */
public class PromptingTextInput extends Composite {

    private String prompt;
    private Text input;
    private boolean textEmpty;

    Font emptyFont;
    Font inputFont;

    public PromptingTextInput(String prompt, Composite parent, int style, boolean passwordField) {
        super(parent, style);

        this.prompt = prompt;
        setLayout(new FillLayout());

        this.textEmpty = true;
        this.input = new Text(this, (passwordField ? SWT.PASSWORD : SWT.NONE));
        setEmptyInputStyle();

        this.input.setText(this.prompt);
        this.input.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                PromptingTextInput.this.focusGained();
            }

            public void focusLost(FocusEvent e) {
                PromptingTextInput.this.focusLost();
            }
        });
        addDisposeListener(new DisposeListener() {
             public void widgetDisposed(DisposeEvent e) {
                 disposeFonts();
             }
         });
    }   

    protected void focusGained() {
        if (this.textEmpty) {
            this.input.setText("");
            setInputStyle();
        }
    }

    protected void focusLost() {
        if (input.getText() == null || input.getText().trim().length() == 0) {
            this.input.setText(this.prompt);
            setEmptyInputStyle();
            this.textEmpty = true;
        } else {
            this.textEmpty = false;
        }
    }

    protected void setInputStyle() {    
        if (this.inputFont == null){
            this.inputFont = new Font(Display.getCurrent(), "Verdana", 8, SWT.DEFAULT);
        }
        this.input.setFont(this.inputFont);
        this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
    }

    protected void setEmptyInputStyle() {   
        if (this.emptyFont == null){
            this.emptyFont = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
        }
        this.input.setFont(this.emptyFont);
        this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
    }

    public String getPrompt() {
        return prompt;
    }

    public void setPrompt(String prompt) {
        this.prompt = prompt;
        if(!this.input.isFocusControl()){
            this.input.setText(this.prompt);
            setEmptyInputStyle();
        }
    }

    public Text getInput() {
        return input;
    }

    public boolean isTextEmpty() {
        return textEmpty;
    }

    public String getText() {
        return this.input.getText();
    }

    public void addModifyListener (ModifyListener listener) {
        this.input.addModifyListener(listener);
    }

    public void disposeFonts(){
        if (this.inputFont != null){
            this.inputFont.dispose();
        }
        if (this.emptyFont != null){
            this.emptyFont.dispose();
        }
    }   
}

更新:正如Baz所说,这不是Indigo中的问题,而且似乎只是Juno中的E4应用程序的问题。

2 个答案:

答案 0 :(得分:1)

根据sambi的回答,我得到了以下工作。也许这在Juno中有效:

private static Font italic;

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1,false));

    italic = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);

    final Text text = new Text(shell, SWT.BORDER);

    text.addListener(SWT.Paint, new Listener() {

        @Override
        public void handleEvent(Event event) {

            if(text.getText().length() < 1 && !text.isFocusControl())
            {
                GC gc = event.gc;
                gc.setFont(italic);
                gc.setForeground(display.getSystemColor(SWT.COLOR_GRAY));

                Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);

                /* Strangely the y positioning doesn't work correctly */
                //gc.drawText("Please enter text", 1, (size.y / 2) - (italic.getFontData()[0].getHeight() / 2));
                gc.drawText("Please enter text", 1, 4);
            }
        }
    });

    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Dummy");

    button.forceFocus();

    shell.setSize(200, 100);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    italic.dispose();
    display.dispose();
}

没有焦点和空白文字:

enter image description here

使用焦点或文字:

enter image description here

答案 1 :(得分:1)

您可以使用Text.setMessage (String message)。这个问题是你将无法使用字体大小和前景等进行自定义。

要自定义它,您实际上可以在Text

的范围内绘制消息