19 package org.sleuthkit.autopsy.url.analytics.domaincategorization;
22 import javax.swing.event.DocumentEvent;
23 import javax.swing.event.DocumentListener;
24 import org.openide.util.NbBundle.Messages;
31 "AddEditCategoryDialog_Edit=Edit Entry",
32 "AddEditCategoryDialog_Add=Add Entry"
34 class AddEditCategoryDialog extends javax.swing.JDialog {
36 private boolean changed =
false;
37 private final Set<String> currentSuffixes;
41 private final DocumentListener updateListener =
new DocumentListener() {
43 public void insertUpdate(DocumentEvent e) {
44 onValueUpdate(domainSuffixTextField.getText(), categoryTextField.getText());
48 public void removeUpdate(DocumentEvent e) {
49 onValueUpdate(domainSuffixTextField.getText(), categoryTextField.getText());
53 public void changedUpdate(DocumentEvent e) {
54 onValueUpdate(domainSuffixTextField.getText(), categoryTextField.getText());
64 AddEditCategoryDialog(java.awt.Frame parent, Set<String> currentSuffixes) {
65 this(parent, currentSuffixes, null);
76 AddEditCategoryDialog(java.awt.Frame parent, Set<String> currentSuffixes,
DomainCategory currentDomainCategory) {
79 this.currentSuffixes = currentSuffixes;
80 this.currentDomainCategory = currentDomainCategory;
84 if (currentDomainCategory == null) {
85 setTitle(Bundle.AddEditCategoryDialog_Add());
86 domainSuffixTextField.setEditable(
true);
87 domainSuffixTextField.setEnabled(
true);
88 onValueUpdate(null, null);
90 setTitle(Bundle.AddEditCategoryDialog_Edit());
91 domainSuffixTextField.setEditable(
false);
92 domainSuffixTextField.setEnabled(
false);
96 validationLabel.setText(
"");
98 categoryTextField.getDocument().addDocumentListener(updateListener);
99 domainSuffixTextField.getDocument().addDocumentListener(updateListener);
110 return new DomainCategory(domainSuffixTextField.getText(), categoryTextField.getText());
118 boolean isChanged() {
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) {
137 String safeSuffixStr = suffixStr == null ?
"" : suffixStr;
138 String normalizedSuffix = WebCategoriesDataModel.getNormalizedSuffix(safeSuffixStr);
139 String safeCategoryStr = categoryStr == null ?
"" : categoryStr;
140 String normalizedCategory = WebCategoriesDataModel.getNormalizedCategory(safeCategoryStr);
143 if (!safeCategoryStr.equals(categoryTextField.getText())) {
144 categoryTextField.setText(safeCategoryStr);
147 if (!safeSuffixStr.equals(domainSuffixTextField.getText())) {
148 domainSuffixTextField.setText(safeSuffixStr);
151 String validationMessage = null;
152 if (normalizedSuffix.length() == 0
153 || normalizedSuffix.length() > WebCategoriesDataModel.getMaxDomainSuffixLength()
154 || normalizedSuffix.indexOf(
'.') < 0) {
156 validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_badSuffix(WebCategoriesDataModel.getMaxCategoryLength());
158 }
else if (normalizedCategory.length() == 0 || normalizedCategory.length() > WebCategoriesDataModel.getMaxCategoryLength()) {
159 validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_badCategory(WebCategoriesDataModel.getMaxCategoryLength());
161 }
else if (currentSuffixes.contains(normalizedSuffix)
162 && (currentDomainCategory == null
163 || !normalizedSuffix.equals(currentDomainCategory.
getHostSuffix()))) {
165 validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_suffixRepeat();
167 }
else if (currentDomainCategory != null
169 && normalizedCategory.equals(WebCategoriesDataModel.getNormalizedCategory(currentDomainCategory.
getCategory()))) {
171 validationMessage = Bundle.AddEditCategoryDialog_onValueUpdate_sameCategory();
174 saveButton.setEnabled(validationMessage == null);
175 validationLabel.setText(validationMessage == null ?
"" : String.format(
"<html>%s</html>", validationMessage));
183 @SuppressWarnings(
"unchecked")
185 private void initComponents() {
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();
195 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
197 categoryLabel.setText(
org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class,
"AddEditCategoryDialog.categoryLabel.text"));
199 domainSuffixLabel.setText(
org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class,
"AddEditCategoryDialog.domainSuffixLabel.text"));
201 validationLabel.setForeground(java.awt.Color.RED);
202 validationLabel.setText(
" ");
203 validationLabel.setToolTipText(
"");
205 cancelButton.setText(
org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class,
"AddEditCategoryDialog.cancelButton.text"));
206 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
207 public void actionPerformed(java.awt.event.ActionEvent evt) {
208 cancelButtonActionPerformed(evt);
212 saveButton.setText(
org.openide.util.NbBundle.getMessage(AddEditCategoryDialog.class,
"AddEditCategoryDialog.saveButton.text"));
213 saveButton.addActionListener(
new java.awt.event.ActionListener() {
214 public void actionPerformed(java.awt.event.ActionEvent evt) {
215 saveButtonActionPerformed(evt);
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()
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))))
242 layout.setVerticalGroup(
243 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
244 .addGroup(layout.createSequentialGroup()
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))
265 private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
270 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
271 this.changed =
false;
277 private javax.swing.JTextField categoryTextField;
278 private javax.swing.JTextField domainSuffixTextField;
279 private javax.swing.JButton saveButton;
280 private javax.swing.JLabel validationLabel;