package ui.view; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.stream.Collectors; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import com.google.gson.JsonObject; import classes.TrackedDataSet; import ui.controller.Control; import ui.model.Model; public class StatisticGraphPanel extends JPanel { private static final long serialVersionUID = 1L; // Model/Controller private Model model; private Control controller; // Components private StatisticGraph sGraph; private final JLabel graphNameLabel; private final JLabel maximumLabel = new JLabel("0"); private JPanel topPanel = new JPanel(); private JButton closeButton = new JButton("X"); private JButton savImgButton = new JButton("Save as Image"); // Variables String graphName; private final JPanel legendPanel = new JPanel(); private JPanel that; private Hashtable graphHashtable; /** * Constructor. * * @param mod * the Model * @param cont * the Controller */ public StatisticGraphPanel(Model mod, Control cont, String name, Hashtable gHt) { super(); setBorder(new EmptyBorder(0, 0, 0, 0)); this.model = mod; this.controller = cont; this.sGraph = new StatisticGraph(mod, cont); this.graphName = name; this.graphHashtable = gHt; setLayout(new BorderLayout(0, 0)); // ******************** Component Propertys ***************// // Graph // this.setPreferredSize(new Dimension(300, 200)); sGraph.setPreferredSize(new Dimension(200, 200)); sGraph.setMinimumSize(new Dimension(100, 150)); // this.setMaximumSize(new Dimension(700, 200)); // this.setMinimumSize(new Dimension(300, 200)); // Graph Name graphNameLabel = new JLabel(graphName); graphNameLabel.setHorizontalTextPosition(JLabel.CENTER); // Panel on top (Name and Close Button) topPanel.setLayout(new BorderLayout(0, 0)); JPanel topPanelHelp = new JPanel(new BorderLayout(0, 0)); topPanelHelp.add(graphNameLabel, BorderLayout.CENTER); topPanelHelp.add(savImgButton, BorderLayout.EAST); topPanel.add(topPanelHelp, BorderLayout.CENTER); topPanel.add(closeButton, BorderLayout.EAST); savImgButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { BufferedImage img = new BufferedImage(that.getWidth(), that.getHeight(), BufferedImage.TYPE_INT_RGB); that.print(img.getGraphics()); try { JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showSaveDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) { String file = fileChooser.getSelectedFile().getPath(); ImageIO.write(img, "jpg", new File(file + ".jpg")); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); topPanel.setBorder(null); // Maximum Label maximumLabel.setVerticalAlignment(SwingConstants.TOP); maximumLabel.setMinimumSize(new Dimension(30, 10)); legendPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT); // Legend Panel legendPanel.setLayout(new GridLayout(0, 5, 0, 0)); // ******************** Component Listener ****************// that = this; closeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JPanel parent = (JPanel) that.getParent(); for (int i = 0; i < parent.getComponentCount(); i++) { if (parent.getComponent(i).equals(that)) { parent.remove(parent.getComponent(i + 1)); break; } } graphHashtable.remove(graphName); parent.remove(that); parent.updateUI(); } }); // ******************** add everything ********************// this.add(sGraph); this.add(topPanel, BorderLayout.NORTH); this.add(maximumLabel, BorderLayout.WEST); this.add(legendPanel, BorderLayout.SOUTH); } /** * Adds the Set to the Graph. * * @param set */ public void addObject(TrackedDataSet set) { sGraph.addObject(set); String property = ""; switch (set.getProperty()) { case TrackedDataSet.CONSUMPTION: case TrackedDataSet.GROUP_CONSUMPTION: property = "consumption"; break; case TrackedDataSet.PRODUCTION: case TrackedDataSet.GROUP_PRODUCTION: property = "production"; break; case TrackedDataSet.ACTIVATED_ELEMENTS: property = "active elements"; break; case TrackedDataSet.ON_OFF: property = "on//off"; break; case TrackedDataSet.TOTAL_PRODUCTION: property = "total production"; break; case TrackedDataSet.TOTAL_CONSUMPTION: property = "total consumption"; break; case TrackedDataSet.PERCENT_SUPPLIED: property = "Percentage of supplied"; break; case TrackedDataSet.PERCENT_NOT_SUPPLIED: property = "Percentage of not supplied"; break; case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED: property = "Percentage of partial supplied"; break; case TrackedDataSet.AMOUNT_HOLONS: property = "Amount of holons"; break; case TrackedDataSet.AMOUNT_CLOSED_SWITCHES: property = "Amount of Closed Switches"; break; case TrackedDataSet.AVG_AMOUNT_OBJECTS_IN_HOLONS: property = "Avg. Amount of Objects in Holons"; break; case TrackedDataSet.AVG_AMOUNT_ELEMENTS_IN_HOLONS: property = "Avg. Amount of Elements in Holons"; break; case TrackedDataSet.AVG_AMOUNT_PRODUCERS_IN_HOLONS: property = "Avg. Amount Producers in Holons"; break; case TrackedDataSet.AVG_CONSUMED_ENERGY_IN_HOLONS: property = "Avg. Consumed Energy in Holons"; break; case TrackedDataSet.AVG_WASTED_ENERGY_IN_HOLONS: property = "Avg. Wasted Energy in Holons"; break; case TrackedDataSet.AMOUNT_BROKEN_EDGES: property = "Amount of Broken Edged"; break; case TrackedDataSet.RATIO_PRODUCERS_CONSUMERS: property = "Ratio Producers:Consumers"; break; case TrackedDataSet.AVG_AMOUNT_CLOSED_SWITCHES_IN_HOLONS: property = "Avg. Amount of Closed Switches in Holons"; break; case TrackedDataSet.AVG_AMOUNT_ACTIVE_ELEMENTS_IN_HOLONS: property = "Avg. Amount of Active Elements in Holons"; break; case TrackedDataSet.AVG_AMOUNT_INACTIVE_ELEMENTS_IN_HOLONS: property = "Avg. Amount of Inactive Elements in Holons"; break; case TrackedDataSet.AVG_PRODUCED_ENERGY_IN_HOLONS: property = "Avg. Produced Energy in Holons"; break; default: property = "null"; break; } JLabel b; if (set.getCpsObject() != null) { b = new JLabel(set.getCpsObject().getId() + ", " + set.getCpsObject().getName() + ": " + property); } else { b = new JLabel(property); } // b.setBackground(set.getColor()); b.setBorder(BorderFactory.createLineBorder(set.getColor())); int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()), set.getColor().getBlue()); b.setOpaque(true); b.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (MouseEvent.BUTTON3 == e.getButton()) { for (int i = 0; i < legendPanel.getComponentCount(); i++) { if (legendPanel.getComponent(i).equals(e.getComponent())) { legendPanel.remove(i); sGraph.removeObject(i); that.updateUI(); } } } } }); legendPanel.add(b); sGraph.calcMaximum(); that.updateUI(); } /** * Set the Maximum Label * * @param max */ public void setMaximumLabel(double max) { maximumLabel.setText(Double.toString(max)); } /** * Get the name of the Graph. * * @return the name of the Graph */ public String getGraphName() { return this.graphName; } /** * Calls the addValue function of the sGraph */ public void addValues() { sGraph.addValues(); } /** * Calls the calcMaximum function of the sGraph */ public void calcMaximum() { sGraph.calcMaximum(); } public StatisticGraph getStatGraph() { return sGraph; } public void setStatisticGraph(StatisticGraph sG) { this.sGraph = sG; } /** * Reset the Graph. Delete all calculated values. */ public void resetGraph() { sGraph.resetGraph(); } /** * Returns the Legend Panel. * @return legendPanel */ public JPanel getLegendPanel(){ return legendPanel; } }