Skip to content

Commit

Permalink
Delete redundant code, use Path API more, add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
BullyWiiPlaza committed Sep 16, 2022
1 parent e7dfc63 commit d5d7506
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 178 deletions.
Binary file modified App Sender.jar
Binary file not shown.
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
21 changes: 11 additions & 10 deletions src/main/java/AppSenderClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import com.wiiudev.homebrew.graphical_interface.utilities.IPAddressValidator;
import com.wiiudev.homebrew.graphical_interface.AppSenderGUI;
import com.wiiudev.homebrew.sending.AppTransmission;
import lombok.val;

import javax.swing.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.wiiudev.homebrew.graphical_interface.utilities.IPAddressValidator.validateIPv4Address;

public class AppSenderClient
{
public static void main(String[] consoleArguments) throws Exception
Expand All @@ -26,25 +27,25 @@ private static void startGUI() throws Exception

SwingUtilities.invokeLater(() ->
{
AppSenderGUI appSenderGUI = AppSenderGUI.getInstance();
val appSenderGUI = AppSenderGUI.getInstance();
appSenderGUI.setVisible(true);
});
}

private static void useCommandLine(String[] consoleArguments) throws Exception
private static void useCommandLine(final String[] consoleArguments) throws Exception
{
String ipAddress = consoleArguments[1];
boolean isValidIPAddress = IPAddressValidator.validateIPv4Address(ipAddress);
val ipAddress = consoleArguments[1];
val isValidIPAddress = validateIPv4Address(ipAddress);

if (!isValidIPAddress)
{
System.err.println("Invalid IP address: " + ipAddress);
System.exit(1);
}

String sourceFile = consoleArguments[0];
Path sourceFilePath = Paths.get(sourceFile);
boolean isValidSourceFile = Files.isRegularFile(sourceFilePath);
val sourceFile = consoleArguments[0];
val sourceFilePath = Paths.get(sourceFile);
val isValidSourceFile = Files.isRegularFile(sourceFilePath);

if (!isValidSourceFile)
{
Expand All @@ -54,4 +55,4 @@ private static void useCommandLine(String[] consoleArguments) throws Exception

AppTransmission.send(sourceFile, ipAddress);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wiiudev.homebrew.graphical_interface;

import com.wiiudev.homebrew.graphical_interface.utilities.FileWatcher;
import lombok.val;

import javax.swing.*;

Expand All @@ -16,8 +17,8 @@ public void onModified()
{
SwingUtilities.invokeLater(() ->
{
AppSenderGUI appSenderGUI = AppSenderGUI.getInstance();
val appSenderGUI = AppSenderGUI.getInstance();
appSenderGUI.sendApp(false);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import com.wiiudev.homebrew.graphical_interface.utilities.PersistentSettings;
import com.wiiudev.homebrew.graphical_interface.utilities.SingleFileChooser;
import com.wiiudev.homebrew.sending.AppTransmission;
import lombok.val;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AppSenderGUI extends JFrame
Expand Down Expand Up @@ -51,7 +51,8 @@ private AppSenderGUI()

private void addSettingsBackupShutdownHook()
{
Runtime.getRuntime().addShutdownHook(new Thread(() ->
val runtime = Runtime.getRuntime();
runtime.addShutdownHook(new Thread(() ->
{
persistentSettings.put("IP_ADDRESS", ipAddressField.getText());
persistentSettings.put("APP_FILE_PATH", appFilePathField.getText());
Expand All @@ -64,19 +65,19 @@ private void restorePersistentSettings()
{
persistentSettings = new PersistentSettings();

String ipAddress = persistentSettings.get("IP_ADDRESS");
val ipAddress = persistentSettings.get("IP_ADDRESS");
if (ipAddress != null)
{
ipAddressField.setText(ipAddress);
}

String appFilePath = persistentSettings.get("APP_FILE_PATH");
val appFilePath = persistentSettings.get("APP_FILE_PATH");
if (appFilePath != null)
{
appFilePathField.setText(appFilePath);
}

String automaticallySend = persistentSettings.get("AUTOMATICALLY_SEND");
val automaticallySend = persistentSettings.get("AUTOMATICALLY_SEND");
if (automaticallySend != null)
{
automaticallySendCheckBox.setSelected(Boolean.parseBoolean(automaticallySend));
Expand All @@ -85,7 +86,8 @@ private void restorePersistentSettings()

private void addAppFileDocumentListener()
{
appFilePathField.getDocument().addDocumentListener(new DocumentListener()
val document = appFilePathField.getDocument();
document.addDocumentListener(new DocumentListener()
{
@Override
public void insertUpdate(DocumentEvent documentEvent)
Expand Down Expand Up @@ -124,10 +126,10 @@ private void startAppFileWatcher()
{
stopAppFileWatcher();

String appFilePath = appFilePathField.getText();
val appFilePath = appFilePathField.getText();
fileWatcher = new AppSenderFileWatcher(appFilePath);
fileWatcher.startWatching();
} catch (Exception exception)
} catch (final Exception exception)
{
exception.printStackTrace();
}
Expand All @@ -143,7 +145,7 @@ private void stopAppFileWatcher()

private void startAppFilePathValidationAsynchronously()
{
Thread appFilePathValidator = new Thread(() ->
val appFilePathValidator = new Thread(() ->
{
while (true)
{
Expand Down Expand Up @@ -178,11 +180,9 @@ private void addInformationButtonListener()
{
informationButton.addActionListener(actionEvent ->
JOptionPane.showMessageDialog(rootPane,
"This application let's you send apps from your computer to your Wii or Wii U and automatically start them!" +
"\nYou need to be in the Homebrew Channel or Homebrew Launcher respectively for this to work.",
informationButton.getText(),
JOptionPane.INFORMATION_MESSAGE,
null));
"This application let's you send apps from your computer to your Wii or Wii U and automatically start them.\n" +
"You need to be in the Homebrew Channel or Homebrew Launcher respectively for this to work.",
informationButton.getText(), JOptionPane.INFORMATION_MESSAGE, null));
}

private void setFrameProperties()
Expand All @@ -200,8 +200,8 @@ void sendApp(boolean displayErrorMessage)
sendAppButton.setText("Sending...");
sendingApp = true;
setSendAppButtonAvailability();
String ipAddress = ipAddressField.getText();
String appFilePath = appFilePathField.getText();
val ipAddress = ipAddressField.getText();
val appFilePath = appFilePathField.getText();

new SwingWorker<String, String>()
{
Expand All @@ -211,14 +211,15 @@ protected String doInBackground()
try
{
AppTransmission.send(appFilePath, ipAddress);
} catch (Exception exception)
} catch (final Exception exception)
{
exception.printStackTrace();

if (displayErrorMessage)
{
JOptionPane.showMessageDialog(AppSenderGUI.this,
"Make sure you're in the Homebrew Channel or Homebrew Launcher when sending\nand that you can connect to the Wii U via the local network.",
"Make sure you're in the Homebrew Channel or Homebrew Launcher when sending\n" +
"and that you can connect to the Wii U via the local network.",
"Connection Failed",
JOptionPane.ERROR_MESSAGE);
}
Expand All @@ -239,7 +240,7 @@ protected void done()

private void setSendAppButtonAvailability()
{
boolean canSend = isAppFilePathValid()
val canSend = isAppFilePathValid()
&& isEnteredIPAddressValid()
&& !sendingApp;
sendAppButton.setEnabled(canSend);
Expand All @@ -250,29 +251,30 @@ private void addBrowseAppFileButtonListener()
{
browseAppFileButton.addActionListener(actionEvent ->
{
SingleFileChooser singleFileChooser = new SingleFileChooser(appFilePathField);
val singleFileChooser = new SingleFileChooser(appFilePathField);
singleFileChooser.allowFileSelection(getRootPane());
});
}

private void validateAppFilePath()
{
boolean exists = isAppFilePathValid();
val exists = isAppFilePathValid();
appFilePathField.setBackground(exists ? Color.GREEN : Color.RED);
setSendAppButtonAvailability();
}

private boolean isAppFilePathValid()
{
String appFilePath = appFilePathField.getText();
Path filePath = Paths.get(appFilePath);
val appFilePath = appFilePathField.getText();
val filePath = Paths.get(appFilePath);

return Files.isRegularFile(filePath);
}

private void addIPAddressValidation()
{
ipAddressField.getDocument().addDocumentListener(new DocumentListener()
val document = ipAddressField.getDocument();
document.addDocumentListener(new DocumentListener()
{
@Override
public void insertUpdate(DocumentEvent documentEvent)
Expand All @@ -298,14 +300,14 @@ public void changedUpdate(DocumentEvent documentEvent)

private void validateIPAddressField()
{
boolean isValid = isEnteredIPAddressValid();
val isValid = isEnteredIPAddressValid();
ipAddressField.setBackground(isValid ? Color.GREEN : Color.RED);
setSendAppButtonAvailability();
}

private boolean isEnteredIPAddressValid()
{
String ipAddress = ipAddressField.getText();
val ipAddress = ipAddressField.getText();
return IPAddressValidator.validateIPv4Address(ipAddress);
}

Expand All @@ -318,4 +320,4 @@ public static AppSenderGUI getInstance()

return instance;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.wiiudev.homebrew.graphical_interface.utilities;

import lombok.val;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.WatchEvent.Kind;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

Expand All @@ -15,9 +16,8 @@ public abstract class FileWatcher

public FileWatcher(String watchFile)
{
Path filePath = Paths.get(watchFile);

boolean isRegularFile = Files.isRegularFile(filePath);
val filePath = Paths.get(watchFile);
val isRegularFile = Files.isRegularFile(filePath);

if (!isRegularFile)
{
Expand All @@ -34,12 +34,12 @@ public FileWatcher(String watchFile)

public void startWatching()
{
Thread watcherThread = new Thread(() ->
val watcherThread = new Thread(() ->
{
try
{
// We obtain the file system of the Path
FileSystem fileSystem = folderPath.getFileSystem();
val fileSystem = folderPath.getFileSystem();
service = fileSystem.newWatchService();

// We watch for modification events
Expand All @@ -49,16 +49,16 @@ public void startWatching()
while (true)
{
// Wait for the next event
WatchKey watchKey = service.take();
val watchKey = service.take();

for (WatchEvent<?> watchEvent : watchKey.pollEvents())
for (val watchEvent : watchKey.pollEvents())
{
// Get the type of the event
Kind<?> kind = watchEvent.kind();
val kind = watchEvent.kind();

if (kind == ENTRY_MODIFY)
{
Path watchEventPath = (Path) watchEvent.context();
val watchEventPath = (Path) watchEvent.context();

// Call this if the right file is involved
if (watchEventPath.toString().equals(watchFile))
Expand All @@ -74,10 +74,10 @@ public void startWatching()
break;
}
}
} catch (ClosedWatchServiceException ignored)
} catch (final ClosedWatchServiceException ignored)
{
// This happens when the watch service has been closed so ignore
} catch (Exception exception)
} catch (final Exception exception)
{
exception.printStackTrace();
}
Expand All @@ -98,4 +98,4 @@ public void stopWatching()
}

public abstract void onModified();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.wiiudev.homebrew.graphical_interface.utilities;

import lombok.val;

import java.util.regex.Pattern;

public class IPAddressValidator
{
public static boolean validateIPv4Address(String ipAddress)
public static boolean validateIPv4Address(final String ipAddress)
{
Pattern pattern = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
val pattern = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
return pattern.matcher(ipAddress).matches();
}
}
}
Loading

0 comments on commit d5d7506

Please sign in to comment.