StatisticGraphPanel.java 8.2 KB

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