Autopsy  3.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  private Node currentNode;
48  private final boolean isMain;
49  private boolean listeningToTabbedPane = false;
50 
59  DataContentPanel(boolean isMain) {
60  this.isMain = isMain;
62 
63  // add all implementors of DataContentViewer and put them in the tabbed pane
64  Collection<? extends DataContentViewer> dcvs = Lookup.getDefault().lookupAll(DataContentViewer.class);
65  for (DataContentViewer factory : dcvs) {
67  if (isMain) {
68  //use the instance from Lookup for the main viewer
69  dcv = factory;
70  }
71  else {
72  dcv = factory.createInstance();
73  }
74  viewers.add(new UpdateWrapper(dcv));
75  jTabbedPane1.addTab(dcv.getTitle(), null,
76  dcv.getComponent(), dcv.getToolTip());
77  }
78 
79  // disable the tabs
80  int numTabs = jTabbedPane1.getTabCount();
81  for (int tab = 0; tab < numTabs; ++tab) {
82  jTabbedPane1.setEnabledAt(tab, false);
83  }
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 
136  String defaultName = NbBundle.getMessage(DataContentTopComponent.class, "CTL_DataContentTopComponent");
137  // set the file path
138  if (selectedNode == null) {
139  setName(defaultName);
140  } else {
141  Content content = selectedNode.getLookup().lookup(Content.class);
142  if (content != null) {
143  //String path = DataConversion.getformattedPath(ContentUtils.getDisplayPath(selectedNode.getLookup().lookup(Content.class)), 0);
144  String path = defaultName;
145  try {
146  path = content.getUniquePath();
147  } catch (TskCoreException ex) {
148  logger.log(Level.SEVERE, "Exception while calling Content.getUniquePath() for {0}", content); //NON-NLS
149  }
150  setName(path);
151  } else {
152  setName(defaultName);
153  }
154  }
155 
156  currentNode = selectedNode;
157 
158  setupTabs(selectedNode);
159  } finally {
160  this.setCursor(null);
161  }
162  }
163 
170  public void setupTabs(Node selectedNode) {
171  // Deferring becoming a listener to the tabbed pane until this point
172  // eliminates handling a superfluous stateChanged event during construction.
173  if (listeningToTabbedPane == false) {
174  jTabbedPane1.addChangeListener(this);
175  listeningToTabbedPane = true;
176  }
177 
178  int currTabIndex = jTabbedPane1.getSelectedIndex();
179  int totalTabs = jTabbedPane1.getTabCount();
180  int maxPreferred = 0;
181  int preferredViewerIndex = 0;
182  for (int i = 0; i < totalTabs; ++i) {
183  UpdateWrapper dcv = viewers.get(i);
184  dcv.resetComponent();
185 
186  // disable an unsupported tab (ex: picture viewer)
187  if ((selectedNode == null) || (dcv.isSupported(selectedNode) == false)) {
188  jTabbedPane1.setEnabledAt(i, false);
189  } else {
190  jTabbedPane1.setEnabledAt(i, true);
191 
192  // remember the viewer with the highest preference value
193  int currentPreferred = dcv.isPreferred(selectedNode);
194  if (currentPreferred > maxPreferred) {
195  preferredViewerIndex = i;
196  maxPreferred = currentPreferred;
197  }
198  }
199  }
200 
201  // let the user decide if we should stay with the current viewer
202  int tabIndex = UserPreferences.keepPreferredContentViewer() ? currTabIndex : preferredViewerIndex;
203 
204  UpdateWrapper dcv = viewers.get(tabIndex);
205  // this is really only needed if no tabs were enabled
206  if (jTabbedPane1.isEnabledAt(tabIndex) == false) {
207  dcv.resetComponent();
208  }
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  }
216 
217  @Override
218  public void propertyChange(PropertyChangeEvent evt) {
219  }
220 
221  @Override
222  public void stateChanged(ChangeEvent evt) {
223  JTabbedPane pane = (JTabbedPane) evt.getSource();
224 
225  // Get and set current selected tab
226  int currentTab = pane.getSelectedIndex();
227  if (currentTab != -1) {
228  UpdateWrapper dcv = viewers.get(currentTab);
229  if (dcv.isOutdated()) {
230  // change the cursor to "waiting cursor" for this operation
231  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
232  try {
233  dcv.setNode(currentNode);
234  } finally {
235  this.setCursor(null);
236  }
237  }
238  }
239  }
240 
241  private static class UpdateWrapper {
242 
244  private boolean outdated;
245 
247  this.wrapped = wrapped;
248  this.outdated = true;
249  }
250 
251  void setNode(Node selectedNode) {
252  this.wrapped.setNode(selectedNode);
253  this.outdated = false;
254  }
255 
256  void resetComponent() {
257  this.wrapped.resetComponent();
258  this.outdated = true;
259  }
260 
261  boolean isOutdated() {
262  return this.outdated;
263  }
264 
265  boolean isSupported(Node node) {
266  return this.wrapped.isSupported(node);
267  }
268 
269  int isPreferred(Node node) {
270  return this.wrapped.isPreferred(node);
271  }
272  }
273 
274 }
static Logger getLogger(String name)
Definition: Logger.java:131

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