Ecco qui, tanto non c'è nulla da nascondere se non errori su errori, anche se ill programma funziona alla perfezione. Ma mi piacerebbe che fosse anche bello da "leggere"
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class main {
private static int nj=1;
public static void main(String[] arg) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, HeadlessException, IOException {
URL url = ClassLoader.getSystemResource("sfondo2.jpeg");
JLabel ape=new JLabel (new ImageIcon (url));
JFrame sfo = new JFrame ("Ping on SAP");
JPanel pnl =new JPanel(); pnl.setSize(150,20);
pnl.setBackground(Color.WHITE);
sfo.setSize(400, 293);
sfo.setLocationRelativeTo(null);
pnl.setLocation(30, 220);
final JProgressBar pb1 = new JProgressBar();
pb1.setMinimum(0);
pb1.setMaximum(30);
pb1.setStringPainted(true);
pnl.add(pb1);
sfo.add(pnl);
sfo.add(ape);
sfo.setVisible(true);
for (int i = 0; i <= 30; i++) {
final int currentValue = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pb1.setValue(currentValue);
}
});
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(sfo, e.getMessage());
}
}
sfo.setVisible(false);
do {
nj=1;
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
JFrame framex = new JFrame("Ping on SAP");
framex.setUndecorated(true);
JPanel panelx = new JPanel();
panelx.setLayout(new FlowLayout());
JLabel labelx = new JLabel("Checking Internet connection", SwingConstants.CENTER);
panelx.add(labelx);
framex.add(panelx);
framex.setSize(300,30);
framex.setLocationRelativeTo(null);
framex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framex.setVisible(true);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Runtime runtime=Runtime.getRuntime();
String ipAddress1="172.28.8.3";
InetAddress inetr = InetAddress.getByName(ipAddress1);
System.out.println(" Sending Ping Request to " + ipAddress1);
if (inetr.isReachable(5000)){
System.out.println(ipAddress1 + " is reachable.");
framex.setVisible(false);
JFrame framexa = new JFrame("Ping on SAP");
framexa.setUndecorated(true);
JPanel panelxa = new JPanel();
panelxa.setLayout(new FlowLayout());
JLabel labelxa = new JLabel("You are ONLINE", SwingConstants.CENTER);
panelxa.add(labelxa);
framexa.add(panelxa);
framexa.setSize(300,30);
framexa.setLocationRelativeTo(null);
framexa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framexa.setVisible(true);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
framexa.setVisible(false);
}
else {
framex.setVisible(false);
JFrame framexx = new JFrame();
JButton but1= new JButton("CLOSE");
JButton but3= new JButton ("RETRY");
JButton but2 = new JButton(" SKIP ");
JLabel label = new JLabel("Internet connection seems to be down!");
framexx.setLocationRelativeTo(null);
final JDialog d= new JDialog(framexx,"Warning",true);
d.setLayout(new FlowLayout());
d.setDefaultCloseOperation(0);
d.setSize(250,100);
d.setLocationRelativeTo(framexx);
d.add(label);
but3.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
nj=3;
d.setVisible(false);
}
});
but1.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
});
but2.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
d.setVisible(false);
}
});
d.add(but1);
d.add(but3);
d.add(but2);
d.setVisible(true);
}
} while (nj>2);
Integer xbr=1;
Integer ni=0;
String ipAddress="1";
//String soc="1";
Integer sock=0;
Integer x=1;
// APRO GUI INIZIALE
xbr=1;
JTextField rd= new JTextField("10");
JTextField ipA= new JTextField("00.000.000.00");
final JTextField sck= new JTextField("0000");
final JCheckBox cbx= new JCheckBox("Enable socket",true);
cbx.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(cbx.isSelected()){
sck.setEnabled(true);
} else{
sck.setEnabled(false);
}
}
});
Object[] message = {
"Interval request in seconds",rd,
"Server IP",ipA ,cbx,
"Socket",sck
};
do {
xbr=1;
int option = JOptionPane.showConfirmDialog (null, message,"Ping on SAP", JOptionPane.OK_CANCEL_OPTION);
if (option==JOptionPane.CLOSED_OPTION) System.exit(0);
if (option==JOptionPane.CANCEL_OPTION) System.exit(0);
if (option == JOptionPane.OK_OPTION) {
ipAddress = ipA.getText();
String soc= sck.getText();
String ns = rd.getText();
System.out.println(soc);
System.out.println(ipAddress);
System.out.println(ns);
xbr=1;
if (cbx.isSelected()== true){
Pattern patterns = Pattern.compile("[0-9]+");
Matcher matcher1 = patterns.matcher(soc);
if (matcher1.matches())
sock=Integer.parseInt(soc);
else {
JOptionPane.showMessageDialog(null,"You can't insert letters in socket field!", "Warning",JOptionPane.ERROR_MESSAGE);
xbr=3;
}
}
Pattern pattern = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
Matcher matcher = pattern.matcher(ipAddress);
if (matcher.matches());
else {
JOptionPane.showMessageDialog(null,"IP Address incorrect!","Warning", JOptionPane.ERROR_MESSAGE);
xbr=3;
}
ns=ns.replaceAll(" ","");
Pattern patternd = Pattern.compile("[0-9]+");
Matcher matcherd = patternd.matcher(ns);
if (matcherd.matches())
ni = Integer.parseInt(ns);
else {
JOptionPane.showMessageDialog(null,"You can't insert letters in request delay!", "Warning",JOptionPane.ERROR_MESSAGE);
xbr=3;
}
}
} while (xbr>2);
if (cbx.isSelected()== true){
try {
do {
Frame frame1 = new Frame();
frame1.setUndecorated(true);
Label ll = new Label(" Sending PING request to SAP Server ", Label.CENTER); frame1.add(ll);
frame1.pack();
frame1.setVisible(true);
//Thread.sleep(1000);
SocketAddress sockaddr = new InetSocketAddress(ipAddress,sock);
Socket socket = new Socket();
boolean online = true;
try {
socket.connect(sockaddr, 10000);
}
catch (IOException IOException) {
online = false;
}
if(!online){
System.out.println("OFFLINE");
x=3;
frame1.setVisible(false);
System.out.println(ipAddress + " NOT reachable.");
Frame framexc = new Frame();
framexc.setUndecorated(true);
Label lc = new Label(" SAP Server is NOT reachable ", Label.CENTER);
framexc.add(lc);
framexc.pack();
framexc.setBackground(Color.RED);
framexc.setVisible(true);
Toolkit.getDefaultToolkit().beep();
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
framexc.setVisible(false);
final int MAX = ni;
final JFrame frame2 = new JFrame();
final JProgressBar pb = new JProgressBar();
pb.setMinimum(0);
pb.setMaximum(MAX);
pb.setStringPainted(false);
pb.setForeground(Color.blue);
pb.setString("10%");
frame2.setUndecorated(true);
frame2.getContentPane().add(pb);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(220, 18);
//frame2.setBackground(Color.DARK_GRAY);
frame2.setVisible(true);
for (int i = 0; i <= MAX; i++) {
final int currentValue = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pb.setValue(currentValue);
}
});
java.lang.Thread.sleep(1000);
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(framexc, e.getMessage());
}
}
frame2.setVisible(false);
}
if(online){
System.out.println("ONLINE");
Date date = new Date();
SimpleDateFormat ora;
ora= new SimpleDateFormat ("hh:mm");
x=2;
frame1.setVisible(false);
System.out.println(ipAddress + " is reachable.");
JFrame frame = new JFrame("Ping on SAP");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("FINALLY!!!");
JLabel label1 = new JLabel("SAP Server is ON at "+ ora.format(date));
JButton button = new JButton("Open SAP Client");
panel.add(label);
panel.add(label1);
panel.add(button);
panel.setBackground(Color.green);
frame.add(panel);
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
Runtime.getRuntime().exec("say -v Victoria Attention! sap server is now reachable");
} catch (IOException e2) {
e2.printStackTrace();
}
button.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
try {
try {
Runtime.getRuntime().exec(new String[] {"open","-a","SAPGUI 7.20rev8.app"});
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
Thread.sleep(1 * 2000);}
catch (InterruptedException h) {
h.printStackTrace();
}
}
});
}
} while(x>2 );
}finally {}
}
// NO SOCKET OPTION
if (cbx.isSelected()== false){
do{
try {
InetAddress inet =InetAddress.getByName(ipAddress);
System.out.println(" Sending Ping Request to " + ipAddress);
Frame frame1x = new Frame();
frame1x.setUndecorated(true);
Label llx = new Label(" Sending PING request to SAP Server ", Label.CENTER); frame1x.add(llx);
frame1x.pack();
frame1x.setVisible(true);
Thread.sleep(1000);
if (inet.isReachable(5000)){
Date datex = new Date();
SimpleDateFormat orax;
orax= new SimpleDateFormat ("hh:mm");
x=2;
frame1x.setVisible(false);
System.out.println(ipAddress + " is reachable.");
JFrame framexv = new JFrame("Ping on SAP");
JPanel panelxv = new JPanel();
panelxv.setLayout(new FlowLayout());
JLabel labelxv = new JLabel("FINALLY!!!");
JLabel label1x = new JLabel("SAP Server is ON at "+ orax.format(datex));
JButton buttonx = new JButton("Open SAP Client");
panelxv.add(labelxv);
panelxv.add(label1x);
panelxv.add(buttonx);
panelxv.setBackground(Color.green);
framexv.add(panelxv);
framexv.setSize(300,100);
framexv.setLocationRelativeTo(null);
framexv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framexv.setVisible(true);
Runtime.getRuntime().exec("say -v Victoria Attention! sap server is now reachable");
buttonx.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
try {
try {
Runtime.getRuntime().exec(new String[] {"open","-a","SAPGUI 7.20rev8.app"});
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
Thread.sleep(1 * 2000);
}catch (InterruptedException h) {
h.printStackTrace();
}
}
});
}
else {
x=3;
frame1x.setVisible(false);
System.out.println(ipAddress + " NOT reachable.");
Frame framexx = new Frame();
framexx.setUndecorated(true);
Label l = new Label(" SAP Server is NOT reachable ", Label.CENTER);
framexx.add(l);
framexx.pack();
framexx.setBackground(Color.RED);
framexx.setVisible(true);
Toolkit.getDefaultToolkit().beep();
Thread.sleep(3000);
framexx.setVisible(false);
final int MAX = ni;
final JFrame frame2 = new JFrame();
final JProgressBar pb = new JProgressBar();
pb.setMinimum(0);
pb.setMaximum(MAX);
pb.setStringPainted(false);
pb.setForeground(Color.blue);
pb.setString("10%");
frame2.setUndecorated(true);
frame2.getContentPane().add(pb);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(220, 18);
//frame2.setBackground(Color.DARK_GRAY);
frame2.setVisible(true);
for (int i = 0; i <= MAX; i++) {
final int currentValue = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pb.setValue(currentValue);
}
});
java.lang.Thread.sleep(1000);
} catch (InterruptedException e) {
//JOptionPane.showMessageDialog(framex, e.getMessage());
}
}
frame2.setVisible(false);
}
}catch ( Exception e ) {
System.out.println("Exception:" + e.getMessage());
}
}while(x>2);
}
}
}
Vi prego di essere gentili Ho iniziato a programmare in Java solo da qualche giorno!!!
P.S. Dentro ho messo un po di "fuffa" per far vedere che è un programma serio!!!