StatisticGraphPanel.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Hashtable;
  16. import java.util.List;
  17. import java.util.stream.Collectors;
  18. import javax.imageio.ImageIO;
  19. import javax.swing.BorderFactory;
  20. import javax.swing.JButton;
  21. import javax.swing.JFileChooser;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.JPanel;
  25. import javax.swing.SwingConstants;
  26. import javax.swing.border.EmptyBorder;
  27. import com.google.gson.JsonObject;
  28. import classes.TrackedDataSet;
  29. import ui.controller.Control;
  30. import ui.model.Model;
  31. public class StatisticGraphPanel extends JPanel {
  32. private static final long serialVersionUID = 1L;
  33. // Model/Controller
  34. private Model model;
  35. private Control controller;
  36. // Components
  37. private StatisticGraph sGraph;
  38. private final JLabel graphNameLabel;
  39. private final JLabel maximumLabel = new JLabel("0");
  40. private JPanel topPanel = new JPanel();
  41. private JButton closeButton = new JButton("X");
  42. private JButton savImgButton = new JButton("Save as Image");
  43. // Variables
  44. String graphName;
  45. private final JPanel legendPanel = new JPanel();
  46. private JPanel that;
  47. private Hashtable<String, StatisticGraphPanel> graphHashtable;
  48. /**
  49. * Constructor.
  50. *
  51. * @param mod
  52. * the Model
  53. * @param cont
  54. * the Controller
  55. */
  56. public StatisticGraphPanel(Model mod, Control cont, String name, Hashtable<String, StatisticGraphPanel> gHt) {
  57. super();
  58. setBorder(new EmptyBorder(0, 0, 0, 0));
  59. this.model = mod;
  60. this.controller = cont;
  61. this.sGraph = new StatisticGraph(mod, cont);
  62. this.graphName = name;
  63. this.graphHashtable = gHt;
  64. setLayout(new BorderLayout(0, 0));
  65. // ******************** Component Propertys ***************//
  66. // Graph
  67. // this.setPreferredSize(new Dimension(300, 200));
  68. sGraph.setPreferredSize(new Dimension(200, 200));
  69. sGraph.setMinimumSize(new Dimension(100, 150));
  70. // this.setMaximumSize(new Dimension(700, 200));
  71. // this.setMinimumSize(new Dimension(300, 200));
  72. // Graph Name
  73. graphNameLabel = new JLabel(graphName);
  74. graphNameLabel.setHorizontalTextPosition(JLabel.CENTER);
  75. // Panel on top (Name and Close Button)
  76. topPanel.setLayout(new BorderLayout(0, 0));
  77. JPanel topPanelHelp = new JPanel(new BorderLayout(0, 0));
  78. topPanelHelp.add(graphNameLabel, BorderLayout.CENTER);
  79. topPanelHelp.add(savImgButton, BorderLayout.EAST);
  80. topPanel.add(topPanelHelp, BorderLayout.CENTER);
  81. topPanel.add(closeButton, BorderLayout.EAST);
  82. savImgButton.addActionListener(new ActionListener() {
  83. @Override
  84. public void actionPerformed(ActionEvent e) {
  85. BufferedImage img = new BufferedImage(that.getWidth(), that.getHeight(), BufferedImage.TYPE_INT_RGB);
  86. that.print(img.getGraphics());
  87. try {
  88. JFileChooser fileChooser = new JFileChooser();
  89. if (fileChooser.showSaveDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) {
  90. String file = fileChooser.getSelectedFile().getPath();
  91. ImageIO.write(img, "jpg", new File(file + ".jpg"));
  92. }
  93. } catch (IOException e1) {
  94. // TODO Auto-generated catch block
  95. e1.printStackTrace();
  96. }
  97. }
  98. });
  99. topPanel.setBorder(null);
  100. // Maximum Label
  101. maximumLabel.setVerticalAlignment(SwingConstants.TOP);
  102. maximumLabel.setMinimumSize(new Dimension(30, 10));
  103. legendPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
  104. // Legend Panel
  105. legendPanel.setLayout(new GridLayout(0, 5, 0, 0));
  106. // ******************** Component Listener ****************//
  107. that = this;
  108. closeButton.addActionListener(new ActionListener() {
  109. @Override
  110. public void actionPerformed(ActionEvent e) {
  111. JPanel parent = (JPanel) that.getParent();
  112. for (int i = 0; i < parent.getComponentCount(); i++) {
  113. if (parent.getComponent(i).equals(that)) {
  114. parent.remove(parent.getComponent(i + 1));
  115. break;
  116. }
  117. }
  118. graphHashtable.remove(graphName);
  119. parent.remove(that);
  120. parent.updateUI();
  121. }
  122. });
  123. // ******************** add everything ********************//
  124. this.add(sGraph);
  125. this.add(topPanel, BorderLayout.NORTH);
  126. this.add(maximumLabel, BorderLayout.WEST);
  127. this.add(legendPanel, BorderLayout.SOUTH);
  128. }
  129. /**
  130. * Adds the Set to the Graph.
  131. *
  132. * @param set
  133. */
  134. public void addObject(TrackedDataSet set) {
  135. sGraph.addObject(set);
  136. String property = "";
  137. switch (set.getProperty()) {
  138. case TrackedDataSet.CONSUMPTION:
  139. case TrackedDataSet.GROUP_CONSUMPTION:
  140. property = "consumption";
  141. break;
  142. case TrackedDataSet.PRODUCTION:
  143. case TrackedDataSet.GROUP_PRODUCTION:
  144. property = "production";
  145. break;
  146. case TrackedDataSet.ACTIVATED_ELEMENTS:
  147. property = "active elements";
  148. break;
  149. case TrackedDataSet.ON_OFF:
  150. property = "on//off";
  151. break;
  152. case TrackedDataSet.TOTAL_PRODUCTION:
  153. property = "total production";
  154. break;
  155. case TrackedDataSet.TOTAL_CONSUMPTION:
  156. property = "total consumption";
  157. break;
  158. case TrackedDataSet.PERCENT_SUPPLIED:
  159. property = "Percentage of supplied";
  160. break;
  161. case TrackedDataSet.PERCENT_NOT_SUPPLIED:
  162. property = "Percentage of not supplied";
  163. break;
  164. case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
  165. property = "Percentage of partial supplied";
  166. break;
  167. case TrackedDataSet.AMOUNT_HOLONS:
  168. property = "Amount of holons";
  169. break;
  170. case TrackedDataSet.AMOUNT_CLOSED_SWITCHES:
  171. property = "Amount of Closed Switches";
  172. break;
  173. case TrackedDataSet.AVG_AMOUNT_OBJECTS_IN_HOLONS:
  174. property = "Avg. Amount of Objects in Holons";
  175. break;
  176. case TrackedDataSet.AVG_AMOUNT_ELEMENTS_IN_HOLONS:
  177. property = "Avg. Amount of Elements in Holons";
  178. break;
  179. case TrackedDataSet.AVG_AMOUNT_PRODUCERS_IN_HOLONS:
  180. property = "Avg. Amount Producers in Holons";
  181. break;
  182. case TrackedDataSet.AVG_CONSUMED_ENERGY_IN_HOLONS:
  183. property = "Avg. Consumed Energy in Holons";
  184. break;
  185. case TrackedDataSet.AVG_WASTED_ENERGY_IN_HOLONS:
  186. property = "Avg. Wasted Energy in Holons";
  187. break;
  188. case TrackedDataSet.AMOUNT_BROKEN_EDGES:
  189. property = "Amount of Broken Edged";
  190. break;
  191. case TrackedDataSet.RATIO_PRODUCERS_CONSUMERS:
  192. property = "Ratio Producers:Consumers";
  193. break;
  194. case TrackedDataSet.AVG_AMOUNT_CLOSED_SWITCHES_IN_HOLONS:
  195. property = "Avg. Amount of Closed Switches in Holons";
  196. break;
  197. case TrackedDataSet.AVG_AMOUNT_ACTIVE_ELEMENTS_IN_HOLONS:
  198. property = "Avg. Amount of Active Elements in Holons";
  199. break;
  200. case TrackedDataSet.AVG_AMOUNT_INACTIVE_ELEMENTS_IN_HOLONS:
  201. property = "Avg. Amount of Inactive Elements in Holons";
  202. break;
  203. case TrackedDataSet.AVG_PRODUCED_ENERGY_IN_HOLONS:
  204. property = "Avg. Produced Energy in Holons";
  205. break;
  206. default:
  207. property = "null";
  208. break;
  209. }
  210. JLabel b;
  211. if (set.getCpsObject() != null) {
  212. b = new JLabel(set.getCpsObject().getId() + ", " + set.getCpsObject().getName() + ": " + property);
  213. } else {
  214. b = new JLabel(property);
  215. }
  216. // b.setBackground(set.getColor());
  217. b.setBorder(BorderFactory.createLineBorder(set.getColor()));
  218. int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()), set.getColor().getBlue());
  219. b.setOpaque(true);
  220. b.addMouseListener(new MouseAdapter() {
  221. @Override
  222. public void mousePressed(MouseEvent e) {
  223. if (MouseEvent.BUTTON3 == e.getButton()) {
  224. for (int i = 0; i < legendPanel.getComponentCount(); i++) {
  225. if (legendPanel.getComponent(i).equals(e.getComponent())) {
  226. legendPanel.remove(i);
  227. sGraph.removeObject(i);
  228. that.updateUI();
  229. }
  230. }
  231. }
  232. }
  233. });
  234. legendPanel.add(b);
  235. sGraph.calcMaximum();
  236. that.updateUI();
  237. }
  238. /**
  239. * Set the Maximum Label
  240. *
  241. * @param max
  242. */
  243. public void setMaximumLabel(double max) {
  244. maximumLabel.setText(Double.toString(max));
  245. }
  246. /**
  247. * Get the name of the Graph.
  248. *
  249. * @return the name of the Graph
  250. */
  251. public String getGraphName() {
  252. return this.graphName;
  253. }
  254. /**
  255. * Calls the addValue function of the sGraph
  256. */
  257. public void addValues() {
  258. sGraph.addValues();
  259. }
  260. /**
  261. * Calls the calcMaximum function of the sGraph
  262. */
  263. public void calcMaximum() {
  264. sGraph.calcMaximum();
  265. }
  266. public StatisticGraph getStatGraph() {
  267. return sGraph;
  268. }
  269. public void setStatisticGraph(StatisticGraph sG) {
  270. this.sGraph = sG;
  271. }
  272. /**
  273. * Reset the Graph. Delete all calculated values.
  274. */
  275. public void resetGraph() {
  276. sGraph.resetGraph();
  277. }
  278. /**
  279. * Returns the Legend Panel.
  280. * @return legendPanel
  281. */
  282. public JPanel getLegendPanel(){
  283. return legendPanel;
  284. }
  285. }