123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package ui.view;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.event.ComponentAdapter;
- import java.awt.event.ComponentEvent;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import javax.swing.BoxLayout;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import classes.AbstractCpsObject;
- import classes.HolonObject;
- import classes.SubNet;
- import interfaces.ObjectListener;
- import ui.controller.Control;
- public class FlexiblePane extends JScrollPane implements ObjectListener {
- private JPanel flexPanel;
- private Control controller;
-
- public FlexiblePane(Control controller) {
- this.controller = controller;
- flexPanel = new JPanel();
- flexPanel.setLayout(new BoxLayout(flexPanel, BoxLayout.Y_AXIS));
- setViewportView(flexPanel);
- class ResizeListener extends ComponentAdapter{
- public void componentResized(ComponentEvent e){
- flexPanel.requestFocusInWindow();
- }
- }
- this.addComponentListener(new ResizeListener());
- }
-
- public void recalculate(){
- LinkedList<Color> netColors = controller.getModel().getSubNetColors();
- flexPanel.removeAll();
- ArrayList<Float> maxVals = calcMax(controller.getSimManager().getSubNets());
- float gridProd = maxVals.get(0);
- float gridCons = maxVals.get(1);
- FlexibleData maingrid = new FlexibleData("Main Grid", gridProd, gridCons, gridProd, gridCons);
- maingrid.getColorPanel().setVisible(false);
- maingrid.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
- flexPanel.add(maingrid);
- int counter = 0;
- int colorCounter = 0;
- ArrayList<SubNet> subnets = controller.getSimManager().getSubNets();
- for(SubNet sn: subnets){
- if(netColors.size() < subnets.size()){
- netColors.add(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
- }
- JPanel objects = new JPanel();
- objects.setLayout(new BoxLayout(objects, BoxLayout.Y_AXIS));
- int index = flexPanel.getComponentCount();
- float subProd = 0;
- float subCons = 0;
- Color subColor = netColors.get(colorCounter);
- for(HolonObject hl: sn.getObjects()){
- subProd += hl.getFlexProd();
- subCons += hl.getFlexCons();
- FlexibleData tmp = new FlexibleData(hl.getName()+" "+hl.getId(),
- hl.getFlexProd(), hl.getFlexCons(), gridProd, gridCons);
- tmp.getColorPanel().setBackground(subColor);
- objects.add(tmp);
- }
- FlexibleData subnet = new FlexibleData("Subnet "+ counter, subProd, subCons, gridProd, gridCons);
- subnet.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
- subnet.getColorPanel().setBackground(subColor);
- FlexSubData wholeSubnet = new FlexSubData(subnet);
- wholeSubnet.setObjects(objects);
- wholeSubnet.setListener(this);
- flexPanel.add(wholeSubnet, index);
- counter++;
- colorCounter++;
- }
- flexPanel.revalidate();
- flexPanel.repaint();
- flexPanel.updateUI();
- }
-
- public JPanel getPanel(){
- return flexPanel;
- }
-
- public ArrayList<Float> calcMax(ArrayList<SubNet> sn){
- ArrayList<Float> result = new ArrayList<Float>();
- float prod = 0;
- float cons = 0;
- if(sn != null){
- for(SubNet s: sn){
- for(HolonObject h: s.getObjects()){
- prod += h.getFlexProd();
- cons += h.getFlexCons();
- }
- }
- result.add(prod);
- result.add(cons);
- return result;
- }else{
- result.add(prod);
- result.add(cons);
- return result;
- }
- }
- @Override
- public void onChange(ArrayList<AbstractCpsObject> objects) {
- recalculate();
- }
- }
|