Skip to content

Commit

Permalink
feat: option to enable or disable of archiving downloads (keeping ava…
Browse files Browse the repository at this point in the history
…ilable)
  • Loading branch information
jagodevreede committed Oct 13, 2024
1 parent 1d2f1a5 commit 0c0ef17
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SdkManUiPreferences {
public boolean canCreateSymlink;
public boolean showInstalled;
public boolean showAvailable;
public boolean keepDownloadsAvailable;

private static SdkManUiPreferences load() throws IOException {
PROPERTY_LOCATION.getParentFile().mkdirs();
Expand All @@ -34,6 +35,7 @@ private static SdkManUiPreferences load() throws IOException {
uiPreferences.canCreateSymlink = Boolean.parseBoolean(properties.getProperty("canCreateSymlink", "true"));
uiPreferences.showInstalled = Boolean.parseBoolean(properties.getProperty("showInstalled", "false"));
uiPreferences.showAvailable = Boolean.parseBoolean(properties.getProperty("showAvailable", "false"));
uiPreferences.keepDownloadsAvailable = Boolean.parseBoolean(properties.getProperty("keepDownloadsAvailable", "true"));
return uiPreferences;
}

Expand Down Expand Up @@ -66,6 +68,7 @@ public void save() throws IOException {
properties.setProperty("canCreateSymlink", String.valueOf(canCreateSymlink));
properties.setProperty("showInstalled", String.valueOf(showInstalled));
properties.setProperty("showAvailable", String.valueOf(showAvailable));
properties.setProperty("keepDownloadsAvailable", String.valueOf(keepDownloadsAvailable));
properties.store(new FileOutputStream(PROPERTY_LOCATION), null);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package io.github.jagodevreede.sdkman.api.files;

import io.github.jagodevreede.sdkman.api.SdkManApi;
import io.github.jagodevreede.sdkman.api.SdkManUiPreferences;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import io.github.jagodevreede.sdkman.api.SdkManApi;
import io.github.jagodevreede.sdkman.api.SdkManUiPreferences;

public final class ZipExtractTask {

private ZipExtractTask() {
// no instantiation
}

public static void extract(File zipFile, File destination) {
final SdkManUiPreferences sdkManUiPreferences = SdkManUiPreferences.getInstance();
try {
String unzipExecutable = SdkManUiPreferences.getInstance().unzipExecutable;
String unzipExecutable = sdkManUiPreferences.unzipExecutable;
File tempDir = new File(SdkManApi.DEFAULT_SDKMAN_HOME, "tmp/out");
FileUtil.deleteRecursively(tempDir);
FileUtil.deleteRecursively(destination);
Expand All @@ -30,6 +31,10 @@ public static void extract(File zipFile, File destination) {
Files.move(sourceExtractedFolder.toPath(), destination.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (sdkManUiPreferences.keepDownloadsAvailable) {
zipFile.delete();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
Expand Down Expand Up @@ -38,9 +40,13 @@ public class ConfigScreenController implements Initializable {

@FXML
Button closeConfigButton;
@FXML
CheckBox checkBoxKeepDownloadsAvailable;

@Override
public void initialize(final URL url, final ResourceBundle resourceBundle) {
// Don't use the sdkManUiPreferences here, as we then change the properties directly event without saving,
// this way we are disconnected from the sdkManUiPreferences.
Properties properties = new Properties();
try {
properties.load(new FileInputStream(PROPERTY_LOCATION));
Expand All @@ -52,6 +58,7 @@ public void initialize(final URL url, final ResourceBundle resourceBundle) {
final String unzipExecutablePropertyPath = properties.getProperty("unzipExecutable");
final String tarExecutablePropertyPath = properties.getProperty("tarExecutable");
final boolean canCreateSymlink = Boolean.parseBoolean(properties.getProperty("canCreateSymlink"));
final boolean keepDownloadsAvailable = Boolean.parseBoolean(properties.getProperty("keepDownloadsAvailable", "true"));

zipExecutablePath.setText(zipExecutablePropertyPath);
unzipExecutablePath.setText(unzipExecutablePropertyPath);
Expand All @@ -62,6 +69,8 @@ public void initialize(final URL url, final ResourceBundle resourceBundle) {
} else {
symlinkCapability.setText(SYMLINK_NOT_CAPABLE);
}
checkBoxKeepDownloadsAvailable.setSelected(keepDownloadsAvailable);
checkBoxKeepDownloadsAvailable.setTooltip(new Tooltip("Keep downloads available in SDKMAN_HOME/archives when they are extracted,\nthis will require more diskpace."));
}

public void browseZipExecutablePath() {
Expand Down Expand Up @@ -102,6 +111,7 @@ public void saveAndCloseConfigWindow() {
sdkManUiPreferences.zipExecutable = zipExecutablePath.getText();
sdkManUiPreferences.unzipExecutable = unzipExecutablePath.getText();
sdkManUiPreferences.tarExecutable = tarExecutablePath.getText();
sdkManUiPreferences.keepDownloadsAvailable = checkBoxKeepDownloadsAvailable.isSelected();

try {
sdkManUiPreferences.save();
Expand All @@ -126,15 +136,17 @@ private String browsePath(String command, Stage stage) {
fileChooser.setInitialDirectory(new File("/usr/bin"));
}
fileChooser.setTitle("Where is the " + command + " executable");
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(command, command + (isWindows() ? ".exe" : "")));
fileChooser.getExtensionFilters()
.add(new FileChooser.ExtensionFilter(command, command + (isWindows() ? ".exe" : "")));
fileChooser.setInitialFileName(command);
File file = fileChooser.showOpenDialog(stage);
if (file == null) {
return null;
}
if (!ProcessStarter.testIfAvailable(file.getAbsolutePath())) {
String name = file != null ? file.getAbsolutePath() : command;
ServiceRegistry.INSTANCE.getPopupView().showInformation("Failed to verify " + name, Alert.AlertType.INFORMATION);
ServiceRegistry.INSTANCE.getPopupView()
.showInformation("Failed to verify " + name, Alert.AlertType.INFORMATION);
return null;
}
return file.getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class MainScreenController implements Initializable {
private static final Logger logger = LoggerFactory.getLogger(MainScreenController.class);
private final SdkManApi api = ServiceRegistry.INSTANCE.getApi();
private final PopupView popupView = ServiceRegistry.INSTANCE.getPopupView();
private final SdkManUiPreferences sdkManUiPreferences = ServiceRegistry.INSTANCE.getSdkManUiPreferences();
@FXML
TableView<VersionView> table;
@FXML
Expand Down Expand Up @@ -95,7 +96,6 @@ public String getSelectedCandidate() {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ServiceRegistry.INSTANCE.setProgressIndicator(progressSpinner);
final SdkManUiPreferences sdkManUiPreferences = ServiceRegistry.INSTANCE.getSdkManUiPreferences();
showInstalledOnly.setSelected(sdkManUiPreferences.showInstalled);
showAvailableOnly.setSelected(sdkManUiPreferences.showAvailable);
table.setPlaceholder(new Label("No versions found"));
Expand Down Expand Up @@ -141,6 +141,7 @@ public void loadData(Runnable onLoaded) {
}
}).toList();
Platform.runLater(() -> {
showAvailableOnly.setVisible(sdkManUiPreferences.keepDownloadsAvailable);
if (tableData == null || tableData.size() != updatedVersions.size()) {
tableData = FXCollections.observableArrayList(updatedVersions.stream().map(j -> new VersionView(j, globalVersionInUse, pathVersionInUse, thiz)).toList());
table.setItems(tableData);
Expand Down Expand Up @@ -180,10 +181,15 @@ private void createColumns() {

table.getColumns().clear();
if ("java".equals(selectedCandidate)) {
table.getColumns().addAll(vendorCol, versionCol, identifierCol, installedCol, availableCol, actionCol);

table.getColumns().addAll(vendorCol, versionCol, identifierCol, installedCol);
} else {
table.getColumns().addAll(versionCol, installedCol, availableCol, actionCol);
table.getColumns().addAll(versionCol, installedCol);
}
if (sdkManUiPreferences.keepDownloadsAvailable) {
table.getColumns().add(availableCol);
}
table.getColumns().add(actionCol);
}

private void setPathVersionLabel(String pathVersionInUse) {
Expand Down
32 changes: 17 additions & 15 deletions sdkman-ui/src/main/resources/config.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.github.jagodevreede.sdkmanui.controller.ConfigScreenController">
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.github.jagodevreede.sdkmanui.controller.ConfigScreenController">
<children>
<Text layoutX="20.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Select zip path:" />
<Text layoutX="20.0" layoutY="77.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Select unzip path:" />
<Text layoutX="20.0" layoutY="117.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Select tar path:" />
<TextField fx:id="zipExecutablePath" layoutX="124.0" layoutY="20.0" prefHeight="25.0" prefWidth="380.0" />
<TextField fx:id="unzipExecutablePath" layoutX="124.0" layoutY="60.0" prefHeight="25.0" prefWidth="380.0" />
<TextField fx:id="tarExecutablePath" layoutX="124.0" layoutY="100.0" prefHeight="25.0" prefWidth="380.0" />
<Button layoutX="518.0" layoutY="20.0" mnemonicParsing="false" onMouseClicked="#browseZipExecutablePath" text="Browse..." />
<Button layoutX="518.0" layoutY="60.0" mnemonicParsing="false" onMouseClicked="#browseUnzipExecutablePath" text="Browse..." />
<Button layoutX="518.0" layoutY="100.0" mnemonicParsing="false" onMouseClicked="#browseTarExecutablePath" text="Browse..." />
<ButtonBar layoutX="357.0" layoutY="340.0" prefHeight="40.0" prefWidth="223.0">
<Text layoutX="10.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Select zip path:" />
<Text layoutX="10.0" layoutY="77.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Select unzip path:" />
<Text layoutX="10.0" layoutY="117.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Select tar path:" />
<TextField fx:id="zipExecutablePath" layoutX="114.0" layoutY="20.0" prefHeight="25.0" prefWidth="400.0" />
<TextField fx:id="unzipExecutablePath" layoutX="114.0" layoutY="60.0" prefHeight="25.0" prefWidth="400.0" />
<TextField fx:id="tarExecutablePath" layoutX="114.0" layoutY="100.0" prefHeight="25.0" prefWidth="400.0" />
<Button layoutX="520.0" layoutY="20.0" mnemonicParsing="false" onMouseClicked="#browseZipExecutablePath" prefWidth="70.0" text="Browse..." />
<Button layoutX="520.0" layoutY="60.0" mnemonicParsing="false" onMouseClicked="#browseUnzipExecutablePath" prefWidth="70.0" text="Browse..." />
<Button layoutX="520.0" layoutY="100.0" mnemonicParsing="false" onMouseClicked="#browseTarExecutablePath" prefWidth="70.0" text="Browse..." />
<ButtonBar layoutX="320.0" layoutY="352.0" prefHeight="40.0" prefWidth="270.0">
<buttons>
<Button mnemonicParsing="false" onMouseClicked="#saveAndCloseConfigWindow" prefHeight="25.0" prefWidth="149.0" text="Save and Close" />
<Button mnemonicParsing="false" onMouseClicked="#saveAndCloseConfigWindow" prefHeight="26.0" prefWidth="160.0" text="Save and Close" />
<Button fx:id="closeConfigButton" mnemonicParsing="false" onMouseClicked="#closeConfigWindow" text="Cancel" />
</buttons>
</ButtonBar>
<Text layoutX="20.0" layoutY="157.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Symlink capability:" />
<Button layoutX="518.0" layoutY="140.0" mnemonicParsing="false" onMouseClicked="#checkSymlinkCapability" prefHeight="25.0" prefWidth="62.0" text="Recheck" />
<Text fx:id="symlinkCapability" layoutX="124.0" layoutY="157.0" strokeType="OUTSIDE" strokeWidth="0.0" text="n/a" />
<Text layoutX="10.0" layoutY="157.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Symlink capability:" />
<Button layoutX="520.0" layoutY="140.0" mnemonicParsing="false" onMouseClicked="#checkSymlinkCapability" prefHeight="26.0" prefWidth="70.0" text="Recheck" />
<Text fx:id="symlinkCapability" layoutX="117.0" layoutY="157.0" strokeType="OUTSIDE" strokeWidth="0.0" text="n/a" />
<CheckBox fx:id="checkBoxKeepDownloadsAvailable" layoutX="11.0" layoutY="175.0" mnemonicParsing="false" text="Keep downloads available" />
</children>
</AnchorPane>

0 comments on commit 0c0ef17

Please sign in to comment.