Fixed some codacy suggestions, added comments, minor changes

This commit is contained in:
U-BASIS\dsmyda 2019-06-05 16:16:13 -04:00
parent 366e784a31
commit 0541a3368b
3 changed files with 85 additions and 66 deletions

View File

@ -46,7 +46,7 @@ MediaViewImagePanel.errorLabel.OOMText=Could not load file into Media View: insu
MediaViewImagePanel.errorLabel.text=Could not load file into Media View. MediaViewImagePanel.errorLabel.text=Could not load file into Media View.
MediaViewImagePanel.exportSaveText=Save MediaViewImagePanel.exportSaveText=Save
MediaViewImagePanel.externalViewerButton.text=Open in External Viewer Ctrl+E MediaViewImagePanel.externalViewerButton.text=Open in External Viewer Ctrl+E
MediaViewImagePanel.fileChooserTitle=Choose a directory to save the image MediaViewImagePanel.fileChooserTitle=Choose a save location
MediaViewImagePanel.successfulExport=Tagged image was successfully saved. MediaViewImagePanel.successfulExport=Tagged image was successfully saved.
MediaViewImagePanel.unsuccessfulExport=Unable to export tagged image to disk. MediaViewImagePanel.unsuccessfulExport=Unable to export tagged image to disk.
MediaViewVideoPanel.pauseButton.text=\u25ba MediaViewVideoPanel.pauseButton.text=\u25ba

View File

@ -77,7 +77,7 @@ import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException;
import org.sleuthkit.autopsy.casemodule.services.applicationtags.ContentViewerTagManager; import org.sleuthkit.autopsy.casemodule.services.applicationtags.ContentViewerTagManager;
import org.sleuthkit.autopsy.casemodule.services.applicationtags.ContentViewerTagManager.ContentViewerTag; import org.sleuthkit.autopsy.casemodule.services.applicationtags.ContentViewerTagManager.ContentViewerTag;
import org.sleuthkit.autopsy.casemodule.services.applicationtags.ContentViewerTagManager.SerializationException; import org.sleuthkit.autopsy.casemodule.services.applicationtags.ContentViewerTagManager.SerializationException;
import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagsUtil; import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagsUtility;
import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagControls; import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagControls;
import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagRegion; import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagRegion;
import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagCreator; import org.sleuthkit.autopsy.contentviewers.imagetagging.ImageTagCreator;
@ -102,7 +102,7 @@ import org.sleuthkit.datamodel.TskCoreException;
class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPanel { class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPanel {
private static final Image EXTERNAL = new Image(MediaViewImagePanel.class.getResource("/org/sleuthkit/autopsy/images/external.png").toExternalForm()); private static final Image EXTERNAL = new Image(MediaViewImagePanel.class.getResource("/org/sleuthkit/autopsy/images/external.png").toExternalForm());
private final Logger LOGGER = Logger.getLogger(MediaViewImagePanel.class.getName()); private final static Logger LOGGER = Logger.getLogger(MediaViewImagePanel.class.getName());
private final boolean fxInited; private final boolean fxInited;
@ -117,10 +117,10 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
private final MaskerPane maskerPane = new MaskerPane(); private final MaskerPane maskerPane = new MaskerPane();
private final JPopupMenu popupMenu = new JPopupMenu(); private final JPopupMenu popupMenu = new JPopupMenu();
private final JMenuItem createTag; private final JMenuItem createTagMenuItem;
private final JMenuItem deleteTag; private final JMenuItem deleteTagMenuItem;
private final JMenuItem hideTags; private final JMenuItem hideTagsMenuItem;
private final JMenuItem exportTags; private final JMenuItem exportTagsMenuItem;
private final JFileChooser exportChooser; private final JFileChooser exportChooser;
@ -166,27 +166,27 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
exportChooser.setDialogTitle(Bundle.MediaViewImagePanel_fileChooserTitle()); exportChooser.setDialogTitle(Bundle.MediaViewImagePanel_fileChooserTitle());
//Build popupMenu when Tags Menu button is pressed. //Build popupMenu when Tags Menu button is pressed.
createTag = new JMenuItem("Create"); createTagMenuItem = new JMenuItem("Create");
createTag.addActionListener((event) -> createTag()); createTagMenuItem.addActionListener((event) -> createTag());
popupMenu.add(createTag); popupMenu.add(createTagMenuItem);
popupMenu.add(new JSeparator()); popupMenu.add(new JSeparator());
deleteTag = new JMenuItem("Delete"); deleteTagMenuItem = new JMenuItem("Delete");
deleteTag.addActionListener((event) -> deleteTag()); deleteTagMenuItem.addActionListener((event) -> deleteTag());
popupMenu.add(deleteTag); popupMenu.add(deleteTagMenuItem);
popupMenu.add(new JSeparator()); popupMenu.add(new JSeparator());
hideTags = new JMenuItem("Hide"); hideTagsMenuItem = new JMenuItem("Hide");
hideTags.addActionListener((event) -> showOrHideTags()); hideTagsMenuItem.addActionListener((event) -> showOrHideTags());
popupMenu.add(hideTags); popupMenu.add(hideTagsMenuItem);
popupMenu.add(new JSeparator()); popupMenu.add(new JSeparator());
exportTags = new JMenuItem("Export"); exportTagsMenuItem = new JMenuItem("Export");
exportTags.addActionListener((event) -> exportTags()); exportTagsMenuItem.addActionListener((event) -> exportTags());
popupMenu.add(exportTags); popupMenu.add(exportTagsMenuItem);
popupMenu.setPopupSize(300, 150); popupMenu.setPopupSize(300, 150);
@ -211,57 +211,59 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
State currentState = (State) event.getNewValue(); State currentState = (State) event.getNewValue();
switch (currentState) { switch (currentState) {
case CREATE: case CREATE:
createTag.setEnabled(true); createTagMenuItem.setEnabled(true);
deleteTag.setEnabled(false); deleteTagMenuItem.setEnabled(false);
hideTags.setEnabled(true); hideTagsMenuItem.setEnabled(true);
exportTags.setEnabled(true); exportTagsMenuItem.setEnabled(true);
break; break;
case SELECTED: case SELECTED:
if (masterGroup.getChildren().contains(imageTagCreator)) { if (masterGroup.getChildren().contains(imageTagCreator)) {
imageTagCreator.disconnect(); imageTagCreator.disconnect();
masterGroup.getChildren().remove(imageTagCreator); masterGroup.getChildren().remove(imageTagCreator);
} }
createTag.setEnabled(false); createTagMenuItem.setEnabled(false);
deleteTag.setEnabled(true); deleteTagMenuItem.setEnabled(true);
hideTags.setEnabled(true); hideTagsMenuItem.setEnabled(true);
exportTags.setEnabled(true); exportTagsMenuItem.setEnabled(true);
break; break;
case HIDDEN: case HIDDEN:
createTag.setEnabled(false); createTagMenuItem.setEnabled(false);
deleteTag.setEnabled(false); deleteTagMenuItem.setEnabled(false);
hideTags.setEnabled(true); hideTagsMenuItem.setEnabled(true);
hideTags.setText(DisplayOptions.SHOW_TAGS.getName()); hideTagsMenuItem.setText(DisplayOptions.SHOW_TAGS.getName());
exportTags.setEnabled(false); exportTagsMenuItem.setEnabled(false);
break; break;
case VISIBLE: case VISIBLE:
createTag.setEnabled(true); createTagMenuItem.setEnabled(true);
deleteTag.setEnabled(false); deleteTagMenuItem.setEnabled(false);
hideTags.setEnabled(true); hideTagsMenuItem.setEnabled(true);
hideTags.setText(DisplayOptions.HIDE_TAGS.getName()); hideTagsMenuItem.setText(DisplayOptions.HIDE_TAGS.getName());
exportTags.setEnabled(true); exportTagsMenuItem.setEnabled(true);
break; break;
case DEFAULT: case DEFAULT:
case EMPTY: case EMPTY:
if (masterGroup.getChildren().contains(imageTagCreator)) { if (masterGroup.getChildren().contains(imageTagCreator)) {
imageTagCreator.disconnect(); imageTagCreator.disconnect();
} }
createTag.setEnabled(true); createTagMenuItem.setEnabled(true);
deleteTag.setEnabled(false); deleteTagMenuItem.setEnabled(false);
hideTags.setEnabled(false); hideTagsMenuItem.setEnabled(false);
hideTags.setText(DisplayOptions.HIDE_TAGS.getName()); hideTagsMenuItem.setText(DisplayOptions.HIDE_TAGS.getName());
exportTags.setEnabled(false); exportTagsMenuItem.setEnabled(false);
break; break;
case NONEMPTY: case NONEMPTY:
createTag.setEnabled(true); createTagMenuItem.setEnabled(true);
deleteTag.setEnabled(false); deleteTagMenuItem.setEnabled(false);
hideTags.setEnabled(true); hideTagsMenuItem.setEnabled(true);
exportTags.setEnabled(true); exportTagsMenuItem.setEnabled(true);
break; break;
case DISABLE: case DISABLE:
createTag.setEnabled(false); createTagMenuItem.setEnabled(false);
deleteTag.setEnabled(false); deleteTagMenuItem.setEnabled(false);
hideTags.setEnabled(false); hideTagsMenuItem.setEnabled(false);
exportTags.setEnabled(false); exportTagsMenuItem.setEnabled(false);
break;
default:
break; break;
} }
}); });
@ -811,8 +813,7 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
try { try {
ContentTag contentTag = Case.getCurrentCaseThrows().getServices().getTagsManager() ContentTag contentTag = Case.getCurrentCaseThrows().getServices().getTagsManager()
.addContentTag(file, result.getTagName(), result.getComment()); .addContentTag(file, result.getTagName(), result.getComment());
ContentViewerTag<ImageTagRegion> contentViewerTag = ContentViewerTagManager.saveTag(contentTag, data); return ContentViewerTagManager.saveTag(contentTag, data);
return contentViewerTag;
} finally { } finally {
scrollPane.setCursor(Cursor.DEFAULT); scrollPane.setCursor(Cursor.DEFAULT);
} }
@ -824,17 +825,17 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
*/ */
private void showOrHideTags() { private void showOrHideTags() {
Platform.runLater(() -> { Platform.runLater(() -> {
if (DisplayOptions.HIDE_TAGS.getName().equals(hideTags.getText())) { if (DisplayOptions.HIDE_TAGS.getName().equals(hideTagsMenuItem.getText())) {
//Temporarily remove the tags group and update buttons //Temporarily remove the tags group and update buttons
masterGroup.getChildren().remove(tagsGroup); masterGroup.getChildren().remove(tagsGroup);
hideTags.setText(DisplayOptions.SHOW_TAGS.getName()); hideTagsMenuItem.setText(DisplayOptions.SHOW_TAGS.getName());
tagsGroup.clearFocus(); tagsGroup.clearFocus();
pcs.firePropertyChange(new PropertyChangeEvent(this, pcs.firePropertyChange(new PropertyChangeEvent(this,
"state", null, State.HIDDEN)); "state", null, State.HIDDEN));
} else { } else {
//Add tags group back in and update buttons //Add tags group back in and update buttons
masterGroup.getChildren().add(tagsGroup); masterGroup.getChildren().add(tagsGroup);
hideTags.setText(DisplayOptions.HIDE_TAGS.getName()); hideTagsMenuItem.setText(DisplayOptions.HIDE_TAGS.getName());
pcs.firePropertyChange(new PropertyChangeEvent(this, pcs.firePropertyChange(new PropertyChangeEvent(this,
"state", null, State.VISIBLE)); "state", null, State.VISIBLE));
} }
@ -845,7 +846,7 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
"MediaViewImagePanel.exportSaveText=Save", "MediaViewImagePanel.exportSaveText=Save",
"MediaViewImagePanel.successfulExport=Tagged image was successfully saved.", "MediaViewImagePanel.successfulExport=Tagged image was successfully saved.",
"MediaViewImagePanel.unsuccessfulExport=Unable to export tagged image to disk.", "MediaViewImagePanel.unsuccessfulExport=Unable to export tagged image to disk.",
"MediaViewImagePanel.fileChooserTitle=Choose a directory to save the image" "MediaViewImagePanel.fileChooserTitle=Choose a save location"
}) })
private void exportTags() { private void exportTags() {
tagsGroup.clearFocus(); tagsGroup.clearFocus();
@ -855,14 +856,14 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan
exportChooser.setCurrentDirectory(exportChooser.getSelectedFile()); exportChooser.setCurrentDirectory(exportChooser.getSelectedFile());
new SwingWorker<Void, Void>() { new SwingWorker<Void, Void>() {
@Override @Override
protected Void doInBackground() throws Exception { protected Void doInBackground() {
try { try {
List<ContentTag> tags = Case.getCurrentCase().getServices() List<ContentTag> tags = Case.getCurrentCase().getServices()
.getTagsManager().getContentTagsByContent(file); .getTagsManager().getContentTagsByContent(file);
List<ContentViewerTag<ImageTagRegion>> contentViewerTags = getContentViewerTags(tags); List<ContentViewerTag<ImageTagRegion>> contentViewerTags = getContentViewerTags(tags);
Collection<ImageTagRegion> regions = contentViewerTags.stream() Collection<ImageTagRegion> regions = contentViewerTags.stream()
.map(cvTag -> cvTag.getDetails()).collect(Collectors.toList()); .map(cvTag -> cvTag.getDetails()).collect(Collectors.toList());
byte[] jpgImage = ImageTagsUtil.exportTags(file, regions, ".jpg"); byte[] jpgImage = ImageTagsUtility.exportTags(file, regions, ".jpg");
Path output = Paths.get(exportChooser.getSelectedFile().getPath(), Path output = Paths.get(exportChooser.getSelectedFile().getPath(),
FilenameUtils.getBaseName(file.getName()) + "-with_tags.jpg"); //NON-NLS FilenameUtils.getBaseName(file.getName()) + "-with_tags.jpg"); //NON-NLS
Files.write(output, jpgImage); Files.write(output, jpgImage);

View File

@ -1,7 +1,20 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Autopsy Forensic Browser
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * Copyright 2019 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.sleuthkit.autopsy.contentviewers.imagetagging; package org.sleuthkit.autopsy.contentviewers.imagetagging;
@ -15,12 +28,17 @@ import org.opencv.highgui.Highgui;
import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.TskCoreException; import org.sleuthkit.datamodel.TskCoreException;
/** public class ImageTagsUtility {
*
* @author dsmyda
*/
public class ImageTagsUtil {
/**
* Embeds the tag regions into an image (represented as an AbstractFile).
*
* @param file Base Image
* @param tagRegions Tag regions to be saved into the image
* @param outputEncoding Output file type encoding (ex. .jpg, .png)
* @return output image in byte array
* @throws TskCoreException
*/
public static byte[] exportTags(AbstractFile file, Collection<ImageTagRegion> tagRegions, String outputEncoding) throws TskCoreException { public static byte[] exportTags(AbstractFile file, Collection<ImageTagRegion> tagRegions, String outputEncoding) throws TskCoreException {
byte[] imageInMemory = new byte[(int) file.getSize()]; byte[] imageInMemory = new byte[(int) file.getSize()];
file.read(imageInMemory, 0, file.getSize()); file.read(imageInMemory, 0, file.getSize());