Autopsy  4.15.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.SwingUtilities;
38 import org.openide.util.NbBundle;
40 
46 final class CommunicationArtifactViewerHelper {
47 
48  // Number of columns in the gridbag layout.
49  private final static int MAX_COLS = 4;
50 
51  final static int LEFT_INSET = 12;
52 
56  private CommunicationArtifactViewerHelper() {
57 
58  }
59 
70  static JLabel addHeader(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String headerString) {
71 
72  Insets savedInsets = constraints.insets;
73 
74  // create label for heading
75  javax.swing.JLabel headingLabel = new javax.swing.JLabel();
76 
77  // add a blank line before the start of new section, unless it's
78  // the first section
79  if (constraints.gridy != 0) {
80  addBlankLine(panel, gridbagLayout, constraints);
81  }
82  constraints.gridy++;
83  constraints.gridx = 0;
84 
85  // let the header span all of the row
86  constraints.gridwidth = MAX_COLS;
87  constraints.insets = new Insets(0, 0, 0, 0); // No inset for header
88 
89  // set text
90  headingLabel.setText(headerString);
91 
92  // make it large and bold
93  headingLabel.setFont(headingLabel.getFont().deriveFont(Font.BOLD, headingLabel.getFont().getSize() + 2));
94 
95  // add to panel
96  gridbagLayout.setConstraints(headingLabel, constraints);
97  panel.add(headingLabel);
98 
99  // reset constraints to normal
100  constraints.gridwidth = 1;
101 
102  // add line end glue
103  addLineEndGlue(panel, gridbagLayout, constraints);
104 
105  //restore insets
106  constraints.insets = savedInsets;
107 
108  return headingLabel;
109  }
110 
121  static void addComponent(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, JComponent component) {
122 
123  // add to panel
124  gridbagLayout.setConstraints(component, constraints);
125  panel.add(component);
126  }
127 
136  static void addLineEndGlue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
137  // Place the filler just past the last column.
138  constraints.gridx = MAX_COLS;
139 
140  double savedWeightX = constraints.weightx;
141  int savedFill = constraints.fill;
142 
143  constraints.weightx = 1.0; // take up all the horizontal space
144  constraints.fill = GridBagConstraints.BOTH;
145 
146  javax.swing.Box.Filler horizontalFiller = new javax.swing.Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(32767, 0));
147  gridbagLayout.setConstraints(horizontalFiller, constraints);
148  panel.add(horizontalFiller);
149 
150  // restore fill & weight
151  constraints.fill = savedFill;
152  constraints.weightx = savedWeightX;
153  }
154 
163  static void addPageEndGlue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
164 
165  constraints.gridx = 0;
166 
167  double savedWeighty = constraints.weighty;
168  int savedFill = constraints.fill;
169 
170  constraints.weighty = 1.0; // take up all the vertical space
171  constraints.fill = GridBagConstraints.VERTICAL;
172 
173  javax.swing.Box.Filler vertFiller = new javax.swing.Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, 32767));
174  gridbagLayout.setConstraints(vertFiller, constraints);
175  panel.add(vertFiller, constraints);
176 
177  //Resore weight & fill
178  constraints.weighty = savedWeighty;
179  constraints.fill = savedFill;
180  }
181 
189  static void addBlankLine(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints) {
190  constraints.gridy++;
191  constraints.gridx = 0;
192 
193  javax.swing.JLabel filler = new javax.swing.JLabel(" ");
194  gridbagLayout.setConstraints(filler, constraints);
195  panel.add(filler);
196 
197  addLineEndGlue(panel, gridbagLayout, constraints);
198  }
199 
210  static JLabel addKey(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString) {
211  return addKeyAtCol(panel, gridbagLayout, constraints, keyString, 0);
212  }
213 
225  static JLabel addKeyAtCol(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String keyString, int gridx) {
226 
227  // create label
228  javax.swing.JLabel keyLabel = new javax.swing.JLabel();
229 
230  constraints.gridy++;
231  constraints.gridx = gridx < MAX_COLS - 1 ? gridx : MAX_COLS - 2;
232 
233  // set text
234  keyLabel.setText(keyString + ": ");
235 
236  // add to panel
237  gridbagLayout.setConstraints(keyLabel, constraints);
238  panel.add(keyLabel);
239 
240  return keyLabel;
241  }
242 
253  static JLabel addValue(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String valueString) {
254  return addValueAtCol(panel, gridbagLayout, constraints, valueString, 1);
255  }
256 
268  static JLabel addValueAtCol(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String valueString, int gridx) {
269  // create label,
270  javax.swing.JLabel valueField = new javax.swing.JLabel();
271 
272  constraints.gridx = gridx < MAX_COLS ? gridx : MAX_COLS - 1;
273 
274  int savedGridwidth = constraints.gridwidth;
275 
276  // let the value span 2 cols
277  constraints.gridwidth = 2;
278 
279  // set text
280  valueField.setText(valueString);
281 
282  // attach a right click menu with Copy option
283  valueField.addMouseListener(new java.awt.event.MouseAdapter() {
284  @Override
285  public void mouseClicked(java.awt.event.MouseEvent evt) {
286  valueLabelMouseClicked(evt, valueField);
287  }
288  });
289 
290  // add label to panel
291  gridbagLayout.setConstraints(valueField, constraints);
292  panel.add(valueField);
293 
294  // restore constraints
295  constraints.gridwidth = savedGridwidth;
296 
297  // end the line
298  addLineEndGlue(panel, gridbagLayout, constraints);
299 
300  return valueField;
301  }
302 
315  static JLabel addMessageRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String messageString) {
316  return addMessageRow(panel, gridbagLayout, constraints, messageString, 0);
317  }
318 
331  static JLabel addMessageRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String messageString, int gridx) {
332 
333  // create label
334  javax.swing.JLabel messageLabel = new javax.swing.JLabel();
335 
336  constraints.gridy++;
337  constraints.gridx = gridx < MAX_COLS - 1 ? gridx : MAX_COLS - 2;
338 
339  int savedGridwidth = constraints.gridwidth;
340 
341  constraints.gridwidth = 3;
342 
343  // set text
344  messageLabel.setText(messageString);
345 
346  // add to panel
347  gridbagLayout.setConstraints(messageLabel, constraints);
348  panel.add(messageLabel);
349 
350  addLineEndGlue(panel, gridbagLayout, constraints);
351 
352  // restore constraints
353  constraints.gridwidth = savedGridwidth;
354 
355  return messageLabel;
356  }
357 
374  @NbBundle.Messages({
375  "CommunicationArtifactViewerHelper_persona_label=Persona: ",
376  "CommunicationArtifactViewerHelper_persona_searching=Searching...",
377  "CommunicationArtifactViewerHelper_persona_unknown=Unknown",
378  "CommunicationArtifactViewerHelper_persona_button_view=View",
379  "CommunicationArtifactViewerHelper_persona_button_create=Create"
380  })
381 
382  static List<AccountPersonaSearcherData> addPersonaRow(JPanel panel, GridBagLayout gridbagLayout, GridBagConstraints constraints, String accountIdentifier) {
383  List<AccountPersonaSearcherData> dataList = new ArrayList<>();
384 
385  constraints.gridy++;
386  constraints.gridx = 1;
387 
388  Insets savedInsets = constraints.insets;
389 
390  // extra Indent in
391  constraints.insets = new java.awt.Insets(0, 2 * LEFT_INSET, 0, 0);
392 
393  // create label
394  javax.swing.JLabel personaLabel = new javax.swing.JLabel();
395  String personaLabelText = Bundle.CommunicationArtifactViewerHelper_persona_label();
396  personaLabelText = personaLabelText.concat(CentralRepository.isEnabled()
397  ? Bundle.CommunicationArtifactViewerHelper_persona_searching()
398  : Bundle.CommunicationArtifactViewerHelper_persona_unknown());
399 
400  personaLabel.setText(personaLabelText);
401 
402  // add to panel
403  gridbagLayout.setConstraints(personaLabel, constraints);
404  panel.add(personaLabel);
405 
406  // restore constraint
407  constraints.insets = savedInsets;
408 
409  constraints.gridx++;
410 
411  // Place a button as place holder. It will be enabled when persona is available.
412  javax.swing.JButton personaButton = new javax.swing.JButton();
413  personaButton.setText(Bundle.CommunicationArtifactViewerHelper_persona_button_view());
414  personaButton.setMargin(new Insets(0, 5, 0, 5));
415  personaButton.setEnabled(false);
416 
417  gridbagLayout.setConstraints(personaButton, constraints);
418  panel.add(personaButton);
419 
420  if (CentralRepository.isEnabled()) {
421  // kick off a task to find the persona for this account
422  dataList.add(new AccountPersonaSearcherData(accountIdentifier, personaLabel, personaButton));
423  } else {
424  personaLabel.setEnabled(false);
425  }
426 
427  addLineEndGlue(panel, gridbagLayout, constraints);
428 
429  return dataList;
430  }
431 
439  @NbBundle.Messages({
440  "CommunicationArtifactViewerHelper_menuitem_copy=Copy"
441  })
442  private static void valueLabelMouseClicked(java.awt.event.MouseEvent evt, JLabel valueLabel) {
443  if (SwingUtilities.isRightMouseButton(evt)) {
444  JPopupMenu popup = new JPopupMenu();
445 
446  JMenuItem copyMenu = new JMenuItem(Bundle.CommunicationArtifactViewerHelper_menuitem_copy()); // NON-NLS
447  copyMenu.addActionListener(new ActionListener() {
448  @Override
449  public void actionPerformed(ActionEvent e) {
450  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(valueLabel.getText()), null);
451 
452  }
453  });
454 
455  popup.add(copyMenu);
456  popup.show(valueLabel, evt.getX(), evt.getY());
457  }
458  }
459 }

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