import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

/**
 * Trieda pouzivana ako singleton pre vytvorenie dialogoveho okna, ktore moze zabit java
 * proces, ktory to okno vyvolal. Okono sa vyvolava pouzitim factory metody
 * createKiller()
 */
public class Killer extends JFrame {
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  JButton jButton1 = new JButton();

  private Killer() {
    try {
      jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }
  /**
   * Kreuje dialogove okno, ktorym sa da zabit proces, ktory ho vykreoval
   */
  public static void createKiller(){
    Killer killer = new Killer();
    killer.pack();
    // Position the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = killer.getSize();


    killer.setLocation((int)(0.05*screenSize.width),
                       (int)(0.95*screenSize.height) - frameSize.height);

    killer.setVisible(true);

  }
  private void jbInit() throws Exception {
    getContentPane().setLayout(gridBagLayout1);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("Process killer");
    jButton1.setText("KILL JAVA PROCESS !!!");
    jButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    this.getContentPane().add(jButton1,
                              new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
        new Insets(41, 39, 45, 25), 21, 0));
  }
  /**
   * Metoda main nie je auscastou API, sluzi len na testovanie a ako priklad pouzitia
   */
  public static void main(String[] args) {
    createKiller();
  }

  private void jButton1_actionPerformed(ActionEvent e) {
    System.exit(0);
  }
}
