Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
StringsContentPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2019 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.contentviewers.textcontentviewer;
20 
21 import java.awt.Cursor;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.logging.Level;
27 import org.openide.util.NbBundle;
29 import javax.swing.JMenuItem;
30 import javax.swing.JOptionPane;
31 import org.apache.commons.lang3.StringUtils;
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 StringsContentPanel extends javax.swing.JPanel {
45 
46  private static long currentOffset = 0;
47  private static final long PAGE_LENGTH = 16384;
48  private static final long serialVersionUID = 1L;
49  private final byte[] data = new byte[(int) PAGE_LENGTH];
50  private static int currentPage = 1;
51  private Content dataSource;
52  //string extract utility
53  private final StringExtract stringExtract = new StringExtract();
54  private static final Logger logger = Logger.getLogger(StringsContentPanel.class.getName());
55 
60  initComponents();
61  customizeComponents();
62  this.resetDisplay();
63  }
64 
65  private void customizeComponents() {
66  outputViewPane.setComponentPopupMenu(rightClickMenu);
67  ActionListener actList = (ActionEvent e) -> {
68  JMenuItem jmi = (JMenuItem) e.getSource();
69  if (jmi.equals(copyMenuItem)) {
70  outputViewPane.copy();
71  } else if (jmi.equals(selectAllMenuItem)) {
72  outputViewPane.selectAll();
73  }
74  };
75  copyMenuItem.addActionListener(actList);
76  selectAllMenuItem.addActionListener(actList);
77 
78  List<SCRIPT> supportedScripts = StringExtract.getSupportedScripts();
79  supportedScripts.forEach((s) -> {
80  languageCombo.addItem(s);
81  });
82 
83  // use wrap layout for better component wrapping
84  WrapLayout layout = new WrapLayout(0,5);
85  layout.setOppositeAligned(Arrays.asList(panelScriptSelect));
86  controlPanel.setLayout(layout);
87 
88  }
89 
90  final void resetDisplay() {
91  // clear / reset the fields
92  currentPage = 1;
93  currentOffset = 0;
94  this.dataSource = null;
95  currentPageLabel.setText("");
96  totalPageLabel.setText("");
97  prevPageButton.setEnabled(false);
98  nextPageButton.setEnabled(false);
99  outputViewPane.setText(""); // reset the output view
100  setComponentsVisibility(false); // hides the components that not needed
101  }
102 
108  @SuppressWarnings("unchecked")
109  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
110  private void initComponents() {
111 
112  rightClickMenu = new javax.swing.JPopupMenu();
113  copyMenuItem = new javax.swing.JMenuItem();
114  selectAllMenuItem = new javax.swing.JMenuItem();
115  controlPanel = new javax.swing.JPanel();
116  javax.swing.JPanel panelPageOfCount = new javax.swing.JPanel();
117  pageLabel = new javax.swing.JLabel();
118  javax.swing.JSeparator jSepMed1 = new javax.swing.JSeparator();
119  currentPageLabel = new javax.swing.JLabel();
120  javax.swing.JSeparator jSepMed2 = new javax.swing.JSeparator();
121  ofLabel = new javax.swing.JLabel();
122  javax.swing.JSeparator jSepMed3 = new javax.swing.JSeparator();
123  totalPageLabel = new javax.swing.JLabel();
124  javax.swing.JSeparator jSepMed4 = new javax.swing.JSeparator();
125  javax.swing.JPanel panelPageNextPrevButton = new javax.swing.JPanel();
126  pageLabel2 = new javax.swing.JLabel();
127  javax.swing.JSeparator jSepMed5 = new javax.swing.JSeparator();
128  prevPageButton = new javax.swing.JButton();
129  nextPageButton = new javax.swing.JButton();
130  javax.swing.JSeparator jSepMed6 = new javax.swing.JSeparator();
131  javax.swing.JPanel panelGoToPage = new javax.swing.JPanel();
132  goToPageLabel = new javax.swing.JLabel();
133  javax.swing.JSeparator jSepSm1 = new javax.swing.JSeparator();
134  goToPageTextField = new javax.swing.JTextField();
135  javax.swing.JSeparator jSepMed7 = new javax.swing.JSeparator();
136  panelScriptSelect = new javax.swing.JPanel();
137  languageLabel = new javax.swing.JLabel();
138  javax.swing.JSeparator jSepSm2 = new javax.swing.JSeparator();
139  languageCombo = new javax.swing.JComboBox<>();
140  outputScrollPane = new javax.swing.JScrollPane();
141  outputViewPane = new javax.swing.JTextPane();
142 
143  copyMenuItem.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.copyMenuItem.text")); // NOI18N
144  rightClickMenu.add(copyMenuItem);
145 
146  selectAllMenuItem.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.selectAllMenuItem.text")); // NOI18N
147  rightClickMenu.add(selectAllMenuItem);
148 
149  setMinimumSize(new java.awt.Dimension(250, 5));
150  setPreferredSize(new java.awt.Dimension(250, 58));
151  setLayout(new java.awt.BorderLayout());
152 
153  controlPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));
154 
155  panelPageOfCount.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
156 
157  pageLabel.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.pageLabel.text_1")); // NOI18N
158  pageLabel.setMaximumSize(new java.awt.Dimension(33, 25));
159  pageLabel.setMinimumSize(new java.awt.Dimension(33, 25));
160  pageLabel.setPreferredSize(new java.awt.Dimension(32, 25));
161  panelPageOfCount.add(pageLabel);
162 
163  jSepMed1.setPreferredSize(new java.awt.Dimension(5, 0));
164  panelPageOfCount.add(jSepMed1);
165 
166  currentPageLabel.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.currentPageLabel.text_1")); // NOI18N
167  currentPageLabel.setMaximumSize(new java.awt.Dimension(18, 25));
168  currentPageLabel.setMinimumSize(new java.awt.Dimension(7, 25));
169  currentPageLabel.setPreferredSize(new java.awt.Dimension(18, 25));
170  panelPageOfCount.add(currentPageLabel);
171 
172  jSepMed2.setPreferredSize(new java.awt.Dimension(5, 0));
173  panelPageOfCount.add(jSepMed2);
174 
175  ofLabel.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.ofLabel.text_1")); // NOI18N
176  ofLabel.setMaximumSize(new java.awt.Dimension(11, 25));
177  ofLabel.setMinimumSize(new java.awt.Dimension(11, 25));
178  ofLabel.setPreferredSize(new java.awt.Dimension(11, 25));
179  panelPageOfCount.add(ofLabel);
180 
181  jSepMed3.setPreferredSize(new java.awt.Dimension(5, 0));
182  panelPageOfCount.add(jSepMed3);
183 
184  totalPageLabel.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.totalPageLabel.text_1")); // NOI18N
185  totalPageLabel.setMaximumSize(new java.awt.Dimension(21, 25));
186  totalPageLabel.setMinimumSize(new java.awt.Dimension(21, 25));
187  totalPageLabel.setPreferredSize(new java.awt.Dimension(21, 25));
188  panelPageOfCount.add(totalPageLabel);
189 
190  jSepMed4.setPreferredSize(new java.awt.Dimension(5, 0));
191  panelPageOfCount.add(jSepMed4);
192 
193  controlPanel.add(panelPageOfCount);
194 
195  panelPageNextPrevButton.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
196 
197  pageLabel2.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.pageLabel2.text")); // NOI18N
198  pageLabel2.setMaximumSize(new java.awt.Dimension(29, 25));
199  pageLabel2.setMinimumSize(new java.awt.Dimension(29, 25));
200  pageLabel2.setPreferredSize(new java.awt.Dimension(29, 25));
201  panelPageNextPrevButton.add(pageLabel2);
202 
203  jSepMed5.setPreferredSize(new java.awt.Dimension(5, 0));
204  panelPageNextPrevButton.add(jSepMed5);
205 
206  prevPageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_back.png"))); // NOI18N
207  prevPageButton.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.prevPageButton.text")); // NOI18N
208  prevPageButton.setBorderPainted(false);
209  prevPageButton.setContentAreaFilled(false);
210  prevPageButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_back_disabled.png"))); // NOI18N
211  prevPageButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
212  prevPageButton.setMaximumSize(new java.awt.Dimension(25, 25));
213  prevPageButton.setMinimumSize(new java.awt.Dimension(20, 25));
214  prevPageButton.setPreferredSize(new java.awt.Dimension(25, 25));
215  prevPageButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_back_hover.png"))); // NOI18N
216  prevPageButton.addActionListener(new java.awt.event.ActionListener() {
217  public void actionPerformed(java.awt.event.ActionEvent evt) {
218  prevPageButtonActionPerformed(evt);
219  }
220  });
221  panelPageNextPrevButton.add(prevPageButton);
222 
223  nextPageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_forward.png"))); // NOI18N
224  nextPageButton.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.nextPageButton.text")); // NOI18N
225  nextPageButton.setBorderPainted(false);
226  nextPageButton.setContentAreaFilled(false);
227  nextPageButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_forward_disabled.png"))); // NOI18N
228  nextPageButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
229  nextPageButton.setMaximumSize(new java.awt.Dimension(25, 25));
230  nextPageButton.setMinimumSize(new java.awt.Dimension(20, 25));
231  nextPageButton.setPreferredSize(new java.awt.Dimension(25, 25));
232  nextPageButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_forward_hover.png"))); // NOI18N
233  nextPageButton.addActionListener(new java.awt.event.ActionListener() {
234  public void actionPerformed(java.awt.event.ActionEvent evt) {
235  nextPageButtonActionPerformed(evt);
236  }
237  });
238  panelPageNextPrevButton.add(nextPageButton);
239 
240  jSepMed6.setPreferredSize(new java.awt.Dimension(5, 0));
241  panelPageNextPrevButton.add(jSepMed6);
242 
243  controlPanel.add(panelPageNextPrevButton);
244 
245  panelGoToPage.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
246 
247  goToPageLabel.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.goToPageLabel.text")); // NOI18N
248  panelGoToPage.add(goToPageLabel);
249 
250  jSepSm1.setPreferredSize(new java.awt.Dimension(2, 0));
251  panelGoToPage.add(jSepSm1);
252 
253  goToPageTextField.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.goToPageTextField.text")); // NOI18N
254  goToPageTextField.setMaximumSize(new java.awt.Dimension(2147483647, 25));
255  goToPageTextField.setMinimumSize(new java.awt.Dimension(50, 25));
256  goToPageTextField.setPreferredSize(new java.awt.Dimension(70, 25));
257  goToPageTextField.addActionListener(new java.awt.event.ActionListener() {
258  public void actionPerformed(java.awt.event.ActionEvent evt) {
259  goToPageTextFieldActionPerformed(evt);
260  }
261  });
262  panelGoToPage.add(goToPageTextField);
263 
264  jSepMed7.setPreferredSize(new java.awt.Dimension(5, 0));
265  panelGoToPage.add(jSepMed7);
266 
267  controlPanel.add(panelGoToPage);
268 
269  panelScriptSelect.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
270 
271  languageLabel.setText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.languageLabel.text")); // NOI18N
272  languageLabel.setToolTipText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.languageLabel.toolTipText")); // NOI18N
273  panelScriptSelect.add(languageLabel);
274 
275  jSepSm2.setPreferredSize(new java.awt.Dimension(2, 0));
276  panelScriptSelect.add(jSepSm2);
277 
278  languageCombo.setToolTipText(org.openide.util.NbBundle.getMessage(StringsContentPanel.class, "StringsContentPanel.languageCombo.toolTipText")); // NOI18N
279  languageCombo.setMinimumSize(new java.awt.Dimension(150, 25));
280  languageCombo.setPreferredSize(new java.awt.Dimension(150, 25));
281  languageCombo.addActionListener(new java.awt.event.ActionListener() {
282  public void actionPerformed(java.awt.event.ActionEvent evt) {
283  languageComboActionPerformed(evt);
284  }
285  });
286  panelScriptSelect.add(languageCombo);
287 
288  controlPanel.add(panelScriptSelect);
289 
290  add(controlPanel, java.awt.BorderLayout.NORTH);
291 
292  outputViewPane.setEditable(false);
293  outputScrollPane.setViewportView(outputViewPane);
294 
295  add(outputScrollPane, java.awt.BorderLayout.CENTER);
296  }// </editor-fold>//GEN-END:initComponents
297 
298  private void languageComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_languageComboActionPerformed
299  if (dataSource != null) {
300  setDataView(dataSource, currentOffset);
301  }
302  }//GEN-LAST:event_languageComboActionPerformed
303 
304  private void goToPageTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goToPageTextFieldActionPerformed
305  String pageNumberStr = goToPageTextField.getText();
306  int pageNumber;
307  int maxPage = Math.round((dataSource.getSize() - 1) / PAGE_LENGTH) + 1;
308  try {
309  pageNumber = Integer.parseInt(pageNumberStr);
310  } catch (NumberFormatException ex) {
311  pageNumber = maxPage + 1;
312  }
313  if (pageNumber > maxPage || pageNumber < 1) {
314  JOptionPane.showMessageDialog(this,
315  NbBundle.getMessage(this.getClass(),
316  "StringsTextViewer.goToPageTextField.msgDlg",
317  maxPage),
318  NbBundle.getMessage(this.getClass(),
319  "StringsTextViewer.goToPageTextField.err"),
320  JOptionPane.WARNING_MESSAGE);
321  return;
322  }
323  currentOffset = (pageNumber - 1) * PAGE_LENGTH;
324  currentPage = pageNumber;
325  currentPageLabel.setText(Integer.toString(currentPage));
326  setDataView(dataSource, currentOffset);
327  }//GEN-LAST:event_goToPageTextFieldActionPerformed
328 
329  private void prevPageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevPageButtonActionPerformed
330  //@@@ this is part of the code dealing with the data viewer. could be copied/removed to implement the scrollbar
331  currentOffset -= PAGE_LENGTH;
332  currentPage -= 1;
333  currentPageLabel.setText(Integer.toString(currentPage));
334  setDataView(dataSource, currentOffset);
335  }//GEN-LAST:event_prevPageButtonActionPerformed
336 
337  private void nextPageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextPageButtonActionPerformed
338  //@@@ this is part of the code dealing with the data viewer. could be copied/removed to implement the scrollbar
339  currentOffset += PAGE_LENGTH;
340  currentPage += 1;
341  currentPageLabel.setText(Integer.toString(currentPage));
342  setDataView(dataSource, currentOffset);
343  }//GEN-LAST:event_nextPageButtonActionPerformed
344 
345  // Variables declaration - do not modify//GEN-BEGIN:variables
346  private javax.swing.JPanel controlPanel;
347  private javax.swing.JMenuItem copyMenuItem;
348  private javax.swing.JLabel currentPageLabel;
349  private javax.swing.JLabel goToPageLabel;
350  private javax.swing.JTextField goToPageTextField;
351  private javax.swing.JComboBox<SCRIPT> languageCombo;
352  private javax.swing.JLabel languageLabel;
353  private javax.swing.JButton nextPageButton;
354  private javax.swing.JLabel ofLabel;
355  private javax.swing.JScrollPane outputScrollPane;
356  private javax.swing.JTextPane outputViewPane;
357  private javax.swing.JLabel pageLabel;
358  private javax.swing.JLabel pageLabel2;
359  private javax.swing.JPanel panelScriptSelect;
360  private javax.swing.JButton prevPageButton;
361  private javax.swing.JPopupMenu rightClickMenu;
362  private javax.swing.JMenuItem selectAllMenuItem;
363  private javax.swing.JLabel totalPageLabel;
364  // End of variables declaration//GEN-END:variables
365 
372  void setDataView(Content dataSource, long offset) {
373  if (dataSource == null) {
374  return;
375  }
376  // change the cursor to "waiting cursor" for this operation
377  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
378  this.dataSource = dataSource;
379  int bytesRead = 0;
380  // set the data on the bottom and show it
381 
382  if (dataSource.getSize() > 0) {
383  try {
384  bytesRead = dataSource.read(data, offset, PAGE_LENGTH); // read the data
385  } catch (TskCoreException ex) {
386  logger.log(Level.WARNING, "Error while trying to show the String content.", ex); //NON-NLS
387  }
388  }
389  String text;
390  if (bytesRead > 0) {
391  //text = DataConversion.getString(data, bytesRead, 4);
392  final SCRIPT selScript = (SCRIPT) languageCombo.getSelectedItem();
393  stringExtract.setEnabledScript(selScript);
394  StringExtractResult res = stringExtract.extract(data, bytesRead, 0);
395  text = res.getText();
396  if (StringUtils.isBlank(text)) {
397  text = NbBundle.getMessage(this.getClass(),
398  "StringsTextViewer.setDataView.errorNoText", currentOffset,
399  currentOffset + PAGE_LENGTH);
400  }
401  } else {
402  text = NbBundle.getMessage(this.getClass(), "StringsTextViewer.setDataView.errorText", currentOffset,
403  currentOffset + PAGE_LENGTH);
404  }
405 
406  // disable or enable the next button
407  if (offset + PAGE_LENGTH < dataSource.getSize()) {
408  nextPageButton.setEnabled(true);
409  } else {
410  nextPageButton.setEnabled(false);
411  }
412 
413  if (offset == 0) {
414  prevPageButton.setEnabled(false);
415  currentPage = 1; // reset the page number
416  } else {
417  prevPageButton.setEnabled(true);
418  }
419 
420  int totalPage = Math.round((dataSource.getSize() - 1) / PAGE_LENGTH) + 1;
421  totalPageLabel.setText(Integer.toString(totalPage));
422  currentPageLabel.setText(Integer.toString(currentPage));
423  outputViewPane.setText(text); // set the output view
424  setComponentsVisibility(true); // shows the components that not needed
425  outputViewPane.moveCaretPosition(0);
426 
427  this.setCursor(null);
428  }
429 
430  void setDataView(StringContent dataSource) {
431  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
432  try {
433  this.dataSource = null;
434 
435  // set the data on the bottom and show it
436  String text = dataSource.getString();
437  nextPageButton.setEnabled(false);
438  prevPageButton.setEnabled(false);
439  currentPage = 1;
440 
441  int totalPage = 1;
442  totalPageLabel.setText(Integer.toString(totalPage));
443  currentPageLabel.setText(Integer.toString(currentPage));
444  outputViewPane.setText(text); // set the output view
445  setComponentsVisibility(true); // shows the components that not needed
446  outputViewPane.moveCaretPosition(0);
447  } finally {
448  this.setCursor(null);
449  }
450  }
451 
457  private void setComponentsVisibility(boolean isVisible) {
458  currentPageLabel.setVisible(isVisible);
459  totalPageLabel.setVisible(isVisible);
460  ofLabel.setVisible(isVisible);
461  prevPageButton.setVisible(isVisible);
462  nextPageButton.setVisible(isVisible);
463  pageLabel.setVisible(isVisible);
464  pageLabel2.setVisible(isVisible);
465  goToPageTextField.setVisible(isVisible);
466  goToPageLabel.setVisible(isVisible);
467  languageCombo.setVisible(isVisible);
468  languageLabel.setVisible(isVisible);
469  }
470 
471 }
StringExtractResult extract(byte[] buff, int len, int offset)
void setOppositeAligned(Collection< Component > oppAlignedComponents)
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.