StatisticGraphPanel.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.BoxLayout;
  32. import javax.swing.GroupLayout.Alignment;
  33. import java.awt.GridLayout;
  34. public class StatisticGraphPanel extends JPanel {
  35. private static final long serialVersionUID = 1L;
  36. // Model/Controller
  37. private Model model;
  38. private Control controller;
  39. // Components
  40. private StatisticGraph sGraph;
  41. private final JLabel graphNameLabel;
  42. private final JLabel maximumLabel = new JLabel("0");
  43. private JPanel topPanel = new JPanel();
  44. private JButton closeButton = new JButton("X");
  45. private JButton savImgButton = new JButton("Save as Image");
  46. // Variables
  47. String graphName;
  48. private final JPanel legendPanel = new JPanel();
  49. private JPanel that;
  50. private Hashtable<String, StatisticGraphPanel> graphHashtable;
  51. /**
  52. * Constructor.
  53. *
  54. * @param mod
  55. * the Model
  56. * @param cont
  57. * the Controller
  58. */
  59. public StatisticGraphPanel(Model mod, Control cont, String name, Hashtable<String, StatisticGraphPanel> gHt) {
  60. super();
  61. this.model = mod;
  62. this.controller = cont;
  63. this.sGraph = new StatisticGraph(mod, cont);
  64. this.graphName = name;
  65. this.graphHashtable = gHt;
  66. setLayout(new BorderLayout(0, 0));
  67. // ******************** Component Propertys ***************//
  68. // Graph
  69. // this.setPreferredSize(new Dimension(280, 300));
  70. sGraph.setPreferredSize(new Dimension(280, 180));
  71. sGraph.setMinimumSize(new Dimension(100, 150));
  72. // this.setMaximumSize(new Dimension(1000, 1000));
  73. // Graph Name
  74. graphNameLabel = new JLabel(graphName);
  75. graphNameLabel.setHorizontalTextPosition(JLabel.CENTER);
  76. // Panel on top (Name and Close Button)
  77. topPanel.setLayout(new BorderLayout(0, 0));
  78. topPanel.add(graphNameLabel, BorderLayout.WEST);
  79. topPanel.add(closeButton, BorderLayout.EAST);
  80. topPanel.add(savImgButton, BorderLayout.CENTER);
  81. savImgButton.addActionListener(new ActionListener() {
  82. @Override
  83. public void actionPerformed(ActionEvent e) {
  84. BufferedImage img = new BufferedImage(that.getWidth(), that.getHeight(), BufferedImage.TYPE_INT_RGB);
  85. that.print(img.getGraphics());
  86. try {
  87. JFileChooser fileChooser = new JFileChooser();
  88. if (fileChooser.showSaveDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) {
  89. String file = fileChooser.getSelectedFile().getPath();
  90. ImageIO.write(img, "jpg", new File(file + ".jpg"));
  91. }
  92. } catch (IOException e1) {
  93. // TODO Auto-generated catch block
  94. e1.printStackTrace();
  95. }
  96. }
  97. });
  98. topPanel.setBorder(null);
  99. // Maximum Label
  100. maximumLabel.setVerticalAlignment(SwingConstants.TOP);
  101. maximumLabel.setMinimumSize(new Dimension(30, 10));
  102. // Legend Panel
  103. legendPanel.setLayout(new GridLayout(0, 5, 0, 0));
  104. // ******************** Component Listener ****************//
  105. that = this;
  106. closeButton.addActionListener(new ActionListener() {
  107. @Override
  108. public void actionPerformed(ActionEvent e) {
  109. JPanel parent = (JPanel) that.getParent();
  110. for (int i = 0; i < parent.getComponentCount(); i++) {
  111. if (parent.getComponent(i).equals(that)) {
  112. parent.remove(parent.getComponent(i + 1));
  113. break;
  114. }
  115. }
  116. graphHashtable.remove(graphName);
  117. parent.remove(that);
  118. parent.updateUI();
  119. }
  120. });
  121. // ******************** add everything ********************//
  122. this.add(sGraph);
  123. this.add(topPanel, BorderLayout.NORTH);
  124. this.add(maximumLabel, BorderLayout.WEST);
  125. this.add(legendPanel, BorderLayout.SOUTH);
  126. }
  127. /**
  128. * Adds the Set to the Graph.
  129. *
  130. * @param set
  131. */
  132. public void addObjec(TrackedDataSet set) {
  133. sGraph.addObject(set);
  134. String property = "";
  135. switch (set.getProperty()) {
  136. case TrackedDataSet.CONSUMPTION:
  137. property = "consumption";
  138. break;
  139. case TrackedDataSet.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. default:
  164. break;
  165. }
  166. JLabel b;
  167. if (set.getCpsObject() != null) {
  168. b = new JLabel(set.getCpsObject().getID() + ", " + set.getCpsObject().getName() + ": " + property);
  169. } else {
  170. b = new JLabel(property);
  171. }
  172. b.setBackground(set.getColor());
  173. int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()), set.getColor().getBlue());
  174. if (color <= 128) {
  175. b.setForeground(Color.WHITE);
  176. }
  177. b.setOpaque(true);
  178. b.addMouseListener(new MouseAdapter() {
  179. @Override
  180. public void mousePressed(MouseEvent e) {
  181. if (MouseEvent.BUTTON3 == e.getButton()) {
  182. for (int i = 0; i < legendPanel.getComponentCount(); i++) {
  183. if (legendPanel.getComponent(i).equals(e.getComponent())) {
  184. legendPanel.remove(i);
  185. sGraph.removeObject(i);
  186. that.updateUI();
  187. }
  188. }
  189. }
  190. }
  191. });
  192. legendPanel.add(b);
  193. sGraph.calcMaximum();
  194. that.updateUI();
  195. }
  196. /**
  197. * Set the Maximum Label
  198. *
  199. * @param max
  200. */
  201. public void setMaximumLabel(double max) {
  202. maximumLabel.setText(Double.toString(max));
  203. }
  204. /**
  205. * Get the name of the Graph.
  206. *
  207. * @return the name of the Graph
  208. */
  209. public String getGraphName() {
  210. return this.graphName;
  211. }
  212. }