20 package org.sleuthkit.autopsy.casemodule;
22 import java.awt.event.ActionEvent;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Deque;
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.logging.Level;
32 import javax.swing.JMenuItem;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35 import org.openide.util.actions.CallableSystemAction;
36 import org.openide.util.actions.Presenter;
37 import org.openide.filesystems.FileUtil;
46 final class RecentCases
extends CallableSystemAction implements Presenter.Menu {
48 static final int LENGTH = 5;
49 static final String NAME_PROP_KEY =
"LBL_RecentCase_Name";
50 static final String PATH_PROP_KEY =
"LBL_RecentCase_Path";
51 static final RecentCase BLANK_RECENTCASE =
new RecentCase(
"",
"");
53 private final static RecentCases INSTANCE =
new RecentCases();
55 private Deque<RecentCase> recentCases;
64 static public RecentCases getInstance() {
65 INSTANCE.refreshRecentCases();
70 private RecentCases() {
72 for (
int i = 0; i < LENGTH; i++) {
74 if(ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i)) == null)
75 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i),
"");
76 if(ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i)) == null)
77 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i),
"");
85 recentCases =
new LinkedList<RecentCase>();
87 for (
int i = 0; i < LENGTH; i++) {
88 final RecentCase rc =
new RecentCase(getName(i), getPath(i));
89 if (!rc.equals(BLANK_RECENTCASE)) {
97 private static void validateCaseIndex(
int i) {
98 if (i < 0 || i >= LENGTH) {
99 throw new IllegalArgumentException(
100 NbBundle.getMessage(RecentCases.class,
"RecentCases.exception.caseIdxOutOfRange.msg", i));
104 private static String nameKey(
int i) {
105 validateCaseIndex(i);
106 return NAME_PROP_KEY + Integer.toString(i + 1);
109 private static String pathKey(
int i) {
110 validateCaseIndex(i);
111 return PATH_PROP_KEY + Integer.toString(i + 1);
114 private String getName(
int i) {
116 return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i));
123 private String getPath(
int i) {
125 return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i));
132 private void setName(
int i, String name) {
133 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), name);
136 private void setPath(
int i, String path) {
137 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), path);
140 private void setRecentCase(
int i, RecentCase rc) {
165 static RecentCase createSafe(String name, String unsafePath) {
174 return !(name.equals(
"") || path.equals(
"") || !
new File(path).exists());
181 hash = 13 * hash + (this.name != null ? this.name.hashCode() : 0);
182 hash = 13 * hash + (this.path != null ? this.path.hashCode() : 0);
191 if (getClass() != obj.getClass()) {
195 if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
198 if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path)) {
209 private void refreshRecentCases() {
210 List<RecentCase> toDelete =
new ArrayList<RecentCase>();
211 for (RecentCase rc : recentCases) {
216 for (RecentCase deleteMe : toDelete) {
217 removeRecentCase(deleteMe.name, deleteMe.path);
221 private void storeRecentCases() throws IOException {
225 for (RecentCase rc : recentCases) {
226 setRecentCase(i, rc);
232 setRecentCase(i, BLANK_RECENTCASE);
244 public JMenuItem getMenuPresenter() {
245 return new UpdateRecentCases();
254 public void actionPerformed(ActionEvent e) {
255 UpdateRecentCases.hasRecentCase =
false;
262 }
catch (Exception ex) {
263 Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING,
"Error: Could not clear the properties file.", ex);
267 private void addRecentCase(RecentCase rc) {
269 recentCases.remove(rc);
272 if (recentCases.size() == LENGTH) recentCases.remove();
285 public void addRecentCase(String name, String unsafePath) {
286 RecentCase rc = RecentCase.createSafe(name, unsafePath);
290 this.getMenuPresenter().setVisible(
true);
294 }
catch (Exception ex) {
295 Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING,
"Error: Could not update the properties file.", ex);
308 public void updateRecentCase(String oldName, String oldPath, String newName, String newPath)
throws Exception {
309 RecentCase oldRc = RecentCase.createSafe(oldName, oldPath);
310 RecentCase newRc = RecentCase.createSafe(newName, newPath);
313 recentCases.removeAll(Arrays.asList(oldRc));
315 addRecentCase(newRc);
317 this.getMenuPresenter().setVisible(
true);
321 }
catch (Exception ex) {
322 Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING,
"Error: Could not update the properties file.", ex);
331 public int getTotalRecentCases() {
332 return recentCases.size();
341 public void removeRecentCase(String name, String path) {
342 RecentCase rc = RecentCase.createSafe(name, path);
345 recentCases.removeAll(Arrays.asList(rc));
348 this.getMenuPresenter().setVisible(
true);
353 }
catch (Exception ex) {
354 Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING,
"Error: Could not update the properties file.", ex);
364 public String[] getRecentCaseNames() {
365 String[] caseNames =
new String[LENGTH];
367 Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
369 while (mostRecentFirst.hasNext()) {
370 caseNames[i] = mostRecentFirst.next().name;
374 while (i < caseNames.length) {
388 public String[] getRecentCasePaths() {
389 String[] casePaths =
new String[LENGTH];
391 Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
393 while (mostRecentFirst.hasNext()) {
394 casePaths[i] = mostRecentFirst.next().path;
398 while (i < casePaths.length) {
410 public void performAction() {
419 public String getName() {
421 return NbBundle.getMessage(this.getClass(),
"RecentCases.getName.text");
430 public HelpCtx getHelpCtx() {
431 return HelpCtx.DEFAULT_HELP;
boolean equals(Object obj)
RecentCase(String name, String path)