HolonInformationPanel.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.GridLayout;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. import java.util.stream.Collectors;
  12. import javax.swing.BorderFactory;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextField;
  17. import javax.swing.SwingUtilities;
  18. import javax.swing.border.EmptyBorder;
  19. import org.knowm.xchart.CategoryChart;
  20. import org.knowm.xchart.CategoryChartBuilder;
  21. import org.knowm.xchart.PieChart;
  22. import org.knowm.xchart.PieChartBuilder;
  23. import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
  24. import org.knowm.xchart.XChartPanel;
  25. import org.knowm.xchart.style.CategoryStyler;
  26. import org.knowm.xchart.style.PieStyler;
  27. import org.knowm.xchart.style.PieStyler.LabelType;
  28. import classes.AbstractCanvasObject;
  29. import classes.GroupNode;
  30. import classes.HolonObject;
  31. import preferences.ColorPreference;
  32. import ui.controller.Control;
  33. import ui.model.DecoratedGroupNode;
  34. import ui.model.DecoratedHolonObject.HolonObjectState;
  35. import ui.model.Model;
  36. public class HolonInformationPanel extends JPanel {
  37. private final int defaultWidth = 50;
  38. private final int defaultHeight = 150;
  39. private PieChart supplyChart = createSupplyStateChart();
  40. private PieChart priorityChart = createPriorityChart();
  41. private PieChart flexibilityChart = createFlexibilityChart();
  42. private CategoryChart energyChart = createProductionChart();
  43. private XChartPanel<PieChart> panelHolonObject;
  44. private XChartPanel<PieChart> panelPriority;
  45. private XChartPanel<PieChart> panelFlexibility;
  46. private XChartPanel<CategoryChart> panelEnergy;
  47. private JPanel graphPanel = new JPanel(new GridLayout(0, 2));
  48. private JPanel titlePanel = new JPanel(new BorderLayout(0, 0));
  49. private Control control;
  50. public static void main(String[] args) {
  51. HolonInformationPanel exampleChart = new HolonInformationPanel(new Control(new Model()));
  52. JFrame frame = new JFrame();
  53. frame.setContentPane(exampleChart);
  54. frame.pack();
  55. frame.setBounds(0, 0, 540, 600);
  56. frame.setLocationRelativeTo(null);
  57. frame.setVisible(true);
  58. }
  59. public void updateCharts() {
  60. // TODO check for a method without instanceof and cast
  61. List<GroupNode> list = control.getModel().getSelectedCpsObjects().stream()
  62. .filter(object -> object instanceof GroupNode).map(object -> (GroupNode) object)
  63. .collect(Collectors.toList());
  64. System.out.println("UPDATE CHART" + list.size());
  65. if(list.size() != 1) {
  66. return;
  67. }
  68. GroupNode groupNode = list.get(0);
  69. DecoratedGroupNode decoratedGroupNode = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().get(groupNode);
  70. //UPDATE SUPPLY STATE
  71. int producerAmount = decoratedGroupNode.getAmountOfSupplier();
  72. int overSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  73. int suppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  74. int partiallySuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  75. int notSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  76. int noEnergyAmount = decoratedGroupNode.getAmountOfPassiv();
  77. supplyChart.updatePieSeries("Producer", producerAmount);
  78. supplyChart.updatePieSeries("Over supplied", overSuppliedAmount);
  79. supplyChart.updatePieSeries("Supplied", suppliedAmount);
  80. supplyChart.updatePieSeries("Partial supplied", partiallySuppliedAmount);
  81. supplyChart.updatePieSeries("Not supplied", notSuppliedAmount);
  82. supplyChart.updatePieSeries("No energy", noEnergyAmount);
  83. panelHolonObject.updateToolTips();
  84. //UPDATE PRIORITYS
  85. //decoratedGroupNode.
  86. this.revalidate();
  87. this.repaint();
  88. }
  89. public HolonInformationPanel(Control control) {
  90. control.OnSelectionUpdate.addListener(() -> updateCharts());
  91. this.control = control;
  92. this.setLayout(new BorderLayout());
  93. initGraphPanel();
  94. initTitlePanel();
  95. this.setBackground(ColorPreference.Panel.Background);
  96. this.add(titlePanel, BorderLayout.PAGE_START);
  97. this.add(graphPanel, BorderLayout.CENTER);
  98. }
  99. public void initGraphPanel() {
  100. graphPanel.setBackground(ColorPreference.Panel.Background);
  101. panelHolonObject = new XChartPanel<PieChart>(supplyChart);
  102. panelHolonObject.setBackground(ColorPreference.Panel.Background);
  103. graphPanel.add(panelHolonObject);
  104. panelPriority = new XChartPanel<PieChart>(priorityChart);
  105. panelPriority.setBackground(ColorPreference.Panel.Background);
  106. graphPanel.add(panelPriority);
  107. panelFlexibility = new XChartPanel<PieChart>(flexibilityChart);
  108. panelFlexibility.setBackground(ColorPreference.Panel.Background);
  109. graphPanel.add(panelFlexibility);
  110. panelEnergy = new XChartPanel<CategoryChart>(energyChart);
  111. panelEnergy.setBackground(ColorPreference.Panel.Background);
  112. graphPanel.add(panelEnergy);
  113. graphPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
  114. }
  115. public void initTitlePanel() {
  116. JTextField textField = new JTextField("WindPanel");
  117. textField.setFont(new Font("Arial", Font.BOLD, 24));
  118. textField.setForeground(ColorPreference.Panel.Title);
  119. textField.setBackground(ColorPreference.Panel.Background);
  120. textField.setBackground(null);
  121. textField.setBorder(null);
  122. titlePanel.setBackground(ColorPreference.Panel.Background);
  123. titlePanel.setBorder(new EmptyBorder(5, 5, 2, 0));
  124. titlePanel.add(textField, BorderLayout.CENTER);
  125. }
  126. public void setDefaultPieChartSettings(PieChart chart) {
  127. PieStyler styler = chart.getStyler();
  128. styler.setChartTitleVisible(true);
  129. styler.setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
  130. styler.setLabelsFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 14));
  131. styler.setLabelType(LabelType.Percentage);
  132. styler.setLabelsFontColor(Color.black);
  133. styler.setLabelsFontColorAutomaticEnabled(false);
  134. styler.setLabelsDistance(0.8);
  135. styler.setChartFontColor(ColorPreference.Panel.Title);
  136. styler.setToolTipsEnabled(true);
  137. styler.setPlotContentSize(0.9);
  138. styler.setPlotBackgroundColor(ColorPreference.Panel.Background);
  139. styler.setPlotBorderColor(ColorPreference.Panel.Background);
  140. styler.setLegendVisible(false);
  141. styler.setChartBackgroundColor(ColorPreference.Panel.Background);
  142. }
  143. public PieChart createSupplyStateChart() {
  144. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Holon Objects").build();
  145. setDefaultPieChartSettings(chart);
  146. Color[] sliceColors = new Color[] { ColorPreference.HolonObject.Producer,
  147. ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.Supplied,
  148. ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.NotSupplied,
  149. ColorPreference.HolonObject.NoEnergy };
  150. chart.getStyler().setSeriesColors(sliceColors);
  151. // TODO: get data
  152. // Series
  153. chart.addSeries("Producer", 40);
  154. chart.addSeries("Over supplied", 21);
  155. chart.addSeries("Supplied", 24);
  156. chart.addSeries("Partial supplied", 39);
  157. chart.addSeries("Not supplied", 17);
  158. chart.addSeries("No energy", 0);
  159. chart.updatePieSeries("Producer", 100);
  160. return chart;
  161. }
  162. public PieChart createPriorityChart() {
  163. // Create Chart
  164. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Priotities").build();
  165. setDefaultPieChartSettings(chart);
  166. // Customize Chart
  167. Color[] sliceColors = new Color[] { ColorPreference.Priority.Essential, ColorPreference.Priority.High,
  168. ColorPreference.Priority.Medium, ColorPreference.Priority.Low, };
  169. chart.getStyler().setSeriesColors(sliceColors);
  170. // TODO: get data
  171. // Series
  172. chart.addSeries("Essential", 20);
  173. chart.addSeries("High", 28);
  174. chart.addSeries("Medium", 29);
  175. chart.addSeries("Low", 70);
  176. return chart;
  177. }
  178. public PieChart createFlexibilityChart() {
  179. // IN_USE, ON_COOLDOWN, OFFERED, NOT_OFFERED, UNAVAILABLE
  180. // Create Chart
  181. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Flexibilities").build();
  182. setDefaultPieChartSettings(chart);
  183. // Customize Chart
  184. Color[] sliceColors = new Color[] { ColorPreference.Flexibility.Offered, ColorPreference.Flexibility.InUse,
  185. ColorPreference.Flexibility.OnCooldown, ColorPreference.Flexibility.NotOffered,
  186. ColorPreference.Flexibility.Unavailable, };
  187. chart.getStyler().setSeriesColors(sliceColors);
  188. // TODO: get data
  189. // Series
  190. chart.addSeries("Offered", 124);
  191. chart.addSeries("In use", 45);
  192. chart.addSeries("On cooldown", 30);
  193. chart.addSeries("Not offered", 15);
  194. chart.addSeries("Unavailable", 2);
  195. return chart;
  196. }
  197. public CategoryChart createProductionChart() {
  198. // Create Chart
  199. CategoryChart chart = new CategoryChartBuilder().width(defaultWidth).height(defaultHeight)
  200. .title("Production vs. Consumption").yAxisTitle("Energy Units").build();
  201. // Customize Chart
  202. CategoryStyler styler = chart.getStyler();
  203. Color[] barColors = new Color[] { ColorPreference.Energy.Production, ColorPreference.Energy.Consumption };
  204. chart.getStyler().setSeriesColors(barColors);
  205. styler.setLegendVisible(false);
  206. styler.setXAxisTitleVisible(false);
  207. styler.setLabelsVisible(false);
  208. styler.setXAxisTicksVisible(false);
  209. styler.setChartFontColor(ColorPreference.Panel.Title);
  210. styler.setChartBackgroundColor(ColorPreference.Panel.Background);
  211. styler.setPlotBackgroundColor(ColorPreference.Panel.Background);
  212. styler.setPlotBorderColor(ColorPreference.Panel.Background);
  213. styler.setToolTipsEnabled(true);
  214. // TODO: get data
  215. // Series
  216. chart.addSeries("Production", new ArrayList<String>(Arrays.asList(new String[] { "Production" })),
  217. new ArrayList<Number>(Arrays.asList(new Number[] { 50.0 })));
  218. chart.addSeries("Consumption", new ArrayList<String>(Arrays.asList(new String[] { "Consumption" })),
  219. new ArrayList<Number>(Arrays.asList(new Number[] { 55.0 })));
  220. return chart;
  221. }
  222. }