[jboss-cvs] JBossAS SVN: r91296 - in projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher: graph and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jul 15 10:54:14 EDT 2009


Author: alesj
Date: 2009-07-15 10:54:14 -0400 (Wed, 15 Jul 2009)
New Revision: 91296

Added:
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/DataGraphLayoutCache.java
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/JGraphGraphFactory.java
Modified:
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/AbstractGraphCreator.java
Log:
Add headless graphics flag.

Modified: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java	2009-07-15 14:35:43 UTC (rev 91295)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java	2009-07-15 14:54:14 UTC (rev 91296)
@@ -98,8 +98,17 @@
       // or is this a new impl
       if (renderer == null)
          renderer = createInstance(defaultRenderer, renderers.get(GrapherConstants.FORMAT), Renderer.class);
+
+      // apply headless graphics
+      System.setProperty("java.awt.headless", "true");
    }
 
+   @Override
+   public void destroy()
+   {
+      System.clearProperty("java.awt.headless");
+   }
+
    /**
     * Get renderer.
     *

Modified: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/AbstractGraphCreator.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/AbstractGraphCreator.java	2009-07-15 14:35:43 UTC (rev 91295)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/AbstractGraphCreator.java	2009-07-15 14:54:14 UTC (rev 91296)
@@ -27,6 +27,7 @@
 import java.util.Hashtable;
 import java.util.Map;
 import java.util.Set;
+import javax.swing.*;
 
 import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.dependency.spi.ControllerState;
@@ -81,10 +82,28 @@
       GraphSettings.layout(cells, attributes);
       // insert cells
       model.insert(cells, attributes, cs, null, null);
-      return new JGraph(model);
+      JGraph graph = new JGraph(model);
+      applySwingHack(graph);
+      return graph;
    }
 
    /**
+    * Apply swing hack.
+    *
+    * @param graph the graph
+    */
+   protected void applySwingHack(JGraph graph)
+   {
+      JPanel panel = new JPanel();
+      panel.setDoubleBuffered(false);// always turn double buffering off when  exporting
+      panel.add(graph);
+      panel.setVisible(true);
+      panel.setEnabled(true);
+      panel.addNotify();// workaround to pack() on a JFrame
+      panel.validate();
+   }
+
+   /**
     * Create cells.
     *
     * @param controller the controller

Added: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/DataGraphLayoutCache.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/DataGraphLayoutCache.java	                        (rev 0)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/DataGraphLayoutCache.java	2009-07-15 14:54:14 UTC (rev 91296)
@@ -0,0 +1,100 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.mctools.grapher.graph;
+
+import org.jgraph.event.GraphModelEvent;
+import org.jgraph.event.GraphModelListener;
+import org.jgraph.graph.CellView;
+import org.jgraph.graph.CellViewFactory;
+import org.jgraph.graph.DefaultCellViewFactory;
+import org.jgraph.graph.DefaultGraphModel;
+import org.jgraph.graph.GraphLayoutCache;
+import org.jgraph.graph.GraphModel;
+
+/**
+ * A Graph Layout Cache that automatically attaches itself as a listener
+ * of its associated graph model. The intend usage is for applications
+ * that do not create a JGraph instance. Without a JGraph instance there
+ * is no UI (in JGraph 5.x) and it is the UI that causes the layout cache
+ * to automatically listen to the model.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class DataGraphLayoutCache extends GraphLayoutCache implements GraphModelListener
+{
+   /**
+    * Constructs a graph layout cache.
+    */
+   public DataGraphLayoutCache()
+   {
+      this(new DefaultGraphModel(), new DefaultCellViewFactory());
+   }
+
+   /**
+    * Constructs a view for the specified model that uses <code>factory</code>
+    * to create its views.
+    *
+    * @param model the model that constitues the data source
+    * @param factory the cell view factory
+    */
+   public DataGraphLayoutCache(GraphModel model, CellViewFactory factory)
+   {
+      this(model, factory, false);
+   }
+
+   /**
+    * Constructs a view for the specified model that uses <code>factory</code>
+    * to create its views.
+    *
+    * @param model the model that constitues the data source
+    * @param factory the cell view factory
+    * @param partial the partial flag
+    */
+   public DataGraphLayoutCache(GraphModel model, CellViewFactory factory, boolean partial)
+   {
+      this(model, factory, null, null, partial);
+   }
+
+   /**
+    * Constructs a view for the specified model that uses <code>factory</code>
+    * to create its views.
+    *
+    * @param model the model that constitues the data source
+    * @param factory the cell view factory
+    * @param cellViews the cell views
+    * @param hiddenCellViews the hidden cell views
+    * @param partial the partial flag
+    */
+   public DataGraphLayoutCache(GraphModel model, CellViewFactory factory, CellView[] cellViews, CellView[] hiddenCellViews, boolean partial)
+   {
+      super(model, factory, cellViews, hiddenCellViews, partial);
+      if (this.graphModel != null)
+      {
+         graphModel.addGraphModelListener(this);
+      }
+   }
+
+   public void graphChanged(GraphModelEvent e)
+   {
+      graphChanged(e.getChange());
+	}
+}
\ No newline at end of file

Added: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/JGraphGraphFactory.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/JGraphGraphFactory.java	                        (rev 0)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/graph/JGraphGraphFactory.java	2009-07-15 14:54:14 UTC (rev 91296)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.mctools.grapher.graph;
+
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.jgraph.graph.AttributeMap;
+import org.jgraph.graph.ConnectionSet;
+import org.jgraph.graph.DefaultGraphModel;
+import org.jgraph.graph.GraphModel;
+import org.jgraph.graph.ParentMap;
+
+/**
+ * A helper class that creates graphs. Currently supports tree graphs and a
+ * random graph where all edges are connected at least once
+ *
+ * @author Gaudenz Alder
+ * @author David Benson
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public final class JGraphGraphFactory
+{
+   /**
+    * Variant of the insert method that allows to pass a default connection set
+    * and parent map and nested map.
+    *
+    * @param model  the graph model
+    * @param cells  the graph cells
+    * @param nested the graph attributes
+    * @param cs     the connection set
+    * @param pm     the parent map
+    */
+   @SuppressWarnings("unchecked")
+   public static void insert(GraphModel model, Object[] cells, Map nested, ConnectionSet cs, ParentMap pm)
+   {
+      if (cells != null)
+      {
+         if (nested == null)
+            nested = new Hashtable();
+         if (cs == null)
+            cs = new ConnectionSet();
+         if (pm == null)
+            pm = new ParentMap();
+
+         for (Object cell : cells)
+         {
+            // Using the children of the vertex we construct the parent map.
+            int childCount = model.getChildCount(cell);
+            for (int j = 0; j < childCount; j++)
+            {
+               Object child = model.getChild(cell, j);
+               pm.addEntry(child, cell);
+
+               // And add their attributes to the nested map
+               AttributeMap attrs = model.getAttributes(child);
+               if (attrs != null)
+                  nested.put(child, attrs);
+            }
+
+            // A nested map with the vertex as key
+            // and its attributes as the value
+            // is required for the model.
+            Map attrsTmp = (Map)nested.get(cell);
+            Map attrs = model.getAttributes(cell);
+            if (attrsTmp != null)
+               attrs.putAll(attrsTmp);
+            nested.put(cell, attrs);
+
+            // Check if we have parameters for a connection set.
+            Object sourcePort = model.getSource(cell);
+            if (sourcePort != null)
+               cs.connect(cell, sourcePort, true);
+
+            Object targetPort = model.getTarget(cell);
+            if (targetPort != null)
+               cs.connect(cell, targetPort, false);
+         }
+         // Create an array with the parent and its children.
+         cells = DefaultGraphModel.getDescendants(model, cells).toArray();
+
+         // Finally call the insert method on the parent class.
+         model.insert(cells, nested, cs, pm, null);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list