123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806 |
- package view;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ComponentAdapter;
- import java.awt.event.ComponentEvent;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JLayeredPane;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.JTree;
- import javax.swing.event.DocumentEvent;
- import javax.swing.event.DocumentListener;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.tree.DefaultTreeModel;
- import javax.swing.tree.TreePath;
- import org.eclipse.elk.graph.ElkNode;
- import animation.Action;
- import animation.AnimationController;
- import bk.BKNodePlacement;
- import bk.ExtremalLayoutCalc.LayoutType;
- import graph.InitializeNodePositions;
- import graph.LayeredGraphEdge;
- import graph.LayeredGraphNode;
- import graph.LayeredNode;
- import graph.RandomGraphGenerator;
- import graph.io.Reader;
- import graph.io.Writer;
- import lib.SweepCrossingMinimizer;
- /**
- * The main window of the application.
- * There should only be one instance of this class at the same time.
- * The JFrame of that single instance can be accessed by the static field {code MainView.frame}.
- * @author kolja
- *
- */
- public class MainView {
- /**
- * The 'frame' of the main window.
- * The reason why there can only be one instance of this class.
- */
- private static int frameCounter = 0;
- private JFrame frame;
- private AnimationController controller;
- private JButton stepForward;
- private JButton stepForwardInto;
- private JButton stepForwardOut;
- private JButton stepBackward;
- private JButton stepBackwardInto;
- private JButton stepBackwardOut;
- private JButton runForward;
- private JButton runBackward;
- private JButton pause;
- private JButton load;
- private JButton save;
- private JButton debug;
- private JButton randomGraph;
- private JLabel delayText;
- private JTextField delay;
- public JTree pseudoTree;
- private LayeredGraphNode graph;
- private String strToLen( String s, int l )
- {
- while( s.length() < l )
- {
- s = " " + s + " ";
- }
- if( s.length() > l )
- return s.substring( 0, l );
- return s;
- }
- private String debugInfo()
- {
- String info = "Debug Information Table: \n";
- info += "_______________________________________________________________________________________________________________________________________________________________________________________________________________________\n";
- info += "|" + strToLen( "Top -> Bottom :> Left", 51 ) + "| |" + strToLen( "Top -> Bottom :> Right", 51 ) + "| |" + strToLen( "Bottom -> Top :> Left", 51 ) + "| |" + strToLen( "Bottom -> Top :> Right", 51 ) + "|\n";
- info += "|___________________________________________________| |___________________________________________________| |___________________________________________________| |___________________________________________________|\n";
- info += "| Node | Shift | Sink | Root | Align | x | xDef | | Node | Shift | Sink | Root | Align | x | xDef | | Node | Shift | Sink | Root | Align | x | xDef | | Node | Shift | Sink | Root | Align | x | xDef |\n";
- for( LayeredGraphNode n : graph.getContainedNodes() )
- {
- info += "|" + strToLen( n.getName(), 6 ) +
- "|" + strToLen( n.getShift( LayoutType.TOP_BOTTOM_LEFT ) + "", 7 ) +
- "|" + strToLen( n.getSink( LayoutType.TOP_BOTTOM_LEFT ).getName(), 6 ) +
- "|" + strToLen( n.getRoot( LayoutType.TOP_BOTTOM_LEFT ).getName(), 6 ) +
- "|" + strToLen( n.getAlign( LayoutType.TOP_BOTTOM_LEFT ).getName(), 7 ) +
- "|" + strToLen( n.getX( LayoutType.TOP_BOTTOM_LEFT ) + "", 5 ) +
- "|" + strToLen( !n.isXUndefined( LayoutType.TOP_BOTTOM_LEFT ) + "", 8 ) + "| " +
- "|" + strToLen( n.getName(), 6 ) +
- "|" + strToLen( n.getShift( LayoutType.TOP_BOTTOM_RIGHT ) + "", 7 ) +
- "|" + strToLen( n.getSink( LayoutType.TOP_BOTTOM_RIGHT ).getName(), 6 ) +
- "|" + strToLen( n.getRoot( LayoutType.TOP_BOTTOM_RIGHT ).getName(), 6 ) +
- "|" + strToLen( n.getAlign( LayoutType.TOP_BOTTOM_RIGHT ).getName(), 7 ) +
- "|" + strToLen( n.getX( LayoutType.TOP_BOTTOM_RIGHT ) + "", 5 ) +
- "|" + strToLen( !n.isXUndefined( LayoutType.TOP_BOTTOM_RIGHT ) + "", 8 ) + "| " +
- "|" + strToLen( n.getName(), 6 ) +
- "|" + strToLen( n.getShift( LayoutType.BOTTOM_TOP_LEFT ) + "", 7 ) +
- "|" + strToLen( n.getSink( LayoutType.BOTTOM_TOP_LEFT ).getName(), 6 ) +
- "|" + strToLen( n.getRoot( LayoutType.BOTTOM_TOP_LEFT ).getName(), 6 ) +
- "|" + strToLen( n.getAlign( LayoutType.BOTTOM_TOP_LEFT ).getName(), 7 ) +
- "|" + strToLen( n.getX( LayoutType.BOTTOM_TOP_LEFT ) + "", 5 ) +
- "|" + strToLen( !n.isXUndefined( LayoutType.BOTTOM_TOP_LEFT ) + "", 8 ) + "| " +
- "|" + strToLen( n.getName(), 6 ) +
- "|" + strToLen( n.getShift( LayoutType.BOTTOM_TOP_RIGHT ) + "", 7 ) +
- "|" + strToLen( n.getSink( LayoutType.BOTTOM_TOP_RIGHT ).getName(), 6 ) +
- "|" + strToLen( n.getRoot( LayoutType.BOTTOM_TOP_RIGHT ).getName(), 6 ) +
- "|" + strToLen( n.getAlign( LayoutType.BOTTOM_TOP_RIGHT ).getName(), 7 ) +
- "|" + strToLen( n.getX( LayoutType.BOTTOM_TOP_RIGHT ) + "", 5 ) +
- "|" + strToLen( !n.isXUndefined( LayoutType.BOTTOM_TOP_RIGHT ) + "", 8 ) + "|\n";
- }
- info += "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
- return info;
- }
- private void showDebugInfo()
- {
- JFrame debugFrame = new JFrame();
- JTextArea info = new JTextArea();
- info.setEditable( false );
- info.setFont( new Font( Font.MONOSPACED, Font.PLAIN, 11 ) );
- String infoS = debugInfo();
- info.setText( infoS );
- JScrollPane view = new JScrollPane( info );
- debugFrame.add( view );
- debugFrame.setSize( frame.getWidth(), frame.getHeight() );
- debugFrame.setVisible( true );
- System.out.println( infoS );
- }
- public MainView( ElkNode graph )
- {
- this( LayeredNode.convertToLayeredGraph( graph ) );
- }
- /**
- * Initialize the window and its contents.
- * @param graph the graph that is displayed in this window.
- */
- public MainView( LayeredGraphNode graph )
- {
- frameCounter++;
- this.graph = graph;
- controller = new AnimationController();
- controller.setTimeBetween( 50 );
- frame = new JFrame( "NodeShuffler" );
- frame.addWindowListener(new java.awt.event.WindowAdapter() {
- @Override
- public void windowClosing(java.awt.event.WindowEvent windowEvent) {
- frameCounter--;
- if( frameCounter == 0 )
- System.exit( 0 );
- }
- });
-
- BKNodePlacement algorithm = new BKNodePlacement( controller, graph, frame );
-
- // Create Menu GUI
- stepForward = new NiceButton( "stepForward" );
- stepForward.setLocation( 10, 10 );
- stepForward.setMnemonic( KeyEvent.VK_DOWN );
- stepForward.setToolTipText( "Forward step over (alt + down arrow key)" );
- stepForward.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- controller.setNextAction( Action.FORWARD_OVER );
- }
-
- });
- stepForwardInto = new NiceButton( "stepForwardInto" );
- stepForwardInto.setLocation( 60, 10 );
- stepForwardInto.setMnemonic( KeyEvent.VK_RIGHT );
- stepForwardInto.setToolTipText( "Forward step into (alt + right arrow key)" );
- stepForwardInto.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- controller.setNextAction( Action.FORWARD );
- }
-
- });
- stepForwardOut = new NiceButton( "stepForwardOut" );
- stepForwardOut.setLocation( 110, 10 );
- stepForwardOut.setMnemonic( KeyEvent.VK_PAGE_DOWN );
- stepForwardOut.setToolTipText( "Forward step out (alt + page down key)" );
- stepForwardOut.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- controller.setNextAction( Action.FORWARD_OUT );
- }
-
- });
- runForward = new NiceButton( "runForward" );
- runForward.setLocation( 160, 10 );
- runForward.setMnemonic( KeyEvent.VK_P );
- runForward.setToolTipText( "Run forwards (alt + p)" );
- runForward.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( true );
- controller.setNextAction( Action.FORWARD );
- }
-
- });
- runBackward = new NiceButton( "runBackward" );
- runBackward.setLocation( 160, 60 );
- runBackward.setMnemonic( KeyEvent.VK_R );
- runBackward.setToolTipText( "Run backwards (alt + r)" );
- runBackward.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( true );
- controller.setNextAction( Action.BACKWARD );
- }
-
- });
- stepBackward = new NiceButton( "stepBackward" );
- stepBackward.setLocation( 10, 60 );
- stepBackward.setMnemonic( KeyEvent.VK_UP );
- stepBackward.setToolTipText( "Backward step over (alt + up arrow key)" );
- stepBackward.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- controller.setNextAction( Action.BACKWARD_OVER );
- }
-
- });
- stepBackwardInto = new NiceButton( "stepBackwardInto" );
- stepBackwardInto.setLocation( 60, 60 );
- stepBackwardInto.setMnemonic( KeyEvent.VK_LEFT );
- stepBackwardInto.setToolTipText( "Backward step into (alt + left arrow key)" );
- stepBackwardInto.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- controller.setNextAction( Action.BACKWARD );
- }
-
- });
- stepBackwardOut = new NiceButton( "stepBackwardOut" );
- stepBackwardOut.setLocation( 110, 60 );
- stepBackwardOut.setMnemonic( KeyEvent.VK_PAGE_UP );
- stepBackwardOut.setToolTipText( "Backward step out (alt + page up)" );
- stepBackwardOut.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- controller.setNextAction( Action.BACKWARD_OUT );
- }
-
- });
- pause = new NiceButton( "pause" );
- pause.setLocation( 210, 10 );
- pause.setMnemonic( KeyEvent.VK_PAUSE );
- pause.setToolTipText( "Pause (alt + pause)" );
- pause.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.setContinuous( false );
- }
-
- });
- debug = new NiceButton( "debug" );
- debug.setLocation( 350, 10 );
- debug.setMnemonic( KeyEvent.VK_D );
- debug.setToolTipText( "Show debug info (alt + d)" );
- debug.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- showDebugInfo();
- }
-
- });
- randomGraph = new NiceButton( "random" );
- randomGraph.setLocation( 350, 60 );
- randomGraph.setMnemonic( KeyEvent.VK_G );
- randomGraph.setToolTipText( "Generate random graph (alt + g)" );
- randomGraph.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JDialog diag = new JDialog( frame, "Generate random graph" );
- diag.setLayout( new GridBagLayout() );
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 0;
- diag.add( new JLabel( "P(subgraph exists)"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 0;
- JTextField pSubgraph = new JTextField( "0.1" );
- pSubgraph.setPreferredSize( new Dimension( 100, 20 ) );
- pSubgraph.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- pSubgraph.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- double d = Double.parseDouble( pSubgraph.getText() );
- if( d > 1 || d < 0 )
- pSubgraph.setBackground( Color.RED );
- } catch( Exception e1 )
- {
- pSubgraph.setBackground( Color.RED );
- }
- }
- });
- diag.add( pSubgraph, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 1;
- diag.add( new JLabel( "P(edge exists)"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 1;
- JTextField pEdge = new JTextField( "0.3" );
- pEdge.setPreferredSize( new Dimension( 100, 20 ) );
- pEdge.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- pEdge.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- double d = Double.parseDouble( pEdge.getText() );
- if( d > 1 || d < 0 )
- pEdge.setBackground( Color.RED );
- } catch( Exception e1 )
- {
- pEdge.setBackground( Color.RED );
- }
- }
- });
- diag.add( pEdge, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 2;
- diag.add( new JLabel( "min. num. layers"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 2;
- JTextField minLayers = new JTextField( "5" );
- JTextField maxLayers = new JTextField( "5" );
- minLayers.setPreferredSize( new Dimension( 100, 20 ) );
- minLayers.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- minLayers.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- int i = Integer.parseInt( minLayers.getText() );
- int max = Integer.parseInt( maxLayers.getText() );
- if( i < 1 || i > max )
- minLayers.setBackground( Color.RED );
- else
- maxLayers.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- minLayers.setBackground( Color.RED );
- }
- }
- });
- diag.add( minLayers, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 3;
- diag.add( new JLabel( "max. num. layers"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 3;
- maxLayers.setPreferredSize( new Dimension( 100, 20 ) );
- maxLayers.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- maxLayers.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- int i = Integer.parseInt( maxLayers.getText() );
- int min = Integer.parseInt( minLayers.getText() );
- if( i < min )
- maxLayers.setBackground( Color.RED );
- else if( min > 0 )
- minLayers.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- maxLayers.setBackground( Color.RED );
- }
- }
- });
- diag.add( maxLayers, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 4;
- diag.add( new JLabel( "min. num. nodes"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 4;
- JTextField minNodes = new JTextField( "5" );
- JTextField maxNodes = new JTextField( "5" );
- minNodes.setPreferredSize( new Dimension( 100, 20 ) );
- minNodes.setToolTipText( "between 1 and 'min. num. nodes'" );
- minNodes.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- minNodes.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- int i = Integer.parseInt( minNodes.getText() );
- int max = Integer.parseInt( maxNodes.getText() );
- if( i < 1 || i > max )
- minNodes.setBackground( Color.RED );
- else
- minNodes.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- minNodes.setBackground( Color.RED );
- }
- }
- });
- diag.add( minNodes, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 5;
- diag.add( new JLabel( "max. num. nodes"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 5;
- maxNodes.setPreferredSize( new Dimension( 100, 20 ) );
- maxNodes.setToolTipText( "between 'min. num. nodes' and +Inf" );
- maxNodes.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- maxNodes.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- int i = Integer.parseInt( maxNodes.getText() );
- int min = Integer.parseInt( minNodes.getText() );
- if( i < min )
- maxNodes.setBackground( Color.RED );
- else if( min > 0 )
- minNodes.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- maxNodes.setBackground( Color.RED );
- }
- }
- });
- diag.add( maxNodes, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 6;
- diag.add( new JLabel( "max. hier. depth"), c );
- c = new GridBagConstraints();
- c.gridx = 1;
- c.gridy = 6;
- JTextField maxDepth = new JTextField( "1" );
- maxDepth.setPreferredSize( new Dimension( 100, 20 ) );
- maxDepth.setToolTipText( "between 1 and +Inf" );
- maxDepth.addFocusListener( new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- maxDepth.setBackground( Color.WHITE );
- }
- @Override
- public void focusLost(FocusEvent e) {
- try {
- int i = Integer.parseInt( maxDepth.getText() );
- if( i < 1 )
- maxDepth.setBackground( Color.RED );
- } catch( Exception e1 )
- {
- maxDepth.setBackground( Color.RED );
- }
- }
- });
- diag.add( maxDepth, c );
- c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 7;
- c.gridwidth = 2;
- JButton gen = new JButton( "generate");
- gen.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- double pSubGraphD = Double.parseDouble( pSubgraph.getText() );
- double pEdgeD = Double.parseDouble( pEdge.getText() );
- int minLayerI = Integer.parseInt( minLayers.getText() );
- int maxLayerI = Integer.parseInt( maxLayers.getText() );
- int minNodeI = Integer.parseInt( minNodes.getText() );
- int maxNodeI = Integer.parseInt( maxNodes.getText() );
- int maxDepthI = Integer.parseInt( maxDepth.getText() );
- boolean ok = true;
- if( pSubGraphD < 0 || pSubGraphD > 1 )
- {
- pSubgraph.setBackground( Color.RED );
- ok = false;
- }
- if( pEdgeD < 0 || pEdgeD > 1 )
- {
- pEdge.setBackground( Color.RED );
- ok = false;
- }
- if( minLayerI < 1 )
- {
- minLayers.setBackground( Color.RED );
- ok = false;
- }
- if( maxLayerI < minLayerI )
- {
- maxLayers.setBackground( Color.RED );
- ok = false;
- }
- if( minNodeI < 1 )
- {
- minNodes.setBackground( Color.RED );
- ok = false;
- }
- if( maxNodeI < minNodeI )
- {
- maxNodes.setBackground( Color.RED );
- ok = false;
- }
- if( maxDepthI < 1 )
- {
- maxDepth.setBackground( Color.RED );
- ok = false;
- }
- if( ok )
- {
- RandomGraphGenerator r = new RandomGraphGenerator( pSubGraphD, pEdgeD, minLayerI, maxLayerI, minNodeI, maxNodeI, maxDepthI );
- try {
- LayeredGraphNode graph = r.createRandomNode( null, 0, true );
- SweepCrossingMinimizer cminzer = new SweepCrossingMinimizer();
- for( int i = 0; i < 10; i++ )
- cminzer.minimizeCrossings( graph );
- InitializeNodePositions.placeNodes( graph );
- new MainView( graph );
- diag.setVisible( false );
- } catch( Exception e1 )
- {
- JOptionPane.showMessageDialog(frame, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
-
- });
- diag.add( gen, c );
- diag.setSize( 270, 220 );
- diag.setLocation( frame.getX() + frame.getWidth() / 2 - diag.getWidth() / 2, frame.getY() + frame.getHeight() / 2 - diag.getHeight() / 2 );
- diag.setVisible( true );
- }
- });
- delayText = new JLabel( "Delay (ms)" );
- delayText.setBounds( 260, 10, 80, 20 );
- delay = new JTextField( "50" );
- delay.setBounds( 260, 30, 80, 20 );
- delay.getDocument().addDocumentListener( new DocumentListener() {
- @Override
- public void insertUpdate(DocumentEvent e) {
- try
- {
- controller.setTimeBetween( Integer.parseInt( delay.getText() ) );
- delay.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- delay.setBackground( Color.RED );
- }
- }
- @Override
- public void removeUpdate(DocumentEvent e) {
- try
- {
- controller.setTimeBetween( Integer.parseInt( delay.getText() ) );
- delay.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- delay.setBackground( Color.RED );
- }
- }
- @Override
- public void changedUpdate(DocumentEvent e) {
- try
- {
- controller.setTimeBetween( Integer.parseInt( delay.getText() ) );
- delay.setBackground( Color.WHITE );
- } catch( Exception e1 )
- {
- delay.setBackground( Color.RED );
- }
- }
-
- });
- load = new NiceButton( "load" );
- load.setLocation( 230, 60 );
- load.setMnemonic( KeyEvent.VK_L );
- load.setToolTipText( "Load a graph (alt + l)" );
- load.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setFileFilter( new FileNameExtensionFilter("Json Graph", "json") );
- chooser.showOpenDialog( frame );
- if( chooser.getSelectedFile() != null )
- {
- Reader r = new Reader( chooser.getSelectedFile().getAbsolutePath() );
- LayeredGraphNode graph = r.readInputGraph();
- InitializeNodePositions.placeNodes( graph );
- new MainView( graph );
- }
- }
-
- });
- save = new NiceButton( "save" );
- save.setLocation( 285, 60 );
- save.setMnemonic( KeyEvent.VK_S );
- save.setToolTipText( "Save graph (alt + s)" );
- save.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setFileFilter( new FileNameExtensionFilter("Json Graph", "json") );
- chooser.showSaveDialog( frame );
- if( chooser.getSelectedFile() != null )
- {
- Writer w = new Writer( chooser.getSelectedFile().getAbsolutePath() );
- w.writeOutputGraph( graph );
- }
- }
-
- });
- pseudoTree = new JTree();
- PseudoCodeNode tree = algorithm.createPseudocodeTree( pseudoTree );
- tree.setController( controller );
- pseudoTree.setModel( new DefaultTreeModel( tree ) );
- pseudoTree.setCellRenderer( new PseudoCodeRenderer() );
- pseudoTree.setSelectionModel( null );
- pseudoTree.addMouseListener( new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- TreePath selPath = pseudoTree.getPathForLocation(e.getX(), e.getY());
- if( selPath != null && e.getClickCount() == 3 ) {
- ((PseudoCodeNode)selPath.getLastPathComponent()).setBreakPoint( !((PseudoCodeNode)selPath.getLastPathComponent()).hasBreakPoint() );
- pseudoTree.repaint();
- }
- }
- } );
- JScrollPane treeView = new JScrollPane( pseudoTree );
- treeView.setBounds( 10, 110, 380, 380 );
-
- frame.setSize( (int)graph.getWidth( LayoutType.TOP_BOTTOM_LEFT ) * 2 + 575, (int)graph.getHeight( LayoutType.TOP_BOTTOM_LEFT ) * 2 + 200 );
- frame.setLocation( 100, 100 );
- frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
- frame.setVisible( true );
- JLayeredPane layne = new JLayeredPane();
- JPanel pl = new JPanel();
- GridLayout grout = new GridLayout( 2, 2, 10, 10 );
- pl.setLayout( grout );
- pl.setLocation( 0, 0 );
- pl.setSize( frame.getSize() );
- NodeView topLeft = createNodeView( graph, LayoutType.TOP_BOTTOM_LEFT );
- NodeView topRight = createNodeView( graph, LayoutType.TOP_BOTTOM_RIGHT );
- NodeView bottomLeft = createNodeView( graph, LayoutType.BOTTOM_TOP_LEFT );
- NodeView bottomRight = createNodeView( graph, LayoutType.BOTTOM_TOP_RIGHT );
- pl.add( topLeft );
- pl.add( topRight );
- pl.add( bottomLeft );
- pl.add( bottomRight );
- layne.add( pl, 1 );
- NodeView combined = createNodeView( graph, LayoutType.COMBINED );
- combined.setSize( 500, 500 );
- layne.add( combined, 0 );
- frame.add( layne );
- JPanel menue = new JPanel();
- menue.setLayout( null );
- menue.setPreferredSize( new Dimension( 400, 500 ) );
- menue.add( stepForward );
- menue.add( stepForwardInto );
- menue.add( stepForwardOut );
- menue.add( runForward );
- menue.add( pause );
- menue.add( debug );
- menue.add( stepBackward );
- menue.add( delayText );
- menue.add( delay );
- menue.add( treeView );
- menue.add( stepBackwardInto );
- menue.add( stepBackwardOut );
- menue.add( runBackward );
- menue.add( randomGraph );
- menue.add( save );
- menue.add( load );
- frame.add( menue, BorderLayout.EAST );
- frame.setSize( frame.getWidth() + 1, frame.getHeight() );
- frame.setSize( frame.getWidth() - 1, frame.getHeight() );
- frame.validate();
- frame.repaint();
- frame.addComponentListener(new ComponentAdapter()
- {
- public void componentResized(ComponentEvent evt) {
- pl.setSize( layne.getSize() );
- menue.setSize( menue.getWidth(), layne.getHeight() );
- treeView.setSize( treeView.getWidth(), layne.getHeight() - 120 );
- if( graph.getColor( LayoutType.COMBINED ) == null )
- {
- grout.setHgap( 10 );
- grout.setVgap( 10 );
- }
- else
- {
- grout.setHgap( layne.getWidth() / 3 );
- grout.setVgap( layne.getHeight() / 3 );
- }
- combined.setSize( layne.getWidth() / 3, layne.getHeight() / 3 );
- combined.setLocation( layne.getWidth() / 3, layne.getHeight() / 3 );
- layne.remove( pl );
- layne.add( pl, 1 );
- frame.repaint();
- }
- });
- algorithm.start();
- }
- private NodeView createNodeView( LayeredGraphNode gNode, LayoutType lt )
- {
- NodeView graphView = new NodeView( gNode, lt );
- graphView.setLayout( null );
- graphView.setOpaque( true );
- for( LayeredGraphNode n : gNode.getContainedNodes() )
- {
- NodeView nv = createNodeView( n, lt );
- nv.setBounds( nv.getX(), nv.getY(), nv.getWidth(), nv.getHeight() );
- graphView.add( nv );
- }
- for( LayeredGraphEdge e : gNode.getContainedEdges() )
- {
- EdgeView ev = new EdgeView( e, lt );
- ev.setOpaque( true );
- graphView.add( ev );
- }
- return graphView;
- }
- }
|