Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Finished UI and tasks
Browse files Browse the repository at this point in the history
Fixed #1, #4 and #5
  • Loading branch information
skyecodes committed Apr 7, 2018
1 parent b82f3aa commit b7267c6
Show file tree
Hide file tree
Showing 34 changed files with 1,619 additions and 113 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
</properties>
<dependencies>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
Expand Down
65 changes: 49 additions & 16 deletions src/main/java/com/github/franckyi/cmpdl/CMPDL.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,79 @@
package com.github.franckyi.cmpdl;

import com.github.franckyi.cmpdl.controller.*;
import com.mashape.unirest.http.Unirest;
import com.github.franckyi.cmpdl.core.ContentControllerView;
import com.github.franckyi.cmpdl.core.ControllerView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CMPDL extends Application {

public static final String NAME = "CMPDL";
public static final String VERSION = "2.1.0-b1";
public static final String AUTHOR = "Franckyi";
public static final String TITLE = String.format("%s v%s by %s", NAME, VERSION, AUTHOR);

public static final String USER_AGENT = String.format("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0 %s/%s (%s)", NAME, VERSION, AUTHOR);

public static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();

public static Stage stage;
public static Scene scene;

public static ControllerView<MainWindowController> mainWindow;
public static ControllerView<ModpackPaneController> modpackPane;
public static ControllerView<FilePaneController> filePane;
public static ControllerView<DestinationPaneController> destinationPane;
public static ControllerView<ProgressPaneController> progressPane;
public static ContentControllerView<ModpackPaneController> modpackPane;
public static ContentControllerView<FilePaneController> filePane;
public static ContentControllerView<DestinationPaneController> destinationPane;
public static ContentControllerView<ProgressPaneController> progressPane;

public static ContentControllerView<?> currentContent;

@Override
public void start(Stage primaryStage) throws Exception {
modpackPane = new ControllerView<>("ModpackPane.fxml", EnumContent.MODPACK);
filePane = new ControllerView<>("FilePane.fxml", EnumContent.FILE);
destinationPane = new ControllerView<>("DestinationPane.fxml", EnumContent.DESTINATION);
progressPane = new ControllerView<>("ProgressPane.fxml", EnumContent.PROGRESS);
mainWindow = new ControllerView<>("MainWindow.fxml");
stage = primaryStage;
stage.setScene(new Scene(mainWindow.getView()));
modpackPane = new ContentControllerView<>("ModpackPane.fxml");
filePane = new ContentControllerView<>("FilePane.fxml");
destinationPane = new ContentControllerView<>("DestinationPane.fxml");
progressPane = new ContentControllerView<>("ProgressPane.fxml");
mainWindow = new ControllerView<>("MainWindow.fxml");
scene = new Scene(mainWindow.getView());
stage.setScene(scene);
stage.setTitle(TITLE);
stage.show();
}

public static void main(String[] args) {
Unirest.clearDefaultHeaders();
Unirest.setDefaultHeader("User-Agent", String.format("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0 %s/%s (%s)", NAME, VERSION, AUTHOR));
if (args.length == 0) {
launch(args);
launch(args);
}

@Override
public void stop() {
EXECUTOR_SERVICE.shutdown();
}

public static void openBrowser(String url) {
if (Desktop.isDesktopSupported()) {
EXECUTOR_SERVICE.execute(() -> {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (IOException | URISyntaxException e) {
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, "Can't open URL", ButtonType.OK).show());
e.printStackTrace();
}
});
} else {
// DO SOMETHING
new Alert(Alert.AlertType.ERROR, "Desktop not supported", ButtonType.OK).show();
}
}
}
60 changes: 49 additions & 11 deletions src/main/java/com/github/franckyi/cmpdl/CurseMetaAPI.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,71 @@
package com.github.franckyi.cmpdl;

import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.github.franckyi.cmpdl.model.Project;
import com.github.franckyi.cmpdl.model.ProjectFile;
import com.github.franckyi.cmpdl.model.ProjectFilesList;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class CurseMetaAPI {

private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String URL = "http://cursemeta.dries007.net/api/v2/direct";

public static JSONObject getProject(int projectId) throws UnirestException {
return get("/GetAddOn", projectId).getObject();
public static Project getProject(int projectId) {
try {
return new Project(new JSONObject(get("/GetAddOn", projectId)));
} catch (JSONException e) {
e.printStackTrace();
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project (%d)", projectId), ButtonType.OK).show());
return null;
}
}

public static JSONArray getProjectFiles(int projectId) throws UnirestException {
return get("/GetAllFilesForAddOn", projectId).getArray();
public static ProjectFilesList getProjectFiles(int projectId) {
try {
return new ProjectFilesList(new JSONArray(get("/GetAllFilesForAddOn", projectId)));
} catch (JSONException e) {
e.printStackTrace();
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project (%d)", projectId), ButtonType.OK).show());
return null;
}
}

public static JSONObject getProjectFile(int projectId, int fileId) throws UnirestException {
return get("/GetAddOnFile", projectId, fileId).getObject();
public static ProjectFile getProjectFile(int projectId, int fileId) {
try {
return new ProjectFile(new JSONObject(get("/GetAddOnFile", projectId, fileId)));
} catch (JSONException e) {
e.printStackTrace();
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project file (%d:%d)", projectId, fileId), ButtonType.OK).show());
return null;
}
}

private static JsonNode get(String path, Object... args) throws UnirestException {
private static String get(String path, Object... args) {
StringBuilder sb = new StringBuilder(URL + path);
for (Object o : args) {
sb.append("/").append(o);
}
return Unirest.get(sb.toString()).asJson().getBody();
Request request = new Request.Builder().header("User-Agent", CMPDL.USER_AGENT).url(sb.toString()).get().build();
try (Response response = CLIENT.newCall(request).execute()) {
if (response.body() != null) {
return response.body().string();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return "";
}
}

}
5 changes: 0 additions & 5 deletions src/main/java/com/github/franckyi/cmpdl/EnumContent.java

This file was deleted.

39 changes: 39 additions & 0 deletions src/main/java/com/github/franckyi/cmpdl/controller/CleanTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.franckyi.cmpdl.controller;

import com.github.franckyi.cmpdl.task.TaskBase;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class CleanTask extends TaskBase<Void> {

private final File temp;

public CleanTask(File temp) {
this.temp = temp;
}

@Override
protected Void call0() throws Throwable {
updateTitle("Cleaning");
Files.walkFileTree(temp.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,124 @@
package com.github.franckyi.cmpdl.controller;

public class DestinationPaneController {
import com.github.franckyi.cmpdl.CMPDL;
import com.github.franckyi.cmpdl.model.Project;
import com.github.franckyi.cmpdl.model.ProjectFile;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.DirectoryChooser;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class DestinationPaneController implements Initializable, IContentController {

private Project project;
private ProjectFile file;

@FXML
private ImageView logoImageView;

@FXML
private Label titleLabel;

@FXML
private Label authorLabel;

@FXML
private Label summaryLabel;

@FXML
private ImageView categoryImageView;

@FXML
private Label categoryLabel;

@FXML
private Label fileNameLabel;

@FXML
private Label mcVersionLabel;

@FXML
private Label releaseTypeLabel;

@FXML
private TextField destinationField;

@FXML
void actionChooseDestination(ActionEvent event) {
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle("Choose the destination folder :");
File dst = dc.showDialog(CMPDL.stage);
if (dst != null) {
destinationField.setText(dst.getAbsolutePath());
}
}

@FXML
void actionViewInBrowser(ActionEvent event) {
CMPDL.openBrowser(project.getUrl());
}

@Override
public void initialize(URL location, ResourceBundle resources) {
destinationField.textProperty().addListener((o, oldValue, newValue) -> CMPDL.mainWindow.getController().getStartButton().setDisable(newValue.isEmpty()));
releaseTypeLabel.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, Font.getDefault().getSize()));
}

@Override
public void handleNext() {

}

@Override
public void handlePrevious() {
CMPDL.mainWindow.getController().setContent(CMPDL.filePane);
CMPDL.mainWindow.getController().getStartButton().setDisable(true);
CMPDL.mainWindow.getController().getNextButton().setDisable(false);
}

@Override
public void handleStart() {
File dst = new File(destinationField.getText());
if (dst.isDirectory()) {
if (!dst.exists()) dst.mkdirs();
if (!dst.canWrite()) {
new Alert(Alert.AlertType.ERROR, "Permission denied. Please choose another destination folder.", ButtonType.OK).show();
} else {
CMPDL.progressPane.getController().setData(project, file, dst);
CMPDL.mainWindow.getController().setContent(CMPDL.progressPane);
CMPDL.mainWindow.getController().getStartButton().setDisable(true);
CMPDL.mainWindow.getController().getPreviousButton().setDisable(true);
CMPDL.progressPane.getController().downloadModpack();
}
} else {
new Alert(Alert.AlertType.ERROR, "The destination must be a folder.", ButtonType.OK).show();
}
}

public void setProjectAndFile(Project project, ProjectFile file) {
this.project = project;
this.file = file;
logoImageView.setImage(new Image(project.getLogoUrl()));
titleLabel.setText(project.getName());
authorLabel.setText("by " + project.getAuthor());
summaryLabel.setText(project.getSummary());
categoryImageView.setImage(new Image(project.getCategoryLogoUrl()));
categoryLabel.setText(project.getCategoryName());
fileNameLabel.setText(file.getFileName());
mcVersionLabel.setText(file.getGameVersion());
releaseTypeLabel.setText(file.getFileType());
releaseTypeLabel.setTextFill(file.getColor());
}
}
Loading

0 comments on commit b7267c6

Please sign in to comment.