Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddEditCategoryDialog.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.url.analytics.domaincategorization;
20 
21 import java.util.Set;
22 import javax.swing.event.DocumentEvent;
23 import javax.swing.event.DocumentListener;
24 import org.openide.util.NbBundle.Messages;
26 
30 @Messages({
31  "AddEditCategoryDialog_Edit=Edit Entry",
32  "AddEditCategoryDialog_Add=Add Entry"
33 })
34 class AddEditCategoryDialog extends javax.swing.JDialog {
35 
36  private boolean changed = false;
37  private final Set<String> currentSuffixes;
38  private final DomainCategory currentDomainCategory;
39 
40  // listens for document updates
41  private final DocumentListener updateListener = new DocumentListener() {
42  @Override
43  public void insertUpdate(DocumentEvent e) {
44  onValueUpdate(domainSuffixTextField.getText(), categoryTextField.getText());
45  }
46 
47  @Override
48  public void removeUpdate(DocumentEvent e) {
49  onValueUpdate(domainSuffixTextField.getText(), categoryTextField.getText());
50  }
51 
52  @Override
53  public void changedUpdate(DocumentEvent e) {
54  onValueUpdate(domainSuffixTextField.getText(), categoryTextField.getText());
55  }
56  };
57 
64  AddEditCategoryDialog(java.awt.Frame parent, Set<String> currentSuffixes) {
65  this(parent, currentSuffixes, null);
66  }
67 
76  AddEditCategoryDialog(java.awt.Frame parent, Set<String> currentSuffixes, DomainCategory currentDomainCategory) {
77  super(parent, true);
78  initComponents();
79  this.currentSuffixes = currentSuffixes;
80  this.currentDomainCategory = currentDomainCategory;
81 
82  // set title based on whether or not we are editing or adding
83  // also don't allow editing of domain suffix if editing
84  if (currentDomainCategory == null) {
85  setTitle(Bundle.AddEditCategoryDialog_Add());
86  domainSuffixTextField.setEditable(true);
87  domainSuffixTextField.setEnabled(true);
88  onValueUpdate(null, null);
89  } else {
90  setTitle(Bundle.AddEditCategoryDialog_Edit());
91  domainSuffixTextField.setEditable(false);
92  domainSuffixTextField.setEnabled(false);
93  onValueUpdate(currentDomainCategory.getHostSuffix(), currentDomainCategory.getCategory());
94  }
95 
96  validationLabel.setText("");
97 
98  categoryTextField.getDocument().addDocumentListener(updateListener);
99  domainSuffixTextField.getDocument().addDocumentListener(updateListener);
100  }
101 
109  DomainCategory getValue() {
110  return new DomainCategory(domainSuffixTextField.getText(), categoryTextField.getText());
111  }
112 
118  boolean isChanged() {
119  return changed;
120  }
121 
128  @Messages({
129  "# {0} - maxSuffixLen",
130  "AddEditCategoryDialog_onValueUpdate_badSuffix=Please provide a domain suffix that is no more than {0} characters that includes at least one period.",
131  "# {0} - maxCategoryLen",
132  "AddEditCategoryDialog_onValueUpdate_badCategory=Please provide a category that is no more than {0} characters.",
133  "AddEditCategoryDialog_onValueUpdate_suffixRepeat=Please provide a unique domain suffix.",
134  "AddEditCategoryDialog_onValueUpdate_sameCategory=Please provide a new category for this domain suffix.",})
135  void onValueUpdate(String suffixStr, String categoryStr) {
136 
137  String safeSuffixStr = suffixStr == null ? "" : suffixStr;
138  String normalizedSuffix = WebCategoriesDataModel.getNormalizedSuffix(safeSuffixStr);
139  String safeCategoryStr = categoryStr == null ? "" : categoryStr;
140  String normalizedCategory = WebCategoriesDataModel.getNormalizedCategory(safeCategoryStr);
141 
142  // update input text field if it is not the same.
143  if (!safeCategoryStr.equals(categoryTextField.getText())) {
144  categoryTextField.setText(safeCategoryStr);
145  }
146 
147  if (!safeSuffixStr.equals(domainSuffixTextField.getText())) {
148  domainSuffixTextField.setText(safeSuffixStr);
149  }
150 
151  String validationMessage = null;
152  if (normalizedSuffix.length() == 0
153  || normalizedSuffix.length() > WebCategoriesDataModel.getMaxDomainSuffixLength()
154  || normalizedSuffix.indexOf('.') < 0) {
155 
156  validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_badSuffix(WebCategoriesDataModel.getMaxCategoryLength());
157 
158  } else if (normalizedCategory.length() == 0 || normalizedCategory.length() > WebCategoriesDataModel.getMaxCategoryLength()) {
159  validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_badCategory(WebCategoriesDataModel.getMaxCategoryLength());
160 
161  } else if (currentSuffixes.contains(normalizedSuffix)
162  && (currentDomainCategory == null
163  || !normalizedSuffix.equals(currentDomainCategory.getHostSuffix()))) {
164 
165  validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_suffixRepeat();
166 
167  } else if (currentDomainCategory != null
168  && currentDomainCategory.getCategory() != null
169  && normalizedCategory.equals(WebCategoriesDataModel.getNormalizedCategory(currentDomainCategory.getCategory()))) {
170 
171  validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_sameCategory();
172  }
173 
174  saveButton.setEnabled(validationMessage == null);
175  validationLabel.setText(validationMessage == null ? "" : String.format("<html>%s</html>", validationMessage));
176  }
177 
183  @SuppressWarnings("unchecked")
184  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
185  private void initComponents() {
186 
187  categoryTextField = new javax.swing.JTextField();
188  domainSuffixTextField = new javax.swing.JTextField();
189  javax.swing.JLabel categoryLabel = new javax.swing.JLabel();
190  javax.swing.JLabel domainSuffixLabel = new javax.swing.JLabel();
191  validationLabel = new javax.swing.JLabel();
192  javax.swing.JButton cancelButton = new javax.swing.JButton();
193  saveButton = new javax.swing.JButton();
194 
195  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
196 
197  categoryLabel.setText(org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class, "AddEditCategoryDialog.categoryLabel.text")); // NOI18N
198 
199  domainSuffixLabel.setText(org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class, "AddEditCategoryDialog.domainSuffixLabel.text")); // NOI18N
200 
201  validationLabel.setForeground(java.awt.Color.RED);
202  validationLabel.setText(" ");
203  validationLabel.setToolTipText("");
204 
205  cancelButton.setText(org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class, "AddEditCategoryDialog.cancelButton.text")); // NOI18N
206  cancelButton.addActionListener(new java.awt.event.ActionListener() {
207  public void actionPerformed(java.awt.event.ActionEvent evt) {
208  cancelButtonActionPerformed(evt);
209  }
210  });
211 
212  saveButton.setText(org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class, "AddEditCategoryDialog.saveButton.text")); // NOI18N
213  saveButton.addActionListener(new java.awt.event.ActionListener() {
214  public void actionPerformed(java.awt.event.ActionEvent evt) {
215  saveButtonActionPerformed(evt);
216  }
217  });
218 
219  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
220  getContentPane().setLayout(layout);
221  layout.setHorizontalGroup(
222  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
223  .addGroup(layout.createSequentialGroup()
224  .addContainerGap()
225  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
226  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
227  .addGap(0, 0, Short.MAX_VALUE)
228  .addComponent(saveButton)
229  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
230  .addComponent(cancelButton))
231  .addComponent(validationLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
232  .addGroup(layout.createSequentialGroup()
233  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
234  .addComponent(domainSuffixLabel)
235  .addComponent(categoryLabel))
236  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
237  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
238  .addComponent(categoryTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
239  .addComponent(domainSuffixTextField))))
240  .addContainerGap())
241  );
242  layout.setVerticalGroup(
243  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
244  .addGroup(layout.createSequentialGroup()
245  .addContainerGap()
246  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
247  .addComponent(domainSuffixTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
248  .addComponent(domainSuffixLabel))
249  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
250  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
251  .addComponent(categoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
252  .addComponent(categoryLabel))
253  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
254  .addComponent(validationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
255  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
256  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
257  .addComponent(cancelButton)
258  .addComponent(saveButton))
259  .addContainerGap(8, Short.MAX_VALUE))
260  );
261 
262  pack();
263  }// </editor-fold>//GEN-END:initComponents
264 
265  private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
266  this.changed = true;
267  dispose();
268  }//GEN-LAST:event_saveButtonActionPerformed
269 
270  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
271  this.changed = false;
272  dispose();
273  }//GEN-LAST:event_cancelButtonActionPerformed
274 
275 
276  // Variables declaration - do not modify//GEN-BEGIN:variables
277  private javax.swing.JTextField categoryTextField;
278  private javax.swing.JTextField domainSuffixTextField;
279  private javax.swing.JButton saveButton;
280  private javax.swing.JLabel validationLabel;
281  // End of variables declaration//GEN-END:variables
282 }

Copyright © 2012-2022 Basis Technology. Generated on: Wed Oct 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.