StatisticGraphPanel.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. JPanel topPanelHelp = new JPanel(new BorderLayout(0,0));
  79. topPanelHelp.add(graphNameLabel, BorderLayout.CENTER);
  80. topPanelHelp.add(savImgButton, BorderLayout.EAST);
  81. topPanel.add(topPanelHelp, BorderLayout.CENTER);
  82. topPanel.add(closeButton, BorderLayout.EAST);
  83. savImgButton.addActionListener(new ActionListener() {
  84. @Override
  85. public void actionPerformed(ActionEvent e) {
  86. BufferedImage img = new BufferedImage(that.getWidth(), that.getHeight(), BufferedImage.TYPE_INT_RGB);
  87. that.print(img.getGraphics());
  88. try {
  89. JFileChooser fileChooser = new JFileChooser();
  90. if (fileChooser.showSaveDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) {
  91. String file = fileChooser.getSelectedFile().getPath();
  92. ImageIO.write(img, "jpg", new File(file + ".jpg"));
  93. }
  94. } catch (IOException e1) {
  95. // TODO Auto-generated catch block
  96. e1.printStackTrace();
  97. }
  98. }
  99. });
  100. topPanel.setBorder(null);
  101. // Maximum Label
  102. maximumLabel.setVerticalAlignment(SwingConstants.TOP);
  103. maximumLabel.setMinimumSize(new Dimension(30, 10));
  104. // Legend Panel
  105. legendPanel.setLayout(new GridLayout(0, 5, 0, 0));
  106. // ******************** Component Listener ****************//
  107. that = this;
  108. closeButton.addActionListener(new ActionListener() {
  109. @Override
  110. public void actionPerformed(ActionEvent e) {
  111. JPanel parent = (JPanel) that.getParent();
  112. for (int i = 0; i < parent.getComponentCount(); i++) {
  113. if (parent.getComponent(i).equals(that)) {
  114. parent.remove(parent.getComponent(i + 1));
  115. break;
  116. }
  117. }
  118. graphHashtable.remove(graphName);
  119. parent.remove(that);
  120. parent.updateUI();
  121. }
  122. });
  123. // ******************** add everything ********************//
  124. this.add(sGraph);
  125. this.add(topPanel, BorderLayout.NORTH);
  126. this.add(maximumLabel, BorderLayout.WEST);
  127. this.add(legendPanel, BorderLayout.SOUTH);
  128. }
  129. /**
  130. * Adds the Set to the Graph.
  131. *
  132. * @param set
  133. */
  134. public void addObjec(TrackedDataSet set) {
  135. sGraph.addObject(set);
  136. String property = "";
  137. switch (set.getProperty()) {
  138. case TrackedDataSet.CONSUMPTION:
  139. case TrackedDataSet.GROUP_CONSUMPTION:
  140. property = "consumption";
  141. break;
  142. case TrackedDataSet.PRODUCTION:
  143. case TrackedDataSet.GROUP_PRODUCTION:
  144. property = "production";
  145. break;
  146. case TrackedDataSet.ACTIVATED_ELEMENTS:
  147. property = "active elements";
  148. break;
  149. case TrackedDataSet.ON_OFF:
  150. property = "on//off";
  151. break;
  152. case TrackedDataSet.TOTAL_PRODUCTION:
  153. property = "total production";
  154. break;
  155. case TrackedDataSet.TOTAL_CONSUMPTION:
  156. property = "total consumption";
  157. break;
  158. case TrackedDataSet.PERCENT_SUPPLIED:
  159. property = "Percentage of supplied";
  160. break;
  161. case TrackedDataSet.PERCENT_NOT_SUPPLIED:
  162. property = "Percentage of not supplied";
  163. break;
  164. case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
  165. property = "Percentage of partial supplied";
  166. break;
  167. case TrackedDataSet.AMOUNT_HOLONS:
  168. property = "Amount of Subnets";
  169. break;
  170. default:
  171. property = "null";
  172. break;
  173. }
  174. JLabel b;
  175. if (set.getCpsObject() != null) {
  176. b = new JLabel(set.getCpsObject().getID() + ", " + set.getCpsObject().getName() + ": " + property);
  177. } else {
  178. b = new JLabel(property);
  179. }
  180. b.setBackground(set.getColor());
  181. int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()), set.getColor().getBlue());
  182. if (color <= 128) {
  183. b.setForeground(Color.WHITE);
  184. }
  185. b.setOpaque(true);
  186. b.addMouseListener(new MouseAdapter() {
  187. @Override
  188. public void mousePressed(MouseEvent e) {
  189. if (MouseEvent.BUTTON3 == e.getButton()) {
  190. for (int i = 0; i < legendPanel.getComponentCount(); i++) {
  191. if (legendPanel.getComponent(i).equals(e.getComponent())) {
  192. legendPanel.remove(i);
  193. sGraph.removeObject(i);
  194. that.updateUI();
  195. }
  196. }
  197. }
  198. }
  199. });
  200. legendPanel.add(b);
  201. sGraph.calcMaximum();
  202. that.updateUI();
  203. }
  204. /**
  205. * Set the Maximum Label
  206. *
  207. * @param max
  208. */
  209. public void setMaximumLabel(double max) {
  210. maximumLabel.setText(Double.toString(max));
  211. }
  212. /**
  213. * Get the name of the Graph.
  214. *
  215. * @return the name of the Graph
  216. */
  217. public String getGraphName() {
  218. return this.graphName;
  219. }
  220. }