Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CommunicationArtifactViewerHelper.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.artifactviewers;
20 
21 import java.awt.Dimension;
22 import java.awt.Font;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.Toolkit;
27 import java.awt.datatransfer.StringSelection;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.util.ArrayList;
31 import java.util.List;
32 import javax.swing.JLabel;
33 import javax.swing.JMenuItem;
34 import javax.swing.JComponent;
35 import javax.swing.JPanel;
36 import javax.swing.JPopupMenu;
37 import javax.swing.JTextPane;
38 import javax.swing.SwingUtilities;
39 import org.openide.util.NbBundle;
41 
47 final class CommunicationArtifactViewerHelper {
48 
49  // Number of columns in the gridbag layout.
50  private final static int MAX_COLS = 4;
51 
52  final static int LEFT_INSET = 12;
53 
57  private CommunicationArtifactViewerHelper() {
58 
59  }
60 
71  static JLabel addHeader(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String headerString) {
72 
73  Insets savedInsets = constraints.insets;
74 
75  // create label for heading
76  javax.swing.JLabel headingLabel = new javax.swing.JLabel();
77 
78  // add a blank line before the start of new section, unless it's
79  // the first section
80  if (constraints.gridy != 0) {
81  addBlankLine(panel, gridbagLayout, constraints);
82  }
83  constraints.gridy++;
84  constraints.gridx = 0;
85 
86  // let the header span all of the row
87  constraints.gridwidth = MAX_COLS;
88  constraints.insets = new Insets(0, 0, 0, 0); // No inset for header
89 
90  // set text
91  headingLabel.setText(headerString);
92 
93  // make it large and bold
94  headingLabel.setFont(headingLabel.getFont().deriveFont(Font.BOLD, headingLabel.getFont().getSize() + 2));
95 
96  // add to panel
97  gridbagLayout.setConstraints(headingLabel, constraints);
98  panel.add(headingLabel);
99 
100  // reset constraints to normal
101  constraints.gridwidth = 1;
102 
103  // add line end glue
104  addLineEndGlue(panel, gridbagLayout, constraints);
105 
106  //restore insets
107  constraints.insets = savedInsets;
108 
109  return headingLabel;
110  }
111 
124  static void addNameValueRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString, String valueString) {
125  addKey(panel, gridbagLayout, constraints, keyString);
126  addValue(panel, gridbagLayout, constraints, valueString);
127  }
128 
139  static void addComponent(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, JComponent component) {
140 
141  // add to panel
142  gridbagLayout.setConstraints(component, constraints);
143  panel.add(component);
144  }
145 
154  static void addLineEndGlue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
155  // Place the filler just past the last column.
156  constraints.gridx = MAX_COLS;
157 
158  double savedWeightX = constraints.weightx;
159  int savedFill = constraints.fill;
160 
161  constraints.weightx = 1.0; // take up all the horizontal space
162  constraints.fill = GridBagConstraints.BOTH;
163 
164  javax.swing.Box.Filler horizontalFiller = new javax.swing.Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(32767, 0));
165  gridbagLayout.setConstraints(horizontalFiller, constraints);
166  panel.add(horizontalFiller);
167 
168  // restore fill & weight
169  constraints.fill = savedFill;
170  constraints.weightx = savedWeightX;
171  }
172 
181  static void addPageEndGlue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
182 
183  constraints.gridx = 0;
184 
185  double savedWeighty = constraints.weighty;
186  int savedFill = constraints.fill;
187 
188  constraints.weighty = 1.0; // take up all the vertical space
189  constraints.fill = GridBagConstraints.VERTICAL;
190 
191  javax.swing.Box.Filler vertFiller = new javax.swing.Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, 32767));
192  gridbagLayout.setConstraints(vertFiller, constraints);
193  panel.add(vertFiller, constraints);
194 
195  //Resore weight & fill
196  constraints.weighty = savedWeighty;
197  constraints.fill = savedFill;
198  }
199 
207  static void addBlankLine(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
208  constraints.gridy++;
209  constraints.gridx = 0;
210 
211  javax.swing.JLabel filler = new javax.swing.JLabel(" ");
212  gridbagLayout.setConstraints(filler, constraints);
213  panel.add(filler);
214 
215  addLineEndGlue(panel, gridbagLayout, constraints);
216  }
217 
228  static JLabel addKey(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString) {
229  return addKeyAtCol(panel, gridbagLayout, constraints, keyString, 0);
230  }
231 
243  static JLabel addKeyAtCol(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString, int gridx) {
244 
245  // create label
246  javax.swing.JLabel keyLabel = new javax.swing.JLabel();
247 
248  constraints.gridy++;
249  constraints.gridx = gridx < MAX_COLS - 1 ? gridx : MAX_COLS - 2;
250 
251  // set text
252  keyLabel.setText(keyString + ": ");
253 
254  // add to panel
255  gridbagLayout.setConstraints(keyLabel, constraints);
256  panel.add(keyLabel);
257 
258  return keyLabel;
259  }
260 
271  static JTextPane addValue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String valueString) {
272  return addValueAtCol(panel, gridbagLayout, constraints, valueString, 1);
273  }
274 
286  static JTextPane addValueAtCol(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String valueString, int gridx) {
287  // create label,
288  JTextPane valueField = new JTextPane();
289  valueField.setEditable(false);
290  valueField.setOpaque(false);
291 
292  constraints.gridx = gridx < MAX_COLS ? gridx : MAX_COLS - 1;
293 
294  GridBagConstraints cloneConstraints = (GridBagConstraints) constraints.clone();
295 
296  // let the value span 2 cols
297  cloneConstraints.gridwidth = 2;
298  cloneConstraints.fill = GridBagConstraints.BOTH;
299 
300  // set text
301  valueField.setText(valueString);
302 
303  // attach a right click menu with Copy option
304  valueField.addMouseListener(new java.awt.event.MouseAdapter() {
305  @Override
306  public void mouseClicked(java.awt.event.MouseEvent evt) {
307  valueLabelMouseClicked(evt, valueField);
308  }
309  });
310 
311  // add label to panel
312  gridbagLayout.setConstraints(valueField, cloneConstraints);
313  panel.add(valueField);
314 
315  // end the line
316  addLineEndGlue(panel, gridbagLayout, constraints);
317 
318  return valueField;
319  }
320 
333  static JLabel addMessageRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String messageString) {
334  return addMessageRow(panel, gridbagLayout, constraints, messageString, 0);
335  }
336 
349  static JLabel addMessageRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String messageString, int gridx) {
350 
351  // create label
352  javax.swing.JLabel messageLabel = new javax.swing.JLabel();
353 
354  constraints.gridy++;
355  constraints.gridx = gridx < MAX_COLS - 1 ? gridx : MAX_COLS - 2;
356 
357  int savedGridwidth = constraints.gridwidth;
358 
359  constraints.gridwidth = 3;
360 
361  // set text
362  messageLabel.setText(messageString);
363 
364  // add to panel
365  gridbagLayout.setConstraints(messageLabel, constraints);
366  panel.add(messageLabel);
367 
368  addLineEndGlue(panel, gridbagLayout, constraints);
369 
370  // restore constraints
371  constraints.gridwidth = savedGridwidth;
372 
373  return messageLabel;
374  }
375 
392  @NbBundle.Messages({
393  "CommunicationArtifactViewerHelper_persona_label=Persona: ",
394  "CommunicationArtifactViewerHelper_persona_searching=Searching...",
395  "CommunicationArtifactViewerHelper_persona_unknown=Unknown",
396  "CommunicationArtifactViewerHelper_persona_button_view=View",
397  "CommunicationArtifactViewerHelper_persona_button_create=Create"
398  })
399 
400  static List<AccountPersonaSearcherData> addPersonaRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String accountIdentifier) {
401  List<AccountPersonaSearcherData> dataList = new ArrayList<>();
402 
403  constraints.gridy++;
404  constraints.gridx = 1;
405 
406  Insets savedInsets = constraints.insets;
407 
408  // extra Indent in
409  constraints.insets = new java.awt.Insets(0, 2 * LEFT_INSET, 0, 0);
410 
411  // create label
412  javax.swing.JLabel personaLabel = new javax.swing.JLabel();
413  String personaLabelText = Bundle.CommunicationArtifactViewerHelper_persona_label();
414  personaLabelText = personaLabelText.concat(CentralRepository.isEnabled()
415  ? Bundle.CommunicationArtifactViewerHelper_persona_searching()
416  : Bundle.CommunicationArtifactViewerHelper_persona_unknown());
417 
418  personaLabel.setText(personaLabelText);
419 
420  // add to panel
421  gridbagLayout.setConstraints(personaLabel, constraints);
422  panel.add(personaLabel);
423 
424  // restore constraint
425  constraints.insets = savedInsets;
426 
427  constraints.gridx++;
428 
429  // Place a button as place holder. It will be enabled when persona is available.
430  javax.swing.JButton personaButton = new javax.swing.JButton();
431  personaButton.setText(Bundle.CommunicationArtifactViewerHelper_persona_button_view());
432  personaButton.setMargin(new Insets(0, 5, 0, 5));
433  personaButton.setEnabled(false);
434 
435  gridbagLayout.setConstraints(personaButton, constraints);
436  panel.add(personaButton);
437 
438  if (CentralRepository.isEnabled()) {
439  // kick off a task to find the persona for this account
440  dataList.add(new AccountPersonaSearcherData(accountIdentifier, personaLabel, personaButton));
441  } else {
442  personaLabel.setEnabled(false);
443  }
444 
445  addLineEndGlue(panel, gridbagLayout, constraints);
446 
447  return dataList;
448  }
449 
460  @NbBundle.Messages({
461  "# {0} - contact name",
462  "CommunicationArtifactViewerHelper_contact_label=Contact: {0}",
463  "CommunicationArtifactViewerHelper_contact_label_unknown=Unknown"
464  })
465  static JComponent addContactRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String contactId) {
466  // Increase the y value because we are not calling the addKey
467  constraints.gridy++;
468  //Don't change the origian constraints, just make a copy to modify
469  GridBagConstraints indentedConstraints = (GridBagConstraints) constraints.clone();
470 
471  // Add an indent to match persona labels
472  indentedConstraints.insets = new java.awt.Insets(0, 2 * LEFT_INSET, 0, 0);
473 
474  String contactInfo = Bundle.CommunicationArtifactViewerHelper_contact_label(contactId != null && !contactId.isEmpty() ? contactId : Bundle.CommunicationArtifactViewerHelper_contact_label_unknown());
475 
476  return addValueAtCol(panel, gridbagLayout, indentedConstraints, contactInfo, 1);
477  }
478 
486  @NbBundle.Messages({
487  "CommunicationArtifactViewerHelper_menuitem_copy=Copy"
488  })
489  private static void valueLabelMouseClicked(java.awt.event.MouseEvent evt, JTextPane valueLabel) {
490  if (SwingUtilities.isRightMouseButton(evt)) {
491  JPopupMenu popup = new JPopupMenu();
492 
493  JMenuItem copyMenu = new JMenuItem(Bundle.CommunicationArtifactViewerHelper_menuitem_copy()); // NON-NLS
494  copyMenu.addActionListener(new ActionListener() {
495  @Override
496  public void actionPerformed(ActionEvent e) {
497  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(valueLabel.getText()), null);
498  }
499  });
500 
501  popup.add(copyMenu);
502  popup.show(valueLabel, evt.getX(), evt.getY());
503  }
504  }
505 }

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.