StatisticGraphPanel.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.SwingConstants;
  10. import javax.swing.Timer;
  11. import javax.swing.text.StyledDocument;
  12. import java.awt.BorderLayout;
  13. import java.awt.Color;
  14. import java.awt.Dimension;
  15. import java.awt.FlowLayout;
  16. import java.awt.Font;
  17. import java.awt.LayoutManager;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.MouseAdapter;
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23. import java.util.Hashtable;
  24. import javax.swing.BoxLayout;
  25. import java.awt.GridLayout;
  26. public class StatisticGraphPanel extends JPanel {
  27. private static final long serialVersionUID = 1L;
  28. // Model/Controller
  29. private Model model;
  30. private Control controller;
  31. // Components
  32. private StatisticGraph sGraph;
  33. private final JLabel graphNameLabel;
  34. private final JLabel maximumLabel = new JLabel("0");
  35. private JPanel topPanel = new JPanel();
  36. private JButton closeButton = new JButton("X");
  37. // Variables
  38. String graphName;
  39. private final JPanel legendPanel = new JPanel();
  40. private JPanel that;
  41. private Hashtable<String, StatisticGraphPanel> graphHashtable;
  42. private JPanel tempP; // for makeLegend
  43. /**
  44. * Constructor.
  45. *
  46. * @param mod
  47. * the Model
  48. * @param cont
  49. * the Controller
  50. */
  51. public StatisticGraphPanel(Model mod, Control cont, String name, Hashtable<String, StatisticGraphPanel> gHt) {
  52. super();
  53. this.model = mod;
  54. this.controller = cont;
  55. this.sGraph = new StatisticGraph(mod, cont);
  56. this.graphName = name;
  57. this.graphHashtable = gHt;
  58. setLayout(new BorderLayout(0, 0));
  59. // ******************** Component Propertys ***************//
  60. // Graph
  61. sGraph.setPreferredSize(new Dimension(280, 180));
  62. sGraph.setMinimumSize(new Dimension(100, 150));
  63. //this.setMaximumSize(new Dimension(1000, 1000));
  64. // Graph Name
  65. graphNameLabel = new JLabel(graphName);
  66. graphNameLabel.setHorizontalTextPosition(JLabel.CENTER);
  67. // Panel on top (Name and Close Button)
  68. topPanel.setLayout(new BorderLayout(0, 0));
  69. topPanel.add(graphNameLabel, BorderLayout.CENTER);
  70. topPanel.add(closeButton, BorderLayout.EAST);
  71. topPanel.setBorder(null);
  72. // Maximum Label
  73. maximumLabel.setVerticalAlignment(SwingConstants.TOP);
  74. maximumLabel.setMinimumSize(new Dimension(30, 10));
  75. //Legend Panel
  76. legendPanel.setLayout(new GridLayout(0, 5, 0, 0));
  77. // ******************** Component Listener ****************//
  78. that = this;
  79. closeButton.addActionListener(new ActionListener() {
  80. @Override
  81. public void actionPerformed(ActionEvent e) {
  82. JPanel parent = (JPanel) that.getParent();
  83. for (int i = 0; i < parent.getComponentCount(); i++) {
  84. if (parent.getComponent(i).equals(that)) {
  85. parent.remove(parent.getComponent(i + 1));
  86. break;
  87. }
  88. }
  89. graphHashtable.remove(graphName);
  90. parent.remove(that);
  91. parent.updateUI();
  92. }
  93. });
  94. // ******************** add everything ********************//
  95. this.add(sGraph);
  96. this.add(topPanel, BorderLayout.NORTH);
  97. this.add(maximumLabel, BorderLayout.WEST);
  98. this.add(legendPanel, BorderLayout.SOUTH);
  99. }
  100. /**
  101. * Adds the Set to the Graph.
  102. *
  103. * @param set
  104. */
  105. public void addObjec(TrackedDataSet set) {
  106. sGraph.addObject(set);
  107. String property = "";
  108. switch (set.getProperty()) {
  109. case TrackedDataSet.CONSUMPTION:
  110. property = "consumption";
  111. break;
  112. case TrackedDataSet.PRODUCTION:
  113. property = "production";
  114. break;
  115. case TrackedDataSet.ACTIVATED_ELEMENTS:
  116. property = "active elements";
  117. break;
  118. case TrackedDataSet.ON_OFF:
  119. property = "on//off";
  120. break;
  121. default:
  122. break;
  123. }
  124. JLabel b = new JLabel(set.getCpsObject().getName() + ": " + property);
  125. b.setBackground(set.getColor());
  126. int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()),set.getColor().getBlue());
  127. if (color<=128) {
  128. b.setForeground(Color.WHITE);
  129. }
  130. b.setOpaque(true);
  131. b.addMouseListener(new MouseAdapter() {
  132. @Override
  133. public void mousePressed(MouseEvent e) {
  134. if (MouseEvent.BUTTON3 == e.getButton()) {
  135. for (int i = 0; i < legendPanel.getComponentCount(); i++) {
  136. if (legendPanel.getComponent(i).equals(e.getComponent())) {
  137. legendPanel.remove(i);
  138. sGraph.removeObject(i);
  139. that.updateUI();
  140. }
  141. }
  142. }
  143. }
  144. });
  145. legendPanel.add(b);
  146. sGraph.calcMaximum();
  147. that.updateUI();
  148. }
  149. /**
  150. * Set the Maximum Label
  151. *
  152. * @param max
  153. */
  154. public void setMaximumLabel(double max) {
  155. maximumLabel.setText(Double.toString(max));
  156. }
  157. /**
  158. * Get the name of the Graph.
  159. *
  160. * @return the name of the Graph
  161. */
  162. public String getGraphName() {
  163. return this.graphName;
  164. }
  165. }