Autopsy  4.7.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  ;
49  private Node currentNode;
50  private final boolean isMain;
51  private boolean listeningToTabbedPane = false;
52 
62  DataContentPanel(boolean isMain) {
63  this.isMain = isMain;
64  initComponents();
65 
66  // add all implementors of DataContentViewer and put them in the tabbed pane
67  Collection<? extends DataContentViewer> dcvs = Lookup.getDefault().lookupAll(DataContentViewer.class);
68  for (DataContentViewer factory : dcvs) {
70  if (isMain) {
71  //use the instance from Lookup for the main viewer
72  dcv = factory;
73  } else {
74  dcv = factory.createInstance();
75  }
76  viewers.add(new UpdateWrapper(dcv));
77  javax.swing.JScrollPane scrollTab = new javax.swing.JScrollPane(dcv.getComponent());
78  scrollTab.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
79  jTabbedPane1.addTab(dcv.getTitle(), null,
80  scrollTab, dcv.getToolTip());
81  }
82 
83  // disable the tabs
84  int numTabs = jTabbedPane1.getTabCount();
85  for (int tab = 0; tab < numTabs; ++tab) {
86  jTabbedPane1.setEnabledAt(tab, false);
87  }
88  }
89 
96  public static DataContentPanel createInstance() {
97  return new DataContentPanel(false);
98  }
99 
100  public JTabbedPane getTabPanels() {
101  return jTabbedPane1;
102  }
103 
109  @SuppressWarnings("unchecked")
110  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
111  private void initComponents() {
112 
113  jTabbedPane1 = new javax.swing.JTabbedPane();
114 
115  setMinimumSize(new java.awt.Dimension(5, 5));
116 
117  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
118  this.setLayout(layout);
119  layout.setHorizontalGroup(
120  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
121  .addComponent(jTabbedPane1)
122  );
123  layout.setVerticalGroup(
124  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
125  .addComponent(jTabbedPane1)
126  );
127  }// </editor-fold>//GEN-END:initComponents
128  // Variables declaration - do not modify//GEN-BEGIN:variables
129  private javax.swing.JTabbedPane jTabbedPane1;
130  // End of variables declaration//GEN-END:variables
131 
132  @Override
133  public void setNode(Node selectedNode) {
134  // change the cursor to "waiting cursor" for this operation
135  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
136  try {
137 
138  String defaultName = NbBundle.getMessage(DataContentTopComponent.class, "CTL_DataContentTopComponent");
139  // set the file path
140  if (selectedNode == null) {
141  setName(defaultName);
142  } else {
143  Content content = selectedNode.getLookup().lookup(Content.class);
144  if (content != null) {
145  //String path = DataConversion.getformattedPath(ContentUtils.getDisplayPath(selectedNode.getLookup().lookup(Content.class)), 0);
146  String path = defaultName;
147  try {
148  path = content.getUniquePath();
149  } catch (TskCoreException ex) {
150  logger.log(Level.SEVERE, "Exception while calling Content.getUniquePath() for {0}", content); //NON-NLS
151  }
152  setName(path);
153  } else {
154  setName(defaultName);
155  }
156  }
157 
158  currentNode = selectedNode;
159 
160  setupTabs(selectedNode);
161  } finally {
162  this.setCursor(null);
163  }
164  }
165 
172  public void setupTabs(Node selectedNode) {
173  // Deferring becoming a listener to the tabbed pane until this point
174  // eliminates handling a superfluous stateChanged event during construction.
175  if (listeningToTabbedPane == false) {
176  jTabbedPane1.addChangeListener(this);
177  listeningToTabbedPane = true;
178  }
179 
180  int currTabIndex = jTabbedPane1.getSelectedIndex();
181  int totalTabs = jTabbedPane1.getTabCount();
182  int maxPreferred = 0;
183  int preferredViewerIndex = 0;
184  for (int i = 0; i < totalTabs; ++i) {
185  UpdateWrapper dcv = viewers.get(i);
186  dcv.resetComponent();
187 
188  // disable an unsupported tab (ex: picture viewer)
189  if ((selectedNode == null) || (dcv.isSupported(selectedNode) == false)) {
190  jTabbedPane1.setEnabledAt(i, false);
191  } else {
192  jTabbedPane1.setEnabledAt(i, true);
193 
194  // remember the viewer with the highest preference value
195  int currentPreferred = dcv.isPreferred(selectedNode);
196  if (currentPreferred > maxPreferred) {
197  preferredViewerIndex = i;
198  maxPreferred = currentPreferred;
199  }
200  }
201  }
202 
203  // let the user decide if we should stay with the current viewer
204  int tabIndex = UserPreferences.keepPreferredContentViewer() ? currTabIndex : preferredViewerIndex;
205 
206  UpdateWrapper dcv = viewers.get(tabIndex);
207  // this is really only needed if no tabs were enabled
208  if (jTabbedPane1.isEnabledAt(tabIndex) == false) {
209  dcv.resetComponent();
210  } else {
211  dcv.setNode(selectedNode);
212  }
213 
214  // set the tab to the one the user wants, then set that viewer's node.
215  jTabbedPane1.setSelectedIndex(tabIndex);
216  jTabbedPane1.getSelectedComponent().repaint();
217  }
218 
219  @Override
220  public void propertyChange(PropertyChangeEvent evt) {
221  }
222 
223  @Override
224  public void stateChanged(ChangeEvent evt) {
225  JTabbedPane pane = (JTabbedPane) evt.getSource();
226 
227  // Get and set current selected tab
228  int currentTab = pane.getSelectedIndex();
229  if (currentTab != -1) {
230  UpdateWrapper dcv = viewers.get(currentTab);
231  if (dcv.isOutdated()) {
232  // change the cursor to "waiting cursor" for this operation
233  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
234  try {
235  dcv.setNode(currentNode);
236  } finally {
237  this.setCursor(null);
238  }
239  }
240  }
241  }
242 
243  private static class UpdateWrapper {
244 
245  private final DataContentViewer wrapped;
246  private boolean outdated;
247 
249  this.wrapped = wrapped;
250  this.outdated = true;
251  }
252 
253  void setNode(Node selectedNode) {
254  this.wrapped.setNode(selectedNode);
255  this.outdated = false;
256  }
257 
258  void resetComponent() {
259  this.wrapped.resetComponent();
260  this.outdated = true;
261  }
262 
263  boolean isOutdated() {
264  return this.outdated;
265  }
266 
267  boolean isSupported(Node node) {
268  return this.wrapped.isSupported(node);
269  }
270 
271  int isPreferred(Node node) {
272  return this.wrapped.isPreferred(node);
273  }
274  }
275 
276 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.