Skip to content

Commit

Permalink
Improved handling of connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
vogti committed Aug 11, 2015
1 parent 880980f commit 1c5a34f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
7 changes: 6 additions & 1 deletion AVR-Remote/src/de/marcovogt/avrremote/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
Expand All @@ -20,7 +21,11 @@ public class Controller {

public static boolean isConnected() {
try {
new URL("http://" + Config.getAVR() + "/goform/formMainZone_MainZoneXmlStatus.xml").openStream();
URL url = new URL("http://" + Config.getAVR() + "/goform/formMainZone_MainZoneXmlStatus.xml");
URLConnection con = url.openConnection();
con.setConnectTimeout(2000);
con.setReadTimeout(2000);
con.getInputStream();
return true;
} catch(Exception e) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion AVR-Remote/src/de/marcovogt/avrremote/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Info(MainWindow window) {
add(lblIcon);

txtpnVersion = new JTextPane();
txtpnVersion.setText("Version: \r\n1.4\r\n\r\nDate: 02.08.2015");
txtpnVersion.setText("Version: \r\n1.5\r\n\r\nDate: 11.08.2015");
txtpnVersion.setFont(new Font("Tahoma", Font.PLAIN, 14));
txtpnVersion.setEditable(false);
txtpnVersion.setBackground(UIManager.getColor("ScrollBar.foreground"));
Expand Down
15 changes: 11 additions & 4 deletions AVR-Remote/src/de/marcovogt/avrremote/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Main(MainWindow window) {
heading.setFont(new Font("Tahoma", Font.PLAIN, 30));
add(heading);

lblVersion = new JLabel("AVR Remote v1.4");
lblVersion = new JLabel("AVR Remote v1.5");
lblVersion.setBounds(45, 346, 130, 25);
lblVersion.setHorizontalAlignment(SwingConstants.CENTER);
add(lblVersion);
Expand Down Expand Up @@ -177,7 +177,6 @@ public void run() {
lblVolume.setText(sliderVolume.getValue() + "");
} else {
connected = false;
window.showNoConnection();
}
}
updateConnected();
Expand All @@ -197,8 +196,16 @@ public void updateSliderMaximumVolume() {
public void updateConnected() {
boolean oldState = connected;
connected = Controller.isConnected();
if(!oldState && connected) {
window.showMain();
if(oldState == connected) {
if(connected) {
if(window.getCurrent() == "noConnection") {
window.showMain();
}
} else {
if(window.getCurrent() != "settings") {
window.showNoConnection();
}
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions AVR-Remote/src/de/marcovogt/avrremote/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class MainWindow {
private Info info;
private Settings settings;
private NoConnection noConnection;

private String current;

/**
* Launch the application.
Expand Down Expand Up @@ -71,12 +73,12 @@ private void initialize() {
frame.getContentPane().add(main, "main");

if(Config.getAVR().equals("")) {
cl.show(frame.getContentPane(), "settings");
showSettings();
} else {
if(Controller.isConnected()) {
cl.show(frame.getContentPane(), "main");
showMain();
} else {
cl.show(frame.getContentPane(), "noConnection");
showNoConnection();
}
}
}
Expand All @@ -85,20 +87,28 @@ public void showMain() {
main.updateFavoriteSourceLabel();
main.updateSliderMaximumVolume();
cl.show(frame.getContentPane(), "main");
current = "main";
}

public void showInfo() {
cl.show(frame.getContentPane(), "info");
current = "info";
}

public void showSettings() {
cl.show(frame.getContentPane(), "settings");
current = "settings";
settings.update();
}

public void showNoConnection() {
cl.show(frame.getContentPane(), "noConnection");
current = "noConnection";
noConnection.update();
}

public String getCurrent() {
return current;
}

}
6 changes: 5 additions & 1 deletion AVR-Remote/src/de/marcovogt/avrremote/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public void actionPerformed(ActionEvent e) {
btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window.showMain();
if(Controller.isConnected()) {
window.showMain();
} else {
window.showNoConnection();
}
}
});
btnCancel.setBounds(10, 337, 98, 37);
Expand Down

0 comments on commit 1c5a34f

Please sign in to comment.