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

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