HolonInformationPanel.java.orig 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Font;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.GridLayout;
  9. import java.awt.Insets;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.stream.Collectors;
  13. import java.util.stream.Stream;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JLayeredPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JTextField;
  20. import javax.swing.OverlayLayout;
  21. import javax.swing.border.EmptyBorder;
  22. import org.knowm.xchart.PieChart;
  23. import org.knowm.xchart.PieChartBuilder;
  24. import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
  25. import org.knowm.xchart.XChartPanel;
  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.controller.FlexManager;
  34. import ui.controller.FlexManager.FlexWrapper;
  35. import ui.model.Consumer;
  36. import ui.model.DecoratedGroupNode;
  37. import ui.model.DecoratedGroupNode.PriorityCounts;
  38. import ui.model.DecoratedHolonObject;
  39. import ui.model.DecoratedHolonObject.HolonObjectState;
  40. import ui.model.Model;
  41. import ui.model.Passiv;
  42. import ui.model.Supplier;
  43. import utility.FormatFloat;
  44. public class HolonInformationPanel extends JPanel {
  45. private final int defaultWidth = 50;
  46. private final int defaultHeight = 200;
  47. private PieChart supplyChart = createSupplyStateChart();
  48. private PieChart priorityChart = createPriorityChart();
  49. private PieChart flexibilityChart = createFlexibilityChart();
  50. private PieChart energyChart = createProductionChart();
  51. private PieChart activeChart = createActiveChart();
  52. private XChartPanel<PieChart> panelHolonObject;
  53. private XChartPanel<PieChart> panelPriority;
  54. private XChartPanel<PieChart> panelFlexibility;
  55. private XChartPanel<PieChart> panelEnergy;
  56. private XChartPanel<PieChart> panelActive;
  57. // TODO is this needed to be fields
  58. private JPanel graphPanel = new JPanel(new GridLayout(0, 2));
  59. private JPanel titlePanel = new JPanel(new BorderLayout(0, 0));
  60. private Control control;
  61. private JTextField titleTextField;
  62. private JLabel differenceEnergyLabelAmount;
  63. public static void main(String[] args) {
  64. HolonInformationPanel exampleChart = new HolonInformationPanel(new Control(new Model()));
  65. JFrame frame = new JFrame();
  66. frame.setContentPane(exampleChart);
  67. frame.pack();
  68. frame.setBounds(0, 0, 540, 600);
  69. frame.setLocationRelativeTo(null);
  70. frame.setVisible(true);
  71. }
  72. public void updateCharts() {
  73. DecoratedGroupNode decoratedGroupNode;
  74. switch (control.getModel().getSelectedObjects().size()) {
  75. case 0:
  76. return;
  77. case 1:
  78. // titleTextField.setText(control.getModel().getSelectedObjects().getName());
  79. titleTextField.setText("TODO");
  80. decoratedGroupNode = multiSelectionToDecoratedGroupNode();
  81. break;
  82. default:
  83. titleTextField.setText(getMultiSelectionName());
  84. decoratedGroupNode = multiSelectionToDecoratedGroupNode();
  85. break;
  86. }
  87. // UPDATE SUPPLY STATE
  88. int producerAmount = decoratedGroupNode.getAmountOfSupplier();
  89. int overSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  90. int suppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  91. int partiallySuppliedAmount = decoratedGroupNode
  92. .getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  93. int notSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  94. int noEnergyAmount = decoratedGroupNode.getAmountOfPassiv();
  95. supplyChart.updatePieSeries("Producer", producerAmount);
  96. supplyChart.updatePieSeries("Over supplied", overSuppliedAmount);
  97. supplyChart.updatePieSeries("Supplied", suppliedAmount);
  98. supplyChart.updatePieSeries("Partial supplied", partiallySuppliedAmount);
  99. supplyChart.updatePieSeries("Not supplied", notSuppliedAmount);
  100. supplyChart.updatePieSeries("No energy", noEnergyAmount);
  101. panelHolonObject.updateToolTips();
  102. // UPDATE PRIORITYS
  103. PriorityCounts priorityCounts = decoratedGroupNode.getPriorityCounts();
  104. priorityChart.updatePieSeries("Essential", priorityCounts.essential);
  105. priorityChart.updatePieSeries("High", priorityCounts.high);
  106. priorityChart.updatePieSeries("Medium", priorityCounts.medium);
  107. priorityChart.updatePieSeries("Low", priorityCounts.low);
  108. boolean hasPriority = priorityCounts.essential + priorityCounts.high + priorityCounts.medium
  109. + priorityCounts.low > 0;
  110. priorityChart.updatePieSeries("No Data", hasPriority ? 0 : 1);
  111. panelPriority.updateToolTips();
  112. // UPDATE PRODUCTION
  113. float production = decoratedGroupNode.getProductionFromSupplier();
  114. float consumption = decoratedGroupNode.getConsumptionFromConsumer();
  115. float difference = Math.abs(production - consumption);
  116. energyChart.updatePieSeries("Production", production);
  117. energyChart.updatePieSeries("Consumption", consumption);
  118. differenceEnergyLabelAmount.setText(FormatFloat.doubleFixedPlaces(1, difference));
  119. panelEnergy.updateToolTips();
  120. // UPDATE FLEXIBILITIES
  121. int inUse = 0;
  122. int offered = 0;
  123. int onCooldown = 0;
  124. int notOffered = 0;
  125. int unavailable = 0;
  126. FlexManager manager = control.getSimManager().getActualFlexManager();
  127. Stream<FlexWrapper> flexWrapperStream = decoratedGroupNode.getFlexibilitiesStream()
  128. .map(flex -> manager.getFlexWrapperFromFlexibility(flex));
  129. List<FlexWrapper> wrapperList = flexWrapperStream.collect(Collectors.toList());
  130. for (FlexWrapper wrapper : wrapperList) {
  131. switch (wrapper.getState()) {
  132. case IN_USE:
  133. inUse++;
  134. break;
  135. case NOT_OFFERED:
  136. notOffered++;
  137. break;
  138. case OFFERED:
  139. offered++;
  140. break;
  141. case ON_COOLDOWN:
  142. onCooldown++;
  143. break;
  144. case UNAVAILABLE:
  145. unavailable++;
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. flexibilityChart.updatePieSeries("Offered", offered);
  152. flexibilityChart.updatePieSeries("In use", inUse);
  153. flexibilityChart.updatePieSeries("On cooldown", onCooldown);
  154. flexibilityChart.updatePieSeries("Not offered", notOffered);
  155. flexibilityChart.updatePieSeries("Unavailable", unavailable);
  156. boolean hasFlex = offered + inUse + onCooldown + notOffered + unavailable > 0;
  157. flexibilityChart.updatePieSeries("No Data", hasFlex ? 0 : 1);
  158. panelFlexibility.updateToolTips();
  159. // UPDATE ActiveInActive
  160. int activeAmount = decoratedGroupNode.getAmountOfAktiveElemntsFromHolonObjects();
  161. int inactiveAmounts = decoratedGroupNode.getAmountOfElemntsFromHolonObjects() - activeAmount;
  162. activeChart.updatePieSeries("Active", activeAmount);
  163. activeChart.updatePieSeries("Inactive", inactiveAmounts);
  164. panelActive.updateToolTips();
  165. this.revalidate();
  166. this.repaint();
  167. }
  168. private String getMultiSelectionName() {
  169. String name = control.getModel().getSelectedObjects().stream().map(AbstractCanvasObject::getName)
  170. .collect(Collectors.joining(", "));
  171. if(name.length() > 25) {
  172. name = name.substring(0, 25);
  173. name += "...";
  174. }
  175. return name;
  176. }
  177. private DecoratedGroupNode multiSelectionToDecoratedGroupNode() {
  178. // TODO Create DecoratedGroupNode
  179. DecoratedGroupNode temp = new DecoratedGroupNode(new GroupNode("Temp"));
  180. // TODO get consumer, supplier, passiv
  181. // GroupNodes
  182. HashMap<GroupNode, DecoratedGroupNode> accessMapGroupNode = control.getSimManager()
  183. .getActualVisualRepresentationalState().getCreatedGroupNodes();
  184. List<DecoratedGroupNode> groupNodeList = control.getModel().getSelectedObjects().stream()
  185. .filter(object -> object instanceof GroupNode).map(object -> accessMapGroupNode.get(object))
  186. .collect(Collectors.toList());
  187. temp.getGroupNodeList().addAll(groupNodeList);
  188. // HolonObjects
  189. HashMap<HolonObject, DecoratedHolonObject> accessMapHolonObject = control.getSimManager()
  190. .getActualVisualRepresentationalState().createdHolonObjects;
  191. List<DecoratedHolonObject> holonObjectList = control.getModel().getSelectedObjects().stream()
  192. .filter(object -> object instanceof HolonObject).map(object -> accessMapHolonObject.get(object))
  193. .collect(Collectors.toList());
  194. for (DecoratedHolonObject object : holonObjectList) {
  195. switch (object.getState()) {
  196. case NO_ENERGY:
  197. temp.getPassivList().add((Passiv) object);
  198. break;
  199. case PRODUCER:
  200. temp.getSupplierList().add((Supplier) object);
  201. break;
  202. default:
  203. temp.getConsumerList().add((Consumer) object);
  204. break;
  205. }
  206. }
  207. return temp;
  208. }
  209. public HolonInformationPanel(Control control) {
  210. control.OnSelectionChanged.addListener(() -> updateCharts());
  211. this.control = control;
  212. this.setLayout(new BorderLayout());
  213. initGraphPanel();
  214. initTitlePanel();
  215. this.setBackground(ColorPreference.Panel.Background);
  216. this.add(titlePanel, BorderLayout.PAGE_START);
  217. this.add(graphPanel, BorderLayout.CENTER);
  218. }
  219. public void initGraphPanel() {
  220. graphPanel.setBackground(ColorPreference.Panel.Background);
  221. panelHolonObject = new XChartPanel<PieChart>(supplyChart);
  222. panelHolonObject.setBackground(ColorPreference.Panel.Background);
  223. graphPanel.add(panelHolonObject);
  224. panelPriority = new XChartPanel<PieChart>(priorityChart);
  225. panelPriority.setBackground(ColorPreference.Panel.Background);
  226. graphPanel.add(panelPriority);
  227. panelFlexibility = new XChartPanel<PieChart>(flexibilityChart);
  228. panelFlexibility.setBackground(ColorPreference.Panel.Background);
  229. graphPanel.add(panelFlexibility);
  230. Component panel = initEnergyChart();
  231. graphPanel.add(panel);
  232. panelActive = new XChartPanel<PieChart>(activeChart);
  233. panelActive.setBackground(ColorPreference.Panel.Background);
  234. panelActive.setLayout(new GridBagLayout());
  235. graphPanel.add(panelActive);
  236. graphPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
  237. }
  238. private JLayeredPane initEnergyChart() {
  239. JLayeredPane panel = new JLayeredPane();
  240. JPanel panelMiddle = new JPanel(new GridBagLayout());
  241. panelEnergy = new XChartPanel<PieChart>(energyChart);
  242. panelEnergy.setBackground(ColorPreference.Panel.Background);
  243. GridBagConstraints c = new GridBagConstraints();
  244. c.gridx = 0;
  245. c.gridy = 0;
  246. c.insets = new Insets(15, 0, 0, 0); // top padding
  247. JLabel difference = new JLabel("Difference:");
  248. difference.setHorizontalAlignment(JLabel.CENTER);
  249. difference.setAlignmentX(Component.CENTER_ALIGNMENT);
  250. difference.setForeground(ColorPreference.Panel.Title);
  251. difference.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 10));
  252. panelMiddle.add(difference, c);
  253. differenceEnergyLabelAmount = new JLabel("");
  254. differenceEnergyLabelAmount.setHorizontalAlignment(JLabel.CENTER);
  255. differenceEnergyLabelAmount.setForeground(Color.red);
  256. differenceEnergyLabelAmount.setAlignmentX(Component.CENTER_ALIGNMENT);
  257. differenceEnergyLabelAmount.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 12));
  258. c.insets = new Insets(0, 0, 0, 0);
  259. c.gridy = 1;
  260. panelMiddle.add(differenceEnergyLabelAmount, c);
  261. panel.setLayout(new OverlayLayout(panel));
  262. panelEnergy.setOpaque(false);
  263. panelMiddle.setOpaque(false);
  264. panel.add(panelMiddle, Integer.valueOf(0));
  265. panel.add(panelEnergy, Integer.valueOf(1));
  266. return panel;
  267. }
  268. public void initTitlePanel() {
  269. titleTextField = new JTextField("");
  270. titleTextField.setFont(new Font("Arial", Font.BOLD, 24));
  271. titleTextField.setForeground(ColorPreference.Panel.Title);
  272. titleTextField.setBackground(ColorPreference.Panel.Background);
  273. titleTextField.setBackground(null);
  274. titleTextField.setBorder(null);
  275. titlePanel.setBackground(ColorPreference.Panel.Background);
  276. titlePanel.setBorder(new EmptyBorder(5, 5, 2, 0));
  277. titlePanel.add(titleTextField, BorderLayout.CENTER);
  278. }
  279. public void setDefaultPieChartSettings(PieChart chart) {
  280. PieStyler styler = chart.getStyler();
  281. styler.setChartTitleVisible(true);
  282. styler.setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
  283. styler.setLabelsFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 14));
  284. styler.setLabelType(LabelType.Percentage);
  285. styler.setLabelsFontColor(Color.black);
  286. styler.setLabelsFontColorAutomaticEnabled(false);
  287. styler.setLabelsDistance(0.8);
  288. styler.setChartFontColor(ColorPreference.Panel.Title);
  289. styler.setToolTipsEnabled(true);
  290. styler.setPlotContentSize(0.9);
  291. styler.setPlotBackgroundColor(ColorPreference.Panel.Transparent);
  292. styler.setPlotBorderColor(ColorPreference.Panel.Transparent);
  293. styler.setLegendVisible(false);
  294. styler.setChartBackgroundColor(ColorPreference.Panel.Transparent);
  295. }
  296. public PieChart createSupplyStateChart() {
  297. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Holon Objects").build();
  298. setDefaultPieChartSettings(chart);
  299. Color[] sliceColors = new Color[] { ColorPreference.HolonObject.Producer,
  300. ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.Supplied,
  301. ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.NotSupplied,
  302. ColorPreference.HolonObject.NoEnergy };
  303. chart.getStyler().setSeriesColors(sliceColors);
  304. // Series
  305. chart.addSeries("Producer", 0);
  306. chart.addSeries("Over supplied", 0);
  307. chart.addSeries("Supplied", 0);
  308. chart.addSeries("Partial supplied", 0);
  309. chart.addSeries("Not supplied", 0);
  310. chart.addSeries("No energy", 0);
  311. return chart;
  312. }
  313. public PieChart createPriorityChart() {
  314. // Create Chart
  315. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Priotities").build();
  316. setDefaultPieChartSettings(chart);
  317. // Customize Chart
  318. Color[] sliceColors = new Color[] { ColorPreference.Element.Priority.Essential,
  319. ColorPreference.Element.Priority.High, ColorPreference.Element.Priority.Medium,
  320. ColorPreference.Element.Priority.Low, ColorPreference.Element.Priority.NoData };
  321. chart.getStyler().setSeriesColors(sliceColors);
  322. // Series
  323. chart.addSeries("Essential", 0);
  324. chart.addSeries("High", 0);
  325. chart.addSeries("Medium", 0);
  326. chart.addSeries("Low", 0);
  327. chart.addSeries("No Data", 0);
  328. return chart;
  329. }
  330. public PieChart createFlexibilityChart() {
  331. // IN_USE, ON_COOLDOWN, OFFERED, NOT_OFFERED, UNAVAILABLE
  332. // Create Chart
  333. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Flexibilities").build();
  334. setDefaultPieChartSettings(chart);
  335. // Customize Chart
  336. Color[] sliceColors = new Color[] { ColorPreference.Flexibility.Offered, ColorPreference.Flexibility.InUse,
  337. ColorPreference.Flexibility.OnCooldown, ColorPreference.Flexibility.NotOffered,
  338. ColorPreference.Flexibility.Unavailable, ColorPreference.Element.Priority.NoData };
  339. chart.getStyler().setSeriesColors(sliceColors);
  340. // Series
  341. chart.addSeries("Offered", 0);
  342. chart.addSeries("In use", 0);
  343. chart.addSeries("On cooldown", 0);
  344. chart.addSeries("Not offered", 0);
  345. chart.addSeries("Unavailable", 0);
  346. chart.addSeries("No Data", 0);
  347. return chart;
  348. }
  349. public PieChart createProductionChart() {
  350. // Create Chart
  351. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight)
  352. .title("Production vs. Consumption").build();
  353. setDefaultPieChartSettings(chart);
  354. // Customize Chart
  355. // Customize Chart
  356. Color[] barColors = new Color[] { ColorPreference.Energy.Production, ColorPreference.Energy.Consumption };
  357. chart.getStyler().setSeriesColors(barColors);
  358. // Series
  359. chart.addSeries("Production", 0);
  360. chart.addSeries("Consumption", 0);
  361. return chart;
  362. }
  363. public PieChart createActiveChart() {
  364. // Create Chart
  365. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Active").build();
  366. setDefaultPieChartSettings(chart);
  367. // Customize Chart
  368. // Customize Chart
  369. Color[] barColors = new Color[] { ColorPreference.Element.Active, ColorPreference.Element.Inactive };
  370. chart.getStyler().setSeriesColors(barColors);
  371. // Series
  372. chart.addSeries("Active", 0);
  373. chart.addSeries("Inactive", 0);
  374. return chart;
  375. }
  376. }