Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DataContentPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.corecomponents;
20 
21 import java.awt.Cursor;
22 import java.beans.PropertyChangeEvent;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.logging.Level;
27 import javax.swing.JTabbedPane;
28 import javax.swing.event.ChangeEvent;
29 import javax.swing.event.ChangeListener;
30 import org.openide.nodes.Node;
31 import org.openide.util.Lookup;
32 import org.openide.util.NbBundle;
37 import org.sleuthkit.datamodel.Content;
38 import org.sleuthkit.datamodel.TskCoreException;
39 
43 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
44 public class DataContentPanel extends javax.swing.JPanel implements DataContent, ChangeListener {
45 
46  private static Logger logger = Logger.getLogger(DataContentPanel.class.getName());
47  private final List<UpdateWrapper> viewers = new ArrayList<>();
48  private Node currentNode;
49  private final boolean isMain;
50  private boolean listeningToTabbedPane = false;
51 
61  DataContentPanel(boolean isMain) {
62  this.isMain = isMain;
63  initComponents();
64 
65  // add all implementors of DataContentViewer and put them in the tabbed pane
66  Collection<? extends DataContentViewer> dcvs = Lookup.getDefault().lookupAll(DataContentViewer.class);
67  for (DataContentViewer factory : dcvs) {
69  if (isMain) {
70  //use the instance from Lookup for the main viewer
71  dcv = factory;
72  } else {
73  dcv = factory.createInstance();
74  }
75  viewers.add(new UpdateWrapper(dcv));
76  javax.swing.JScrollPane scrollTab = new javax.swing.JScrollPane(dcv.getComponent());
77  scrollTab.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
78  jTabbedPane1.addTab(dcv.getTitle(), null,
79  scrollTab, dcv.getToolTip());
80  }
81 
82  // disable the tabs
83  int numTabs = jTabbedPane1.getTabCount();
84  for (int tab = 0; tab < numTabs; ++tab) {
85  jTabbedPane1.setEnabledAt(tab, false);
86  }
87  }
88 
95  public static DataContentPanel createInstance() {
96  return new DataContentPanel(false);
97  }
98 
99  public JTabbedPane getTabPanels() {
100  return jTabbedPane1;
101  }
102 
108  @SuppressWarnings("unchecked")
109  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
110  private void initComponents() {
111 
112  jTabbedPane1 = new javax.swing.JTabbedPane();
113 
114  setMinimumSize(new java.awt.Dimension(5, 5));
115 
116  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
117  this.setLayout(layout);
118  layout.setHorizontalGroup(
119  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
120  .addComponent(jTabbedPane1)
121  );
122  layout.setVerticalGroup(
123  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
124  .addComponent(jTabbedPane1)
125  );
126  }// </editor-fold>//GEN-END:initComponents
127  // Variables declaration - do not modify//GEN-BEGIN:variables
128  private javax.swing.JTabbedPane jTabbedPane1;
129  // End of variables declaration//GEN-END:variables
130 
131  @Override
132  public void setNode(Node selectedNode) {
133  // change the cursor to "waiting cursor" for this operation
134  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
135  try {
136 
137  String defaultName = NbBundle.getMessage(DataContentTopComponent.class, "CTL_DataContentTopComponent");
138  // set the file path
139  if (selectedNode == null) {
140  setName(defaultName);
141  } else {
142  Content content = selectedNode.getLookup().lookup(Content.class);
143  if (content != null) {
144  //String path = DataConversion.getformattedPath(ContentUtils.getDisplayPath(selectedNode.getLookup().lookup(Content.class)), 0);
145  String path = defaultName;
146  try {
147  path = content.getUniquePath();
148  } catch (TskCoreException ex) {
149  logger.log(Level.SEVERE, "Exception while calling Content.getUniquePath() for {0}", content); //NON-NLS
150  }
151  setName(path);
152  } else {
153  setName(defaultName);
154  }
155  }
156 
157  currentNode = selectedNode;
158 
159  setupTabs(selectedNode);
160  } finally {
161  this.setCursor(null);
162  }
163  }
164 
171  public void setupTabs(Node selectedNode) {
172  // Deferring becoming a listener to the tabbed pane until this point
173  // eliminates handling a superfluous stateChanged event during construction.
174  if (listeningToTabbedPane == false) {
175  jTabbedPane1.addChangeListener(this);
176  listeningToTabbedPane = true;
177  }
178 
179  int currTabIndex = jTabbedPane1.getSelectedIndex();
180  int totalTabs = jTabbedPane1.getTabCount();
181  int maxPreferred = 0;
182  int preferredViewerIndex = 0;
183  for (int i = 0; i < totalTabs; ++i) {
184  UpdateWrapper dcv = viewers.get(i);
185  dcv.resetComponent();
186 
187  // disable an unsupported tab (ex: picture viewer)
188  if ((selectedNode == null) || (dcv.isSupported(selectedNode) == false)) {
189  jTabbedPane1.setEnabledAt(i, false);
190  } else {
191  jTabbedPane1.setEnabledAt(i, true);
192 
193  // remember the viewer with the highest preference value
194  int currentPreferred = dcv.isPreferred(selectedNode);
195  if (currentPreferred > maxPreferred) {
196  preferredViewerIndex = i;
197  maxPreferred = currentPreferred;
198  }
199  }
200  }
201 
202  // let the user decide if we should stay with the current viewer
203  int tabIndex = UserPreferences.keepPreferredContentViewer() ? currTabIndex : preferredViewerIndex;
204 
205  UpdateWrapper dcv = viewers.get(tabIndex);
206  // this is really only needed if no tabs were enabled
207  if (jTabbedPane1.isEnabledAt(tabIndex) == false) {
208  dcv.resetComponent();
209  } else {
210  dcv.setNode(selectedNode);
211  }
212 
213  // set the tab to the one the user wants, then set that viewer's node.
214  jTabbedPane1.setSelectedIndex(tabIndex);
215  jTabbedPane1.getSelectedComponent().repaint();
216  }
217 
218  @Override
219  public void propertyChange(PropertyChangeEvent evt) {
220  }
221 
222  @Override
223  public void stateChanged(ChangeEvent evt) {
224  JTabbedPane pane = (JTabbedPane) evt.getSource();
225 
226  // Get and set current selected tab
227  int currentTab = pane.getSelectedIndex();
228  if (currentTab != -1) {
229  UpdateWrapper dcv = viewers.get(currentTab);
230  if (dcv.isOutdated()) {
231  // change the cursor to "waiting cursor" for this operation
232  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
233  try {
234  dcv.setNode(currentNode);
235  } finally {
236  this.setCursor(null);
237  }
238  }
239  }
240  }
241 
242  private static class UpdateWrapper {
243 
244  private final DataContentViewer wrapped;
245  private boolean outdated;
246 
248  this.wrapped = wrapped;
249  this.outdated = true;
250  }
251 
252  void setNode(Node selectedNode) {
253  this.wrapped.setNode(selectedNode);
254  this.outdated = false;
255  }
256 
257  void resetComponent() {
258  this.wrapped.resetComponent();
259  this.outdated = true;
260  }
261 
262  boolean isOutdated() {
263  return this.outdated;
264  }
265 
266  boolean isSupported(Node node) {
267  return this.wrapped.isSupported(node);
268  }
269 
270  int isPreferred(Node node) {
271  return this.wrapped.isPreferred(node);
272  }
273  }
274 
275 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.