JFace消息对话框:内联文本着色

时间:2018-03-27 14:33:31

标签: java dialog swt jface

我想要做的是打开一个确认对话框,其中一些文字是彩色的。

示例:

MessageDialog.openConfirm(shell, "title", "message a b c foobar");

假设我想在普通黑色中显示“message”和“foobar”,但“a”,“b”和“c”应该是不同颜色的(例如红色,绿色,蓝色)。
像这样:enter image description here

是否可以使用MessageDialog.openConfirm?如果是的话,如何,如果没有,还有另一种方法来实现这一目标吗?

1 个答案:

答案 0 :(得分:3)

我建议创建MessageDialog的子类并使用StyledText作为消息。与评论建议一样,您可以覆盖createMessageArea(Composite)方法。您可以借用IconAndMessageDialog中的代码,而不是Label,使用构造函数中给出的StyleRange对象创建StyledText

@Override
protected Control createMessageArea(final Composite composite) {
    // ...
    if (message != null) {
        styledText = new StyledText(composite, SWT.NONE);
        styledText.setEditable(false);
        styledText.setEnabled(false);
        styledText.setBackground(composite.getBackground());
        styledText.setText(message);
        styledText.setStyleRanges(styleRanges);
        GridDataFactory.fillDefaults()
                .align(SWT.FILL, SWT.BEGINNING)
                .grab(true, false)
                .hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT)
                .applyTo(styledText);
    }
    // ...
}

您还可以创建一个与MessageDialog.openConfirm(Shell, String, String)相当的新静态方法:

public static boolean openConfirm(final Shell parent, final String title, final String message, final StyleRange[] styleRanges) {
    final MessageDialog dialog = new StyledTextMessageDialog(parent, title, message, styleRanges);
    return dialog.open() == 0;
}

enter image description here

完整StyledTextMessageDialog课程:

public class StyledTextMessageDialog extends MessageDialog {

    private static final String[] DIALOG_BUTTON_LABELS = new String[]{
            IDialogConstants.OK_LABEL,
            IDialogConstants.CANCEL_LABEL
    };
    private static final int DEFAULT_INDEX = 0;

    private final StyleRange[] styleRanges;
    private StyledText styledText;

    public StyledTextMessageDialog(final Shell parent, final String title, final String message, final StyleRange[] styleRanges) {
        super(parent, title, null, message, MessageDialog.QUESTION, DIALOG_BUTTON_LABELS, DEFAULT_INDEX);
        if (styleRanges == null) {
            throw new IllegalArgumentException("style ranges cannot be null");
        }
        this.styleRanges = styleRanges;
    }

    @Override
    protected Control createMessageArea(final Composite composite) {
        final Image image = getQuestionImage();
        if (image != null) {
            imageLabel = new Label(composite, SWT.NONE);
            image.setBackground(imageLabel.getBackground());
            imageLabel.setImage(image);
            addAccessibleListeners(imageLabel);
            GridDataFactory.fillDefaults()
                    .align(SWT.CENTER, SWT.BEGINNING)
                    .applyTo(imageLabel);
        }
        if (message != null) {
            styledText = new StyledText(composite, SWT.NONE);
            styledText.setEditable(false);
            styledText.setEnabled(false);
            styledText.setBackground(composite.getBackground());
            styledText.setText(message);
            styledText.setStyleRanges(styleRanges);
            GridDataFactory.fillDefaults()
                    .align(SWT.FILL, SWT.BEGINNING)
                    .grab(true, false)
                    .hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT)
                    .applyTo(styledText);
        }
        return composite;
    }

    private void addAccessibleListeners(final Label label) {
        label.getAccessible().addAccessibleListener(new AccessibleAdapter() {
            public void getName(final AccessibleEvent event) {
                final String accessibleMessage = JFaceResources.getString("question");
                if (accessibleMessage != null) {
                    event.result = accessibleMessage;
                }
            }
        });
    }

    public static boolean openConfirm(final Shell parent, final String title, final String message, final StyleRange[] styleRanges) {
        final MessageDialog dialog = new StyledTextMessageDialog(parent, title, message, styleRanges);
        return dialog.open() == 0;
    }

}

请注意,私有方法取自IconAndMessageDialog,因为我们需要一个确认/问题对话框,所以会有一些自由。

使用示例:

public class StyledTextMessageDialogTest {

    public static void main(String... args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        final String title = "Hello, stylish dialog!";
        final String message = "message a b c foobar";
        final StyleRange[] styleRanges = new StyleRange[]{
                new StyleRange(8, 1, display.getSystemColor(SWT.COLOR_RED), null),
                new StyleRange(10, 1, display.getSystemColor(SWT.COLOR_GREEN), null),
                new StyleRange(12, 1, display.getSystemColor(SWT.COLOR_BLUE), null)
        };
        StyledTextMessageDialog.openConfirm(shell, title, message, styleRanges);
    }

}