Merge pull request #1963 from millmanorama/IG-cat-colors-in-menus

add category color icons to the right click menus in IG
This commit is contained in:
Richard Cordovano 2016-02-12 16:19:57 -05:00
commit e90d429b84
7 changed files with 212 additions and 218 deletions

View File

@ -25,7 +25,6 @@ import javafx.event.ActionEvent;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javax.swing.SwingUtilities;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
@ -33,7 +32,6 @@ import org.sleuthkit.autopsy.actions.GetTagNameAndCommentDialog;
import org.sleuthkit.autopsy.actions.GetTagNameDialog;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryController;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryTopComponent;
import org.sleuthkit.autopsy.imagegallery.datamodel.CategoryManager;
import org.sleuthkit.datamodel.TagName;
/**
@ -133,12 +131,8 @@ abstract class AddTagAction {
SwingUtilities.invokeLater(() -> {
GetTagNameAndCommentDialog.TagNameAndComment tagNameAndComment = GetTagNameAndCommentDialog.doDialog(getIGWindow());
if (null != tagNameAndComment) {
if (CategoryManager.isCategoryTagName(tagNameAndComment.getTagName())) {
new CategorizeAction(controller).addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
} else {
new AddDrawableTagAction(controller).addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
}
}
});
});
getItems().add(tagAndCommentItem);

View File

@ -1,7 +1,7 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2013 Basis Technology Corp.
* Copyright 2013-16 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -21,26 +21,28 @@ package org.sleuthkit.autopsy.imagegallery.actions;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;
import javafx.event.ActionEvent;
import javafx.collections.ObservableSet;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.swing.JOptionPane;
import org.controlsfx.control.action.Action;
import org.controlsfx.control.action.ActionUtils;
import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryController;
import org.sleuthkit.autopsy.imagegallery.datamodel.Category;
import org.sleuthkit.autopsy.imagegallery.datamodel.CategoryManager;
import org.sleuthkit.autopsy.imagegallery.datamodel.DrawableAttribute;
import org.sleuthkit.autopsy.imagegallery.datamodel.DrawableFile;
import org.sleuthkit.autopsy.imagegallery.datamodel.DrawableTagsManager;
import org.sleuthkit.datamodel.ContentTag;
@ -49,48 +51,42 @@ import org.sleuthkit.datamodel.TagName;
import org.sleuthkit.datamodel.TskCoreException;
/**
* Adaptation of Tag Actions to enforce category-tag uniqueness
*
* TODO: since we are not using actionsGlobalContext anymore and this has
* diverged from autopsy action, make this extend from controlsfx Action
*/
@NbBundle.Messages({"CategorizeAction.displayName=Categorize"})
public class CategorizeAction extends AddTagAction {
public class CategorizeAction extends Action {
private static final Logger LOGGER = Logger.getLogger(CategorizeAction.class.getName());
private final ImageGalleryController controller;
private final UndoRedoManager undoManager;
private final Category cat;
private final Set<Long> selectedFileIDs;
private final Boolean createUndo;
public CategorizeAction(ImageGalleryController controller) {
super();
this.controller = controller;
undoManager = controller.getUndoManager();
public CategorizeAction(ImageGalleryController controller, Category cat, Set<Long> selectedFileIDs) {
this(controller, cat, selectedFileIDs, true);
}
public Menu getPopupMenu() {
private CategorizeAction(ImageGalleryController controller, Category cat, Set<Long> selectedFileIDs, Boolean createUndo) {
super(cat.getDisplayName());
this.controller = controller;
this.undoManager = controller.getUndoManager();
this.cat = cat;
this.selectedFileIDs = selectedFileIDs;
this.createUndo = createUndo;
setGraphic(cat.getGraphic());
setEventHandler(actionEvent -> addCatToFiles());
setAccelerator(new KeyCodeCombination(KeyCode.getKeyCode(Integer.toString(cat.getCategoryNumber()))));
}
static public Menu getCategoriesMenu(ImageGalleryController controller) {
return new CategoryMenu(controller);
}
@Override
protected String getActionDisplayName() {
return Bundle.CategorizeAction_displayName();
}
@Override
public void addTag(TagName tagName, String comment) {
Set<Long> selectedFiles = new HashSet<>(controller.getSelectionModel().getSelected());
addTagsToFiles(tagName, comment, selectedFiles);
}
@Override
protected void addTagsToFiles(TagName tagName, String comment, Set<Long> selectedFiles) {
addTagsToFiles(tagName, comment, selectedFiles, true);
}
public void addTagsToFiles(TagName tagName, String comment, Set<Long> selectedFiles, boolean createUndo) {
Logger.getAnonymousLogger().log(Level.INFO, "categorizing{0} as {1}", new Object[]{selectedFiles.toString(), tagName.getDisplayName()}); //NON-NLS
controller.queueDBWorkerTask(new CategorizeTask(selectedFiles, tagName, comment, createUndo));
private void addCatToFiles() {
Logger.getAnonymousLogger().log(Level.INFO, "categorizing{0} as {1}", new Object[]{selectedFileIDs.toString(), cat.getDisplayName()}); //NON-NLS
controller.queueDBWorkerTask(new CategorizeTask(selectedFileIDs, cat, createUndo));
}
/**
@ -101,17 +97,13 @@ public class CategorizeAction extends AddTagAction {
CategoryMenu(ImageGalleryController controller) {
super(Bundle.CategorizeAction_displayName());
setGraphic(new ImageView(DrawableAttribute.CATEGORY.getIcon()));
ObservableSet<Long> selected = controller.getSelectionModel().getSelected();
// Each category get an item in the sub-menu. Selecting one of these menu items adds
// a tag with the associated category.
for (final Category cat : Category.values()) {
MenuItem categoryItem = new MenuItem(cat.getDisplayName());
categoryItem.setOnAction((ActionEvent t) -> {
final CategorizeAction categorizeAction = new CategorizeAction(controller);
categorizeAction.addTag(controller.getCategoryManager().getTagName(cat), NO_COMMENT);
});
categoryItem.setAccelerator(new KeyCodeCombination(KeyCode.getKeyCode(Integer.toString(cat.getCategoryNumber()))));
MenuItem categoryItem = ActionUtils.createMenuItem(new CategorizeAction(controller, cat, selected));
getItems().add(categoryItem);
}
}
@ -123,27 +115,25 @@ public class CategorizeAction extends AddTagAction {
private class CategorizeTask extends ImageGalleryController.InnerTask {
private final Set<Long> fileIDs;
@Nonnull
private final TagName tagName;
private final String comment;
private final boolean createUndo;
CategorizeTask(Set<Long> fileIDs, @Nonnull TagName tagName, String comment, boolean createUndo) {
private final boolean createUndo;
private final Category cat;
CategorizeTask(Set<Long> fileIDs, @Nonnull Category cat, boolean createUndo) {
super();
this.fileIDs = fileIDs;
java.util.Objects.requireNonNull(tagName);
this.tagName = tagName;
this.comment = comment;
java.util.Objects.requireNonNull(cat);
this.cat = cat;
this.createUndo = createUndo;
}
@Override
public void run() {
final DrawableTagsManager tagsManager = controller.getTagsManager();
final CategoryManager categoryManager = controller.getCategoryManager();
Map<Long, TagName> oldCats = new HashMap<>();
Map<Long, Category> oldCats = new HashMap<>();
TagName tagName = categoryManager.getTagName(cat);
TagName catZeroTagName = categoryManager.getTagName(Category.ZERO);
for (long fileID : fileIDs) {
try {
DrawableFile<?> file = controller.getFileFromId(fileID); //drawable db access
@ -151,12 +141,12 @@ public class CategorizeAction extends AddTagAction {
Category oldCat = file.getCategory(); //drawable db access
TagName oldCatTagName = categoryManager.getTagName(oldCat);
if (false == tagName.equals(oldCatTagName)) {
oldCats.put(fileID, oldCatTagName);
oldCats.put(fileID, oldCat);
}
}
final List<ContentTag> fileTags = tagsManager.getContentTagsByContent(file);
if (tagName == categoryManager.getTagName(Category.ZERO)) {
if (tagName.equals(catZeroTagName)) {
// delete all cat tags for cat-0
fileTags.stream()
.filter(tag -> CategoryManager.isCategoryTagName(tag.getName()))
@ -173,7 +163,7 @@ public class CategorizeAction extends AddTagAction {
.map(Tag::getName)
.filter(tagName::equals)
.collect(Collectors.toList()).isEmpty()) {
tagsManager.addContentTag(file, tagName, comment);
tagsManager.addContentTag(file, tagName, "");
}
}
} catch (TskCoreException ex) {
@ -186,7 +176,7 @@ public class CategorizeAction extends AddTagAction {
}
if (createUndo && oldCats.isEmpty() == false) {
undoManager.addToUndo(new CategorizationChange(controller, tagName, oldCats));
undoManager.addToUndo(new CategorizationChange(controller, cat, oldCats));
}
}
}
@ -197,11 +187,11 @@ public class CategorizeAction extends AddTagAction {
@Immutable
private final class CategorizationChange implements UndoRedoManager.UndoableCommand {
private final TagName newCategory;
private final ImmutableMap<Long, TagName> oldCategories;
private final Category newCategory;
private final ImmutableMap<Long, Category> oldCategories;
private final ImageGalleryController controller;
CategorizationChange(ImageGalleryController controller, TagName newCategory, Map<Long, TagName> oldCategories) {
CategorizationChange(ImageGalleryController controller, Category newCategory, Map<Long, Category> oldCategories) {
this.controller = controller;
this.newCategory = newCategory;
this.oldCategories = ImmutableMap.copyOf(oldCategories);
@ -213,8 +203,8 @@ public class CategorizeAction extends AddTagAction {
*/
@Override
public void run() {
CategorizeAction categorizeAction = new CategorizeAction(controller);
categorizeAction.addTagsToFiles(newCategory, "", this.oldCategories.keySet(), false);
CategorizeAction categorizeAction = new CategorizeAction(controller, newCategory, this.oldCategories.keySet(), false);
categorizeAction.addCatToFiles();
}
/**
@ -223,9 +213,10 @@ public class CategorizeAction extends AddTagAction {
*/
@Override
public void undo() {
CategorizeAction categorizeAction = new CategorizeAction(controller);
for (Map.Entry<Long, TagName> entry : oldCategories.entrySet()) {
categorizeAction.addTagsToFiles(entry.getValue(), "", Collections.singleton(entry.getKey()), false);
for (Map.Entry<Long, Category> entry : oldCategories.entrySet()) {
new CategorizeAction(controller, entry.getValue(), Collections.singleton(entry.getKey()), false)
.addCatToFiles();
}
}
}

View File

@ -18,8 +18,7 @@
*/
package org.sleuthkit.autopsy.imagegallery.actions;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import java.util.HashSet;
import org.controlsfx.control.action.Action;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryController;
import org.sleuthkit.autopsy.imagegallery.datamodel.Category;
@ -30,10 +29,9 @@ import org.sleuthkit.autopsy.imagegallery.datamodel.Category;
public class CategorizeGroupAction extends Action {
public CategorizeGroupAction(Category cat, ImageGalleryController controller) {
super(cat.getDisplayName(), (javafx.event.ActionEvent actionEvent) -> {
Set<Long> fileIdSet = ImmutableSet.copyOf(controller.viewState().get().getGroup().getFileIDs());
new CategorizeAction(controller).addTagsToFiles(controller.getTagsManager().getTagName(cat), "", fileIdSet);
});
setGraphic(cat.getGraphic());
super(cat.getDisplayName(), actionEvent ->
new CategorizeAction(controller, cat, new HashSet<>(controller.viewState().get().getGroup().getFileIDs()))
.handle(actionEvent)
);
}
}

View File

@ -18,17 +18,15 @@
*/
package org.sleuthkit.autopsy.imagegallery.actions;
import org.controlsfx.control.action.Action;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryController;
import org.sleuthkit.autopsy.imagegallery.datamodel.Category;
/**
*
*/
public class CategorizeSelectedFilesAction extends Action {
public class CategorizeSelectedFilesAction extends CategorizeAction {
public CategorizeSelectedFilesAction(Category cat, ImageGalleryController controller) {
super(cat.getDisplayName(), (javafx.event.ActionEvent actionEvent) -> new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(cat), ""));
setGraphic(cat.getGraphic());
super(controller, cat, controller.getSelectionModel().getSelected());
}
}

View File

@ -25,6 +25,9 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
@ -94,6 +97,7 @@ public enum Category {
private final String displayName;
private final int id;
private Image snapshot;
private Category(Color color, int id, String name) {
this.color = color;
@ -118,11 +122,15 @@ public enum Category {
return displayName;
}
public Node getGraphic() {
synchronized public Node getGraphic() {
if (snapshot == null) {
Region region = new Region();
region.setBackground(new Background(new BackgroundFill(getColor(), CORNER_RADII_4, Insets.EMPTY)));
region.setPrefSize(16, 16);
region.setBorder(new Border(new BorderStroke(getColor().darker(), BorderStrokeStyle.SOLID, CORNER_RADII_4, BORDER_WIDTHS_2)));
return region;
Scene scene = new Scene(region, 16, 16, Color.TRANSPARENT);
snapshot = region.snapshot(null, null);
}
return new ImageView(snapshot);
}
}

View File

@ -182,11 +182,10 @@ public abstract class DrawableTileBase extends DrawableUIBase {
private ContextMenu buildContextMenu(DrawableFile<?> file) {
final ArrayList<MenuItem> menuItems = new ArrayList<>();
menuItems.add(new CategorizeAction(getController()).getPopupMenu());
menuItems.add(CategorizeAction.getCategoriesMenu(getController()));
menuItems.add(new AddDrawableTagAction(getController()).getPopupMenu());
final MenuItem extractMenuItem = new MenuItem(Bundle.DrawableTileBase_menuItem_extractFiles());
extractMenuItem.setOnAction(actionEvent -> {
SwingUtilities.invokeLater(() -> {
@ -196,7 +195,6 @@ public abstract class DrawableTileBase extends DrawableUIBase {
});
menuItems.add(extractMenuItem);
MenuItem contentViewer = new MenuItem(Bundle.DrawableTileBase_menuItem_showContentViewer());
contentViewer.setOnAction(actionEvent -> {
SwingUtilities.invokeLater(() -> {

View File

@ -19,6 +19,7 @@
package org.sleuthkit.autopsy.imagegallery.gui.drawableviews;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Arrays;
@ -47,6 +48,7 @@ import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
@ -403,9 +405,9 @@ public class GroupPane extends BorderPane {
toggleForCategory.getStyleClass().add("toggle-button");
toggleForCategory.selectedProperty().addListener((ov, wasSelected, toggleSelected) -> {
if (toggleSelected && slideShowPane != null) {
slideShowPane.getFileID().ifPresent((fileID) -> {
slideShowPane.getFileID().ifPresent(fileID -> {
selectionModel.clearAndSelect(fileID);
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(cat), "");
new CategorizeAction(controller, cat, ImmutableSet.of(fileID)).handle(null);
});
}
});
@ -762,35 +764,40 @@ public class GroupPane extends BorderPane {
selectAllFiles();
t.consume();
}
if (selectionModel.getSelected().isEmpty() == false) {
switch (t.getCode()) {
ObservableSet<Long> selected = selectionModel.getSelected();
if (selected.isEmpty() == false) {
Category cat = keyCodeToCat(t.getCode());
if (cat != null) {
new CategorizeAction(controller, cat, selected).handle(null);
}
}
}
}
private Category keyCodeToCat(KeyCode t) {
if (t != null) {
switch (t) {
case NUMPAD0:
case DIGIT0:
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(Category.ZERO), "");
break;
return Category.ZERO;
case NUMPAD1:
case DIGIT1:
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(Category.ONE), "");
break;
return Category.ONE;
case NUMPAD2:
case DIGIT2:
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(Category.TWO), "");
break;
return Category.TWO;
case NUMPAD3:
case DIGIT3:
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(Category.THREE), "");
break;
return Category.THREE;
case NUMPAD4:
case DIGIT4:
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(Category.FOUR), "");
break;
return Category.FOUR;
case NUMPAD5:
case DIGIT5:
new CategorizeAction(controller).addTag(controller.getTagsManager().getTagName(Category.FIVE), "");
break;
}
return Category.FIVE;
}
}
return null;
}
private void handleArrows(KeyEvent t) {
@ -826,7 +833,7 @@ public class GroupPane extends BorderPane {
private ContextMenu buildContextMenu() {
ArrayList<MenuItem> menuItems = new ArrayList<>();
menuItems.add(new CategorizeAction(controller).getPopupMenu());
menuItems.add(CategorizeAction.getCategoriesMenu(controller));
menuItems.add(new AddDrawableTagAction(controller).getPopupMenu());
Collection<? extends ContextMenuActionsProvider> menuProviders = Lookup.getDefault().lookupAll(ContextMenuActionsProvider.class);