Search in sources :

Example 1 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class XMLViewer method updateProperties.

public void updateProperties() {
    Node node = (Node) treeView.getSelectedNode();
    if (node == null) {
    // no selection, but it's ok
    } else if (node instanceof TextNode) {
        TextNode textNode = (TextNode) node;
        textArea.setText(textNode.getText());
        propertiesCardPane.setSelectedIndex(1);
    } else if (node instanceof Element) {
        Element element = (Element) node;
        // Populate the namespaces table
        ArrayList<HashMap<String, String>> namespacesTableData = new ArrayList<>();
        String defaultNamespaceURI = element.getDefaultNamespaceURI();
        if (defaultNamespaceURI != null) {
            HashMap<String, String> row = new HashMap<>();
            row.put("prefix", "(default)");
            row.put("uri", defaultNamespaceURI);
            namespacesTableData.add(row);
        }
        Element.NamespaceDictionary namespaceDictionary = element.getNamespaces();
        for (String prefix : namespaceDictionary) {
            HashMap<String, String> row = new HashMap<>();
            row.put("prefix", prefix);
            row.put("uri", namespaceDictionary.get(prefix));
            namespacesTableData.add(row);
        }
        namespacesTableView.setTableData(namespacesTableData);
        // Populate the attributes table
        ArrayList<HashMap<String, String>> attributesTableData = new ArrayList<>();
        for (Element.Attribute attribute : element.getAttributes()) {
            HashMap<String, String> row = new HashMap<>();
            String attributeName = attribute.getName();
            row.put("name", attributeName);
            row.put("value", element.getElementDictionary().get(attributeName));
            attributesTableData.add(row);
        }
        attributesTableView.setTableData(attributesTableData);
        propertiesCardPane.setSelectedIndex(0);
    } else {
        throw new IllegalStateException();
    }
}
Also used : HashMap(org.apache.pivot.collections.HashMap) Node(org.apache.pivot.xml.Node) TextNode(org.apache.pivot.xml.TextNode) Element(org.apache.pivot.xml.Element) ArrayList(org.apache.pivot.collections.ArrayList) TextNode(org.apache.pivot.xml.TextNode)

Example 2 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class XMLSerializerTest method equalsTest.

@Test
public void equalsTest() throws IOException, SerializationException {
    XMLSerializer xmlSerializer = new XMLSerializer();
    XMLSerializerListener xmlSerializerListener = new XMLSerializerListener() {

        @Override
        public void beginElement(XMLSerializer xmlSerializerArgument, Element element) {
            System.out.println("Begin element: " + element);
        }

        @Override
        public void endElement(XMLSerializer xmlSerializerArgument) {
            System.out.println("End element");
        }

        @Override
        public void readTextNode(XMLSerializer xmlSerializerArgument, TextNode textNode) {
            System.out.println("Read text node: " + textNode);
        }
    };
    xmlSerializer.getXMLSerializerListeners().add(xmlSerializerListener);
    Element root1 = xmlSerializer.readObject(getClass().getResourceAsStream("sample.xml"));
    assertNotNull(root1);
    xmlSerializer.getXMLSerializerListeners().remove(xmlSerializerListener);
    Element root2 = xmlSerializer.readObject(getClass().getResourceAsStream("sample.xml"));
    assertNotNull(root2);
}
Also used : XMLSerializer(org.apache.pivot.xml.XMLSerializer) XMLSerializerListener(org.apache.pivot.xml.XMLSerializerListener) Element(org.apache.pivot.xml.Element) TextNode(org.apache.pivot.xml.TextNode) Test(org.junit.Test)

Example 3 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class RSSItemRenderer method render.

@Override
public void render(Object item, int index, ListView listView, boolean selected, Button.State state, boolean highlighted, boolean disabled) {
    if (item != null) {
        Element itemElement = (Element) item;
        String title = XML.getText(itemElement, "title");
        titleLabel.setText(title);
        String categories = "Categories:";
        List<Element> categoryElements = itemElement.getElements("category");
        for (int i = 0, n = categoryElements.getLength(); i < n; i++) {
            Element categoryElement = categoryElements.get(i);
            TextNode categoryTextNode = (TextNode) categoryElement.get(0);
            String category = categoryTextNode.getText();
            if (i > 0) {
                categories += ", ";
            }
            categories += category;
        }
        categoriesLabel.setText(categories);
        String submitter = XML.getText(itemElement, "dz:submitter/dz:username");
        submitterLabel.setText("Submitter: " + submitter);
    }
    Font font = listView.getStyles().getFont(Style.font);
    Font largeFont = font.deriveFont(Font.BOLD, 14);
    titleLabel.getStyles().put(Style.font, largeFont);
    categoriesLabel.getStyles().put(Style.font, font);
    submitterLabel.getStyles().put(Style.font, font);
    Color color;
    if (listView.isEnabled() && !disabled) {
        if (selected) {
            if (listView.isFocused()) {
                color = listView.getStyles().getColor(Style.selectionColor);
            } else {
                color = listView.getStyles().getColor(Style.inactiveSelectionColor);
            }
        } else {
            color = listView.getStyles().getColor(Style.color);
        }
    } else {
        color = listView.getStyles().getColor(Style.disabledColor);
    }
    titleLabel.getStyles().put(Style.color, color);
    categoriesLabel.getStyles().put(Style.color, color);
    submitterLabel.getStyles().put(Style.color, color);
}
Also used : Element(org.apache.pivot.xml.Element) Color(java.awt.Color) TextNode(org.apache.pivot.xml.TextNode) Font(java.awt.Font)

Example 4 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class NodeRenderer method toString.

@Override
public String toString(Object node) {
    String string;
    if (node instanceof Element) {
        Element element = (Element) node;
        string = element.getName();
    } else if (node instanceof TextNode) {
        TextNode textNode = (TextNode) node;
        string = textNode.getText();
    } else {
        throw new IllegalArgumentException("Unknown node type: " + node.getClass().getName());
    }
    return string;
}
Also used : Element(org.apache.pivot.xml.Element) TextNode(org.apache.pivot.xml.TextNode)

Example 5 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class NodeRenderer method render.

@Override
public void render(Object node, Sequence.Tree.Path path, int rowIndex, TreeView treeView, boolean expanded, boolean selected, TreeView.NodeCheckState checkState, boolean highlighted, boolean disabled) {
    if (node != null) {
        String text;
        if (node instanceof Element) {
            Element element = (Element) node;
            text = "<" + element.getName() + ">";
        } else if (node instanceof TextNode) {
            TextNode textNode = (TextNode) node;
            text = textNode.getText();
            if (text.length() > MAXIMUM_TEXT_LENGTH) {
                text = "\"" + text.substring(0, MAXIMUM_TEXT_LENGTH) + "\"...";
            } else {
                text = "\"" + text + "\"";
            }
        } else {
            throw new IllegalArgumentException("Unknown node type: " + node.getClass().getName());
        }
        setText(text);
        Font font = treeView.getStyles().getFont(Style.font);
        getStyles().put(Style.font, font);
        Color color;
        if (treeView.isEnabled() && !disabled) {
            if (selected) {
                if (treeView.isFocused()) {
                    color = treeView.getStyles().getColor(Style.selectionColor);
                } else {
                    color = treeView.getStyles().getColor(Style.inactiveSelectionColor);
                }
            } else {
                color = treeView.getStyles().getColor(Style.color);
            }
        } else {
            color = treeView.getStyles().getColor(Style.disabledColor);
        }
        getStyles().put(Style.color, color);
    }
}
Also used : Element(org.apache.pivot.xml.Element) Color(java.awt.Color) TextNode(org.apache.pivot.xml.TextNode) Font(java.awt.Font)

Aggregations

Element (org.apache.pivot.xml.Element)7 TextNode (org.apache.pivot.xml.TextNode)5 XMLSerializer (org.apache.pivot.xml.XMLSerializer)3 Color (java.awt.Color)2 Font (java.awt.Font)2 Test (org.junit.Test)2 Desktop (java.awt.Desktop)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (org.apache.pivot.collections.ArrayList)1 HashMap (org.apache.pivot.collections.HashMap)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 GetQuery (org.apache.pivot.web.GetQuery)1 Component (org.apache.pivot.wtk.Component)1 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)1 Mouse (org.apache.pivot.wtk.Mouse)1 Node (org.apache.pivot.xml.Node)1