001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.graphgenerator;
005    
006    import graphlab.graph.event.GraphEvent;
007    import graphlab.graph.graph.*;
008    import graphlab.graph.ui.GTabbedGraphPane;
009    import graphlab.graph.ui.GraphRectRegionSelect;
010    import graphlab.platform.core.BlackBoard;
011    import graphlab.platform.plugin.PluginMethods;
012    import graphlab.plugins.graphgenerator.core.GraphGeneratorInterface;
013    import graphlab.plugins.graphgenerator.core.SimpleGeneratorInterface;
014    import graphlab.plugins.graphgenerator.generators.*;
015    import graphlab.plugins.main.core.CorePluginMethods;
016    import graphlab.plugins.main.core.actions.vertex.AddVertex;
017    
018    import java.awt.*;
019    import java.awt.geom.Rectangle2D;
020    
021    /**
022     * @author azin azadi
023    
024     */
025    public class GraphGenerator implements PluginMethods {
026    
027        /**
028         * first request a GUI rectangular region from user and then generates and adds a graph from gen to current working graph
029         *
030         * @param gen
031         * @param blackboard
032         */
033        public static void generateInRectangularBounds(final GraphGeneratorInterface gen, final BlackBoard blackboard) {
034    //        long
035            blackboard.setData(AddVertex.DISABLE, true);
036    
037            //select a region and then generate the graph
038            GTabbedGraphPane.showNotificationMessage("<HTML><BODY><Center><b>Please select a region to put the generated graph.</b></center></body></html>", blackboard, true);
039    //        JOptionPane.showMessageDialog(null, "Please select a region to put the generated graph");
040            new GraphRectRegionSelect(blackboard) {
041    
042                public void onMouseMoved(GraphEvent data) {
043                    //do nothing;
044                }
045    
046                public void onDrop(GraphEvent data) {
047    //                final blackboard blackboard = blackboard;
048                    new Thread() {
049                        public void run() {
050                            synchronized (gv) {
051                                generateGraph(gen, blackboard, rect);
052                                GTabbedGraphPane.hideNotificationMessage(blackboard);
053                            }
054                        }
055                    }.start();
056                }
057            }.startSelectingRegion();
058        }
059    
060        /**
061         * generates and adds a graph from gen to current working graph
062         *
063         * @param gen
064         * @param blackboard
065         */
066        public static void generateGraph(final GraphGeneratorInterface gen, final BlackBoard blackboard, final Rectangle rect) {
067            //                            g.view.ignoreUpdates = true;
068    //                            blackboard blackboard = g.blackboard;
069            final AbstractGraphRenderer ren = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
070            if (rect.width < 5)
071                rect.width = 250;
072            if (rect.height < 5)
073                rect.height = 250;
074            new Thread() {
075                public void run() {
076                    ren.ignoreRepaints(new Runnable() {
077                        public void run() {
078                            GTabbedGraphPane.showNotificationMessage("Generating Graph...", blackboard, true);
079                            GraphModel gg = CorePluginMethods.getGraph(blackboard);
080                            GraphModel g = gen.generateGraph();
081                            Rectangle2D.Double bounds1 = g.getAbsBounds();
082                            gg.addSubGraph(g, rect);
083    
084                            GTabbedGraphPane.showTimeNotificationMessage("The Graph generated Completely: Vertices:" + g.getVerticesCount() + ", Edges:" + g.getEdgesCount(), blackboard, 4000, true);
085                            blackboard.setData(AddVertex.DISABLE, false);
086                        }
087                    });
088    
089                }
090            }.start();
091        }
092    
093        /**
094         * generates and return a graph from the given interface, not showing it on GUI
095         */
096        public static GraphModel getGraph(boolean isDirected, SimpleGeneratorInterface gi) {
097            GraphModel ret = new GraphModel(isDirected);
098            gi.setWorkingGraph(ret);
099            VertexModel[] vertices = gi.getVertices();
100            Point[] pos = gi.getVertexPositions();
101            EdgeModel[] edges = gi.getEdges();
102            for (VertexModel v : vertices)
103                ret.insertVertex(v);
104            for (int i = 0; i < vertices.length; i++)
105                vertices[i].setLocation(new GraphPoint(pos[i].x, pos[i].y));
106            for (EdgeModel e : edges)
107                ret.insertEdge(e);
108            return ret;
109        }
110    
111        //________________    Graph Generators    _______________
112        /**
113         * @see graphlab.plugins.graphgenerator.generators.CircleGenerator#generateCircle(int)
114         */
115        public static GraphModel generateCircle(int n) {
116            return CircleGenerator.generateCircle(n);
117        }
118    
119        /**
120         * @see graphlab.plugins.graphgenerator.generators.CmnGenerator#generateCmn(int,int)
121         */
122        public static GraphModel generateCmn(int m, int n) {
123            return CmnGenerator.generateCmn(m, n);
124        }
125    
126        /**
127         * @see graphlab.plugins.graphgenerator.generators.GeneralizedPetersonGenerator#generateGeneralizedPeterson(int,int)
128         */
129        public static GraphModel generateGeneralizedPeterson(int n, int k) {
130            return GeneralizedPetersonGenerator.generateGeneralizedPeterson(n, k);
131        }
132    
133        /**
134         * @see graphlab.plugins.graphgenerator.generators.CompleteGraphGenerator#generateCompleteGraph(int)
135         */
136        public static GraphModel generateCompleteGraph(int n) {
137            return CompleteGraphGenerator.generateCompleteGraph(n);
138        }
139    
140        /**
141         * @see graphlab.plugins.graphgenerator.generators.KenserGraphGenerator#generateKenserGraph(int,int)
142         */
143        public static GraphModel generateKenserGraph(int n, int d) {
144            return KenserGraphGenerator.generateKenserGraph(n, d);
145        }
146    
147    
148        /**
149         * @see graphlab.plugins.graphgenerator.generators.KmnGenerator#generateKmn(int,int)
150         */
151        public static GraphModel generateKmn(int m, int n) {
152            return KmnGenerator.generateKmn(m, n);
153        }
154    
155        /**
156         * @see graphlab.plugins.graphgenerator.generators.PathGenerator#generatePath(int)
157         */
158        public static GraphModel generatePath(int n) {
159            return PathGenerator.generatePath(n);
160        }
161    
162        /**
163         * @see graphlab.plugins.graphgenerator.generators.PmnGenerator#generatePmn(int,int)
164         */
165        public static GraphModel generatePmn(int m, int n) {
166            return PmnGenerator.generatePmn(m, n);
167        }
168    
169        /**
170         * @see graphlab.plugins.graphgenerator.generators.RandomGenerator#generateRandomGraph(int)
171         */
172        public static GraphModel generateRandomGraph(int n, int e) {
173            return RandomGenerator.generateRandomGraph(n, e);
174        }
175    
176        /**
177         * @see graphlab.plugins.graphgenerator.generators.StarGenerator#generateStar(int)
178         */
179        public static GraphModel generateStar(int n) {
180            return StarGenerator.generateStar(n);
181        }
182    
183        /**
184         * @see graphlab.plugins.graphgenerator.generators.TreeGenerator#generateTree(int,int)
185         */
186        public static GraphModel generateTree(int depth, int degree) {
187            return TreeGenerator.generateTree(depth, degree);
188        }
189    
190        /**
191         * @see graphlab.plugins.graphgenerator.generators.WheelGenerator#generateWheel(int)
192         */
193        public static GraphModel generateWheel(int n) {
194            return WheelGenerator.generateWheel(n);
195        }
196    
197    
198    }
199