Skip to content

Commit 3c6b82a

Browse files
committed
Add delete method and ProgressBar for the GUI
1 parent 3c2cac0 commit 3c6b82a

4 files changed

Lines changed: 126 additions & 87 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tasks.jar {
3333

3434

3535
group = "fr.jachou"
36-
version = "0.1.2"
36+
version = "0.1.3"
3737

3838
repositories {
3939
mavenCentral()

src/main/java/fr/jachou/jvm/JavaVersionDownloader.java

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.swing.*;
99
import java.io.*;
1010
import java.net.URL;
11+
import java.net.URLConnection;
1112
import java.nio.file.Files;
1213
import java.nio.file.Path;
1314
import java.nio.file.Paths;
@@ -106,7 +107,7 @@ public static void main(String[] args) {
106107
* @param version The version of java to unzip
107108
*/
108109

109-
private static void downloader(int version) {
110+
public static void downloader(int version) {
110111
try {
111112
String versionString = "Java_" + version;
112113
JavaVersionList.valueOf(versionString);
@@ -117,38 +118,38 @@ private static void downloader(int version) {
117118

118119
Logger.logPass("The version " + version + " exists!");
119120

120-
System.out.print(Colors.BOLD + "[~] Checking if the link is good...");
121+
Logger.logWarning( "[~] Checking if the link is good...");
121122
String url = String.format("https://chiss.fr/jvm/download/Java_%d.zip", version);
122123
try {
123124
URL link = new URL(url);
124125
link.openStream().close();
125-
System.out.println(Colors.OKGREEN + "[✓] The link is good!");
126+
Logger.logPass("[✓] The link is good!");
126127
Thread.sleep(1000);
127128
String path = System.getProperty("user.home") + "/.jdks";
128129
System.out.println(Colors.OKCYAN + "The JDK " + version + " will be downloaded in the folder '" + path + "'");
129130
File directory = new File(path);
130131
if (!directory.exists()) {
131132
if (directory.mkdirs()) {
132-
System.out.println(Colors.OKGREEN + "[✓] The folder has been created!");
133+
Logger.logPass("[✓] The folder has been created!");
133134
}
134135
} else {
135136
System.out.println(Colors.WARNING + "[~] The folder already exists");
136137
}
137138
Thread.sleep(1000);
138-
System.out.print(Colors.BOLD + "[~] Downloading...");
139+
Logger.logWarning( "[~] Downloading...");
139140
Path destination = Paths.get(path + "/Java_" + version + ".zip");
140141
Files.copy(link.openStream(), destination, StandardCopyOption.REPLACE_EXISTING);
141-
System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded!");
142+
Logger.logPass("[✓] The JDK " + version + " has been downloaded!");
142143
Thread.sleep(1000);
143-
System.out.print(Colors.BOLD + "[~] Unzipping...");
144+
Logger.logWarning( "[~] Unzipping...");
144145
unzip(destination.toString(), path + "/Java_" + version);
145-
System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been unzipped!");
146+
Logger.logPass("[✓] The JDK " + version + " has been unzipped!");
146147
Thread.sleep(1000);
147-
System.out.print(Colors.BOLD + "[~] Deleting zip file...");
148+
Logger.logWarning( "[~] Deleting zip file...");
148149
Files.delete(destination);
149-
System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been deleted!");
150+
Logger.logPass("[✓] The JDK " + version + " has been deleted!");
150151
Thread.sleep(1000);
151-
System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded and installed!");
152+
Logger.logPass("[✓] The JDK " + version + " has been downloaded and installed!");
152153
Thread.sleep(1000);
153154
} catch (IOException e) {
154155
System.out.println(Colors.FAIL + "[X] The link does not exist!");
@@ -158,6 +159,76 @@ private static void downloader(int version) {
158159
}
159160
}
160161

162+
/**
163+
* Unzip a file
164+
* @param version The version of java to unzip
165+
* @param progressBar The progress bar
166+
*/
167+
168+
public static void downloaderWithProgressBar(int version, JProgressBar progressBar) {
169+
progressBar.setMaximum(100);
170+
try {
171+
String versionString = "Java_" + version;
172+
JavaVersionList.valueOf(versionString);
173+
} catch (IllegalArgumentException e) {
174+
System.out.println(Colors.FAIL + "[X] The version " + version + " does not exist!");
175+
return;
176+
}
177+
178+
Logger.logPass("The version " + version + " exists!");
179+
180+
progressBar.setValue(0);
181+
182+
Logger.logWarning( "[~] Checking if the link is good...");
183+
String url = String.format("https://chiss.fr/jvm/download/Java_%d.zip", version);
184+
try {
185+
URL link = new URL(url);
186+
link.openStream().close();
187+
Logger.logPass("[✓] The link is good!");
188+
progressBar.setValue(10);
189+
Thread.sleep(1000);
190+
String path = System.getProperty("user.home") + "/.jdks";
191+
System.out.println(Colors.OKCYAN + "The JDK " + version + " will be downloaded in the folder '" + path + "'");
192+
File directory = new File(path);
193+
if (!directory.exists()) {
194+
if (directory.mkdirs()) {
195+
Logger.logPass("[✓] The folder has been created!");
196+
}
197+
} else {
198+
System.out.println(Colors.WARNING + "[~] The folder already exists");
199+
}
200+
progressBar.setValue(20);
201+
Thread.sleep(1000);
202+
Logger.logWarning( "[~] Downloading...");
203+
Path destination = Paths.get(path + "/Java_" + version + ".zip");
204+
Files.copy(link.openStream(), destination, StandardCopyOption.REPLACE_EXISTING);
205+
progressBar.setValue(30);
206+
Logger.logPass("[✓] The JDK " + version + " has been downloaded!");
207+
progressBar.setValue(50);
208+
Thread.sleep(1000);
209+
Logger.logWarning( "[~] Unzipping...");
210+
progressBar.setValue(60);
211+
unzip(destination.toString(), path + "/Java_" + version);
212+
Logger.logPass("[✓] The JDK " + version + " has been unzipped!");
213+
progressBar.setValue(80);
214+
Thread.sleep(1000);
215+
Logger.logWarning( "[~] Deleting zip file...");
216+
progressBar.setValue(90);
217+
Files.delete(destination);
218+
Logger.logPass("[✓] The JDK " + version + " has been deleted!");
219+
Thread.sleep(1000);
220+
Logger.logPass("[✓] The JDK " + version + " has been downloaded and installed!");
221+
Thread.sleep(1000);
222+
progressBar.setValue(100);
223+
} catch (IOException e) {
224+
System.out.println(Colors.FAIL + "[X] The link does not exist!");
225+
e.printStackTrace();
226+
} catch (InterruptedException e) {
227+
e.printStackTrace();
228+
}
229+
}
230+
231+
161232
/**
162233
* Unzip it
163234
* @param zipFilePath input zip file

src/main/java/fr/jachou/jvm/managers/JavaManager.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,41 @@ public void executeJarWithSpecificJavaVersion(String jarPathFile, String javaVer
101101
}
102102
}
103103

104+
/**
105+
* Delete Java
106+
*/
107+
108+
public void deleteJava(Path java) {
109+
File file = new File(String.valueOf(java));
110+
if (!file.exists()) {
111+
Logger.logWarning("The Java folder does not exist");
112+
return;
113+
}
114+
if (!file.isDirectory()) {
115+
Logger.logWarning("The Java folder is not a directory");
116+
return;
117+
}
118+
Logger.log("Deleting Java folder...");
119+
file.delete();
120+
Logger.logPass("The Java folder has been deleted successfully");
121+
}
122+
123+
/**
124+
* Delete Java
125+
*/
126+
127+
public void deleteJava() {
128+
if (useJavaVersionManager) {
129+
Logger.log("Deleting Java folder...");
130+
File file = new File(String.valueOf(javaVersionManager.getPathOfJarExe()));
131+
file.delete();
132+
Logger.logPass("The Java folder has been deleted successfully");
133+
} else {
134+
Logger.logWarning("Java version manager is not enabled. Please sepcify the Java version manager in the constructor or specify a path to the java folder");
135+
}
136+
}
137+
138+
104139
/**
105140
* Executes dynamically provided Java code.
106141
* @param code The Java code to execute

src/main/java/fr/jachou/jvm/ui/JVMGui.java

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public JVMGui() {
2424
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2525
setLayout(new FlowLayout());
2626

27+
JProgressBar progressBar = new JProgressBar();
28+
2729
JTextField textField = new JTextField("Press a button to download a JDK");
2830
textField.setEditable(false);
2931
textField.setPreferredSize(new Dimension(300, 30));
@@ -36,88 +38,19 @@ public JVMGui() {
3638
@Override
3739
public void actionPerformed(ActionEvent e) {
3840
// Appeler la méthode downloader avec la version correspondante
39-
downloader(version.ordinal() + 8);
41+
JavaVersionDownloader.downloaderWithProgressBar(version.ordinal() + 8, progressBar);
4042
}
4143
});
4244
add(button);
4345
}
4446

47+
48+
progressBar.setPreferredSize(new Dimension(300, 30));
49+
progressBar.setStringPainted(true);
50+
add(progressBar);
51+
4552
pack();
4653
setLocationRelativeTo(null);
4754
setVisible(true);
4855
}
49-
50-
private static void downloader(int version) {
51-
try {
52-
String versionString = "Java_" + version;
53-
JavaVersionList.valueOf(versionString);
54-
} catch (IllegalArgumentException e) {
55-
System.out.println(JavaVersionDownloader.Colors.FAIL + "[X] The version " + version + " does not exist!");
56-
return;
57-
}
58-
59-
Logger.logPass("The version " + version + " exists!");
60-
61-
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Checking if the link is good...");
62-
String url = String.format("https://chiss.fr/jvm/download/Java_%d.zip", version);
63-
try {
64-
URL link = new URL(url);
65-
link.openStream().close();
66-
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The link is good!");
67-
Thread.sleep(1000);
68-
String path = System.getProperty("user.home") + "/.jdks";
69-
System.out.println(JavaVersionDownloader.Colors.OKCYAN + "The JDK " + version + " will be downloaded in the folder '" + path + "'");
70-
File directory = new File(path);
71-
if (!directory.exists()) {
72-
if (directory.mkdirs()) {
73-
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The folder has been created!");
74-
}
75-
} else {
76-
System.out.println(JavaVersionDownloader.Colors.WARNING + "[~] The folder already exists");
77-
}
78-
Thread.sleep(1000);
79-
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Downloading...");
80-
Path destination = Paths.get(path + "/Java_" + version + ".zip");
81-
Files.copy(link.openStream(), destination, StandardCopyOption.REPLACE_EXISTING);
82-
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded!");
83-
Thread.sleep(1000);
84-
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Unzipping...");
85-
unzip(destination.toString(), path + "/Java_" + version);
86-
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been unzipped!");
87-
Thread.sleep(1000);
88-
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Deleting zip file...");
89-
Files.delete(destination);
90-
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been deleted!");
91-
Thread.sleep(1000);
92-
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded and installed!");
93-
Thread.sleep(1000);
94-
} catch (IOException e) {
95-
System.out.println(JavaVersionDownloader.Colors.FAIL + "[X] The link does not exist!");
96-
} catch (InterruptedException e) {
97-
e.printStackTrace();
98-
}
99-
}
100-
101-
private static void unzip(String zipFilePath, String destinationDir) throws IOException {
102-
File dir = new File(destinationDir);
103-
if (!dir.exists()) {
104-
dir.mkdirs();
105-
}
106-
byte[] buffer = new byte[1024];
107-
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(zipFilePath)))) {
108-
ZipEntry entry = zis.getNextEntry();
109-
while (entry != null) {
110-
String fileName = entry.getName();
111-
File newFile = new File(destinationDir + File.separator + fileName);
112-
if (entry.isDirectory()) {
113-
newFile.mkdirs();
114-
} else {
115-
new File(newFile.getParent()).mkdirs();
116-
Files.copy(zis, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
117-
}
118-
zis.closeEntry();
119-
entry = zis.getNextEntry();
120-
}
121-
}
122-
}
12356
}

0 commit comments

Comments
 (0)