StatisticGraphPanel.java 13 KB

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