StatisticGraphPanel.java 8.5 KB

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