StatisticGraphPanel.java 9.9 KB

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