JTextPane с пользовательским документом и редактором неправильно отображает значки - PullRequest
2 голосов
/ 20 октября 2011

Мы реализуем очень маленькое приложение, похожее на мессенджер, на Java и используем JTextPane для нашей панели сообщений. Теперь мы пытаемся заменить обычные смайлики файлами .gif и используем некоторый пример, который делает эту работу ... за исключением того, что значки выровнены по центру, и это раздражает. Есть ли способ заставить его выглядеть встроенным (выравнивание по левому краю или нормальное)? Некоторая помощь в решении этой проблемы была бы оценена.

Вот код инициализации:

messagePane = new JTextPane();
messagePane.setEditable(false);
messagePane.setEditorKit(new WrapHTMLEditorKit());
messagePane.setContentType("text/html");
MessageDocument doc = new MessageDocument();
doc.addDocumentListener(new SmileyDocumentListener(messagePane));
messagePane.setStyledDocument(doc);

Класс MessageDocument:

public class MessageDocument extends HTMLDocument {

    static private String NEW_LINE = System.getProperty("line.separator");

    static private SimpleAttributeSet DEFAULT_STYLE = new SimpleAttributeSet();
    static private SimpleAttributeSet ANNOUNCE_GLOBAL_STYLE = new SimpleAttributeSet();
    static private SimpleAttributeSet MESSAGE_GLOBAL_STYLE = new SimpleAttributeSet();
    static private SimpleAttributeSet MESSAGE_PRIVATE_STYLE = new SimpleAttributeSet();
    static private SimpleAttributeSet ERROR_STYLE = new SimpleAttributeSet();

    static{
        StyleConstants.setFontFamily(DEFAULT_STYLE, "Courier New");
        StyleConstants.setItalic(DEFAULT_STYLE, true);
        StyleConstants.setForeground(DEFAULT_STYLE, Color.black);       

        StyleConstants.setFontFamily(ANNOUNCE_GLOBAL_STYLE, "Courier New");
        StyleConstants.setBold(ANNOUNCE_GLOBAL_STYLE, true);
        StyleConstants.setForeground(ANNOUNCE_GLOBAL_STYLE, new Color(0, 100, 0));      

        StyleConstants.setFontFamily(MESSAGE_GLOBAL_STYLE, "Courier New");
        StyleConstants.setForeground(MESSAGE_GLOBAL_STYLE, Color.blue);

        StyleConstants.setFontFamily(MESSAGE_PRIVATE_STYLE, "Courier New");
        StyleConstants.setForeground(MESSAGE_PRIVATE_STYLE, Color.black);
        StyleConstants.setBackground(MESSAGE_PRIVATE_STYLE, new Color(220, 220, 255));

        StyleConstants.setFontFamily(ERROR_STYLE, "Courier New");
        StyleConstants.setForeground(ERROR_STYLE, Color.yellow);
        StyleConstants.setBackground(ERROR_STYLE, Color.red);

    }


    public MessageDocument() {
        super();
    }

    public void addMessage(ServerMessage msg) {
        SimpleAttributeSet style;

        if (MessageType.ANNOUNCE_GLOBAL.equals(msg.getType()) || MessageType.CONNECTION_EVENT.equals(msg.getType()) || MessageType.DISCONNECTION_EVENT.equals(msg.getType())) {
            style = ANNOUNCE_GLOBAL_STYLE;
        } else if (MessageType.MESSAGE_GLOBAL.equals(msg.getType())) {
            style = MESSAGE_GLOBAL_STYLE;
        } else if (MessageType.MESSAGE_PRIVATE.equals(msg.getType())) {
            style = MESSAGE_PRIVATE_STYLE;
        } else if (MessageType.ERROR.equals(msg.getType())) {
            style = ERROR_STYLE;
        } else {
            style = DEFAULT_STYLE;
        }

        try {
            insertString(getLength(), msg.getMessage() + NEW_LINE, style);          
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}

Класс WrapHTMLEditorKit:

public class WrapHTMLEditorKit extends HTMLEditorKit {

    @Override
    public ViewFactory getViewFactory() {

        return new HTMLFactory() {
            public View create(Element e) {
                View v = super.create(e);
                if (v instanceof InlineView) {
                    return new InlineView(e) {
                        public int getBreakWeight(int axis, float pos, float len) {
                            return GoodBreakWeight;
                        }

                        public View breakView(int axis, int p0, float pos, float len) {
                            if (axis == View.X_AXIS) {
                                checkPainter();
                                int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
                                if (p0 == getStartOffset() && p1 == getEndOffset()) {
                                    return this;
                                }
                                return createFragment(p0, p1);
                            }
                            return this;
                        }
                    };
                } else if (v instanceof ParagraphView) {
                    return new ParagraphView(e) {
                        protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
                            if (r == null) {
                                r = new SizeRequirements();
                            }
                            float pref = layoutPool.getPreferredSpan(axis);
                            float min = layoutPool.getMinimumSpan(axis);
                            // Don't include insets, Box.getXXXSpan will include them.
                            r.minimum = (int) min;
                            r.preferred = Math.max(r.minimum, (int) pref);
                            r.maximum = Integer.MAX_VALUE;
                            r.alignment = 0.5f;
                            return r;
                        }

                    };
                }
                return v;
            }
        };
    }
}

И, наконец, класс SmileyDocumentListener:

public class SmileyDocumentListener implements DocumentListener {

    private JTextComponent owner;

    private HashMap<String,ImageIcon> smileMap;

    public SmileyDocumentListener(JTextComponent owner) {
        this.owner = owner;
        this.smileMap = new HashMap<String, ImageIcon>();

        this.smileMap.put(":)", new ImageIcon("resources/images/smileys/smile.gif"));
    }

    @Override
    public void insertUpdate(DocumentEvent event) {
        final DocumentEvent e = event;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (e.getDocument() instanceof StyledDocument) {
                    try {
                        StyledDocument doc=(StyledDocument)e.getDocument();
                        int start = Utilities.getRowStart(owner, Math.max(0, e.getOffset() - 1));
                        int end = Utilities.getWordStart(owner, e.getOffset() + e.getLength());
                        String text = doc.getText(start, end-start);

                        for (String token : smileMap.keySet()) {
                            int i = text.indexOf(token);
                            while (i >= 0) {
                                final SimpleAttributeSet attrs = new SimpleAttributeSet(doc.getCharacterElement(start + i).getAttributes());
                                if (StyleConstants.getIcon(attrs) == null) {
                                    StyleConstants.setIcon(attrs, smileMap.get(token));
                                    doc.remove(start + i, 2);
                                    doc.insertString(start + i, token, attrs);
                                }
                                i = text.indexOf(token, i+token.length());
                            }
                        }
                    } catch (BadLocationException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void removeUpdate(DocumentEvent e) {}

    @Override
    public void changedUpdate(DocumentEvent e) {}

}

Примечание: , который я пытался изменить r.alignment = 0.5f; на 0.0f в WrapHTMLEditorKit.java

Ответы [ 2 ]

2 голосов
/ 20 октября 2011

Вы можете попробовать вставить HTML-тег "image". Выделите текст и примените какой-либо символьный атрибут (например, полужирный), чтобы создать отдельный элемент листа. Затем замените HTML-код элемента тегом IMG.

1 голос
/ 21 октября 2011

Хорошо, поэтому я не знаю, почему значки отображаются таким образом в JTextPane с использованием типа контента text/html (и я попытался использовать JLabel для отображения значка, прямой настройки HTML и т. Д.В итоге я подумал, что HTML необходим.

Я заменил

public class MessageDocument extends HTMLDocument {

на

public class MessageDocument extends StyledDocument {

Я удалил WrapHTMLEditorKit и изменил код инициализации с помощью

messagePane = new JTextPane();
messagePane.setEditable(false);
messagePane.setEditorKit(new StyledEditorKit());
MessageDocument doc = new MessageDocument();
doc.addDocumentListener(new SmileyDocumentListener(messagePane));
messagePane.setStyledDocument(doc);

и значки отображаются нормально.

Содержимое переносится без специального класса, и я все еще могу использовать SimpleAttributeSet объекты.

...