123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- 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.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JSplitPane;
- 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 JSplitPane {
- 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");
- private JPanel topContainer = new JPanel();
- private JPanel buttomContainer = new JPanel();
- // Variables
- String graphName;
- private final JPanel legendPanel = new JPanel();
- private JSplitPane that;
- private Hashtable<String, StatisticGraphPanel> graphHashtable;
- /**
- * Constructor.
- *
- * @param mod
- * the Model
- * @param cont
- * the Controller
- */
- public StatisticGraphPanel(Model mod, Control cont, String name, Hashtable<String, StatisticGraphPanel> gHt) {
- super();
- setDividerSize(0);
- setPreferredSize(new Dimension(600, 300));
- setContinuousLayout(true);
- setMinimumSize(new Dimension(600, 300));
- setOrientation(JSplitPane.VERTICAL_SPLIT);
- 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;
- topContainer.setLayout(new BorderLayout(0,0));
- buttomContainer.setPreferredSize(new Dimension(0, 0));
- buttomContainer.setMinimumSize(new Dimension(0, 0));
- buttomContainer.setAlignmentX(Component.LEFT_ALIGNMENT);
- buttomContainer.setAlignmentY(Component.TOP_ALIGNMENT);
- buttomContainer.setLayout(new BorderLayout(0,0));
- this.setTopComponent(topContainer);
- this.setBottomComponent(buttomContainer);
- //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)) {
- if(parent.getComponentCount() > i + 1){
- parent.remove(parent.getComponent(i+1));
- }
- break;
- }
- }
- graphHashtable.remove(graphName);
- parent.remove(that);
- parent.updateUI();
- }
- });
- // ******************** add everything ********************//
- topContainer.add(sGraph);
- topContainer.add(topPanel, BorderLayout.NORTH);
- topContainer.add(maximumLabel, BorderLayout.WEST);
- buttomContainer.add(legendPanel, BorderLayout.NORTH);
- this.setEnabled(false);
- }
- /**
- * Adds the Set to the Graph.
- *
- * @param set
- */
- public void addObject(TrackedDataSet set) {
- if(legendPanel.getComponentCount() >= 20){
- JOptionPane.showMessageDialog(null, "You can not add more than 20 Properties to a Graph");
- return;
- }
- 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(), 2));
- b.setHorizontalAlignment(SwingConstants.CENTER);
- 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;
- }
- }
|