Skip to content

Commit fc68f2c

Browse files
committed
Fixed progress bar iterator and drag drop
1 parent b2d358f commit fc68f2c

File tree

17 files changed

+239
-139
lines changed

17 files changed

+239
-139
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.i-comit</groupId>
5-
<version>1.5.1</version>
5+
<version>1.5.2</version>
66
<packaging>jar</packaging>
77
<properties>
88
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/com/i_comit/windows/AES.java

+77-21
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
import static com.i_comit.windows.AES.decrypt;
88
import static com.i_comit.windows.AES.encrypt;
9+
import static com.i_comit.windows.DragDrop.decFiles;
10+
import static com.i_comit.windows.DragDrop.encFiles;
911
import static com.i_comit.windows.Main.jAlertLabel;
10-
import static com.i_comit.windows.Memory.byteFormatter;
12+
import static com.i_comit.windows.Main.jProgressBar1;
1113
import static com.i_comit.windows.Statics.*;
1214
import java.io.*;
1315
import java.nio.file.*;
@@ -32,10 +34,20 @@ public class AES {
3234

3335
public static Thread t;
3436

35-
public static void AESThread() {
37+
// public static void AESThread() {
38+
// t = new Thread(() -> {
39+
// try {
40+
// AES_T.AESQuery();
41+
// } catch (InterruptedException ex) {
42+
// ex.printStackTrace();
43+
// }
44+
// });
45+
// t.start();
46+
// }
47+
public static void AESThread(List<Path> paths, boolean AESBool) {
3648
t = new Thread(() -> {
3749
try {
38-
AES_T.AESQuery();
50+
AES_T.AESQuery(paths, AESBool);
3951
} catch (InterruptedException ex) {
4052
ex.printStackTrace();
4153
}
@@ -81,6 +93,7 @@ public static void getFileAttr(File inputFile, File outputFile) throws IOExcepti
8193

8294
private static void doCrypto(int cipherMode, String key, File inputFile,
8395
File outputFile) throws CryptoException {
96+
8497
try {
8598
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
8699
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
@@ -102,13 +115,12 @@ private static void doCrypto(int cipherMode, String key, File inputFile,
102115
System.gc();
103116
System.runFinalization();
104117
}
105-
106-
int iterator = Statics.fileIter++;
107-
float percentage = ((float) iterator / AES_T.paths.size() * 100);
118+
fileIter++;
119+
float percentage = ((float) fileIter / AES_T.paths.size() * 100);
108120
DecimalFormat format = new DecimalFormat("0.#");
109121
String percentageStr = format.format(percentage);
110-
Main.jProgressBar1.setValue(iterator);
111-
Main.jProgressBar1.setString(percentageStr + "% | " + iterator + "/" + AES_T.paths.size());
122+
Main.jProgressBar1.setString(percentageStr + "% | " + fileIter + "/" + AES_T.paths.size());
123+
Main.jProgressBar1.setValue(fileIter);
112124
GUI.loggerThread(outputFile);
113125
getFileAttr(inputFile, outputFile);
114126

@@ -146,23 +158,25 @@ public void run() {
146158

147159
public static List<Path> paths = null;
148160

149-
public static void AESQuery() throws InterruptedException {
150-
contents = directory.listFiles();
151-
try {
152-
paths = listAESPaths(path);
161+
public static void AESQuery(List<Path> paths, boolean AESBool) throws InterruptedException {
162+
AES_T.paths = paths;
163+
Main.jProgressBar1.setString("0% | " + "0/" + AES_T.paths.size());
164+
if (AESBool) {
165+
contents = directory.listFiles();
166+
// paths = listAESPaths(path);
153167
if (contents != null) {
154168
if (contents.length != 0) {
155169
if (!paths.isEmpty()) {
156170
Main.toolBtnsBool(false);
157171
Main.jButton2.setVisible(true);
158172
Main.jTextArea5.setVisible(false);
159-
switch (Statics.AESMode) {
173+
switch (AESMode) {
160174
case 0 -> {
161175
Main.jProgressBar1.setStringPainted(true);
162176
GUI.labelCutterThread(jAlertLabel, "encrypting " + paths.size() + " files", 0, 15, 300);
163177
paths.forEach(x -> {
164178
try {
165-
if (x.toFile().length() > 1024 * 32) {
179+
if (x.toFile().length() > maxFileBytes) {
166180
if (GUI.t.isAlive()) {
167181
GUI.t.interrupt();
168182
}
@@ -173,7 +187,7 @@ public static void AESQuery() throws InterruptedException {
173187
} catch (AES.CryptoException ex) {
174188
}
175189
});
176-
if (Statics.fileIter == 0) {
190+
if (fileIter == 0) {
177191
GUI.t.interrupt();
178192
GUI.labelCutterThread(jAlertLabel, "incorrect key", 10, 25, 500);
179193
} else {
@@ -187,7 +201,7 @@ public static void AESQuery() throws InterruptedException {
187201
GUI.labelCutterThread(jAlertLabel, "decrypting files...", 0, 15, 300);
188202
paths.forEach(x -> {
189203
try {
190-
if (x.toFile().length() > 1024 * 32) {
204+
if (x.toFile().length() > maxFileBytes) {
191205
if (GUI.t.isAlive()) {
192206
GUI.t.interrupt();
193207
}
@@ -198,7 +212,7 @@ public static void AESQuery() throws InterruptedException {
198212
} catch (AES.CryptoException ex) {
199213
}
200214
});
201-
if (Statics.fileIter == 0) {
215+
if (fileIter == 0) {
202216
GUI.t.interrupt();
203217
GUI.labelCutterThread(jAlertLabel, "incorrect key", 10, 25, 500);
204218
} else {
@@ -210,7 +224,7 @@ public static void AESQuery() throws InterruptedException {
210224

211225
} else {
212226
if (!Main.jToggleButton1.isSelected()) {
213-
switch (Statics.AESMode) {
227+
switch (AESMode) {
214228
case 0 -> {
215229
GUI.labelCutterThread(jAlertLabel, "no files to encrypt", 10, 20, 400);
216230
}
@@ -229,8 +243,50 @@ public static void AESQuery() throws InterruptedException {
229243
GUI.labelCutterThread(jAlertLabel, "i-ncript folder does not exist", 20, 40, 800);
230244
Main.jToggleButton2.setEnabled(true);
231245
}
232-
} catch (IOException ex) {
233-
//ex.printStackTrace();
246+
} else {
247+
jProgressBar1.setMaximum(paths.size());
248+
jProgressBar1.setStringPainted(true);
249+
jProgressBar1.setValue(fileIter);
250+
251+
paths.forEach(x -> {
252+
try {
253+
String fileStr = x.toString();
254+
File file = Paths.get(fileStr).toFile();
255+
if (x.toString().endsWith(".enc") || x.toString().startsWith("Thumbs.db")) {
256+
encFiles++;
257+
if (x.toFile().length() > maxFileBytes) {
258+
if (GUI.t.isAlive()) {
259+
GUI.t.interrupt();
260+
}
261+
Main.jProgressBar2.setVisible(true);
262+
Main.jAlertLabel.setText("decrypting " + x.toFile().getName());
263+
}
264+
AES.decrypt(Hasher.modHash(password), file, file);
265+
}
266+
if (!x.toString().endsWith(".enc")) {
267+
decFiles++;
268+
if (x.toFile().length() > maxFileBytes) {
269+
if (GUI.t.isAlive()) {
270+
GUI.t.interrupt();
271+
}
272+
Main.jProgressBar2.setVisible(true);
273+
Main.jAlertLabel.setText("encrypting " + x.toFile().getName());
274+
}
275+
AES.encrypt(Hasher.modHash(password), file, file);
276+
}
277+
System.out.println("ENC " + encFiles + " " + decFiles);
278+
} catch (AES.CryptoException ex) {
279+
ex.printStackTrace();
280+
}
281+
});
282+
283+
if (fileIter == 0) {
284+
GUI.t.interrupt();
285+
GUI.labelCutterThread(Main.jAlertLabel, "incorrect key", 10, 25, 500);
286+
} else {
287+
DragDrop.resetProgressBar(encFiles, decFiles);
288+
GUI.getGB();
289+
}
234290
}
235291
}
236292

@@ -247,7 +303,7 @@ public static List<Path> listAESPaths(Path path) throws IOException {
247303

248304
List<Path> result = null;
249305
try ( Stream<Path> walk = Files.walk(path)) {
250-
switch (Statics.AESMode) {
306+
switch (AESMode) {
251307
case 0 ->
252308
result = walk.filter(Files::isRegularFile).filter(p -> !p.getFileName().toString().endsWith(".enc")).filter(p -> !p.getFileName().toString().startsWith("Thumbs.db"))
253309
.collect(Collectors.toList());

src/main/java/com/i_comit/windows/DragDrop.java

+33-37
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
import static com.i_comit.windows.Main.jAlertLabel;
88
import static com.i_comit.windows.Main.jProgressBar1;
9-
import static com.i_comit.windows.Statics.password;
109
import java.awt.datatransfer.DataFlavor;
1110
import java.awt.datatransfer.Transferable;
1211
import java.awt.dnd.*;
1312
import java.io.File;
14-
import java.io.IOException;
1513
import java.nio.file.Path;
16-
import java.nio.file.Paths;
1714
import java.util.ArrayList;
1815
import java.util.List;
1916

@@ -24,6 +21,9 @@
2421
class DragDrop implements DropTargetListener {
2522

2623
public static Thread t;
24+
public static Thread t2;
25+
static int encFiles = 0;
26+
static int decFiles = 0;
2727

2828
public static void progressBarThread(int encFiles, int decFiles) {
2929
t = new Thread(() -> DragDrop_T.resetProgressBar(encFiles, decFiles));
@@ -50,19 +50,19 @@ public void drop(DropTargetDropEvent event) {
5050
List files = (List) transferable.getTransferData(flavor);
5151
List<Path> paths = new ArrayList<>();
5252

53-
if (files.size() <= 10) {
53+
if (files.size() <= 15) {
5454
// Loop them through
5555
for (int i = 0; i < files.size(); i++) {
5656
String sf = files.get(i).toString();
5757
File filesf = new File(sf);
5858
paths.add(filesf.toPath());
5959
if (i >= files.size() - 1) {
60-
listDragDropFiles(paths);
60+
AES.AESThread(paths, false);
6161
}
6262
}
6363
} else {
64-
GUI.labelCutterThread(Main.jAlertLabel, "you can only drop up to 10 files at once", 10, 25, 500);
65-
Main.jTextArea1.append("For security reasons, you can only drop up to 10 files at once\n");
64+
GUI.labelCutterThread(Main.jAlertLabel, "you can only drop up to 15 files at once", 10, 25, 500);
65+
Main.jTextArea1.append("For security reasons, you can only drop up to 15 files at once\n");
6666
}
6767
}
6868

@@ -74,39 +74,34 @@ public void drop(DropTargetDropEvent event) {
7474
// Inform that the drop is complete
7575
event.dropComplete(true);
7676
}
77-
static int encFiles = 0;
78-
static int decFiles = 0;
7977

80-
public static void listDragDropFiles(List paths) throws IOException {
81-
AES_T.paths = paths;
82-
Statics.fileIter = 0;
83-
jProgressBar1.setMaximum(paths.size());
84-
jProgressBar1.setStringPainted(true);
85-
jProgressBar1.setValue(Statics.fileIter);
78+
public static void resetProgressBar(int encFiles, int decFiles) {
79+
Main.jProgressBar1.setString("100% | " + AES_T.paths.size() + "/" + AES_T.paths.size());
80+
jProgressBar1.setMaximum(100);
81+
jProgressBar1.setValue(jProgressBar1.getMaximum());
82+
Main.jButton2.setVisible(false);
83+
84+
try {
85+
Thread.sleep(400);
86+
GUI.labelCutterThread(jAlertLabel, decFiles + " encrypted | " + encFiles + " decrypted", 15, 30, 300);
8687

87-
paths.forEach(x -> {
88-
try {
89-
String fileStr = x.toString();
90-
File file = Paths.get(fileStr).toFile();
91-
if (x.toString().endsWith(".enc") || x.toString().startsWith("Thumbs.db")) {
92-
encFiles++;
93-
AES.decrypt(Hasher.modHash(password), file, file);
94-
}
95-
if (!x.toString().endsWith(".enc")) {
96-
decFiles++;
97-
AES.encrypt(Hasher.modHash(password), file, file);
98-
}
99-
} catch (AES.CryptoException ex) {
100-
ex.printStackTrace();
88+
Thread.sleep(300);
89+
for (int x = jProgressBar1.getMaximum(); x >= 0; x--) {
90+
Thread.sleep(5);
91+
jProgressBar1.setValue(x);
92+
}
93+
if (jProgressBar1.getValue() == 0) {
94+
Statics.fileIter = 0;
95+
Statics.fileCount = 0;
96+
jProgressBar1.setValue(Statics.fileIter);
97+
jProgressBar1.setStringPainted(false);
98+
DragDrop.encFiles = 0;
99+
DragDrop.decFiles = 0;
100+
Main.jTextArea5.setVisible(true);
101+
Main.toolBtnsBool(true);
101102
}
102-
});
103-
104-
if (Statics.fileIter == 0) {
105-
GUI.t.interrupt();
106-
GUI.labelCutterThread(Main.jAlertLabel, "incorrect key", 10, 25, 500);
107-
} else {
108-
progressBarThread(encFiles, decFiles);
109-
GUI.getGB();
103+
} catch (InterruptedException ex) {
104+
ex.printStackTrace();
110105
}
111106
}
112107

@@ -161,6 +156,7 @@ public static void resetProgressBar(int encFiles, int decFiles) {
161156
DragDrop.decFiles = 0;
162157
Main.jTextArea5.setVisible(true);
163158
Main.toolBtnsBool(true);
159+
AES_T.paths = null;
164160
}
165161
} catch (InterruptedException ex) {
166162
ex.printStackTrace();

src/main/java/com/i_comit/windows/FileHider.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public void run() {
4444
// ex.printStackTrace();
4545
// }
4646
}
47-
4847
public static void FileHider_T(boolean fileHideBool) throws IOException {
4948
Statics.fileHideIter = 0;
5049
List<Path> paths = listPaths(path);
5150
File[] contents = directory.listFiles();
5251
int fileCount = GUI.countAllFiles(path);
5352
Main.toolBtnsBool(false);
5453
fileCt = 0;
55-
54+
Statics.fileIter = 0;
55+
Statics.fileCount = 0;
5656
if (fileHideBool) {
5757
if (contents != null) {
5858
if (contents.length != 0) {
@@ -89,7 +89,6 @@ public static void FileHider_T(boolean fileHideBool) throws IOException {
8989
try {
9090
getFileAttr(x, fileHideBool);
9191
if (Statics.fileHideIter == fileCount) {
92-
9392
if (fileCt > 10) {
9493
Thread.sleep(300);
9594
GUI.labelCutterThread(Main.jAlertLabel, fileCt + " files unhidden", 30, 25, 350);

src/main/java/com/i_comit/windows/GUI.java

-7
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ public static void resetProgressBar() {
101101
jProgressBar1.setValue(x);
102102
}
103103
if (jProgressBar1.getValue() == 0) {
104-
Statics.fileIter = 0;
105-
Statics.fileCount = 0;
106-
jProgressBar1.setValue(Statics.fileIter);
107104
jProgressBar1.setStringPainted(false);
108105
FileHider.FileHiderThread(Main.jToggleButton2.isSelected());
109106
}
@@ -119,15 +116,12 @@ class labelCutter_T implements Runnable {
119116
public void run() {
120117

121118
}
122-
123-
//Thread-4
124119
public static void labelCutter_T(JLabel jLabel, String labelMsg, int initSleep, int sleep, int pause) {
125120
jLabel.setText("");
126121
int msgL = labelMsg.length();
127122
try {
128123
Thread.sleep(initSleep);
129124
for (int i = 0; i <= msgL; i++) {
130-
//labelMsg = "";
131125
CharSequence cutLabel = labelMsg.subSequence(0, i);
132126
jLabel.setText(cutLabel.toString());
133127
Thread.sleep(sleep);
@@ -139,7 +133,6 @@ public static void labelCutter_T(JLabel jLabel, String labelMsg, int initSleep,
139133
Thread.sleep(sleep);
140134
}
141135
} catch (InterruptedException ex) {
142-
// ex.printStackTrace();
143136
System.out.println("label thread interrupted.");
144137
}
145138
}

0 commit comments

Comments
 (0)