Search in sources :

Example 1 with ElementIterator

use of javax.swing.text.ElementIterator in project energy3d by concord-consortium.

the class MyEditorPane method setText.

public void setText(final String text) {
    editorPane.setText(text);
    if (editorPane.getDocument() instanceof HTMLDocument) {
        final HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
        final ElementIterator it = new ElementIterator(doc);
        Element element;
        while ((element = it.next()) != null) {
            final AttributeSet as = element.getAttributes();
            final Enumeration<?> en = as.getAttributeNames();
            DefaultButtonModel buttonModel = null;
            PlainDocument document = null;
            String action = null;
            String question = null;
            String choice = null;
            String key = null;
            String dataName = null;
            while (en.hasMoreElements()) {
                final Object n = en.nextElement();
                final Object v = as.getAttribute(n);
                if (v instanceof DefaultButtonModel) {
                    buttonModel = (DefaultButtonModel) v;
                } else if (v instanceof PlainDocument) {
                    document = (PlainDocument) v;
                } else if (n.toString().equals("action")) {
                    action = v.toString();
                } else if (n.toString().equals("question")) {
                    question = v.toString();
                } else if (n.toString().equals("choice")) {
                    choice = v.toString();
                } else if (n.toString().equals("key")) {
                    key = v.toString();
                } else if (n.toString().equals("data")) {
                    dataName = v.toString();
                }
            }
            if (action != null) {
                final String a = action;
                if (document != null) {
                    final String n = dataName;
                    final PlainDocument d = document;
                    document.addDocumentListener(new DocumentListener() {

                        @Override
                        public void removeUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }

                        @Override
                        public void insertUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }

                        @Override
                        public void changedUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }
                    });
                }
                if (buttonModel != null) {
                    final QuestionnaireModel qm;
                    if (question != null && choice != null) {
                        boolean isKey = false;
                        if ("yes".equalsIgnoreCase(key) || "true".equalsIgnoreCase(key)) {
                            isKey = true;
                        }
                        qm = new QuestionnaireModel(question, choice, isKey);
                    } else {
                        qm = null;
                    }
                    final DefaultButtonModel bm = buttonModel;
                    if (buttonModel instanceof JToggleButton.ToggleButtonModel) {
                        buttonModel.addItemListener(new ItemListener() {

                            @Override
                            public void itemStateChanged(final ItemEvent e) {
                                if (qm != null) {
                                    // fire only one for questionnaires
                                    if (e.getStateChange() == ItemEvent.SELECTED) {
                                        buttonActionPerformed(a, qm, bm);
                                    }
                                } else {
                                    buttonActionPerformed(a, qm, bm);
                                }
                            }
                        });
                    } else {
                        buttonModel.addActionListener(new ActionListener() {

                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                buttonActionPerformed(a, qm, bm);
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : ElementIterator(javax.swing.text.ElementIterator) DocumentListener(javax.swing.event.DocumentListener) DefaultButtonModel(javax.swing.DefaultButtonModel) ItemEvent(java.awt.event.ItemEvent) HTMLDocument(javax.swing.text.html.HTMLDocument) ActionEvent(java.awt.event.ActionEvent) Element(javax.swing.text.Element) EventString(org.concord.energy3d.agents.EventString) PlainDocument(javax.swing.text.PlainDocument) DocumentEvent(javax.swing.event.DocumentEvent) ActionListener(java.awt.event.ActionListener) AttributeSet(javax.swing.text.AttributeSet) ItemListener(java.awt.event.ItemListener) QuestionnaireModel(org.concord.energy3d.agents.QuestionnaireModel)

Aggregations

ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 DefaultButtonModel (javax.swing.DefaultButtonModel)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1 AttributeSet (javax.swing.text.AttributeSet)1 Element (javax.swing.text.Element)1 ElementIterator (javax.swing.text.ElementIterator)1 PlainDocument (javax.swing.text.PlainDocument)1 HTMLDocument (javax.swing.text.html.HTMLDocument)1 EventString (org.concord.energy3d.agents.EventString)1 QuestionnaireModel (org.concord.energy3d.agents.QuestionnaireModel)1