|
| 1 | +package com.uniovi.nmapgui; |
| 2 | + |
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.awt.Container; |
| 5 | +import java.awt.Desktop; |
| 6 | +import java.awt.FlowLayout; |
| 7 | +import java.awt.GridLayout; |
| 8 | +import java.awt.event.ActionEvent; |
| 9 | +import java.awt.event.ActionListener; |
| 10 | +import java.awt.image.BufferedImage; |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.MalformedURLException; |
| 13 | +import java.net.URI; |
| 14 | +import java.net.URISyntaxException; |
| 15 | +import java.net.URL; |
| 16 | + |
| 17 | +import javax.imageio.ImageIO; |
| 18 | +import javax.swing.BorderFactory; |
| 19 | +import javax.swing.ImageIcon; |
| 20 | +import javax.swing.JButton; |
| 21 | +import javax.swing.JFrame; |
| 22 | +import javax.swing.JLabel; |
| 23 | +import javax.swing.JPanel; |
| 24 | + |
| 25 | +import org.springframework.context.ConfigurableApplicationContext; |
| 26 | + |
| 27 | +import com.uniovi.nmapgui.executor.CommandExecutor; |
| 28 | +import com.uniovi.nmapgui.model.Command; |
| 29 | + |
| 30 | + |
| 31 | +public class NMapLoaderWindow extends JFrame { |
| 32 | + |
| 33 | + |
| 34 | + private static final long serialVersionUID = 3028171478038465651L; |
| 35 | + |
| 36 | + |
| 37 | + private static final String IMG_PATH = "static/img/header.jpg"; |
| 38 | + |
| 39 | + |
| 40 | + private JLabel image; |
| 41 | + private JButton start; |
| 42 | + private JButton stop; |
| 43 | + private ConfigurableApplicationContext springContext; |
| 44 | + private JButton go; |
| 45 | + private CommandExecutor executor = new CommandExecutor(new Command("-V")); |
| 46 | + private boolean nmapInstalled; |
| 47 | + |
| 48 | + |
| 49 | + public NMapLoaderWindow(){ |
| 50 | + super("NMapGUI"); |
| 51 | + try { |
| 52 | + executor.execute(); |
| 53 | + executor.getCommandThread().join(); |
| 54 | + |
| 55 | + } catch (InterruptedException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + nmapInstalled=executor.getCmd().getOutput().getText().contains("Nmap version"); |
| 59 | + |
| 60 | + setSize(400, 300); |
| 61 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 62 | + Container cp = getContentPane(); |
| 63 | + BorderLayout layout = new BorderLayout(); |
| 64 | + cp.setLayout(layout); |
| 65 | + cp.add(image(), BorderLayout.CENTER); |
| 66 | + cp.add(buttons(), BorderLayout.SOUTH); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + private JPanel image() { |
| 71 | + JPanel panel = new JPanel(); |
| 72 | + panel.setLayout(new BorderLayout()); |
| 73 | + BufferedImage img = null; |
| 74 | + |
| 75 | + try { |
| 76 | + img = ImageIO.read(NMapLoaderWindow.class.getClassLoader().getResource(IMG_PATH)); |
| 77 | + } catch (IOException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + ImageIcon icon = new ImageIcon(img); |
| 81 | + image = new JLabel(icon); |
| 82 | + image.setLayout(new FlowLayout(FlowLayout.RIGHT)); |
| 83 | + |
| 84 | + |
| 85 | + go= new JButton("Go!"); |
| 86 | + go.setEnabled(false); |
| 87 | + |
| 88 | + go.addActionListener(new ActionListener() { |
| 89 | + @Override |
| 90 | + public void actionPerformed(ActionEvent e) { |
| 91 | + try { |
| 92 | + openWebpage(new URL("http://localhost:8080/nmap")); |
| 93 | + } catch (MalformedURLException e1) { |
| 94 | + e1.printStackTrace(); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + image.add(go); |
| 99 | + panel.add(image); |
| 100 | + |
| 101 | + return panel; |
| 102 | + } |
| 103 | + |
| 104 | + private JPanel buttons() { |
| 105 | + JPanel buttons = new JPanel(); |
| 106 | + buttons.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| 107 | + GridLayout gl = new GridLayout(2,1); |
| 108 | + gl.setHgap(10); |
| 109 | + gl.setVgap(5); |
| 110 | + buttons.setLayout(gl); |
| 111 | + start = new JButton("Start NMapGUI"); |
| 112 | + if(!nmapInstalled){ |
| 113 | + start.setEnabled(false); |
| 114 | + start.setText("NMap is not installed"); |
| 115 | + |
| 116 | + } |
| 117 | + start.addActionListener(new ActionListener() { |
| 118 | + @Override |
| 119 | + public void actionPerformed(ActionEvent e) { |
| 120 | + start.setEnabled(false); |
| 121 | + String[] args = {}; |
| 122 | + springContext = NMapGuiApplication.mainExec(args); |
| 123 | + start.setText("NMapGUI running"); |
| 124 | + go.setEnabled(true); |
| 125 | + stop.setText("Stop NMapGUI"); |
| 126 | + stop.setEnabled(true); |
| 127 | + } |
| 128 | + }); |
| 129 | + buttons.add(start); |
| 130 | + |
| 131 | + stop = new JButton("Nmap not running"); |
| 132 | + stop.addActionListener(new ActionListener() { |
| 133 | + @Override |
| 134 | + public void actionPerformed(ActionEvent e) { |
| 135 | + stop.setEnabled(false); |
| 136 | + go.setEnabled(false); |
| 137 | + springContext.close(); |
| 138 | + stop.setText("NMapGUI not running"); |
| 139 | + start.setText("Start NMapGUI"); |
| 140 | + start.setEnabled(true); |
| 141 | + } |
| 142 | + }); |
| 143 | + stop.setEnabled(false); |
| 144 | + buttons.add(stop); |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + return buttons; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + public static void openWebpage(URI uri) { |
| 155 | + Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; |
| 156 | + if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { |
| 157 | + try { |
| 158 | + desktop.browse(uri); |
| 159 | + } catch (Exception e) { |
| 160 | + e.printStackTrace(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + public static void openWebpage(URL url) { |
| 166 | + try { |
| 167 | + openWebpage(url.toURI()); |
| 168 | + } catch (URISyntaxException e) { |
| 169 | + e.printStackTrace(); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments