mirror of
https://github.com/Steffo99/unimore-oop-2020-cleaver.git
synced 2024-11-22 08:04:19 +00:00
Improve Jobs API
This commit is contained in:
parent
cc20a5289f
commit
bc2591b7a2
4 changed files with 40 additions and 19 deletions
|
@ -5,10 +5,6 @@ import eu.steffo.cleaver.logic.crypt.CryptConfig;
|
|||
import eu.steffo.cleaver.logic.split.SplitConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
|
||||
public class ChopJob extends Job {
|
||||
|
||||
|
@ -19,6 +15,13 @@ public class ChopJob extends Job {
|
|||
this.compressConfig = compressConfig;
|
||||
}
|
||||
|
||||
public ChopJob(File file, Runnable swingCallLaterOnProgressChanges, SplitConfig splitConfig, CryptConfig cryptConfig, CompressConfig compressConfig) {
|
||||
super(file, swingCallLaterOnProgressChanges);
|
||||
this.splitConfig = splitConfig;
|
||||
this.cryptConfig = cryptConfig;
|
||||
this.compressConfig = compressConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Chop";
|
||||
|
@ -26,7 +29,6 @@ public class ChopJob extends Job {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("CHOP");
|
||||
}
|
||||
|
||||
public SplitConfig getSplitConfig() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package eu.steffo.cleaver.logic;
|
||||
|
||||
import java.io.File;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import eu.steffo.cleaver.logic.compress.CompressConfig;
|
||||
import eu.steffo.cleaver.logic.crypt.CryptConfig;
|
||||
|
@ -10,17 +11,22 @@ import eu.steffo.cleaver.logic.split.SplitConfig;
|
|||
|
||||
public abstract class Job extends Thread {
|
||||
protected File file;
|
||||
protected SplitConfig splitConfig;
|
||||
protected CryptConfig cryptConfig;
|
||||
protected CompressConfig compressConfig;
|
||||
protected Progress progress;
|
||||
|
||||
private Progress progress;
|
||||
protected Runnable swingCallLaterOnProgressChanges = null;
|
||||
|
||||
protected SplitConfig splitConfig = null;
|
||||
protected CryptConfig cryptConfig = null;
|
||||
protected CompressConfig compressConfig = null;
|
||||
|
||||
public Job(File file) {
|
||||
this.file = file;
|
||||
this.progress = new NotStartedProgress();
|
||||
splitConfig = null;
|
||||
cryptConfig = null;
|
||||
compressConfig = null;
|
||||
}
|
||||
|
||||
public Job(File file, Runnable swingCallLaterOnProgressChanges) {
|
||||
this(file);
|
||||
this.swingCallLaterOnProgressChanges = swingCallLaterOnProgressChanges;
|
||||
}
|
||||
|
||||
public abstract String getType();
|
||||
|
@ -33,6 +39,11 @@ public abstract class Job extends Thread {
|
|||
return progress;
|
||||
}
|
||||
|
||||
protected void setProgress(Progress progress) {
|
||||
this.progress = progress;
|
||||
SwingUtilities.invokeLater(swingCallLaterOnProgressChanges);
|
||||
}
|
||||
|
||||
public SplitConfig getSplitConfig() {
|
||||
return splitConfig;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,10 @@ public class StitchJob extends Job {
|
|||
|
||||
public StitchJob(File file, String cryptKey) throws ChpFileError, ProgrammingError {
|
||||
super(file);
|
||||
parseChp(openChp(file), cryptKey);
|
||||
}
|
||||
|
||||
protected static Document openChp(File file) throws ChpFileError, ProgrammingError {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder;
|
||||
try {
|
||||
|
@ -39,26 +43,30 @@ public class StitchJob extends Job {
|
|||
} catch (SAXException e) {
|
||||
throw new ProgrammingError();
|
||||
} catch (IOException e) {
|
||||
throw new ChpFileError(".chp file does not exist anymore!");
|
||||
throw new ChpFileError("The .chp file does not exist anymore!");
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
protected final void parseChp(Document doc, String cryptKey) {
|
||||
Element root = doc.getDocumentElement();
|
||||
|
||||
NodeList splits = root.getElementsByTagName("Split");
|
||||
NodeList crypts = root.getElementsByTagName("Crypt");
|
||||
NodeList compresses = root.getElementsByTagName("Compress");
|
||||
|
||||
Node splitNode = splits.item(0);
|
||||
Node cryptNode = crypts.item(0);
|
||||
Node compressNode = compresses.item(0);
|
||||
|
||||
if(splitNode != null) {
|
||||
Element split = (Element)splitNode;
|
||||
String splitMode = split.getAttribute("mode");
|
||||
if(splitMode.equals("by-parts")) {
|
||||
splitConfig = new SplitByPartsConfig(Integer.parseInt(split.getTextContent()));
|
||||
}
|
||||
else if(splitMode.equals("by-size")) {
|
||||
splitConfig = new SplitBySizeConfig(Integer.parseInt(split.getTextContent()));
|
||||
}
|
||||
else {
|
||||
throw new ChpFileError(".chp file contains invalid split mode!");
|
||||
splitConfig = new SplitBySizeConfig(Integer.parseInt(split.getTextContent()));
|
||||
}
|
||||
}
|
||||
if(cryptNode != null) {
|
||||
|
@ -76,6 +84,6 @@ public class StitchJob extends Job {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("STITCH");
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package eu.steffo.cleaver.logic.progress;
|
||||
|
||||
public class WorkingProgress extends Progress {
|
||||
protected float progress;
|
||||
public final float progress;
|
||||
|
||||
public WorkingProgress() {
|
||||
this.progress = 0f;
|
||||
|
|
Loading…
Reference in a new issue