123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- package ui.view;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.Insets;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- import javax.swing.BorderFactory;
- import javax.swing.BoxLayout;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JLayeredPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.OverlayLayout;
- import javax.swing.SwingUtilities;
- import javax.swing.border.EmptyBorder;
- import org.knowm.xchart.CategoryChart;
- import org.knowm.xchart.CategoryChartBuilder;
- import org.knowm.xchart.PieChart;
- import org.knowm.xchart.PieChartBuilder;
- import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
- import org.knowm.xchart.XChartPanel;
- import org.knowm.xchart.style.CategoryStyler;
- import org.knowm.xchart.style.PieStyler;
- import org.knowm.xchart.style.PieStyler.LabelType;
- import classes.AbstractCanvasObject;
- import classes.Flexibility;
- import classes.GroupNode;
- import classes.HolonElement;
- import classes.HolonObject;
- import preferences.ColorPreference;
- import ui.controller.Control;
- import ui.controller.FlexManager;
- import ui.controller.FlexManager.FlexWrapper;
- import ui.model.Consumer;
- import ui.model.DecoratedGroupNode;
- import ui.model.DecoratedGroupNode.PriorityCounts;
- import ui.model.DecoratedHolonObject;
- import ui.model.DecoratedHolonObject.HolonObjectState;
- import ui.model.Model;
- import ui.model.Passiv;
- import ui.model.Supplier;
- import util.StringFormat;
- public class HolonInformationPanel extends JPanel {
- private final int defaultWidth = 50;
- private final int defaultHeight = 200;
- private PieChart supplyChart = createSupplyStateChart();
- private PieChart priorityChart = createPriorityChart();
- private PieChart flexibilityChart = createFlexibilityChart();
- private PieChart energyChart = createProductionChart();
- private PieChart activeChart = createActiveChart();
- private XChartPanel<PieChart> panelHolonObject;
- private XChartPanel<PieChart> panelPriority;
- private XChartPanel<PieChart> panelFlexibility;
- private XChartPanel<PieChart> panelEnergy;
- private XChartPanel<PieChart> panelActive;
- // TODO is this needed to be fields
- private JPanel graphPanel = new JPanel(new GridLayout(0, 2));
- private JPanel titlePanel = new JPanel(new BorderLayout(0, 0));
- private Control control;
- private JTextField titleTextField;
- private JLabel differenceEnergyLabelAmount;
- public static void main(String[] args) {
- HolonInformationPanel exampleChart = new HolonInformationPanel(new Control(new Model()));
- JFrame frame = new JFrame();
- frame.setContentPane(exampleChart);
- frame.pack();
- frame.setBounds(0, 0, 540, 600);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public void updateCharts() {
- DecoratedGroupNode decoratedGroupNode;
- switch (control.getModel().getSelectedCpsObjects().size()) {
- case 0:
- return;
- case 1:
- titleTextField.setText(control.getModel().getSelectedCpsObjects().get(0).getName());
- decoratedGroupNode = multiSelectionToDecoratedGroupNode();
- break;
- default:
- titleTextField.setText(getMultiSelectionName());
- decoratedGroupNode = multiSelectionToDecoratedGroupNode();
- break;
- }
- // UPDATE SUPPLY STATE
- int producerAmount = decoratedGroupNode.getAmountOfSupplier();
- int overSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
- int suppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
- int partiallySuppliedAmount = decoratedGroupNode
- .getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
- int notSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
- int noEnergyAmount = decoratedGroupNode.getAmountOfPassiv();
- supplyChart.updatePieSeries("Producer", producerAmount);
- supplyChart.updatePieSeries("Over supplied", overSuppliedAmount);
- supplyChart.updatePieSeries("Supplied", suppliedAmount);
- supplyChart.updatePieSeries("Partial supplied", partiallySuppliedAmount);
- supplyChart.updatePieSeries("Not supplied", notSuppliedAmount);
- supplyChart.updatePieSeries("No energy", noEnergyAmount);
- panelHolonObject.updateToolTips();
- // UPDATE PRIORITYS
- PriorityCounts priorityCounts = decoratedGroupNode.getPriorityCounts();
- priorityChart.updatePieSeries("Essential", priorityCounts.essential);
- priorityChart.updatePieSeries("High", priorityCounts.high);
- priorityChart.updatePieSeries("Medium", priorityCounts.medium);
- priorityChart.updatePieSeries("Low", priorityCounts.low);
- boolean hasPriority = priorityCounts.essential + priorityCounts.high + priorityCounts.medium
- + priorityCounts.low > 0;
- priorityChart.updatePieSeries("No Data", hasPriority ? 0 : 1);
- panelPriority.updateToolTips();
- // UPDATE PRODUCTION
- float production = decoratedGroupNode.getProductionFromSupplier();
- float consumption = decoratedGroupNode.getConsumptionFromConsumer();
- float difference = Math.abs(production - consumption);
- energyChart.updatePieSeries("Production", production);
- energyChart.updatePieSeries("Consumption", consumption);
- differenceEnergyLabelAmount.setText(StringFormat.doubleFixedPlaces(1, difference));
- panelEnergy.updateToolTips();
- // UPDATE FLEXIBILITIES
- int inUse = 0;
- int offered = 0;
- int onCooldown = 0;
- int notOffered = 0;
- int unavailable = 0;
- FlexManager manager = control.getSimManager().getActualFlexManager();
- Stream<FlexWrapper> flexWrapperStream = decoratedGroupNode.getFlexibilitiesStream()
- .map(flex -> manager.getFlexWrapperFromFlexibility(flex));
- List<FlexWrapper> wrapperList = flexWrapperStream.collect(Collectors.toList());
- for (FlexWrapper wrapper : wrapperList) {
- switch (wrapper.getState()) {
- case IN_USE:
- inUse++;
- break;
- case NOT_OFFERED:
- notOffered++;
- break;
- case OFFERED:
- offered++;
- break;
- case ON_COOLDOWN:
- onCooldown++;
- break;
- case UNAVAILABLE:
- unavailable++;
- break;
- default:
- break;
- }
- }
- flexibilityChart.updatePieSeries("Offered", offered);
- flexibilityChart.updatePieSeries("In use", inUse);
- flexibilityChart.updatePieSeries("On cooldown", onCooldown);
- flexibilityChart.updatePieSeries("Not offered", notOffered);
- flexibilityChart.updatePieSeries("Unavailable", unavailable);
- boolean hasFlex = offered + inUse + onCooldown + notOffered + unavailable > 0;
- flexibilityChart.updatePieSeries("No Data", hasFlex ? 0 : 1);
- panelFlexibility.updateToolTips();
- // UPDATE ActiveInActive
- int activeAmount = decoratedGroupNode.getAmountOfAktiveElemntsFromHolonObjects();
- int inactiveAmounts = decoratedGroupNode.getAmountOfElemntsFromHolonObjects() - activeAmount;
- activeChart.updatePieSeries("Active", activeAmount);
- activeChart.updatePieSeries("Inactive", inactiveAmounts);
- panelActive.updateToolTips();
- this.revalidate();
- this.repaint();
- }
- private String getMultiSelectionName() {
- String name = control.getModel().getSelectedCpsObjects().stream().map(AbstractCanvasObject::getName)
- .collect(Collectors.joining(", "));
- if(name.length() > 25) {
- name = name.substring(0, 25);
- name += "...";
- }
- return name;
- }
- private DecoratedGroupNode multiSelectionToDecoratedGroupNode() {
- // TODO Create DecoratedGroupNode
- DecoratedGroupNode temp = new DecoratedGroupNode(new GroupNode("Temp"));
- // TODO get consumer, supplier, passiv
- // GroupNodes
- HashMap<GroupNode, DecoratedGroupNode> accessMapGroupNode = control.getSimManager()
- .getActualVisualRepresentationalState().getCreatedGroupNodes();
- List<DecoratedGroupNode> groupNodeList = control.getModel().getSelectedCpsObjects().stream()
- .filter(object -> object instanceof GroupNode).map(object -> accessMapGroupNode.get(object))
- .collect(Collectors.toList());
- temp.getGroupNodeList().addAll(groupNodeList);
- // HolonObjects
- HashMap<HolonObject, DecoratedHolonObject> accessMapHolonObject = control.getSimManager()
- .getActualVisualRepresentationalState().createdHolonObjects;
- List<DecoratedHolonObject> holonObjectList = control.getModel().getSelectedCpsObjects().stream()
- .filter(object -> object instanceof HolonObject).map(object -> accessMapHolonObject.get(object))
- .collect(Collectors.toList());
- for (DecoratedHolonObject object : holonObjectList) {
- switch (object.getState()) {
- case NO_ENERGY:
- temp.getPassivList().add((Passiv) object);
- break;
- case PRODUCER:
- temp.getSupplierList().add((Supplier) object);
- break;
- default:
- temp.getConsumerList().add((Consumer) object);
- break;
- }
- }
- return temp;
- }
- public HolonInformationPanel(Control control) {
- control.OnSelectionUpdate.addListener(() -> updateCharts());
- this.control = control;
- this.setLayout(new BorderLayout());
- initGraphPanel();
- initTitlePanel();
- this.setBackground(ColorPreference.Panel.Background);
- this.add(titlePanel, BorderLayout.PAGE_START);
- this.add(graphPanel, BorderLayout.CENTER);
- }
- public void initGraphPanel() {
- graphPanel.setBackground(ColorPreference.Panel.Background);
- panelHolonObject = new XChartPanel<PieChart>(supplyChart);
- panelHolonObject.setBackground(ColorPreference.Panel.Background);
- graphPanel.add(panelHolonObject);
- panelPriority = new XChartPanel<PieChart>(priorityChart);
- panelPriority.setBackground(ColorPreference.Panel.Background);
- graphPanel.add(panelPriority);
- panelFlexibility = new XChartPanel<PieChart>(flexibilityChart);
- panelFlexibility.setBackground(ColorPreference.Panel.Background);
- graphPanel.add(panelFlexibility);
- Component panel = initEnergyChart();
- graphPanel.add(panel);
- panelActive = new XChartPanel<PieChart>(activeChart);
- panelActive.setBackground(ColorPreference.Panel.Background);
- panelActive.setLayout(new GridBagLayout());
- graphPanel.add(panelActive);
- graphPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
- }
- private JLayeredPane initEnergyChart() {
- JLayeredPane panel = new JLayeredPane();
- JPanel panelMiddle = new JPanel(new GridBagLayout());
- panelEnergy = new XChartPanel<PieChart>(energyChart);
- panelEnergy.setBackground(ColorPreference.Panel.Background);
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 0;
- c.insets = new Insets(15, 0, 0, 0); // top padding
- JLabel difference = new JLabel("Difference:");
- difference.setHorizontalAlignment(JLabel.CENTER);
- difference.setAlignmentX(Component.CENTER_ALIGNMENT);
- difference.setForeground(ColorPreference.Panel.Title);
- difference.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 10));
- panelMiddle.add(difference, c);
- differenceEnergyLabelAmount = new JLabel("");
- differenceEnergyLabelAmount.setHorizontalAlignment(JLabel.CENTER);
- differenceEnergyLabelAmount.setForeground(Color.red);
- differenceEnergyLabelAmount.setAlignmentX(Component.CENTER_ALIGNMENT);
- differenceEnergyLabelAmount.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 12));
- c.insets = new Insets(0, 0, 0, 0);
- c.gridy = 1;
- panelMiddle.add(differenceEnergyLabelAmount, c);
- panel.setLayout(new OverlayLayout(panel));
- panelEnergy.setOpaque(false);
- panelMiddle.setOpaque(false);
- panel.add(panelMiddle, Integer.valueOf(0));
- panel.add(panelEnergy, Integer.valueOf(1));
- return panel;
- }
- public void initTitlePanel() {
- titleTextField = new JTextField("");
- titleTextField.setFont(new Font("Arial", Font.BOLD, 24));
- titleTextField.setForeground(ColorPreference.Panel.Title);
- titleTextField.setBackground(ColorPreference.Panel.Background);
- titleTextField.setBackground(null);
- titleTextField.setBorder(null);
- titlePanel.setBackground(ColorPreference.Panel.Background);
- titlePanel.setBorder(new EmptyBorder(5, 5, 2, 0));
- titlePanel.add(titleTextField, BorderLayout.CENTER);
- }
- public void setDefaultPieChartSettings(PieChart chart) {
- PieStyler styler = chart.getStyler();
- styler.setChartTitleVisible(true);
- styler.setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
- styler.setLabelsFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 14));
- styler.setLabelType(LabelType.Percentage);
- styler.setLabelsFontColor(Color.black);
- styler.setLabelsFontColorAutomaticEnabled(false);
- styler.setLabelsDistance(0.8);
- styler.setChartFontColor(ColorPreference.Panel.Title);
- styler.setToolTipsEnabled(true);
- styler.setPlotContentSize(0.9);
- styler.setPlotBackgroundColor(ColorPreference.Panel.Transparent);
- styler.setPlotBorderColor(ColorPreference.Panel.Transparent);
- styler.setLegendVisible(false);
- styler.setChartBackgroundColor(ColorPreference.Panel.Transparent);
- }
- public PieChart createSupplyStateChart() {
- PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Holon Objects").build();
- setDefaultPieChartSettings(chart);
- Color[] sliceColors = new Color[] { ColorPreference.HolonObject.Producer,
- ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.Supplied,
- ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.NotSupplied,
- ColorPreference.HolonObject.NoEnergy };
- chart.getStyler().setSeriesColors(sliceColors);
- // Series
- chart.addSeries("Producer", 0);
- chart.addSeries("Over supplied", 0);
- chart.addSeries("Supplied", 0);
- chart.addSeries("Partial supplied", 0);
- chart.addSeries("Not supplied", 0);
- chart.addSeries("No energy", 0);
- return chart;
- }
- public PieChart createPriorityChart() {
- // Create Chart
- PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Priotities").build();
- setDefaultPieChartSettings(chart);
- // Customize Chart
- Color[] sliceColors = new Color[] { ColorPreference.Element.Priority.Essential,
- ColorPreference.Element.Priority.High, ColorPreference.Element.Priority.Medium,
- ColorPreference.Element.Priority.Low, ColorPreference.Element.Priority.NoData };
- chart.getStyler().setSeriesColors(sliceColors);
- // Series
- chart.addSeries("Essential", 0);
- chart.addSeries("High", 0);
- chart.addSeries("Medium", 0);
- chart.addSeries("Low", 0);
- chart.addSeries("No Data", 0);
- return chart;
- }
- public PieChart createFlexibilityChart() {
- // IN_USE, ON_COOLDOWN, OFFERED, NOT_OFFERED, UNAVAILABLE
- // Create Chart
- PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Flexibilities").build();
- setDefaultPieChartSettings(chart);
- // Customize Chart
- Color[] sliceColors = new Color[] { ColorPreference.Flexibility.Offered, ColorPreference.Flexibility.InUse,
- ColorPreference.Flexibility.OnCooldown, ColorPreference.Flexibility.NotOffered,
- ColorPreference.Flexibility.Unavailable, ColorPreference.Element.Priority.NoData };
- chart.getStyler().setSeriesColors(sliceColors);
- // Series
- chart.addSeries("Offered", 0);
- chart.addSeries("In use", 0);
- chart.addSeries("On cooldown", 0);
- chart.addSeries("Not offered", 0);
- chart.addSeries("Unavailable", 0);
- chart.addSeries("No Data", 0);
- return chart;
- }
- public PieChart createProductionChart() {
- // Create Chart
- PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight)
- .title("Production vs. Consumption").build();
- setDefaultPieChartSettings(chart);
- // Customize Chart
- // Customize Chart
- Color[] barColors = new Color[] { ColorPreference.Energy.Production, ColorPreference.Energy.Consumption };
- chart.getStyler().setSeriesColors(barColors);
- // Series
- chart.addSeries("Production", 0);
- chart.addSeries("Consumption", 0);
- return chart;
- }
- public PieChart createActiveChart() {
- // Create Chart
- PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Active").build();
- setDefaultPieChartSettings(chart);
- // Customize Chart
- // Customize Chart
- Color[] barColors = new Color[] { ColorPreference.Element.Active, ColorPreference.Element.Inactive };
- chart.getStyler().setSeriesColors(barColors);
- // Series
- chart.addSeries("Active", 0);
- chart.addSeries("Inactive", 0);
- return chart;
- }
- }
|