Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddEditHostDialog.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021 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.datamodel.hosts;
20 
21 import java.awt.Color;
22 import org.sleuthkit.datamodel.Host;
23 import java.util.Collection;
24 import java.util.Set;
25 import javax.swing.event.DocumentEvent;
26 import javax.swing.event.DocumentListener;
27 import org.openide.util.NbBundle.Messages;
28 
33 class AddEditHostDialog extends javax.swing.JDialog {
34 
35  private static final long serialVersionUID = 1L;
36 
37  private boolean changed = false;
38 
39  // host names to upper and trimmed
40  private final Set<String> hostNamesSanitized;
41  private final Host initialHost;
42 
49  AddEditHostDialog(java.awt.Frame parent, Collection<Host> currentHosts) {
50  this(parent, currentHosts, null);
51  }
52 
62  @Messages({
63  "AddEditHostDialog_addHost_title=Add Host",
64  "AddEditHostDialog_editHost_title=Edit Host"
65  })
66  AddEditHostDialog(java.awt.Frame parent, Collection<Host> currentHosts, Host initialHost) {
67  super(parent, true);
68  this.initialHost = initialHost;
69  setTitle(initialHost == null ? Bundle.AddEditHostDialog_addHost_title() : Bundle.AddEditHostDialog_editHost_title());
70 
71  hostNamesSanitized = HostNameValidator.getSanitizedHostNames(currentHosts);
72 
73  initComponents();
74  onNameUpdate(initialHost == null ? null : initialHost.getName());
75 
76  // initially, don't show validation message (for empty strings or repeat),
77  // but do disable ok button if not valid.
78  validationLabel.setText("");
79 
80  inputTextField.getDocument().addDocumentListener(new DocumentListener() {
81  @Override
82  public void changedUpdate(DocumentEvent e) {
83  onNameUpdate(inputTextField.getText());
84  }
85 
86  @Override
87  public void removeUpdate(DocumentEvent e) {
88  onNameUpdate(inputTextField.getText());
89  }
90 
91  @Override
92  public void insertUpdate(DocumentEvent e) {
93  onNameUpdate(inputTextField.getText());
94  }
95  });
96  }
97 
102  String getValue() {
103  return inputTextField.getText();
104  }
105 
110  boolean isChanged() {
111  return changed;
112  }
113 
119  private void onNameUpdate(String newNameValue) {
120  String newNameValueOrEmpty = newNameValue == null ? "" : newNameValue;
121  // update input text field if it is not the same.
122  if (!newNameValueOrEmpty.equals(this.inputTextField.getText())) {
123  inputTextField.setText(newNameValue);
124  }
125 
126  // validate text input against invariants setting validation
127  // message and whether or not okay button is enabled accordingly.
128  String validationMessage = HostNameValidator.getValidationMessage(
129  newNameValue, initialHost == null ? null : initialHost.getName(), hostNamesSanitized);
130 
131  okButton.setEnabled(validationMessage == null);
132  validationLabel.setText(validationMessage == null ? "" : validationMessage);
133  }
134 
140  @SuppressWarnings("unchecked")
141  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
142  private void initComponents() {
143 
144  inputTextField = new javax.swing.JTextField();
145  javax.swing.JLabel nameLabel = new javax.swing.JLabel();
146  validationLabel = new javax.swing.JLabel();
147  okButton = new javax.swing.JButton();
148  javax.swing.JButton cancelButton = new javax.swing.JButton();
149 
150  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
151 
152  inputTextField.setText(org.openide.util.NbBundle.getMessage(AddEditHostDialog.class, "AddEditHostDialog.inputTextField.text")); // NOI18N
153 
154  nameLabel.setText(org.openide.util.NbBundle.getMessage(AddEditHostDialog.class, "AddEditHostDialog.nameLabel.text")); // NOI18N
155 
156  validationLabel.setForeground(Color.RED);
157  validationLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
158 
159  okButton.setText(org.openide.util.NbBundle.getMessage(AddEditHostDialog.class, "AddEditHostDialog.okButton.text")); // NOI18N
160  okButton.addActionListener(new java.awt.event.ActionListener() {
161  public void actionPerformed(java.awt.event.ActionEvent evt) {
162  okButtonActionPerformed(evt);
163  }
164  });
165 
166  cancelButton.setText(org.openide.util.NbBundle.getMessage(AddEditHostDialog.class, "AddEditHostDialog.cancelButton.text")); // NOI18N
167  cancelButton.addActionListener(new java.awt.event.ActionListener() {
168  public void actionPerformed(java.awt.event.ActionEvent evt) {
169  cancelButtonActionPerformed(evt);
170  }
171  });
172 
173  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
174  getContentPane().setLayout(layout);
175  layout.setHorizontalGroup(
176  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
177  .addGroup(layout.createSequentialGroup()
178  .addContainerGap()
179  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
180  .addComponent(validationLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
181  .addComponent(inputTextField)
182  .addGroup(layout.createSequentialGroup()
183  .addComponent(nameLabel)
184  .addGap(0, 0, Short.MAX_VALUE))
185  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
186  .addGap(0, 288, Short.MAX_VALUE)
187  .addComponent(okButton)
188  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
189  .addComponent(cancelButton)))
190  .addContainerGap())
191  );
192  layout.setVerticalGroup(
193  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
194  .addGroup(layout.createSequentialGroup()
195  .addContainerGap()
196  .addComponent(nameLabel)
197  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
198  .addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
199  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
200  .addComponent(validationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
201  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
202  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
203  .addComponent(cancelButton)
204  .addComponent(okButton))
205  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
206  );
207 
208  pack();
209  }// </editor-fold>//GEN-END:initComponents
210 
211  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
212  this.changed = true;
213  dispose();
214  }//GEN-LAST:event_okButtonActionPerformed
215 
216 
217  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
218  this.changed = false;
219  dispose();
220  }//GEN-LAST:event_cancelButtonActionPerformed
221 
222 
223  // Variables declaration - do not modify//GEN-BEGIN:variables
224  private javax.swing.JTextField inputTextField;
225  private javax.swing.JButton okButton;
226  private javax.swing.JLabel validationLabel;
227  // End of variables declaration//GEN-END:variables
228 }
static String getValidationMessage(String curName, String initialName, Set< String > currentHostsTrimmedUpper)
static Set< String > getSanitizedHostNames(Collection< Host > hosts)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.