FlexiblePane.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 java.util.ArrayList;
  8. import java.util.LinkedList;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JPanel;
  11. import javax.swing.JScrollPane;
  12. import classes.AbstractCpsObject;
  13. import classes.HolonObject;
  14. import classes.SubNet;
  15. import interfaces.ObjectListener;
  16. import ui.controller.Control;
  17. public class FlexiblePane extends JScrollPane implements ObjectListener {
  18. private JPanel flexPanel;
  19. private Control controller;
  20. public FlexiblePane(Control controller) {
  21. this.controller = controller;
  22. flexPanel = new JPanel();
  23. flexPanel.setLayout(new BoxLayout(flexPanel, BoxLayout.Y_AXIS));
  24. setViewportView(flexPanel);
  25. class ResizeListener extends ComponentAdapter{
  26. public void componentResized(ComponentEvent e){
  27. flexPanel.requestFocusInWindow();
  28. }
  29. }
  30. this.addComponentListener(new ResizeListener());
  31. }
  32. public void recalculate(){
  33. LinkedList<Color> netColors = controller.getModel().getSubNetColors();
  34. flexPanel.removeAll();
  35. ArrayList<Float> maxVals = calcMax(controller.getSimManager().getSubNets());
  36. float gridProd = maxVals.get(0);
  37. float gridCons = maxVals.get(1);
  38. FlexibleData maingrid = new FlexibleData("Main Grid", gridProd, gridCons, gridProd, gridCons);
  39. maingrid.getColorPanel().setVisible(false);
  40. maingrid.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
  41. flexPanel.add(maingrid);
  42. int counter = 0;
  43. int colorCounter = 0;
  44. ArrayList<SubNet> subnets = controller.getSimManager().getSubNets();
  45. for(SubNet sn: subnets){
  46. if(netColors.size() < subnets.size()){
  47. netColors.add(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
  48. }
  49. JPanel objects = new JPanel();
  50. objects.setLayout(new BoxLayout(objects, BoxLayout.Y_AXIS));
  51. int index = flexPanel.getComponentCount();
  52. float subProd = 0;
  53. float subCons = 0;
  54. Color subColor = netColors.get(colorCounter);
  55. for(HolonObject hl: sn.getObjects()){
  56. subProd += hl.getFlexProd();
  57. subCons += hl.getFlexCons();
  58. FlexibleData tmp = new FlexibleData(hl.getName()+" "+hl.getId(),
  59. hl.getFlexProd(), hl.getFlexCons(), gridProd, gridCons);
  60. tmp.getColorPanel().setBackground(subColor);
  61. objects.add(tmp);
  62. }
  63. FlexibleData subnet = new FlexibleData("Subnet "+ counter, subProd, subCons, gridProd, gridCons);
  64. subnet.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
  65. subnet.getColorPanel().setBackground(subColor);
  66. FlexSubData wholeSubnet = new FlexSubData(subnet);
  67. wholeSubnet.setObjects(objects);
  68. wholeSubnet.setListener(this);
  69. flexPanel.add(wholeSubnet, index);
  70. counter++;
  71. colorCounter++;
  72. }
  73. flexPanel.revalidate();
  74. flexPanel.repaint();
  75. flexPanel.updateUI();
  76. }
  77. public JPanel getPanel(){
  78. return flexPanel;
  79. }
  80. public ArrayList<Float> calcMax(ArrayList<SubNet> sn){
  81. ArrayList<Float> result = new ArrayList<Float>();
  82. float prod = 0;
  83. float cons = 0;
  84. if(sn != null){
  85. for(SubNet s: sn){
  86. for(HolonObject h: s.getObjects()){
  87. prod += h.getFlexProd();
  88. cons += h.getFlexCons();
  89. }
  90. }
  91. result.add(prod);
  92. result.add(cons);
  93. return result;
  94. }else{
  95. result.add(prod);
  96. result.add(cons);
  97. return result;
  98. }
  99. }
  100. @Override
  101. public void onChange(ArrayList<AbstractCpsObject> objects) {
  102. recalculate();
  103. }
  104. }