From 366e784a317f920960e75e16a5afd1e6ec4e6893 Mon Sep 17 00:00:00 2001 From: "U-BASIS\\dsmyda" Date: Wed, 5 Jun 2019 15:49:08 -0400 Subject: [PATCH] Made filechooser remember selection, fixed some enable/disable bugs, removed tooltips --- .../contentviewers/Bundle.properties-MERGED | 1 + .../contentviewers/MediaViewImagePanel.java | 112 +++++++++++------- .../imagetagging/ImageTagsGroup.java | 2 +- 3 files changed, 73 insertions(+), 42 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/contentviewers/Bundle.properties-MERGED b/Core/src/org/sleuthkit/autopsy/contentviewers/Bundle.properties-MERGED index 10bc33a767..2779cef39a 100755 --- a/Core/src/org/sleuthkit/autopsy/contentviewers/Bundle.properties-MERGED +++ b/Core/src/org/sleuthkit/autopsy/contentviewers/Bundle.properties-MERGED @@ -46,6 +46,7 @@ MediaViewImagePanel.errorLabel.OOMText=Could not load file into Media View: insu MediaViewImagePanel.errorLabel.text=Could not load file into Media View. MediaViewImagePanel.exportSaveText=Save MediaViewImagePanel.externalViewerButton.text=Open in External Viewer Ctrl+E +MediaViewImagePanel.fileChooserTitle=Choose a directory to save the image MediaViewImagePanel.successfulExport=Tagged image was successfully saved. MediaViewImagePanel.unsuccessfulExport=Unable to export tagged image to disk. MediaViewVideoPanel.pauseButton.text=\u25ba diff --git a/Core/src/org/sleuthkit/autopsy/contentviewers/MediaViewImagePanel.java b/Core/src/org/sleuthkit/autopsy/contentviewers/MediaViewImagePanel.java index a4bf52cfd4..e1c0e70281 100644 --- a/Core/src/org/sleuthkit/autopsy/contentviewers/MediaViewImagePanel.java +++ b/Core/src/org/sleuthkit/autopsy/contentviewers/MediaViewImagePanel.java @@ -65,6 +65,7 @@ import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JSeparator; import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; import org.apache.commons.io.FilenameUtils; import org.controlsfx.control.MaskerPane; import org.openide.util.NbBundle; @@ -121,6 +122,8 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan private final JMenuItem hideTags; private final JMenuItem exportTags; + private final JFileChooser exportChooser; + private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); private double zoomRatio; @@ -158,30 +161,31 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan public MediaViewImagePanel() { initComponents(); fxInited = org.sleuthkit.autopsy.core.Installer.isJavaFxInited(); + + exportChooser = new JFileChooser(); + exportChooser.setDialogTitle(Bundle.MediaViewImagePanel_fileChooserTitle()); + + //Build popupMenu when Tags Menu button is pressed. createTag = new JMenuItem("Create"); createTag.addActionListener((event) -> createTag()); - createTag.setToolTipText("You may drag anywhere on the image after selecting this option."); popupMenu.add(createTag); - popupMenu.add(new JSeparator()); // SEPARATOR + popupMenu.add(new JSeparator()); deleteTag = new JMenuItem("Delete"); deleteTag.addActionListener((event) -> deleteTag()); - deleteTag.setToolTipText("Delete the selected tag."); popupMenu.add(deleteTag); - popupMenu.add(new JSeparator()); // SEPARATOR + popupMenu.add(new JSeparator()); hideTags = new JMenuItem("Hide"); hideTags.addActionListener((event) -> showOrHideTags()); - hideTags.setToolTipText("Hide the tags on this image."); popupMenu.add(hideTags); - popupMenu.add(new JSeparator()); // SEPARATOR + popupMenu.add(new JSeparator()); exportTags = new JMenuItem("Export"); exportTags.addActionListener((event) -> exportTags()); - exportTags.setToolTipText("Save the image with tags applied."); popupMenu.add(exportTags); popupMenu.setPopupSize(300, 150); @@ -201,6 +205,8 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan } }); + //Respond to state events by enabling/disabling the correct + //buttons. pcs.addPropertyChangeListener((event) -> { State currentState = (State) event.getNewValue(); switch (currentState) { @@ -265,8 +271,17 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan //Update buttons when users select (or unselect) image tags. tagsGroup.addFocusChangeListener((event) -> { if (event.getPropertyName().equals(ImageTagControls.NOT_FOCUSED.getName())) { - pcs.firePropertyChange(new PropertyChangeEvent(this, + if (masterGroup.getChildren().contains(imageTagCreator)) { + return; + } + + if(tagsGroup.getChildren().isEmpty()) { + pcs.firePropertyChange(new PropertyChangeEvent(this, + "state", null, State.EMPTY)); + } else { + pcs.firePropertyChange(new PropertyChangeEvent(this, "state", null, State.CREATE)); + } } else if (event.getPropertyName().equals(ImageTagControls.FOCUSED.getName())) { pcs.firePropertyChange(new PropertyChangeEvent(this, "state", null, State.SELECTED)); @@ -374,9 +389,9 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan List> contentViewerTags = getContentViewerTags(tags); //Add all image tags tagsGroup = buildImageTagsGroup(contentViewerTags); - if(!tagsGroup.getChildren().isEmpty()) { + if (!tagsGroup.getChildren().isEmpty()) { pcs.firePropertyChange(new PropertyChangeEvent(this, - "state", null, State.NONEMPTY)); + "state", null, State.NONEMPTY)); } } catch (TskCoreException | NoCurrentCaseException ex) { LOGGER.log(Level.WARNING, "Could not retrieve image tags for file in case db", ex); //NON-NLS @@ -687,7 +702,8 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan }//GEN-LAST:event_formComponentResized /** - * + * Deletes the selected tag when the Delete button is pressed in the Tag + * Menu. */ private void deleteTag() { Platform.runLater(() -> { @@ -715,7 +731,8 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan } /** - * + * Enables create tag logic when the Create button is pressed in the Tags + * Menu. */ private void createTag() { pcs.firePropertyChange(new PropertyChangeEvent(this, @@ -791,15 +808,19 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan private ContentViewerTag storeImageTag(ImageTagRegion data, TagNameAndComment result) throws TskCoreException, SerializationException, NoCurrentCaseException { scrollPane.setCursor(Cursor.WAIT); - ContentTag contentTag = Case.getCurrentCaseThrows().getServices().getTagsManager() - .addContentTag(file, result.getTagName(), result.getComment()); - ContentViewerTag contentViewerTag = ContentViewerTagManager.saveTag(contentTag, data); - scrollPane.setCursor(Cursor.DEFAULT); - return contentViewerTag; + try { + ContentTag contentTag = Case.getCurrentCaseThrows().getServices().getTagsManager() + .addContentTag(file, result.getTagName(), result.getComment()); + ContentViewerTag contentViewerTag = ContentViewerTagManager.saveTag(contentTag, data); + return contentViewerTag; + } finally { + scrollPane.setCursor(Cursor.DEFAULT); + } } /** - * + * Hides or show tags when the Hide or Show button is pressed in the Tags + * Menu. */ private void showOrHideTags() { Platform.runLater(() -> { @@ -809,13 +830,13 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan hideTags.setText(DisplayOptions.SHOW_TAGS.getName()); tagsGroup.clearFocus(); pcs.firePropertyChange(new PropertyChangeEvent(this, - "state", null, State.HIDDEN)); + "state", null, State.HIDDEN)); } else { //Add tags group back in and update buttons masterGroup.getChildren().add(tagsGroup); hideTags.setText(DisplayOptions.HIDE_TAGS.getName()); - pcs.firePropertyChange(new PropertyChangeEvent(this, - "state", null, State.VISIBLE)); + pcs.firePropertyChange(new PropertyChangeEvent(this, + "state", null, State.VISIBLE)); } }); } @@ -823,31 +844,36 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan @NbBundle.Messages({ "MediaViewImagePanel.exportSaveText=Save", "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" }) private void exportTags() { tagsGroup.clearFocus(); - JFileChooser fileChooser = new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - int returnVal = fileChooser.showDialog(this, Bundle.MediaViewImagePanel_exportSaveText()); + exportChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + int returnVal = exportChooser.showDialog(this, Bundle.MediaViewImagePanel_exportSaveText()); if (returnVal == JFileChooser.APPROVE_OPTION) { - Platform.runLater(() -> { - try { - List tags = Case.getCurrentCase().getServices() - .getTagsManager().getContentTagsByContent(file); - List> contentViewerTags = getContentViewerTags(tags); - Collection regions = contentViewerTags.stream() - .map(cvTag -> cvTag.getDetails()).collect(Collectors.toList()); - byte[] jpgImage = ImageTagsUtil.exportTags(file, regions, ".jpg"); - Path output = Paths.get(fileChooser.getSelectedFile().getPath(), - FilenameUtils.getBaseName(file.getName()) + "-with_tags.jpg"); - Files.write(output, jpgImage); - JOptionPane.showMessageDialog(null, Bundle.MediaViewImagePanel_successfulExport()); - } catch (TskCoreException | NoCurrentCaseException | IOException ex) { - LOGGER.log(Level.WARNING, "Unable to export tagged image to disk", ex); //NON-NLS - JOptionPane.showMessageDialog(null, Bundle.MediaViewImagePanel_unsuccessfulExport()); + exportChooser.setCurrentDirectory(exportChooser.getSelectedFile()); + new SwingWorker() { + @Override + protected Void doInBackground() throws Exception { + try { + List tags = Case.getCurrentCase().getServices() + .getTagsManager().getContentTagsByContent(file); + List> contentViewerTags = getContentViewerTags(tags); + Collection regions = contentViewerTags.stream() + .map(cvTag -> cvTag.getDetails()).collect(Collectors.toList()); + byte[] jpgImage = ImageTagsUtil.exportTags(file, regions, ".jpg"); + Path output = Paths.get(exportChooser.getSelectedFile().getPath(), + FilenameUtils.getBaseName(file.getName()) + "-with_tags.jpg"); //NON-NLS + Files.write(output, jpgImage); + JOptionPane.showMessageDialog(null, Bundle.MediaViewImagePanel_successfulExport()); + } catch (TskCoreException | NoCurrentCaseException | IOException ex) { + LOGGER.log(Level.WARNING, "Unable to export tagged image to disk", ex); //NON-NLS + JOptionPane.showMessageDialog(null, Bundle.MediaViewImagePanel_unsuccessfulExport()); + } + return null; } - }); + }.execute(); } } @@ -873,6 +899,10 @@ class MediaViewImagePanel extends JPanel implements MediaFileViewer.MediaViewPan } } + /** + * Different states that the content viewer can be in. These states drive + * which buttons are enabled for tagging. + */ enum State { HIDDEN, VISIBLE, diff --git a/Core/src/org/sleuthkit/autopsy/contentviewers/imagetagging/ImageTagsGroup.java b/Core/src/org/sleuthkit/autopsy/contentviewers/imagetagging/ImageTagsGroup.java index e226c9f2a3..fcbbb145bd 100755 --- a/Core/src/org/sleuthkit/autopsy/contentviewers/imagetagging/ImageTagsGroup.java +++ b/Core/src/org/sleuthkit/autopsy/contentviewers/imagetagging/ImageTagsGroup.java @@ -52,11 +52,11 @@ public final class ImageTagsGroup extends Group { if (currentFocus != null) { currentFocus.getEventDispatcher().dispatchEvent( new Event(ImageTagControls.NOT_FOCUSED), NO_OP_CHAIN); - currentFocus = null; } this.pcs.firePropertyChange(new PropertyChangeEvent(this, ImageTagControls.NOT_FOCUSED.getName(), currentFocus, null)); + currentFocus = null; }); //Set the focus of selected tag