StatisticGraphPanel.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. if(parent.getComponentCount() > i + 1){
  132. parent.remove(parent.getComponent(i+1));
  133. }
  134. break;
  135. }
  136. }
  137. graphHashtable.remove(graphName);
  138. parent.remove(that);
  139. parent.updateUI();
  140. }
  141. });
  142. // ******************** add everything ********************//
  143. topContainer.add(sGraph);
  144. topContainer.add(topPanel, BorderLayout.NORTH);
  145. topContainer.add(maximumLabel, BorderLayout.WEST);
  146. buttomContainer.add(legendPanel, BorderLayout.NORTH);
  147. this.setEnabled(false);
  148. }
  149. /**
  150. * Adds the Set to the Graph.
  151. *
  152. * @param set
  153. */
  154. public void addObject(TrackedDataSet set) {
  155. if(legendPanel.getComponentCount() >= 20){
  156. JOptionPane.showMessageDialog(null, "You can not add more than 20 Properties to a Graph");
  157. return;
  158. }
  159. sGraph.addObject(set);
  160. String property = "";
  161. switch (set.getProperty()) {
  162. case TrackedDataSet.CONSUMPTION:
  163. case TrackedDataSet.GROUP_CONSUMPTION:
  164. property = "consumption";
  165. break;
  166. case TrackedDataSet.PRODUCTION:
  167. case TrackedDataSet.GROUP_PRODUCTION:
  168. property = "production";
  169. break;
  170. case TrackedDataSet.ACTIVATED_ELEMENTS:
  171. property = "active elements";
  172. break;
  173. case TrackedDataSet.ON_OFF:
  174. property = "on//off";
  175. break;
  176. case TrackedDataSet.TOTAL_PRODUCTION:
  177. property = "total production";
  178. break;
  179. case TrackedDataSet.TOTAL_CONSUMPTION:
  180. property = "total consumption";
  181. break;
  182. case TrackedDataSet.PERCENT_SUPPLIED:
  183. property = "Percentage of supplied";
  184. break;
  185. case TrackedDataSet.PERCENT_NOT_SUPPLIED:
  186. property = "Percentage of not supplied";
  187. break;
  188. case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
  189. property = "Percentage of partial supplied";
  190. break;
  191. case TrackedDataSet.AMOUNT_HOLONS:
  192. property = "Amount of holons";
  193. break;
  194. case TrackedDataSet.AMOUNT_CLOSED_SWITCHES:
  195. property = "Amount of Closed Switches";
  196. break;
  197. case TrackedDataSet.AVG_AMOUNT_OBJECTS_IN_HOLONS:
  198. property = "Avg. Amount of Objects in Holons";
  199. break;
  200. case TrackedDataSet.AVG_AMOUNT_ELEMENTS_IN_HOLONS:
  201. property = "Avg. Amount of Elements in Holons";
  202. break;
  203. case TrackedDataSet.AVG_AMOUNT_PRODUCERS_IN_HOLONS:
  204. property = "Avg. Amount Producers in Holons";
  205. break;
  206. case TrackedDataSet.AVG_CONSUMED_ENERGY_IN_HOLONS:
  207. property = "Avg. Consumed Energy in Holons";
  208. break;
  209. case TrackedDataSet.AVG_WASTED_ENERGY_IN_HOLONS:
  210. property = "Avg. Wasted Energy in Holons";
  211. break;
  212. case TrackedDataSet.AMOUNT_BROKEN_EDGES:
  213. property = "Amount of Broken Edged";
  214. break;
  215. case TrackedDataSet.RATIO_PRODUCERS_CONSUMERS:
  216. property = "Ratio Producers:Consumers";
  217. break;
  218. case TrackedDataSet.AVG_AMOUNT_CLOSED_SWITCHES_IN_HOLONS:
  219. property = "Avg. Amount of Closed Switches in Holons";
  220. break;
  221. case TrackedDataSet.AVG_AMOUNT_ACTIVE_ELEMENTS_IN_HOLONS:
  222. property = "Avg. Amount of Active Elements in Holons";
  223. break;
  224. case TrackedDataSet.AVG_AMOUNT_INACTIVE_ELEMENTS_IN_HOLONS:
  225. property = "Avg. Amount of Inactive Elements in Holons";
  226. break;
  227. case TrackedDataSet.AVG_PRODUCED_ENERGY_IN_HOLONS:
  228. property = "Avg. Produced Energy in Holons";
  229. break;
  230. default:
  231. property = "null";
  232. break;
  233. }
  234. JLabel b;
  235. if (set.getCpsObject() != null) {
  236. b = new JLabel(set.getCpsObject().getId() + ", " + set.getCpsObject().getName() + ": " + property);
  237. } else {
  238. b = new JLabel(property);
  239. }
  240. // b.setBackground(set.getColor());
  241. b.setBorder(BorderFactory.createLineBorder(set.getColor(), 2));
  242. b.setHorizontalAlignment(SwingConstants.CENTER);
  243. int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()), set.getColor().getBlue());
  244. b.setOpaque(true);
  245. b.addMouseListener(new MouseAdapter() {
  246. @Override
  247. public void mousePressed(MouseEvent e) {
  248. if (MouseEvent.BUTTON3 == e.getButton()) {
  249. for (int i = 0; i < legendPanel.getComponentCount(); i++) {
  250. if (legendPanel.getComponent(i).equals(e.getComponent())) {
  251. legendPanel.remove(i);
  252. sGraph.removeObject(i);
  253. that.updateUI();
  254. }
  255. }
  256. }
  257. }
  258. });
  259. legendPanel.add(b);
  260. sGraph.calcMaximum();
  261. that.updateUI();
  262. }
  263. /**
  264. * Set the Maximum Label
  265. *
  266. * @param max
  267. */
  268. public void setMaximumLabel(double max) {
  269. maximumLabel.setText(Double.toString(max));
  270. }
  271. /**
  272. * Get the name of the Graph.
  273. *
  274. * @return the name of the Graph
  275. */
  276. public String getGraphName() {
  277. return this.graphName;
  278. }
  279. /**
  280. * Calls the addValue function of the sGraph
  281. */
  282. public void addValues() {
  283. sGraph.addValues();
  284. }
  285. /**
  286. * Calls the calcMaximum function of the sGraph
  287. */
  288. public void calcMaximum() {
  289. sGraph.calcMaximum();
  290. }
  291. public StatisticGraph getStatGraph() {
  292. return sGraph;
  293. }
  294. public void setStatisticGraph(StatisticGraph sG) {
  295. this.sGraph = sG;
  296. }
  297. /**
  298. * Reset the Graph. Delete all calculated values.
  299. */
  300. public void resetGraph() {
  301. sGraph.resetGraph();
  302. }
  303. /**
  304. * Returns the Legend Panel.
  305. * @return legendPanel
  306. */
  307. public JPanel getLegendPanel(){
  308. return legendPanel;
  309. }
  310. }