/**
 * Copyright (c) 2012, JGraph Ltd
 */

package com.mxgraph.examples.swing.editor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.mxgraph.analysis.StructuralException;
import com.mxgraph.analysis.mxAnalysisGraph;
import com.mxgraph.analysis.mxGraphGenerator;
import com.mxgraph.analysis.mxTraversal;
import com.mxgraph.analysis.mxGraphProperties;
import com.mxgraph.analysis.mxGraphProperties.GraphType;
import com.mxgraph.analysis.mxGraphStructure;
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.mxOrganicLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraph.mxICellVisitor;
import com.mxgraph.view.mxGraphView;
import com.mxgraph.costfunction.mxCostFunction;
import com.mxgraph.costfunction.mxDoubleValCostFunction;

public class GraphConfigDialog extends JDialog
{
	/**
	 * Number of nodes
	 */
	protected int numNodes = 6;

	/**
	 * Number of edges
	 */
	protected int numEdges = 6;

	/**
	 * Valence
	 */
	protected int valence = 2;

	/**
	 * Number of rows for a grid graph
	 */
	protected int numRows = 8;

	protected int numVertexesInBranch = 3;

	public int getNumVertexesInBranch()
	{
		return numVertexesInBranch;
	}

	public void setNumVertexesInBranch(int numVertexesInBranch)
	{
		this.numVertexesInBranch = numVertexesInBranch;
	}

	/**
	 * Number of columns for a grid graph
	 */
	protected int numColumns = 8;

	protected int minWeight = 1;

	public int getMinWeight()
	{
		return minWeight;
	}

	public void setMinWeight(int minWeight)
	{
		this.minWeight = minWeight;
	}

	public int getMaxWeight()
	{
		return maxWeight;
	}

	public void setMaxWeight(int maxWeight)
	{
		this.maxWeight = maxWeight;
	}

	protected int maxWeight = 10;

	/**
	 * Number of vertexes for the left group in a bipartite graph
	 */
	protected int numVertexesLeft = 5;

	/**
	 * Number of vertexes for the right group in a bipartite graph
	 */
	protected int numVertexesRight = 5;

	/**
	 * The start vertex (by value) for various algorithms
	 */
	protected int startVertexValue = 0;

	/**
	 * The end vertex (by value) for various algorithms (mostly pathfinding)
	 */
	protected int endVertexValue = 0;

	protected int numBranches = 4;

	public int getNumBranches()
	{
		return numBranches;
	}

	public void setNumBranches(int numBranches)
	{
		this.numBranches = numBranches;
	}

	/**
	 * If set, arrowheads are drawn
	 */
	protected boolean arrows = false;

	protected boolean weighted = false;

	/**
	 * If set, self-loops are allowed during graph generation
	 */
	protected boolean allowSelfLoops = false;

	/**
	 * If set, parallel edges are allowed during graph generation
	 */
	protected boolean allowMultipleEdges = false;

	/**
	 * If set, the generated graph will be always connected
	 */
	protected boolean forceConnected = false;

	/**
	 * Spacing for groups in a bipartite graph
	 */
	protected float groupSpacing = 200;

	/**
	 * Grid spacing for a grid graph
	 */
	protected float gridSpacing = 80;

	private static final long serialVersionUID = 1535851135077957959L;

	protected boolean insertGraph = false;

	protected mxGraph graph;

	protected mxAnalysisGraph aGraph;

	protected GraphType graphType;

	protected JTextField maxTreeNodeChildren = new JTextField();

	protected JTextField numNodesField = new JTextField();

	protected JTextField numEdgesField = new JTextField();

	protected JTextField valenceField = new JTextField();

	protected JTextField numRowsField = new JTextField();

	protected JTextField numColumnsField = new JTextField();

	protected JTextField gridSpacingField = new JTextField();

	protected JTextField numVertexesLeftField = new JTextField();

	protected JTextField numVertexesRightField = new JTextField();

	protected JTextField groupSpacingField = new JTextField();

	protected JCheckBox arrowsBox = new JCheckBox();

	protected JTextField startVertexValueField = new JTextField();

	protected JTextField endVertexValueField = new JTextField();

	protected JCheckBox selfLoopBox = new JCheckBox();

	protected JCheckBox multipleEdgeBox = new JCheckBox();

	protected JCheckBox forceConnectedBox = new JCheckBox();

	protected JCheckBox weightedBox = new JCheckBox();

	protected JTextField maxWeightField = new JTextField();

	protected JTextField minWeightField = new JTextField();

	protected JTextField numBranchesField = new JTextField();

	protected JTextField numVertexesInBranchField = new JTextField();

	public GraphConfigDialog(final GraphType graphType2, String dialogText)
	{

		super((Frame) null, dialogText, true);
		if ((graphType2 == GraphType.NULL) || (graphType2 == GraphType.SIMPLE_RANDOM_TREE))
		{
			JPanel panel = new JPanel(new GridLayout(1, 2, 4, 4));
			panel.add(new JLabel("Number of nodes"));
			panel.add(numNodesField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int nodeCount = Integer.parseInt(numNodesField.getText());
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					if (graphType2 == GraphType.NULL)
					{
						mxGraphGenerator generator = new mxGraphGenerator(null, new mxDoubleValCostFunction());
						Map<String, Object> props = new HashMap<String, Object>();
						mxGraphProperties.setDirected(props, false);
						configAnalysisGraph(graph, generator, props);

						generator.getNullGraph(aGraph, nodeCount);

						mxGraphStructure.setDefaultGraphStyle(aGraph, false);
						mxCircleLayout layout = new mxCircleLayout(graph);
						layout.execute(graph.getDefaultParent());
					}
					else if (graphType2 == GraphType.SIMPLE_RANDOM_TREE)
					{
						graph.getModel().beginUpdate();

						mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, false, 0, 10),
								new mxDoubleValCostFunction());
						Map<String, Object> props = new HashMap<String, Object>();
						mxGraphProperties.setDirected(props, false);
						configAnalysisGraph(graph, generator, props);

						generator.getSimpleRandomTree(aGraph, nodeCount);

						mxGraphProperties.setDirected(props, true);
						mxGraphStructure.setDefaultGraphStyle(aGraph, false);
						setVisible(false);
						mxCompactTreeLayout layout = new mxCompactTreeLayout(graph, false);
						layout.execute(graph.getDefaultParent());
						graph.getModel().endUpdate();
					}

					graph.getModel().endUpdate();
					setVisible(false);
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if (graphType2 == GraphType.COMPLETE)
		{
			JPanel panel = new JPanel(new GridLayout(5, 1, 4, 4));
			panel.add(new JLabel("Number of nodes"));
			panel.add(numNodesField);
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int vertexNumParam = Integer.parseInt(numNodesField.getText());
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					graph.getModel().beginUpdate();

					graph.selectAll();
					graph.removeCells();
					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					configAnalysisGraph(graph, generator, props);

					generator.getCompleteGraph(aGraph, vertexNumParam);

					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					mxCircleLayout layout = new mxCircleLayout(graph);
					layout.execute(graph.getDefaultParent());
					graph.getModel().endUpdate();
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if (graphType2 == GraphType.FULL_WINDMILL || graphType2 == GraphType.FRIENDSHIP_WINDMILL)
		{
			JPanel panel = new JPanel(new GridLayout(6, 1, 4, 4));
			panel.add(new JLabel("Number of branches"));
			panel.add(numBranchesField);
			panel.add(new JLabel("Number of vertexes per branch"));
			panel.add(numVertexesInBranchField);
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					graph.selectAll();
					graph.removeCells();
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					int numBranchesParam = Integer.parseInt(numBranchesField.getText());
					int numVertexesInBranchParam = Integer.parseInt(numVertexesInBranchField.getText());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					configAnalysisGraph(graph, generator, props);

					if (graphType2 == GraphType.FRIENDSHIP_WINDMILL)
					{
						generator.getFriendshipWindmillGraph(aGraph, numBranchesParam, numVertexesInBranchParam);
					}
					else if (graphType2 == GraphType.FULL_WINDMILL)
					{
						generator.getWindmillGraph(aGraph, numBranchesParam, numVertexesInBranchParam);
					}

					generator.setWindmillGraphLayout(aGraph, numBranchesParam, numVertexesInBranchParam, 1000);
					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if ((graphType2 == GraphType.WHEEL) || (graphType2 == GraphType.STAR) || (graphType2 == GraphType.PATH))
		{
			JPanel panel = new JPanel(new GridLayout(5, 1, 4, 4));
			panel.add(new JLabel("Number of nodes"));
			panel.add(numNodesField);
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int numNodesParam = Integer.parseInt(numNodesField.getText());
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					configAnalysisGraph(graph, generator, props);
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					if (graphType2 == GraphType.WHEEL)
					{
						generator.getWheelGraph(aGraph, numNodesParam);
						generator.setStarGraphLayout(aGraph, 400);
					}
					else if (graphType2 == GraphType.STAR)
					{
						generator.getStarGraph(aGraph, numNodesParam);
						generator.setStarGraphLayout(aGraph, 400);
					}
					else if (graphType2 == GraphType.PATH)
					{
						generator.getPathGraph(aGraph, numNodesParam);
						generator.setPathGraphSpacing(aGraph, 80);
					}

					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					graph.getModel().endUpdate();
				}
			});

			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if (graphType2 == GraphType.PETERSEN)
		{
			JPanel panel = new JPanel(new GridLayout(4, 1, 4, 4));
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					configAnalysisGraph(graph, generator, props);
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					generator.getPetersenGraph(aGraph);
					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					mxCircleLayout layout = new mxCircleLayout(graph);
					layout.execute(graph.getDefaultParent());

					graph.getModel().endUpdate();
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if (graphType2 == GraphType.GRID)
		{
			JPanel panel = new JPanel(new GridLayout(3, 2, 4, 4));
			panel.add(new JLabel("Number of rows"));
			panel.add(numRowsField);
			panel.add(new JLabel("Number of columns"));
			panel.add(numColumnsField);
			panel.add(new JLabel("Grid spacing"));
			panel.add(gridSpacingField);
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int yDim = Integer.parseInt(numRowsField.getText());
					int xDim = Integer.parseInt(numColumnsField.getText());
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					float spacing = Float.parseFloat(gridSpacingField.getText());
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					configAnalysisGraph(graph, generator, props);

					generator.getGridGraph(aGraph, xDim, yDim);
					generator.setGridGraphSpacing(aGraph, spacing, spacing, xDim, yDim);

					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					graph.getModel().endUpdate();
				}
			});

			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if ((graphType2 == GraphType.KNIGHT) || (graphType2 == GraphType.KING))
		{
			JPanel panel = new JPanel(new GridLayout(5, 2, 4, 4));
			panel.add(new JLabel("Number of rows"));
			panel.add(numRowsField);
			panel.add(new JLabel("Number of columns"));
			panel.add(numColumnsField);
			panel.add(new JLabel("Grid spacing"));
			panel.add(gridSpacingField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int yDim = Integer.parseInt(numRowsField.getText());
					int xDim = Integer.parseInt(numColumnsField.getText());
					float spacing = Float.parseFloat(gridSpacingField.getText());

					mxGraphGenerator generator = new mxGraphGenerator(null, new mxDoubleValCostFunction());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					configAnalysisGraph(graph, generator, props);
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					if (graphType2 == GraphType.KNIGHT)
					{
						generator.getKnightGraph(aGraph, xDim, yDim);
					}
					else if (graphType2 == GraphType.KING)
					{
						generator.getKingGraph(aGraph, xDim, yDim);
					}

					generator.setGridGraphSpacing(aGraph, spacing, spacing, xDim, yDim);
					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					graph.getModel().endUpdate();
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if (graphType2 == GraphType.KNIGHT_TOUR)
		{
			JPanel panel = new JPanel(new GridLayout(4, 2, 4, 4));
			panel.add(new JLabel("Starting node"));
			panel.add(startVertexValueField);
			panel.add(new JLabel("X dimension of chessboard"));
			panel.add(numColumnsField);
			panel.add(new JLabel("Y dimension of chessboard"));
			panel.add(numRowsField);
			panel.add(new JLabel("Grid spacing"));
			panel.add(gridSpacingField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int yDim = Integer.parseInt(numRowsField.getText());
					int xDim = Integer.parseInt(numColumnsField.getText());
					int value = Integer.parseInt(startVertexValueField.getText());
					float spacing = Float.parseFloat(gridSpacingField.getText());
					mxGraphGenerator generator = new mxGraphGenerator(null, new mxDoubleValCostFunction());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, true);
					configAnalysisGraph(graph, generator, props);
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					try
					{
						generator.getKnightTour(aGraph, xDim, yDim, value);
					}
					catch (StructuralException e1)
					{
						System.out.println(e1);
					}

					generator.setGridGraphSpacing(aGraph, spacing, spacing, xDim, yDim);
					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					graph.getModel().endUpdate();
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if ((graphType2 == GraphType.BIPARTITE) || (graphType2 == GraphType.COMPLETE_BIPARTITE))
		{
			JPanel panel = new JPanel(new GridLayout(3, 2, 4, 4));
			panel.add(new JLabel("Number of vertexes in group 1"));
			panel.add(numVertexesLeftField);
			panel.add(new JLabel("Number of vertexes in group 2"));
			panel.add(numVertexesRightField);
			panel.add(new JLabel("Group spacing"));
			panel.add(groupSpacingField);
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);

			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int leftNodeCount = Integer.parseInt(numVertexesLeftField.getText());
					int rightNodeCount = Integer.parseInt(numVertexesRightField.getText());
					float spacing = Float.parseFloat(groupSpacingField.getText());
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					configAnalysisGraph(graph, generator, props);
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					if (graphType2 == GraphType.BIPARTITE)
					{
						generator.getBipartiteGraph(aGraph, leftNodeCount, rightNodeCount);
					}
					else if (graphType2 == GraphType.COMPLETE_BIPARTITE)
					{
						generator.getCompleteBipartiteGraph(aGraph, leftNodeCount, rightNodeCount);
					}

					generator.setBipartiteGraphSpacing(aGraph, leftNodeCount, rightNodeCount, spacing, spacing * 2);
					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					setVisible(false);
					graph.getModel().endUpdate();
				}
			});

			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
			// setLocationRelativeTo(parent);
		}
		else if (graphType2 == GraphType.SIMPLE_RANDOM)
		{
			JPanel panel = new JPanel(new GridLayout(15, 2, 4, 4));
			panel.add(new JLabel("Number of nodes"));
			panel.add(numNodesField);
			panel.add(new JLabel("Number of edges"));
			panel.add(numEdgesField);
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);
			panel.add(selfLoopBox = new JCheckBox("Allow self-loops", false));
			panel.add(multipleEdgeBox = new JCheckBox("Allow multiple edges", false));
			panel.add(forceConnectedBox = new JCheckBox("Always connected (edge count may be inaccurate)", false));
			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
			buttonPanel.add(closeButton);
			buttonPanel.add(applyButton);
			getRootPane().setDefaultButton(applyButton);

			applyButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					applyValues();
					int nodeCount = Integer.parseInt(numNodesField.getText());
					int edgeCount = Integer.parseInt(numEdgesField.getText());
					int minWeightParam = Integer.parseInt(minWeightField.getText());
					int maxWeightParam = Integer.parseInt(maxWeightField.getText());
					Map<String, Object> props = new HashMap<String, Object>();
					mxGraphProperties.setDirected(props, arrows);
					mxGraphGenerator generator = new mxGraphGenerator(mxGraphGenerator.getGeneratorFunction(graph, weighted,
							minWeightParam, maxWeightParam), new mxDoubleValCostFunction());
					configAnalysisGraph(graph, generator, props);
					graph.getModel().beginUpdate();
					graph.selectAll();
					graph.removeCells();

					generator.getSimpleRandomGraph(aGraph, nodeCount, edgeCount, allowSelfLoops, allowMultipleEdges, forceConnected);

					mxGraphStructure.setDefaultGraphStyle(aGraph, false);
					mxOrganicLayout layout = new mxOrganicLayout(graph);
					layout.execute(graph.getDefaultParent());
					graph.getModel().endUpdate();
					setVisible(false);
				}
			});
			closeButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					insertGraph = false;
					setVisible(false);
				}
			});

			getContentPane().add(panelBorder, BorderLayout.CENTER);
			getContentPane().add(buttonPanel, BorderLayout.SOUTH);
			pack();
			setResizable(false);
		}
		else if (graphType2 == GraphType.RESET_STYLE)
		{
			JPanel panel = new JPanel(new GridLayout(4, 2, 4, 4));
			panel.add(arrowsBox = new JCheckBox("Directed", false));
			panel.add(weightedBox = new JCheckBox("Weighted", false));
			panel.add(new JLabel("Min. weight"));
			panel.add(minWeightField);
			panel.add(new JLabel("Max. weight"));
			panel.add(maxWeightField);
			JPanel panelBorder = new JPanel();
			panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10));
			panelBorder.add(panel);

			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
					BorderFactory.createEmptyBorder(16, 8, 8, 8)));

			JButton applyButton = new JButton("Generate");
			JButton closeButton = new JButton("Cancel");
