19 package org.sleuthkit.autopsy.datamodel.hosts;
21 import java.awt.Dialog;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Vector;
29 import java.util.logging.Level;
30 import java.util.stream.Collectors;
31 import javax.swing.JFrame;
32 import javax.swing.ListModel;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.openide.util.NbBundle;
35 import org.openide.util.NbBundle.Messages;
49 "ManageHostsDialog_title_text=Manage Hosts"
58 private final Host host;
59 private final List<DataSource> dataSources;
69 this.dataSources = dataSources;
82 List<DataSource> getDataSources() {
88 return host == null ?
"" : host.getName();
94 hash = 89 * hash + Objects.hashCode(this.host == null ? 0 : this.host.getHostId());
106 if (getClass() != obj.getClass()) {
110 if (this.host == null || other.getHost() == null) {
111 return this.host == null && other.getHost() == null;
114 return this.host.getHostId() == other.getHost().getHostId();
120 private static final long serialVersionUID = 1L;
122 private Map<Host, List<DataSource>> hostChildrenMap = Collections.emptyMap();
130 super(parent, Bundle.ManageHostsDialog_title_text(),
true);
140 super(parent, Bundle.ManageHostsDialog_title_text(),
true);
152 this.hostList.addListSelectionListener((evt) -> refreshComponents());
159 Host getSelectedHost() {
160 return (hostList.getSelectedValue() == null) ? null : hostList.getSelectedValue().getHost();
166 @NbBundle.Messages({
"# {0} - hostname",
167 "ManageHostsDialog.failureToAdd.txt=Unable to add new host {0} at this time.",
168 "ManageHostsDialog.seeLog.txt= See log for more information."})
170 String newHostName = getAddEditDialogName(null);
171 if (newHostName != null) {
172 Long selectedId = null;
175 selectedId = newHost == null ? null : newHost.getHostId();
177 logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToAdd_txt(newHostName), e);
181 setSelectedHostById(selectedId);
190 @NbBundle.Messages({
"# {0} - hostname",
191 "ManageHostsDialog.failureToDelete.txt=Unable to delete host {0} at this time."})
193 if (selectedHost != null && selectedHost.getName() != null) {
197 logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName()), e);
198 MessageNotifyUtil.
Message.
error(Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName()) + Bundle.ManageHostsDialog_seeLog_txt());
210 ListModel<HostListItem> model = hostList.getModel();
212 if (selectedId == null) {
213 hostList.clearSelection();
216 for (
int i = 0; i < model.getSize(); i++) {
217 Object o = model.getElementAt(i);
222 Host host = ((HostListItem) o).getHost();
227 if (host.getHostId() == selectedId) {
228 hostList.setSelectedIndex(i);
233 hostList.clearSelection();
241 @NbBundle.Messages({
"# {0} - hostname",
243 "ManageHostsDialog.failureToEdit.txt=Unable to update host {0} with id: {1} at this time."})
246 if (selectedHost != null) {
247 String newHostName = getAddEditDialogName(selectedHost);
248 if (newHostName != null) {
252 logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId()), e);
253 MessageNotifyUtil.
Message.
warn(Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId()) + Bundle.ManageHostsDialog_seeLog_txt());
256 HostListItem selectedItem = hostList.getSelectedValue();
257 Long selectedId = selectedItem == null || selectedItem.getHost() == null ? null : selectedItem.getHost().getHostId();
261 setSelectedHostById(selectedId);
275 JFrame parent = (this.getRootPane() != null && this.getRootPane().getParent() instanceof JFrame)
276 ? (JFrame) this.getRootPane().getParent()
279 AddEditHostDialog addEditDialog =
new AddEditHostDialog(parent, hostChildrenMap.keySet(), origValue);
280 addEditDialog.setResizable(
false);
281 addEditDialog.setLocationRelativeTo(parent);
282 addEditDialog.setVisible(
true);
283 addEditDialog.toFront();
285 if (addEditDialog.isChanged()) {
286 String newHostName = addEditDialog.getValue();
307 hostChildrenMap = getHostListData();
309 Vector<HostListItem> jlistData = hostChildrenMap.entrySet().stream()
310 .sorted((a, b) -> getNameOrEmpty(a.getKey()).compareTo(getNameOrEmpty(b.getKey())))
311 .map(entry ->
new HostListItem(entry.getKey(), entry.getValue()))
312 .collect(Collectors.toCollection(Vector::new));
314 hostList.setListData(jlistData);
326 return (h == null || h.getName() == null) ?
"" : h.getName();
335 @NbBundle.Messages({
"ManageHostsDialog.failureToGetHosts.txt=There was an error while fetching hosts for current case."})
337 Map<Host, List<DataSource>> hostMapping =
new HashMap<>();
340 List<Host> hosts = curCase.getHostManager().getAllHosts();
341 List<DataSource> dataSources = curCase.getDataSources();
343 if (dataSources != null) {
344 for (DataSource ds : dataSources) {
345 List<DataSource> hostDataSources = hostMapping.computeIfAbsent(ds.getHost(), (d) ->
new ArrayList<>());
346 hostDataSources.add(ds);
352 for (Host host : hosts) {
353 hostMapping.putIfAbsent(host, Collections.emptyList());
358 logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToGetHosts_txt(), ex);
369 HostListItem selectedItem = hostList.getSelectedValue();
370 Host selectedHost = selectedItem == null ? null : selectedItem.getHost();
371 List<DataSource> dataSources = selectedItem == null ? null : selectedItem.getDataSources();
372 this.editButton.setEnabled(selectedHost != null);
373 this.deleteButton.setEnabled(selectedHost != null && CollectionUtils.isEmpty(dataSources));
374 String nameTextFieldStr = selectedHost != null && selectedHost.getName() != null ? selectedHost.getName() :
"";
375 this.hostNameTextField.setText(nameTextFieldStr);
384 @SuppressWarnings(
"unchecked")
386 private
void initComponents() {
388 javax.swing.JScrollPane manageHostsScrollPane =
new javax.swing.JScrollPane();
389 javax.swing.JPanel manageHostsPanel =
new javax.swing.JPanel();
390 javax.swing.JScrollPane hostListScrollPane =
new javax.swing.JScrollPane();
391 hostList =
new javax.swing.JList<>();
392 javax.swing.JScrollPane hostDescriptionScrollPane =
new javax.swing.JScrollPane();
393 hostDescriptionTextArea =
new javax.swing.JTextArea();
394 newButton =
new javax.swing.JButton();
395 deleteButton =
new javax.swing.JButton();
396 closeButton =
new javax.swing.JButton();
397 javax.swing.JLabel hostListLabel =
new javax.swing.JLabel();
398 javax.swing.JSeparator jSeparator1 =
new javax.swing.JSeparator();
399 javax.swing.JLabel hostNameLabel =
new javax.swing.JLabel();
400 hostNameTextField =
new javax.swing.JTextField();
401 editButton =
new javax.swing.JButton();
402 javax.swing.JLabel hostDetailsLabel =
new javax.swing.JLabel();
404 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
405 setMinimumSize(
new java.awt.Dimension(600, 450));
407 manageHostsScrollPane.setMinimumSize(
new java.awt.Dimension(600, 450));
408 manageHostsScrollPane.setPreferredSize(
new java.awt.Dimension(600, 450));
410 manageHostsPanel.setPreferredSize(
new java.awt.Dimension(527, 407));
412 hostList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
413 hostListScrollPane.setViewportView(hostList);
415 hostDescriptionTextArea.setEditable(
false);
416 hostDescriptionTextArea.setBackground(
new java.awt.Color(240, 240, 240));
417 hostDescriptionTextArea.setColumns(20);
418 hostDescriptionTextArea.setLineWrap(
true);
419 hostDescriptionTextArea.setRows(3);
420 hostDescriptionTextArea.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.hostDescriptionTextArea.text"));
421 hostDescriptionTextArea.setWrapStyleWord(
true);
422 hostDescriptionScrollPane.setViewportView(hostDescriptionTextArea);
424 newButton.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.newButton.text"));
425 newButton.setMargin(
new java.awt.Insets(2, 6, 2, 6));
426 newButton.setMaximumSize(
new java.awt.Dimension(70, 23));
427 newButton.setMinimumSize(
new java.awt.Dimension(70, 23));
428 newButton.setPreferredSize(
new java.awt.Dimension(70, 23));
429 newButton.addActionListener(
new java.awt.event.ActionListener() {
430 public void actionPerformed(java.awt.event.ActionEvent evt) {
431 newButtonActionPerformed(evt);
435 deleteButton.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.deleteButton.text"));
436 deleteButton.setMargin(
new java.awt.Insets(2, 6, 2, 6));
437 deleteButton.setMaximumSize(
new java.awt.Dimension(70, 23));
438 deleteButton.setMinimumSize(
new java.awt.Dimension(70, 23));
439 deleteButton.setPreferredSize(
new java.awt.Dimension(70, 23));
440 deleteButton.addActionListener(
new java.awt.event.ActionListener() {
441 public void actionPerformed(java.awt.event.ActionEvent evt) {
442 deleteButtonActionPerformed(evt);
446 closeButton.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.closeButton.text"));
447 closeButton.addActionListener(
new java.awt.event.ActionListener() {
448 public void actionPerformed(java.awt.event.ActionEvent evt) {
449 closeButtonActionPerformed(evt);
453 hostListLabel.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.hostListLabel.text"));
455 jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
457 hostNameLabel.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.hostNameLabel.text"));
459 hostNameTextField.setEditable(
false);
461 editButton.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.editButton.text"));
462 editButton.setMaximumSize(
new java.awt.Dimension(70, 23));
463 editButton.setMinimumSize(
new java.awt.Dimension(70, 23));
464 editButton.setPreferredSize(
new java.awt.Dimension(70, 23));
465 editButton.addActionListener(
new java.awt.event.ActionListener() {
466 public void actionPerformed(java.awt.event.ActionEvent evt) {
467 editButtonActionPerformed(evt);
471 hostDetailsLabel.setText(
org.openide.util.NbBundle.getMessage(
ManageHostsDialog.class,
"ManageHostsDialog.hostDetailsLabel.text"));
473 javax.swing.GroupLayout manageHostsPanelLayout =
new javax.swing.GroupLayout(manageHostsPanel);
474 manageHostsPanel.setLayout(manageHostsPanelLayout);
475 manageHostsPanelLayout.setHorizontalGroup(
476 manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
477 .addGroup(manageHostsPanelLayout.createSequentialGroup()
479 .addGroup(manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
480 .addComponent(hostDescriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
481 .addComponent(hostListLabel)
482 .addGroup(manageHostsPanelLayout.createSequentialGroup()
483 .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
484 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
485 .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
486 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
487 .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
488 .addComponent(hostListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
489 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
490 .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
491 .addGroup(manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
492 .addGroup(manageHostsPanelLayout.createSequentialGroup()
493 .addGroup(manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
494 .addGroup(manageHostsPanelLayout.createSequentialGroup()
495 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
496 .addComponent(closeButton))
497 .addGroup(manageHostsPanelLayout.createSequentialGroup()
499 .addComponent(hostNameLabel)
500 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
501 .addComponent(hostNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)))
503 .addGroup(manageHostsPanelLayout.createSequentialGroup()
504 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
505 .addComponent(hostDetailsLabel)
506 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
508 manageHostsPanelLayout.setVerticalGroup(
509 manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
510 .addGroup(manageHostsPanelLayout.createSequentialGroup()
512 .addGroup(manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
513 .addGroup(manageHostsPanelLayout.createSequentialGroup()
514 .addComponent(hostDetailsLabel)
515 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
516 .addGroup(manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
517 .addComponent(hostNameLabel)
518 .addComponent(hostNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
519 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
520 .addComponent(closeButton))
521 .addComponent(jSeparator1)
522 .addGroup(manageHostsPanelLayout.createSequentialGroup()
523 .addComponent(hostDescriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
524 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
525 .addComponent(hostListLabel)
526 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
527 .addComponent(hostListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
528 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
529 .addGroup(manageHostsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
530 .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
531 .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
532 .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
536 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle(
"org/sleuthkit/autopsy/datamodel/hosts/Bundle");
537 newButton.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.newButton.text"));
538 deleteButton.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.deleteButton.text"));
539 closeButton.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.closeButton.text"));
540 hostListLabel.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.hostListLabel.text"));
541 hostNameLabel.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.hostNameLabel.text"));
542 editButton.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.editButton.text"));
543 hostDetailsLabel.getAccessibleContext().setAccessibleName(bundle.getString(
"ManageHostsDialog.hostDetailsLabel.text"));
545 manageHostsScrollPane.setViewportView(manageHostsPanel);
547 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
548 getContentPane().setLayout(layout);
549 layout.setHorizontalGroup(
550 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
551 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
553 .addComponent(manageHostsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
555 layout.setVerticalGroup(
556 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
557 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
559 .addComponent(manageHostsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
570 HostListItem listItem = this.hostList.getSelectedValue();
571 if (listItem != null && listItem.getHost() != null) {
572 deleteHost(listItem.getHost());
581 HostListItem listItem = this.hostList.getSelectedValue();
582 if (listItem != null && listItem.getHost() != null) {
583 editHost(listItem.getHost());
void deleteHost(Host selectedHost)
static void warn(String message)
void editButtonActionPerformed(java.awt.event.ActionEvent evt)
void deleteButtonActionPerformed(java.awt.event.ActionEvent evt)
void closeButtonActionPerformed(java.awt.event.ActionEvent evt)
javax.swing.JButton deleteButton
String getAddEditDialogName(Host origValue)
void newButtonActionPerformed(java.awt.event.ActionEvent evt)
void editHost(Host selectedHost)
ManageHostsDialog(java.awt.Frame parent)
String getNameOrEmpty(Host h)
boolean equals(Object obj)
javax.swing.JTextArea hostDescriptionTextArea
Map< Host, List< DataSource > > getHostListData()
SleuthkitCase getSleuthkitCase()
javax.swing.JButton closeButton
ManageHostsDialog(Dialog parent)
void setSelectedHostById(Long selectedId)
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()
javax.swing.JButton newButton
javax.swing.JTextField hostNameTextField
javax.swing.JButton editButton
static void error(String message)
javax.swing.JList< org.sleuthkit.autopsy.datamodel.hosts.ManageHostsDialog.HostListItem > hostList