You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/22 16:46:23 UTC

svn commit: r767548 - in /incubator/pivot/trunk/demos/src/pivot/demos/rss: ./ NodeListAdapter.java RSSFeedDemo.java rss_feed_demo.wtkx

Author: gbrown
Date: Wed Apr 22 14:46:22 2009
New Revision: 767548

URL: http://svn.apache.org/viewvc?rev=767548&view=rev
Log: (empty)

Added:
    incubator/pivot/trunk/demos/src/pivot/demos/rss/
    incubator/pivot/trunk/demos/src/pivot/demos/rss/NodeListAdapter.java
    incubator/pivot/trunk/demos/src/pivot/demos/rss/RSSFeedDemo.java
    incubator/pivot/trunk/demos/src/pivot/demos/rss/rss_feed_demo.wtkx

Added: incubator/pivot/trunk/demos/src/pivot/demos/rss/NodeListAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/demos/src/pivot/demos/rss/NodeListAdapter.java?rev=767548&view=auto
==============================================================================
--- incubator/pivot/trunk/demos/src/pivot/demos/rss/NodeListAdapter.java (added)
+++ incubator/pivot/trunk/demos/src/pivot/demos/rss/NodeListAdapter.java Wed Apr 22 14:46:22 2009
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2009 VMware, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.demos.rss;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import pivot.collections.List;
+import pivot.collections.ListListener;
+import pivot.collections.Sequence;
+import pivot.util.ListenerList;
+
+/**
+ * Wraps a W3C DOM {@link org.w3c.dom.NodeList} in a list collection.
+ *
+ * @author gbrown
+ */
+public class NodeListAdapter implements List<Node> {
+    public class NodeListIterator implements Iterator<Node> {
+        private int index = 0;
+
+        public boolean hasNext() {
+            return (index < nodeList.getLength());
+        }
+
+        public Node next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+
+            return nodeList.item(index++);
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    private NodeList nodeList;
+
+    private ListListenerList<Node> listListeners = new ListListenerList<Node>();
+
+    public NodeListAdapter(NodeList nodeList) {
+        if (nodeList == null) {
+            throw new IllegalArgumentException();
+        }
+
+        this.nodeList = nodeList;
+    }
+
+    public int add(Node node) {
+        throw new UnsupportedOperationException();
+    }
+
+    public void insert(Node node, int index) {
+        throw new UnsupportedOperationException();
+    }
+
+    public int remove(Node node) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Sequence<Node> remove(int index, int count) {
+        throw new UnsupportedOperationException();
+    }
+
+    public void clear() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Node update(int index, Node node) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Node get(int index) {
+        return nodeList.item(index);
+    }
+
+    public int indexOf(Node node) {
+        int index = 0;
+        while (index < nodeList.getLength()
+            && node != nodeList.item(index)) {
+            index++;
+        }
+
+        return (index < nodeList.getLength()) ? index : -1;
+    }
+
+    public int getLength() {
+        return nodeList.getLength();
+    }
+
+    public Comparator<Node> getComparator() {
+        return null;
+    }
+
+    public void setComparator(Comparator<Node> comparator) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Iterator<Node> iterator() {
+        return new NodeListIterator();
+    }
+
+    public ListenerList<ListListener<Node>> getListListeners() {
+        return listListeners;
+    }
+}

Added: incubator/pivot/trunk/demos/src/pivot/demos/rss/RSSFeedDemo.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/demos/src/pivot/demos/rss/RSSFeedDemo.java?rev=767548&view=auto
==============================================================================
--- incubator/pivot/trunk/demos/src/pivot/demos/rss/RSSFeedDemo.java (added)
+++ incubator/pivot/trunk/demos/src/pivot/demos/rss/RSSFeedDemo.java Wed Apr 22 14:46:22 2009
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2009 VMware, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.demos.rss;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Iterator;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+
+import pivot.collections.Dictionary;
+import pivot.wtk.Application;
+import pivot.wtk.BrowserApplicationContext;
+import pivot.wtk.Component;
+import pivot.wtk.ComponentMouseButtonListener;
+import pivot.wtk.Display;
+import pivot.wtk.FlowPane;
+import pivot.wtk.Insets;
+import pivot.wtk.Label;
+import pivot.wtk.ListView;
+import pivot.wtk.Mouse;
+import pivot.wtk.Orientation;
+import pivot.wtk.Window;
+import pivot.wtkx.WTKXSerializer;
+
+/**
+ * RSS feed demo application.
+ *
+ * @author gbrown
+ */
+public class RSSFeedDemo implements Application {
+    /**
+     * Prepares an RSS feed news item for presentation in a list view.
+     *
+     * @author gbrown
+     */
+    private class RSSItemRenderer extends FlowPane implements ListView.ItemRenderer {
+        private Label titleLabel = new Label();
+        private Label categoriesHeadingLabel = new Label("subject:");
+        private Label categoriesLabel = new Label();
+        private Label submitterHeadingLabel = new Label("submitter:");
+        private Label submitterLabel = new Label();
+
+        public RSSItemRenderer() {
+            super(Orientation.VERTICAL);
+
+            getStyles().put("padding", new Insets(2, 2, 8, 2));
+
+            add(titleLabel);
+
+            FlowPane categoriesFlowPane = new FlowPane();
+            add(categoriesFlowPane);
+
+            categoriesFlowPane.add(categoriesHeadingLabel);
+            categoriesFlowPane.add(categoriesLabel);
+
+            FlowPane submitterFlowPane = new FlowPane();
+            add(submitterFlowPane);
+
+            submitterFlowPane.add(submitterHeadingLabel);
+            submitterFlowPane.add(submitterLabel);
+        }
+
+        @Override
+        public void setSize(int width, int height) {
+            super.setSize(width, height);
+
+            // Since this component doesn't have a parent, it won't be validated
+            // via layout; ensure that it is valid here
+            validate();
+        }
+
+        public void render(Object item, ListView listView, boolean selected,
+            boolean checked, boolean highlighted, boolean disabled) {
+            Element itemElement = (Element)item;
+
+            try {
+                String title = (String)xpath.evaluate("title", itemElement, XPathConstants.STRING);
+                titleLabel.setText(title);
+
+                String categories = "";
+                NodeList categoryNodeList = (NodeList)xpath.evaluate("category", itemElement,
+                    XPathConstants.NODESET);
+                for (int j = 0; j < categoryNodeList.getLength(); j++) {
+                    Element categoryElement = (Element)categoryNodeList.item(j);
+                    String category = categoryElement.getTextContent();
+                    if (j > 0) {
+                        categories += ", ";
+                    }
+
+                    categories += category;
+                }
+
+                categoriesLabel.setText(categories);
+
+                String submitter = (String)xpath.evaluate("dz:submitter/dz:username", itemElement,
+                    XPathConstants.STRING);
+                submitterLabel.setText(submitter);
+            } catch(XPathExpressionException exception) {
+                System.err.println(exception);
+            }
+
+            Font labelFont = (Font)listView.getStyles().get("font");
+            Font largeFont = labelFont.deriveFont(Font.BOLD, 14);
+            titleLabel.getStyles().put("font", largeFont);
+            categoriesLabel.getStyles().put("font", labelFont);
+            submitterLabel.getStyles().put("font", labelFont);
+
+            Color color = null;
+            if (listView.isEnabled() && !disabled) {
+                if (selected) {
+                    if (listView.isFocused()) {
+                        color = (Color)listView.getStyles().get("selectionColor");
+                    } else {
+                        color = (Color)listView.getStyles().get("inactiveSelectionColor");
+                    }
+                } else {
+                    color = (Color)listView.getStyles().get("color");
+                }
+            } else {
+                color = (Color)listView.getStyles().get("disabledColor");
+            }
+
+            if (color instanceof Color) {
+                titleLabel.getStyles().put("color", color);
+                categoriesHeadingLabel.getStyles().put("color", color);
+                categoriesLabel.getStyles().put("color", color);
+                submitterHeadingLabel.getStyles().put("color", color);
+                submitterLabel.getStyles().put("color", color);
+            }
+        }
+    }
+
+    private Window window = null;
+    private ListView feedListView = null;
+
+    private XPath xpath = XPathFactory.newInstance().newXPath();
+
+    public static final String FEED_URI = "http://feeds.dzone.com/javalobby/frontpage?format=xml";
+
+    public RSSFeedDemo() {
+        xpath.setNamespaceContext(new NamespaceContext() {
+            public String getNamespaceURI(String prefix) {
+                String namespaceURI;
+                if (prefix.equals("dz")) {
+                    namespaceURI = "http://www.developerzone.com/modules/dz/1.0";
+                } else {
+                    namespaceURI = XMLConstants.NULL_NS_URI;
+                }
+
+                return namespaceURI;
+            }
+
+            public String getPrefix(String uri) {
+                throw new UnsupportedOperationException();
+            }
+
+            public Iterator<String> getPrefixes(String uri) {
+                throw new UnsupportedOperationException();
+            }
+        });
+    }
+
+    public void startup(Display display, Dictionary<String, String> properties)
+        throws Exception {
+        WTKXSerializer wtkxSerializer = new WTKXSerializer();
+        window = new Window((Component)wtkxSerializer.readObject(getClass().getResource("rss_feed_demo.wtkx")));
+
+        feedListView = (ListView)wtkxSerializer.getObjectByName("feedListView");
+        feedListView.setItemRenderer(new RSSItemRenderer());
+
+        feedListView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
+            @Override
+            public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
+                if (count == 2) {
+                    Element itemElement = (Element)feedListView.getListData().get(feedListView.getItemAt(y));
+
+                    try {
+                        String link = (String)xpath.evaluate("link", itemElement, XPathConstants.STRING);
+                        BrowserApplicationContext.open(new URL(link));
+                    } catch(XPathExpressionException exception) {
+                        System.err.print(exception);
+                    } catch(MalformedURLException exception) {
+                        System.err.print(exception);
+                    }
+                }
+
+                return false;
+            }
+        });
+
+        // In a real app, we'd load this in a task so we don't bog down the UI
+        InputSource inputSource = new InputSource(FEED_URI);
+
+        NodeList itemNodeList = (NodeList)xpath.evaluate("/rss/channel/item",
+            inputSource, XPathConstants.NODESET);
+        feedListView.setListData(new NodeListAdapter(itemNodeList));
+
+        window.setMaximized(true);
+        window.open(display);
+    }
+
+    public boolean shutdown(boolean optional) throws Exception {
+        if (window != null) {
+            window.close();
+        }
+
+        return true;
+    }
+
+    public void suspend() {
+    }
+
+    public void resume() {
+    }
+}

Added: incubator/pivot/trunk/demos/src/pivot/demos/rss/rss_feed_demo.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/demos/src/pivot/demos/rss/rss_feed_demo.wtkx?rev=767548&view=auto
==============================================================================
--- incubator/pivot/trunk/demos/src/pivot/demos/rss/rss_feed_demo.wtkx (added)
+++ incubator/pivot/trunk/demos/src/pivot/demos/rss/rss_feed_demo.wtkx Wed Apr 22 14:46:22 2009
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Copyright (c) 2009 VMware, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<Border styles="{padding:0, color:10}"
+    xmlns:wtkx="http://incubator.apache.org/pivot/wtkx/1.1"
+    xmlns:effects="pivot.wtk.effects"
+    xmlns:rss="pivot.demos.rss"
+    xmlns="pivot.wtk">
+    <content>
+	    <ScrollPane horizontalScrollBarPolicy="fill">
+	        <view>
+	            <ListView wtkx:id="feedListView"/>
+	        </view>
+	    </ScrollPane>
+    </content>
+</Border>