StatisticGraphPanel.java 13 KB

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