FlexiblePane.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package ui.view;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Font;
  5. import java.awt.event.ComponentAdapter;
  6. import java.awt.event.ComponentEvent;
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JPanel;
  9. import javax.swing.JScrollPane;
  10. import classes.HolonObject;
  11. import classes.SubNet;
  12. import ui.controller.Control;
  13. public class FlexiblePane extends JScrollPane {
  14. private JPanel flexPanel;
  15. private Control controller;
  16. public FlexiblePane(Control controller) {
  17. this.controller = controller;
  18. flexPanel = new JPanel();
  19. flexPanel.setLayout(new BoxLayout(flexPanel, BoxLayout.Y_AXIS));
  20. setViewportView(flexPanel);
  21. }
  22. public void recalculate(){
  23. flexPanel.removeAll();
  24. FlexibleData maingrid = new FlexibleData("Main Grid", 0,0);
  25. maingrid.getColorPanel().setVisible(false);
  26. maingrid.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
  27. flexPanel.add(maingrid);
  28. float gridProd = 0;
  29. float gridCons = 0;
  30. int counter = 1;
  31. for(SubNet sn: controller.getSimManager().getSubNets()){
  32. JPanel objects = new JPanel();
  33. objects.setLayout(new BoxLayout(objects, BoxLayout.Y_AXIS));
  34. int index = flexPanel.getComponentCount();
  35. float subProd = 0;
  36. float subCons = 0;
  37. Color subColor = sn.getObjects().get(0).getBorderColor();
  38. for(HolonObject hl: sn.getObjects()){
  39. subProd += hl.getFlexProd();
  40. subCons += hl.getFlexCons();
  41. FlexibleData tmp = new FlexibleData(hl.getName()+" "+hl.getId(),
  42. hl.getFlexProd(), hl.getFlexCons());
  43. tmp.getColorPanel().setBackground(subColor);
  44. objects.add(tmp);
  45. }
  46. gridProd += subProd;
  47. gridCons += subCons;
  48. FlexibleData subnet = new FlexibleData("Subnet "+ counter, subProd, subCons);
  49. subnet.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
  50. subnet.getColorPanel().setBackground(subColor);
  51. FlexSubData wholeSubnet = new FlexSubData(subnet);
  52. wholeSubnet.setObjects(objects);
  53. wholeSubnet.setListener(this);
  54. flexPanel.add(wholeSubnet, index);
  55. //flexPanel.add(new FlexibleData("Subnet "+ counter, subProd, subCons), index);
  56. counter++;
  57. }
  58. if (flexPanel.getComponent(0) instanceof FlexibleData){
  59. ((FlexibleData)flexPanel.getComponent(0)).setProd(gridProd);
  60. ((FlexibleData)flexPanel.getComponent(0)).setCons(gridCons);
  61. ((FlexibleData)flexPanel.getComponent(0)).setProdConsVal(gridProd, gridCons);
  62. }
  63. class ResizeListener extends ComponentAdapter{
  64. public void componentResized(ComponentEvent e){
  65. flexPanel.requestFocusInWindow();
  66. }
  67. }
  68. this.addComponentListener(new ResizeListener());
  69. }
  70. public JPanel getPanel(){
  71. return flexPanel;
  72. }
  73. }