Skip to content

Commit

Permalink
Adds a command that checks for ABBA updates - fix #207
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoKiaru committed Aug 12, 2024
1 parent 8e88e1a commit 19cbfad
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ch.epfl.biop.atlas.aligner.command;

import ch.epfl.biop.atlas.aligner.gui.bdv.ABBABdvStartCommand;
import org.scijava.command.Command;
import org.scijava.plugin.Plugin;
import org.scijava.util.VersionUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

// Merci chaton gpt

@Plugin(type = Command.class,
menuPath = "Plugins>BIOP>Atlas>Multi Image To Atlas>Help>ABBA - Check for updates",
description = "Check for updates")
public class ABBACheckForUpdateCommand implements Command {

private static final String REPO_API_URL = "https://api.github.com/repos/BIOP/ijp-imagetoatlas/releases/latest";

@Override
public void run() {
String currentVersion = VersionUtils.getVersion(ABBABdvStartCommand.class);

try {
String latestVersion = getLatestReleaseTag();
StringBuilder bodyBuilder = new StringBuilder();
bodyBuilder.append("<html> Current version: ").append(currentVersion).append("<br>");
bodyBuilder.append("Latest release: ").append(latestVersion).append("<br>");

if (isLatestVersion(currentVersion, latestVersion)) {
showMessage("You are running the latest version.",
bodyBuilder.toString(), JOptionPane.INFORMATION_MESSAGE);
} else {
showMessage("A newer version is available !",
bodyBuilder.toString(), JOptionPane.WARNING_MESSAGE);
}

} catch (Exception e) {
showMessage("Error occurred while checking for updates: ", e.getMessage(), JOptionPane.ERROR_MESSAGE);
}

}

public static void showMessage(String title, String message, int messageType) {
// Create a panel to hold the checkbox
JPanel panel = new JPanel();
JLabel label = new JLabel(message);
panel.add(label);

// Show the option dialog with the warning message and the checkbox
JOptionPane.showConfirmDialog(null, panel, title, JOptionPane.DEFAULT_OPTION, messageType);
}

private static String getLatestReleaseTag() throws Exception {
URL url = new URL(REPO_API_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/vnd.github.v3+json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
StringBuilder response = new StringBuilder();

while ((output = br.readLine()) != null) {
response.append(output);
}

conn.disconnect();

// Parse the response JSON to get the tag name
JSONObject jsonResponse = new JSONObject(response.toString());
return jsonResponse.getString("tag_name");
}

private static boolean isLatestVersion(String currentVersion, String latestVersion) {
String[] currentParts = currentVersion.split("\\.");
String[] latestParts = latestVersion.split("\\.");

for (int i = 0; i < Math.min(currentParts.length, latestParts.length); i++) {
int currentPart = Integer.parseInt(currentParts[i]);
int latestPart = Integer.parseInt(latestParts[i]);

if (currentPart < latestPart) {
return false;
} else if (currentPart > latestPart) {
return true;
}
}

return currentParts.length >= latestParts.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.epfl.biop.atlas.aligner.ReslicedAtlas;
import ch.epfl.biop.atlas.aligner.SliceSources;
import ch.epfl.biop.atlas.aligner.adapter.AlignerState;
import ch.epfl.biop.atlas.aligner.command.ABBACheckForUpdateCommand;
import ch.epfl.biop.atlas.aligner.command.ABBADocumentationCommand;
import ch.epfl.biop.atlas.aligner.command.ABBAForumHelpCommand;
import ch.epfl.biop.atlas.aligner.command.ABBAStateLoadCommand;
Expand Down Expand Up @@ -403,10 +404,6 @@ private void installBdvMenu(int hierarchyLevelsSkipped) {
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), SetSlicesThicknessCommand.class, hierarchyLevelsSkipped,"mp", msp);
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), SetSlicesThicknessMatchNeighborsCommand.class, hierarchyLevelsSkipped,"mp", msp);

// Help commands
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBAForumHelpCommand.class, hierarchyLevelsSkipped);
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBADocumentationCommand.class, hierarchyLevelsSkipped);
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBAUserFeedbackCommand.class, hierarchyLevelsSkipped);

if (DeepSliceHelper.isDeepSliceMouseCompatible(msp.getReslicedAtlas().ba.getName())) {

Expand Down Expand Up @@ -445,6 +442,16 @@ private void installBdvMenu(int hierarchyLevelsSkipped) {
msp.subscribeToInfoMessages(infoLogger);
addToCleanUpHook(() -> msp.unSubscribeFromInfoMessages(infoLogger));



// Help commands
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBAForumHelpCommand.class, hierarchyLevelsSkipped);
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBADocumentationCommand.class, hierarchyLevelsSkipped);
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBAUserFeedbackCommand.class, hierarchyLevelsSkipped);

// Update check
BdvScijavaHelper.addCommandToBdvHandleMenu(bdvh, msp.getContext(), ABBACheckForUpdateCommand.class, hierarchyLevelsSkipped);

}

private void installRegistrationPluginUI(int hierarchyLevelsSkipped) {
Expand Down

0 comments on commit 19cbfad

Please sign in to comment.