Download this file

ExecutionTimeObserver.java    257 lines (232 with data), 8.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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;
}
}