|
@@ -0,0 +1,256 @@
|
|
|
+package ui.view;
|
|
|
+
|
|
|
+import java.awt.BorderLayout;
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Font;
|
|
|
+import java.awt.GridLayout;
|
|
|
+import java.awt.event.MouseAdapter;
|
|
|
+import java.awt.event.MouseEvent;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import javax.swing.BorderFactory;
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JPanel;
|
|
|
+import javax.swing.JTextField;
|
|
|
+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.GroupNode;
|
|
|
+import classes.HolonObject;
|
|
|
+import preferences.ColorPreference;
|
|
|
+import ui.controller.Control;
|
|
|
+import ui.model.DecoratedGroupNode;
|
|
|
+import ui.model.DecoratedHolonObject.HolonObjectState;
|
|
|
+import ui.model.Model;
|
|
|
+
|
|
|
+public class HolonInformationPanel extends JPanel {
|
|
|
+
|
|
|
+ private final int defaultWidth = 50;
|
|
|
+ private final int defaultHeight = 150;
|
|
|
+
|
|
|
+ private PieChart supplyChart = createSupplyStateChart();
|
|
|
+ private PieChart priorityChart = createPriorityChart();
|
|
|
+ private PieChart flexibilityChart = createFlexibilityChart();
|
|
|
+ private CategoryChart energyChart = createProductionChart();
|
|
|
+
|
|
|
+ private XChartPanel<PieChart> panelHolonObject;
|
|
|
+ private XChartPanel<PieChart> panelPriority;
|
|
|
+ private XChartPanel<PieChart> panelFlexibility;
|
|
|
+ private XChartPanel<CategoryChart> panelEnergy;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private JPanel graphPanel = new JPanel(new GridLayout(0, 2));
|
|
|
+ private JPanel titlePanel = new JPanel(new BorderLayout(0, 0));
|
|
|
+
|
|
|
+ private Control control;
|
|
|
+
|
|
|
+ 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() {
|
|
|
+ // TODO check for a method without instanceof and cast
|
|
|
+ List<GroupNode> list = control.getModel().getSelectedCpsObjects().stream()
|
|
|
+ .filter(object -> object instanceof GroupNode).map(object -> (GroupNode) object)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ System.out.println("UPDATE CHART" + list.size());
|
|
|
+ if(list.size() != 1) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ GroupNode groupNode = list.get(0);
|
|
|
+ DecoratedGroupNode decoratedGroupNode = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().get(groupNode);
|
|
|
+
|
|
|
+ //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
|
|
|
+ //decoratedGroupNode.
|
|
|
+
|
|
|
+
|
|
|
+ this.revalidate();
|
|
|
+ this.repaint();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ panelEnergy = new XChartPanel<CategoryChart>(energyChart);
|
|
|
+ panelEnergy.setBackground(ColorPreference.Panel.Background);
|
|
|
+ graphPanel.add(panelEnergy);
|
|
|
+ graphPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void initTitlePanel() {
|
|
|
+ JTextField textField = new JTextField("WindPanel");
|
|
|
+ textField.setFont(new Font("Arial", Font.BOLD, 24));
|
|
|
+ textField.setForeground(ColorPreference.Panel.Title);
|
|
|
+ textField.setBackground(ColorPreference.Panel.Background);
|
|
|
+ textField.setBackground(null);
|
|
|
+ textField.setBorder(null);
|
|
|
+ titlePanel.setBackground(ColorPreference.Panel.Background);
|
|
|
+ titlePanel.setBorder(new EmptyBorder(5, 5, 2, 0));
|
|
|
+ titlePanel.add(textField, 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.Background);
|
|
|
+ styler.setPlotBorderColor(ColorPreference.Panel.Background);
|
|
|
+ styler.setLegendVisible(false);
|
|
|
+ styler.setChartBackgroundColor(ColorPreference.Panel.Background);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // TODO: get data
|
|
|
+ // Series
|
|
|
+ chart.addSeries("Producer", 40);
|
|
|
+ chart.addSeries("Over supplied", 21);
|
|
|
+ chart.addSeries("Supplied", 24);
|
|
|
+ chart.addSeries("Partial supplied", 39);
|
|
|
+ chart.addSeries("Not supplied", 17);
|
|
|
+ chart.addSeries("No energy", 0);
|
|
|
+ chart.updatePieSeries("Producer", 100);
|
|
|
+
|
|
|
+ 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.Priority.Essential, ColorPreference.Priority.High,
|
|
|
+ ColorPreference.Priority.Medium, ColorPreference.Priority.Low, };
|
|
|
+ chart.getStyler().setSeriesColors(sliceColors);
|
|
|
+ // TODO: get data
|
|
|
+ // Series
|
|
|
+ chart.addSeries("Essential", 20);
|
|
|
+ chart.addSeries("High", 28);
|
|
|
+ chart.addSeries("Medium", 29);
|
|
|
+ chart.addSeries("Low", 70);
|
|
|
+ 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, };
|
|
|
+ chart.getStyler().setSeriesColors(sliceColors);
|
|
|
+ // TODO: get data
|
|
|
+ // Series
|
|
|
+ chart.addSeries("Offered", 124);
|
|
|
+ chart.addSeries("In use", 45);
|
|
|
+ chart.addSeries("On cooldown", 30);
|
|
|
+ chart.addSeries("Not offered", 15);
|
|
|
+ chart.addSeries("Unavailable", 2);
|
|
|
+ return chart;
|
|
|
+ }
|
|
|
+
|
|
|
+ public CategoryChart createProductionChart() {
|
|
|
+ // Create Chart
|
|
|
+ CategoryChart chart = new CategoryChartBuilder().width(defaultWidth).height(defaultHeight)
|
|
|
+ .title("Production vs. Consumption").yAxisTitle("Energy Units").build();
|
|
|
+
|
|
|
+ // Customize Chart
|
|
|
+ CategoryStyler styler = chart.getStyler();
|
|
|
+ Color[] barColors = new Color[] { ColorPreference.Energy.Production, ColorPreference.Energy.Consumption };
|
|
|
+ chart.getStyler().setSeriesColors(barColors);
|
|
|
+
|
|
|
+ styler.setLegendVisible(false);
|
|
|
+ styler.setXAxisTitleVisible(false);
|
|
|
+ styler.setLabelsVisible(false);
|
|
|
+ styler.setXAxisTicksVisible(false);
|
|
|
+ styler.setChartFontColor(ColorPreference.Panel.Title);
|
|
|
+ styler.setChartBackgroundColor(ColorPreference.Panel.Background);
|
|
|
+ styler.setPlotBackgroundColor(ColorPreference.Panel.Background);
|
|
|
+ styler.setPlotBorderColor(ColorPreference.Panel.Background);
|
|
|
+ styler.setToolTipsEnabled(true);
|
|
|
+ // TODO: get data
|
|
|
+ // Series
|
|
|
+ chart.addSeries("Production", new ArrayList<String>(Arrays.asList(new String[] { "Production" })),
|
|
|
+ new ArrayList<Number>(Arrays.asList(new Number[] { 50.0 })));
|
|
|
+
|
|
|
+ chart.addSeries("Consumption", new ArrayList<String>(Arrays.asList(new String[] { "Consumption" })),
|
|
|
+ new ArrayList<Number>(Arrays.asList(new Number[] { 55.0 })));
|
|
|
+
|
|
|
+ return chart;
|
|
|
+ }
|
|
|
+}
|