You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2006/06/05 16:31:27 UTC

svn commit: r411801 - in /incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java: ./ org/apache/cayenne/swing/plugin/frame/

Author: aadamchik
Date: Mon Jun  5 07:31:27 2006
New Revision: 411801

URL: http://svn.apache.org/viewvc?rev=411801&view=rev
Log:
added support for menu loading from XML

Added:
    incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FrameMenuBuilder.java
    incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/XMLUtil.java
Modified:
    incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FramePlugin.java
    incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/plugin.xml

Added: incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FrameMenuBuilder.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FrameMenuBuilder.java?rev=411801&view=auto
==============================================================================
--- incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FrameMenuBuilder.java (added)
+++ incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FrameMenuBuilder.java Mon Jun  5 07:31:27 2006
@@ -0,0 +1,100 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  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 org.apache.cayenne.swing.plugin.frame;
+
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.swing.Action;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+
+import org.apache.cayenne.swing.CayenneSwingException;
+import org.platonos.pluginengine.Plugin;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class FrameMenuBuilder {
+
+    protected FramePlugin framePlugin;
+
+    public FrameMenuBuilder(FramePlugin framePlugin) {
+        this.framePlugin = framePlugin;
+    }
+
+    /**
+     * Adds a menu contributed by plugin.
+     */
+    public void addMenu(Plugin plugin, String xmlPath) {
+        JFrame frame = framePlugin.getFrameController().getFrame();
+
+        JMenuBar menu = frame.getJMenuBar();
+        if (menu == null) {
+            menu = new JMenuBar();
+            frame.setJMenuBar(menu);
+        }
+
+        InputStream menuXML = plugin.getPluginClassLoader().getResourceAsStream(xmlPath);
+        if (menuXML == null) {
+            throw new CayenneSwingException("No menu XML file found at " + xmlPath);
+        }
+
+        Document doc;
+        try {
+            doc = XMLUtil.newBuilder().parse(menuXML);
+        }
+        catch (Exception e) {
+            throw new CayenneSwingException("Error parsing menu XML '" + xmlPath + "'", e);
+        }
+
+        List children = XMLUtil.getChildren(doc.getDocumentElement(), "menu");
+        Iterator it = children.iterator();
+        while (it.hasNext()) {
+            menu.add(processMenu((Element) it.next()));
+        }
+
+    }
+
+    /**
+     * Recursively loads menus from the DOM tree.
+     */
+    protected JMenuItem processMenu(Element menuXML) {
+
+        List children = XMLUtil.getChildren(menuXML, "menu");
+
+        Action action = framePlugin.getActionMap().get(menuXML.getAttribute("action"));
+        String key = menuXML.getAttribute("name");
+
+        JMenuItem menu = (children.isEmpty()) ? new JMenuItem() : new JMenu();
+
+        if (action != null) {
+            menu.setAction(action);
+        }
+        else if (key != null) {
+            menu.setText(key);
+        }
+
+        Iterator it = children.iterator();
+        while (it.hasNext()) {
+            menu.add(processMenu((Element) it.next()));
+        }
+
+        return menu;
+    }
+}

Modified: incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FramePlugin.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FramePlugin.java?rev=411801&r1=411800&r2=411801&view=diff
==============================================================================
--- incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FramePlugin.java (original)
+++ incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/FramePlugin.java Mon Jun  5 07:31:27 2006
@@ -30,15 +30,17 @@
  */
 public class FramePlugin extends PluginLifecycle {
 
-    public static final String QUIT_ACTION = "frame.action.quit";
+    public static final String QUIT_ACTION = "action.quit";
 
     public static final String FRAME_BUILDERS_EXT = "frameBuilders";
 
+    protected FrameMenuBuilder menuBuilder;
     protected FrameController frameController;
     protected ActionMap actionMap;
 
     protected void start() {
 
+        menuBuilder = new FrameMenuBuilder(this);
         frameController = new FrameController(this);
         actionMap = new ActionMap();
 
@@ -106,5 +108,9 @@
 
     public void setFrameController(FrameController frameController) {
         this.frameController = frameController;
+    }
+
+    public FrameMenuBuilder getMenuBuilder() {
+        return menuBuilder;
     }
 }

Added: incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/XMLUtil.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/XMLUtil.java?rev=411801&view=auto
==============================================================================
--- incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/XMLUtil.java (added)
+++ incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/org/apache/cayenne/swing/plugin/frame/XMLUtil.java Mon Jun  5 07:31:27 2006
@@ -0,0 +1,85 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  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 org.apache.cayenne.swing.plugin.frame;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.commons.collections.Predicate;
+import org.objectstyle.cayenne.CayenneRuntimeException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+// TODO: andrus, 6/5/2006 -this code is copied from non-public
+// org.objectstyle.cayenne.xml.XMLUtil - merge it somehow or make it public...
+class XMLUtil {
+
+    static DocumentBuilderFactory sharedFactory;
+
+    /**
+     * Creates a new instance of DocumentBuilder using the default factory.
+     */
+    static DocumentBuilder newBuilder() throws CayenneRuntimeException {
+        if (sharedFactory == null) {
+            sharedFactory = DocumentBuilderFactory.newInstance();
+        }
+
+        try {
+            return sharedFactory.newDocumentBuilder();
+        }
+        catch (ParserConfigurationException e) {
+            throw new CayenneRuntimeException("Can't create DocumentBuilder", e);
+        }
+    }
+
+    /**
+     * Returns all elements among the direct children that have a matching name.
+     */
+    static List getChildren(Node node, final String name) {
+        Predicate p = new Predicate() {
+
+            public boolean evaluate(Object object) {
+                if (object instanceof Element) {
+                    Element e = (Element) object;
+                    return name.equals(e.getNodeName());
+                }
+
+                return false;
+            }
+        };
+
+        return allMatches(node.getChildNodes(), p);
+    }
+    
+    private static List allMatches(NodeList list, Predicate predicate) {
+        int len = list.getLength();
+        List children = new ArrayList(len);
+
+        for (int i = 0; i < len; i++) {
+            Node node = list.item(i);
+            if (predicate.evaluate(node)) {
+                children.add(node);
+            }
+        }
+
+        return children;
+    }
+}

Modified: incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/plugin.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/plugin.xml?rev=411801&r1=411800&r2=411801&view=diff
==============================================================================
--- incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/plugin.xml (original)
+++ incubator/cayenne/main/branches/PROTO-3.0/swing-plugin/src/main/java/plugin.xml Mon Jun  5 07:31:27 2006
@@ -1,5 +1,5 @@
 <plugin start="true">
-   <uid>org.apache.cayenne:swing-frame-plugin</uid>
+   <uid>org.apache.cayenne:swing-plugin</uid>
    <name>Generic Application Frame</name>
    <lifecycleclass>org.apache.cayenne.swing.plugin.frame.FramePlugin</lifecycleclass>
    <extensionpoints>