package cologne.eck.all_peas.files;
/**
* Based upon the number and the overall size of a set of
* selected files, this observer checks whether the amount of files
* will result in an unexpected long execution time. In this case
* a warning message appears in two levels of urgency and the user
* is asked to cancel the process.
* The limits depend on the type of the PEA: For Image Lock PEA and
* Notebook PEA, only one file of the selected files is decrypted, the
* other files are only checked for the file identifier, but this file
* must be processed as text or image,
* for File Lock PEA all selected files are decrypted.
* Each PEA should set its own limits.
*/
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.all_peas.gui.PeaDialog;
public class ExecutionTimeObserver {
/**
* These variables can be set by a PEA to PEA-specific values
*/
private static int numberLimit = 256; // Warning if selected file number > numberLimit
private static int extremeNumberLimit = 512;
private static long sizeLimit = 1024 * 1024 * 64; // 64 MiB
private static long extremeSizeLimit = 1024 * 1024 * 1024; // 1 GiB
/*
* true if a warning (long execution time because of size) was already shown
* (this warning should appear at most once for each session)
*/
private static boolean sizeWarning = false;
/*
* true if a warning (extreme long execution time) was already shown
* (this warning should appear at most once for each session)
*/
private static boolean extremeSizeWarning = false;
/*
* true if a warning (long execution time because of number of files) was already shown
* (this warning should appear at most once for each session)
*/
private static boolean numberWarning = false;
/*
* true if a warning (extreme long execution time because of number of files) was already shown
* (this warning should appear at most once for each session)
*/
private static boolean extremeNumberWarning = false;
/**
* Display a warning (long execution time) and an option to break
* if a large number or large overall size of files was selected
*
* @param newNumber the number of files to check
* @param newSize the overall size of the files to check
* @param fileModel the FileModel that hold fileNumber and allSize
* @param window the component or window for the warning dialog
*
* @return true if the process should continue ( no warning
* or warning accepted), false if the files should not
* be added (user selected the cancel or close option),
* in this case the file must be removed from the map
* manually, because it was added by checking access...
*/
public final static boolean warnExecutionTime(long newNumber, long newSize,
FileModel fileModel, Object window) {
// extreme high size of files:
if ((fileModel.getAllSize() + newSize) > extremeSizeLimit
&& extremeSizeWarning == false) {
String warningMessage = PeaProperties.getBundle().getString("extreme_size_warning");
boolean continueOption = showWarningAndBreakOption(window,
warningMessage,
true); // default is to break the process
if (continueOption == true){
extremeSizeWarning = true;
sizeWarning = true;
return true;
} else {
return false;
}
}
// high size of files:
if (fileModel.getAllSize() + newSize > sizeLimit
&& sizeWarning == false) {
String warningMessage = PeaProperties.getBundle().getString("size_warning");
boolean continueOption = showWarningAndBreakOption(window,
warningMessage,
false); // default is to continue
if (continueOption == true){
sizeWarning = true;
return true;
} else {
return false;
}
}
// extreme high number of files:
if (fileModel.getFileNumber() + newNumber > extremeNumberLimit
&& extremeNumberWarning == false) {
String warningMessage = PeaProperties.getBundle().getString("extrem_number_warning");
boolean continueOption = showWarningAndBreakOption(window,
warningMessage,
true); // default is to break the process
if (continueOption == true){
extremeNumberWarning = true;
numberWarning = true;
return true;
} else {
return false;
}
}
// high number:
if (fileModel.getFileNumber() + newNumber > numberLimit
&& numberWarning == false) {
String warningMessage = PeaProperties.getBundle().getString("number_warning");
boolean continueOption = showWarningAndBreakOption(window,
warningMessage,
false); // default is to continue
if (continueOption == true){
numberWarning = true;
return true;
} else {
return false;
} }
return true;
}
/**
* Show a warning and option to break or to go on.
*
* @param window the window owner
* @param warningMessage the warning message
* @param extremeWarning true for extreme size/number warning:
* the default option is to break, other option
*
* @return true if the process goes on,
* false for breaking the process
*/
private static boolean showWarningAndBreakOption(
Object window,
String warningMessage,
boolean extremeWarning) {
String goOnOption = ""; // String for ok option
int initialValue = 0; // default: break (1) or continue (0)
if (extremeWarning == true) {
goOnOption = PeaProperties.getBundle().getString("ignore_and_go_on");
initialValue = 1;
} else {
goOnOption = PeaProperties.getBundle().getString("go_on");
initialValue = 0;
}
Object[] options = {goOnOption,
PeaProperties.getBundle().getString("cancel")};
int n = PeaDialog.showOptions(window, // window owner
warningMessage, // the message, specifying the warning
PeaProperties.getBundle().getString("warning"), // title of the dialog window
0, // JOptionPane.YES_NO_OPTION
2, // JOptionPane.WARNING_MESSAGE
options, // options as Strings
options[initialValue]); // default option
if (n == 0) {
return true;
} else { // cancel or close
return false;
}
}
//================ Getter & Setter ==================================
/**
* Show a warning message for long execution time
* if the number of files reaches this limit
*
* @param newNumberLimit the limit, where a warning message
* should be shown
*/
public static void setNumberLimit(int newNumberLimit) {
numberLimit = newNumberLimit;
}
/**
* Get the maximum number of files without
* showing a warning message
*
* @return number of files
*/
public static int getNumberLimit() {
return numberLimit;
}
/**
* Show an urgent warning message for long execution time
* if the number of files reaches this limit
*
* @param newNumberLimit the limit, where an urgent warning message
* should be shown
*/
public static void setExtremeNumberLimit(int newExtremeNumberLimit) {
extremeNumberLimit = newExtremeNumberLimit;
}
/**
* Get the maximum number of files handled by this PEA
*
* @return the maximum number of files
*/
public static int getExtremeNumberLimit() {
return extremeNumberLimit;
}
/**
* Get the maximum recommended number of files
*
* @param newSizeLimit recommended maximum number of files
*/
public static void setSizeLimit(int newSizeLimit) {
sizeLimit = newSizeLimit;
}
/**
* Get the size of all added files where a warning message is displayed
*
* @return the size of all added files
*/
public static long getSizeLimit() {
return sizeLimit;
}
/**
* Show an urgent warning message for long execution time
* if the size of files reaches this limit
*
* @param newExtremeSizeLimit the limit, where an urgent warning message
* should be shown
*/
public static void setExtremeSizeLimit(int newExtremeSizeLimit) {
extremeSizeLimit = newExtremeSizeLimit;
}
/**
* Get the maximum recommended size of all added files
*
* @return the maximum recommended size of all added files
*/
public static long getExtremeSizeLimit() {
return extremeSizeLimit;
}
}