MainView.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package view;
  2. import java.awt.Component;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ComponentAdapter;
  5. import java.awt.event.ComponentEvent;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLayeredPane;
  11. import javax.swing.JPanel;
  12. import animation.Action;
  13. import animation.AnimationController;
  14. import bk.BKNodePlacement;
  15. import bk.ExtremalLayoutCalc.LayoutType;
  16. import graph.LayeredGraphEdge;
  17. import graph.LayeredGraphNode;
  18. import graph.io.Writer;
  19. /**
  20. * The main window of the application.
  21. * There should only be one instance of this class at the same time.
  22. * The JFrame of that single instance can be accessed by the static field {code MainView.frame}.
  23. * @author kolja
  24. *
  25. */
  26. public class MainView {
  27. /**
  28. * The 'frame' of the main window.
  29. * The reason why there can only be one instance of this class.
  30. */
  31. public static JFrame frame;
  32. AnimationController controller;
  33. private String strToLen( String s, int l )
  34. {
  35. while( s.length() < l )
  36. {
  37. s = " " + s + " ";
  38. }
  39. if( s.length() > l )
  40. return s.substring( 0, l );
  41. return s;
  42. }
  43. /**
  44. * Initialize the window and its contents.
  45. * @param graph the graph that is displayed in this window.
  46. */
  47. public MainView( LayeredGraphNode graph )
  48. {
  49. controller = new AnimationController();
  50. controller.setTimeBetween( 10 );
  51. frame = new JFrame(); // this may write to a static field because there should be only one instance of this class.
  52. frame.setSize( Math.min( (int)graph.getWidth( LayoutType.TOP_BOTTOM_LEFT ) * 2 + 200, 1700 ), Math.min( (int)graph.getHeight( LayoutType.TOP_BOTTOM_LEFT ) * 2 + 200, 900 ) );
  53. frame.setLocation( 100, 100 );
  54. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  55. frame.setVisible( true );
  56. frame.addKeyListener( new KeyListener() {
  57. @Override
  58. public void keyTyped(KeyEvent e) {
  59. // TODO Auto-generated method stub
  60. }
  61. @Override
  62. public void keyPressed(KeyEvent e) {
  63. if( e.getKeyCode() == KeyEvent.VK_S )
  64. {
  65. Writer w = new Writer( "save.json" );
  66. w.writeOutputGraph( graph );
  67. }
  68. if( e.getKeyCode() == KeyEvent.VK_LEFT )
  69. controller.setNextAction( Action.BACKWARD );
  70. if( e.getKeyCode() == KeyEvent.VK_RIGHT )
  71. controller.setNextAction( Action.FORWARD );
  72. if( e.getKeyCode() == KeyEvent.VK_P )
  73. controller.setContinuous( !controller.isContinuous() );
  74. frame.validate();
  75. if( e.getKeyCode() == KeyEvent.VK_D )
  76. {
  77. System.out.println( "Debug Information Table: " );
  78. System.out.println( "_______________________________________________________________________________________________________________________________________________________________________________________________________________________" );
  79. System.out.println( "|" + strToLen( "Top -> Bottom :> Left", 51 ) + "| |" + strToLen( "Top -> Bottom :> Right", 51 ) + "| |" + strToLen( "Bottom -> Top :> Left", 51 ) + "| |" + strToLen( "Bottom -> Top :> Right", 51 ) + "|" );
  80. System.out.println( "|___________________________________________________| |___________________________________________________| |___________________________________________________| |___________________________________________________|" );
  81. System.out.println( "| 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 |" );
  82. for( LayeredGraphNode n : graph.getContainedNodes() )
  83. {
  84. System.out.println( "|" + strToLen( n.getName(), 6 ) +
  85. "|" + strToLen( n.getShift( LayoutType.TOP_BOTTOM_LEFT ) + "", 7 ) +
  86. "|" + strToLen( n.getSink( LayoutType.TOP_BOTTOM_LEFT ).getName(), 6 ) +
  87. "|" + strToLen( n.getRoot( LayoutType.TOP_BOTTOM_LEFT ).getName(), 6 ) +
  88. "|" + strToLen( n.getAlignedTo( LayoutType.TOP_BOTTOM_LEFT ).getName(), 7 ) +
  89. "|" + strToLen( n.getX( LayoutType.TOP_BOTTOM_LEFT ) + "", 5 ) +
  90. "|" + strToLen( !n.isXUndefined( LayoutType.TOP_BOTTOM_LEFT ) + "", 8 ) + "| " +
  91. "|" + strToLen( n.getName(), 6 ) +
  92. "|" + strToLen( n.getShift( LayoutType.TOP_BOTTOM_RIGHT ) + "", 7 ) +
  93. "|" + strToLen( n.getSink( LayoutType.TOP_BOTTOM_RIGHT ).getName(), 6 ) +
  94. "|" + strToLen( n.getRoot( LayoutType.TOP_BOTTOM_RIGHT ).getName(), 6 ) +
  95. "|" + strToLen( n.getAlignedTo( LayoutType.TOP_BOTTOM_RIGHT ).getName(), 7 ) +
  96. "|" + strToLen( n.getX( LayoutType.TOP_BOTTOM_RIGHT ) + "", 5 ) +
  97. "|" + strToLen( !n.isXUndefined( LayoutType.TOP_BOTTOM_RIGHT ) + "", 8 ) + "| " +
  98. "|" + strToLen( n.getName(), 6 ) +
  99. "|" + strToLen( n.getShift( LayoutType.BOTTOM_TOP_LEFT ) + "", 7 ) +
  100. "|" + strToLen( n.getSink( LayoutType.BOTTOM_TOP_LEFT ).getName(), 6 ) +
  101. "|" + strToLen( n.getRoot( LayoutType.BOTTOM_TOP_LEFT ).getName(), 6 ) +
  102. "|" + strToLen( n.getAlignedTo( LayoutType.BOTTOM_TOP_LEFT ).getName(), 7 ) +
  103. "|" + strToLen( n.getX( LayoutType.BOTTOM_TOP_LEFT ) + "", 5 ) +
  104. "|" + strToLen( !n.isXUndefined( LayoutType.BOTTOM_TOP_LEFT ) + "", 8 ) + "| " +
  105. "|" + strToLen( n.getName(), 6 ) +
  106. "|" + strToLen( n.getShift( LayoutType.BOTTOM_TOP_RIGHT ) + "", 7 ) +
  107. "|" + strToLen( n.getSink( LayoutType.BOTTOM_TOP_RIGHT ).getName(), 6 ) +
  108. "|" + strToLen( n.getRoot( LayoutType.BOTTOM_TOP_RIGHT ).getName(), 6 ) +
  109. "|" + strToLen( n.getAlignedTo( LayoutType.BOTTOM_TOP_RIGHT ).getName(), 7 ) +
  110. "|" + strToLen( n.getX( LayoutType.BOTTOM_TOP_RIGHT ) + "", 5 ) +
  111. "|" + strToLen( !n.isXUndefined( LayoutType.BOTTOM_TOP_RIGHT ) + "", 8 ) + "|");
  112. }
  113. System.out.println( "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" );
  114. }
  115. }
  116. @Override
  117. public void keyReleased(KeyEvent e) {
  118. // TODO Auto-generated method stub
  119. }
  120. });
  121. JLayeredPane layne = new JLayeredPane();
  122. JPanel pl = new JPanel();
  123. pl.setLayout(new GridLayout( 2, 2, 500, 500 ) );
  124. pl.setLocation( 0, 0 );
  125. pl.setSize( frame.getSize() );
  126. pl.add(createNodeView( graph, LayoutType.TOP_BOTTOM_LEFT ));
  127. pl.add(createNodeView( graph, LayoutType.TOP_BOTTOM_RIGHT ));
  128. pl.add(createNodeView( graph, LayoutType.BOTTOM_TOP_LEFT ));
  129. pl.add(createNodeView( graph, LayoutType.BOTTOM_TOP_RIGHT ));
  130. layne.add( pl, 1 );
  131. NodeView combined = createNodeView( graph, LayoutType.COMBINED );
  132. combined.setSize( 500, 500 );
  133. layne.add( combined, 0 );
  134. frame.add( layne );
  135. frame.validate();
  136. frame.repaint();
  137. frame.addComponentListener(new ComponentAdapter()
  138. {
  139. public void componentResized(ComponentEvent evt) {
  140. pl.setSize( layne.getSize() );
  141. combined.setLocation( layne.getWidth() / 2 - combined.getWidth() / 2, layne.getHeight() / 2 - combined.getHeight() / 2 );
  142. }
  143. });
  144. new BKNodePlacement( controller, graph ).start();
  145. }
  146. private NodeView createNodeView( LayeredGraphNode gNode, LayoutType lt )
  147. {
  148. NodeView graphView = new NodeView( gNode, lt );
  149. graphView.setLayout( null );
  150. graphView.setOpaque( true );
  151. for( LayeredGraphNode n : gNode.getContainedNodes() )
  152. {
  153. NodeView nv = createNodeView( n, lt );
  154. nv.setBounds( nv.getX(), nv.getY(), nv.getWidth(), nv.getHeight() );
  155. graphView.add( nv );
  156. }
  157. for( LayeredGraphEdge e : gNode.getContainedEdges() )
  158. {
  159. EdgeView ev = new EdgeView( e, lt );
  160. ev.setOpaque( true );
  161. graphView.add( ev );
  162. }
  163. return graphView;
  164. }
  165. }