Autopsy  4.16.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 
122  static void addComponent(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, JComponent component) {
123 
124  // add to panel
125  gridbagLayout.setConstraints(component, constraints);
126  panel.add(component);
127  }
128 
137  static void addLineEndGlue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
138  // Place the filler just past the last column.
139  constraints.gridx = MAX_COLS;
140 
141  double savedWeightX = constraints.weightx;
142  int savedFill = constraints.fill;
143 
144  constraints.weightx = 1.0; // take up all the horizontal space
145  constraints.fill = GridBagConstraints.BOTH;
146 
147  javax.swing.Box.Filler horizontalFiller = new javax.swing.Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(32767, 0));
148  gridbagLayout.setConstraints(horizontalFiller, constraints);
149  panel.add(horizontalFiller);
150 
151  // restore fill & weight
152  constraints.fill = savedFill;
153  constraints.weightx = savedWeightX;
154  }
155 
164  static void addPageEndGlue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
165 
166  constraints.gridx = 0;
167 
168  double savedWeighty = constraints.weighty;
169  int savedFill = constraints.fill;
170 
171  constraints.weighty = 1.0; // take up all the vertical space
172  constraints.fill = GridBagConstraints.VERTICAL;
173 
174  javax.swing.Box.Filler vertFiller = new javax.swing.Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, 32767));
175  gridbagLayout.setConstraints(vertFiller, constraints);
176  panel.add(vertFiller, constraints);
177 
178  //Resore weight & fill
179  constraints.weighty = savedWeighty;
180  constraints.fill = savedFill;
181  }
182 
190  static void addBlankLine(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
191  constraints.gridy++;
192  constraints.gridx = 0;
193 
194  javax.swing.JLabel filler = new javax.swing.JLabel(" ");
195  gridbagLayout.setConstraints(filler, constraints);
196  panel.add(filler);
197 
198  addLineEndGlue(panel, gridbagLayout, constraints);
199  }
200 
211  static JLabel addKey(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString) {
212  return addKeyAtCol(panel, gridbagLayout, constraints, keyString, 0);
213  }
214 
226  static JLabel addKeyAtCol(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString, int gridx) {
227 
228  // create label
229  javax.swing.JLabel keyLabel = new javax.swing.JLabel();
230 
231  constraints.gridy++;
232  constraints.gridx = gridx < MAX_COLS - 1 ? gridx : MAX_COLS - 2;
233 
234  // set text
235  keyLabel.setText(keyString + ": ");
236 
237  // add to panel
238  gridbagLayout.setConstraints(keyLabel, constraints);
239  panel.add(keyLabel);
240 
241  return keyLabel;
242  }
243 
254  static JTextPane addValue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String valueString) {
255  return addValueAtCol(panel, gridbagLayout, constraints, valueString, 1);
256  }
257 
269  static JTextPane addValueAtCol(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String valueString, int gridx) {
270  // create label,
271  JTextPane valueField = new JTextPane();
272  valueField.setEditable(false);
273  valueField.setOpaque(false);
274 
275  constraints.gridx = gridx < MAX_COLS ? gridx : MAX_COLS - 1;
276 
277  GridBagConstraints cloneConstraints = (GridBagConstraints) constraints.clone();
278 
279  // let the value span 2 cols
280  cloneConstraints.gridwidth = 2;
281  cloneConstraints.fill = GridBagConstraints.BOTH;
282 
283  // set text
284  valueField.setText(valueString);
285 
286  // attach a right click menu with Copy option
287  valueField.addMouseListener(new java.awt.event.MouseAdapter() {
288  @Override
289  public void mouseClicked(java.awt.event.MouseEvent evt) {
290  valueLabelMouseClicked(evt, valueField);
291  }
292  });
293 
294  // add label to panel
295  gridbagLayout.setConstraints(valueField, cloneConstraints);
296  panel.add(valueField);
297 
298  // end the line
299  addLineEndGlue(panel, gridbagLayout, constraints);
300 
301  return valueField;
302  }
303 
316  static JLabel addMessageRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String messageString) {
317  return addMessageRow(panel, gridbagLayout, constraints, messageString, 0);
318  }
319 
332  static JLabel addMessageRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String messageString, int gridx) {
333 
334  // create label
335  javax.swing.JLabel messageLabel = new javax.swing.JLabel();
336 
337  constraints.gridy++;
338  constraints.gridx = gridx < MAX_COLS - 1 ? gridx : MAX_COLS - 2;
339 
340  int savedGridwidth = constraints.gridwidth;
341 
342  constraints.gridwidth = 3;
343 
344  // set text
345  messageLabel.setText(messageString);
346 
347  // add to panel
348  gridbagLayout.setConstraints(messageLabel, constraints);
349  panel.add(messageLabel);
350 
351  addLineEndGlue(panel, gridbagLayout, constraints);
352 
353  // restore constraints
354  constraints.gridwidth = savedGridwidth;
355 
356  return messageLabel;
357  }
358 
375  @NbBundle.Messages({
376  "CommunicationArtifactViewerHelper_persona_label=Persona: ",
377  "CommunicationArtifactViewerHelper_persona_searching=Searching...",
378  "CommunicationArtifactViewerHelper_persona_unknown=Unknown",
379  "CommunicationArtifactViewerHelper_persona_button_view=View",
380  "CommunicationArtifactViewerHelper_persona_button_create=Create"
381  })
382 
383  static List<AccountPersonaSearcherData> addPersonaRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String accountIdentifier) {
384  List<AccountPersonaSearcherData> dataList = new ArrayList<>();
385 
386  constraints.gridy++;
387  constraints.gridx = 1;
388 
389  Insets savedInsets = constraints.insets;
390 
391  // extra Indent in
392  constraints.insets = new java.awt.Insets(0, 2 * LEFT_INSET, 0, 0);
393 
394  // create label
395  javax.swing.JLabel personaLabel = new javax.swing.JLabel();
396  String personaLabelText = Bundle.CommunicationArtifactViewerHelper_persona_label();
397  personaLabelText = personaLabelText.concat(CentralRepository.isEnabled()
398  ? Bundle.CommunicationArtifactViewerHelper_persona_searching()
399  : Bundle.CommunicationArtifactViewerHelper_persona_unknown());
400 
401  personaLabel.setText(personaLabelText);
402 
403  // add to panel
404  gridbagLayout.setConstraints(personaLabel, constraints);
405  panel.add(personaLabel);
406 
407  // restore constraint
408  constraints.insets = savedInsets;
409 
410  constraints.gridx++;
411 
412  // Place a button as place holder. It will be enabled when persona is available.
413  javax.swing.JButton personaButton = new javax.swing.JButton();
414  personaButton.setText(Bundle.CommunicationArtifactViewerHelper_persona_button_view());
415  personaButton.setMargin(new Insets(0, 5, 0, 5));
416  personaButton.setEnabled(false);
417 
418  gridbagLayout.setConstraints(personaButton, constraints);
419  panel.add(personaButton);
420 
421  if (CentralRepository.isEnabled()) {
422  // kick off a task to find the persona for this account
423  dataList.add(new AccountPersonaSearcherData(accountIdentifier, personaLabel, personaButton));
424  } else {
425  personaLabel.setEnabled(false);
426  }
427 
428  addLineEndGlue(panel, gridbagLayout, constraints);
429 
430  return dataList;
431  }
432 
443  @NbBundle.Messages({
444  "# {0} - contact name",
445  "CommunicationArtifactViewerHelper_contact_label=Contact: {0}",
446  "CommunicationArtifactViewerHelper_contact_label_unknown=Unknown"
447  })
448  static JComponent addContactRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String contactId) {
449  // Increase the y value because we are not calling the addKey
450  constraints.gridy++;
451  //Don't change the origian constraints, just make a copy to modify
452  GridBagConstraints indentedConstraints = (GridBagConstraints) constraints.clone();
453 
454  // Add an indent to match persona labels
455  indentedConstraints.insets = new java.awt.Insets(0, 2 * LEFT_INSET, 0, 0);
456 
457  String contactInfo = Bundle.CommunicationArtifactViewerHelper_contact_label(contactId != null && !contactId.isEmpty() ? contactId : Bundle.CommunicationArtifactViewerHelper_contact_label_unknown());
458 
459  return addValueAtCol(panel, gridbagLayout, indentedConstraints, contactInfo, 1);
460  }
461 
469  @NbBundle.Messages({
470  "CommunicationArtifactViewerHelper_menuitem_copy=Copy"
471  })
472  private static void valueLabelMouseClicked(java.awt.event.MouseEvent evt, JTextPane valueLabel) {
473  if (SwingUtilities.isRightMouseButton(evt)) {
474  JPopupMenu popup = new JPopupMenu();
475 
476  JMenuItem copyMenu = new JMenuItem(Bundle.CommunicationArtifactViewerHelper_menuitem_copy()); // NON-NLS
477  copyMenu.addActionListener(new ActionListener() {
478  @Override
479  public void actionPerformed(ActionEvent e) {
480  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(valueLabel.getText()), null);
481  }
482  });
483 
484  popup.add(copyMenu);
485  popup.show(valueLabel, evt.getX(), evt.getY());
486  }
487  }
488 }

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.