Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
EditFullPathsRulePanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 2019 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.logicalimager.configuration;
20 
21 import java.awt.event.ActionEvent;
22 import java.awt.event.KeyAdapter;
23 import java.awt.event.KeyEvent;
24 import java.io.IOException;
25 import java.util.List;
26 import javax.swing.JButton;
27 import javax.swing.JComponent;
28 import javax.swing.JOptionPane;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTextArea;
31 import javax.swing.SwingUtilities;
32 import javax.swing.event.DocumentEvent;
33 import javax.swing.event.DocumentListener;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.commons.lang3.tuple.ImmutablePair;
36 import org.openide.util.NbBundle;
37 
41 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
42 final class EditFullPathsRulePanel extends javax.swing.JPanel {
43 
44  private JButton okButton;
45  private JButton cancelButton;
46  private final JTextArea fullPathsTextArea;
47 
51  @NbBundle.Messages({
52  "EditFullPathsRulePanel.example=Example: "
53  })
54  EditFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) {
55  initComponents();
56 
57  this.setRule(ruleName, rule);
58  this.setButtons(okButton, cancelButton);
59 
60  fullPathsTextArea = new JTextArea();
61  initTextArea(fullPathsScrollPane, fullPathsTextArea);
62  setTextArea(fullPathsTextArea, rule.getFullPaths());
63 
64  EditRulePanel.setTextFieldPrompts(fullPathsTextArea,
65  "<html>" + Bundle.EditFullPathsRulePanel_example() + "<br>/Program Files/Common Files/system/wab32.dll<br>/Windows/System32/1033/VsGraphicsResources.dll</html>"); // NON-NLS
66  ruleNameTextField.requestFocus();
67  addDocumentListeners();
68  validate();
69  repaint();
70  }
71 
75  private void addDocumentListeners() {
76  SwingUtilities.invokeLater(() -> {
77  setOkButton(); //ensure initial state before listeners added is correct
78  });
79  DocumentListener docListener;
80  docListener = new DocumentListener() {
81  @Override
82  public void changedUpdate(DocumentEvent e) {
83  setOkButton();
84  }
85 
86  @Override
87  public void insertUpdate(DocumentEvent e) {
88  setOkButton();
89  }
90 
91  @Override
92  public void removeUpdate(DocumentEvent e) {
93  setOkButton();
94  }
95  };
96  ruleNameTextField.getDocument().addDocumentListener(docListener);
97  fullPathsTextArea.getDocument().addDocumentListener(docListener);
98  }
99 
106  private void initTextArea(JScrollPane pane, JTextArea textArea) {
107  textArea.setColumns(20);
108  textArea.setRows(5);
109  pane.setViewportView(textArea);
110  textArea.addKeyListener(new KeyAdapter() {
111  @Override
112  public void keyPressed(KeyEvent e) {
113  if (e.getKeyCode() == KeyEvent.VK_TAB) {
114  if (e.getModifiers() > 0) {
115  textArea.transferFocusBackward();
116  } else {
117  textArea.transferFocus();
118  }
119  e.consume();
120  }
121  }
122  });
123  }
124 
130  @SuppressWarnings("unchecked")
131  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
132  private void initComponents() {
133 
134  shouldSaveCheckBox = new javax.swing.JCheckBox();
135  shouldAlertCheckBox = new javax.swing.JCheckBox();
136  fullPathsLabel = new javax.swing.JLabel();
137  descriptionTextField = new javax.swing.JTextField();
138  ruleNameLabel = new javax.swing.JLabel();
139  ruleNameTextField = new javax.swing.JTextField();
140  fullPathsScrollPane = new javax.swing.JScrollPane();
141  jSeparator1 = new javax.swing.JSeparator();
142  jSeparator2 = new javax.swing.JSeparator();
143  jLabel1 = new javax.swing.JLabel();
144  jLabel2 = new javax.swing.JLabel();
145 
146  shouldSaveCheckBox.setSelected(true);
147  org.openide.awt.Mnemonics.setLocalizedText(shouldSaveCheckBox, org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.shouldSaveCheckBox.text")); // NOI18N
148  shouldSaveCheckBox.addActionListener(new java.awt.event.ActionListener() {
149  public void actionPerformed(java.awt.event.ActionEvent evt) {
150  shouldSaveCheckBoxActionPerformed(evt);
151  }
152  });
153 
154  org.openide.awt.Mnemonics.setLocalizedText(shouldAlertCheckBox, org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.shouldAlertCheckBox.text")); // NOI18N
155  shouldAlertCheckBox.setActionCommand(org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.shouldAlertCheckBox.actionCommand")); // NOI18N
156  shouldAlertCheckBox.addActionListener(new java.awt.event.ActionListener() {
157  public void actionPerformed(java.awt.event.ActionEvent evt) {
158  shouldAlertCheckBoxActionPerformed(evt);
159  }
160  });
161 
162  org.openide.awt.Mnemonics.setLocalizedText(fullPathsLabel, org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.fullPathsLabel.text")); // NOI18N
163  fullPathsLabel.setToolTipText(org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.fullPathsLabel.toolTipText")); // NOI18N
164  fullPathsLabel.setPreferredSize(new java.awt.Dimension(112, 14));
165 
166  org.openide.awt.Mnemonics.setLocalizedText(ruleNameLabel, org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.ruleNameLabel.text")); // NOI18N
167  ruleNameLabel.setPreferredSize(new java.awt.Dimension(112, 14));
168 
169  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.jLabel1.text")); // NOI18N
170  jLabel1.setPreferredSize(new java.awt.Dimension(112, 14));
171 
172  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(EditFullPathsRulePanel.class, "EditFullPathsRulePanel.jLabel2.text")); // NOI18N
173 
174  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
175  this.setLayout(layout);
176  layout.setHorizontalGroup(
177  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
178  .addGroup(layout.createSequentialGroup()
179  .addContainerGap()
180  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
181  .addComponent(jSeparator2)
182  .addComponent(jSeparator1)
183  .addGroup(layout.createSequentialGroup()
184  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
185  .addGroup(layout.createSequentialGroup()
186  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
187  .addComponent(ruleNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
188  .addComponent(fullPathsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
189  .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
190  .addGap(0, 0, 0)
191  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
192  .addComponent(ruleNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 648, javax.swing.GroupLayout.PREFERRED_SIZE)
193  .addComponent(descriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 648, javax.swing.GroupLayout.PREFERRED_SIZE)
194  .addComponent(fullPathsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 648, javax.swing.GroupLayout.PREFERRED_SIZE)))
195  .addComponent(shouldSaveCheckBox)
196  .addComponent(shouldAlertCheckBox)
197  .addComponent(jLabel2))
198  .addGap(0, 0, Short.MAX_VALUE)))
199  .addContainerGap())
200  );
201 
202  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {fullPathsLabel, jLabel1, ruleNameLabel});
203 
204  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {descriptionTextField, fullPathsScrollPane, ruleNameTextField});
205 
206  layout.setVerticalGroup(
207  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
208  .addGroup(layout.createSequentialGroup()
209  .addGap(8, 8, 8)
210  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
211  .addComponent(ruleNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
212  .addComponent(ruleNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
213  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
214  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
215  .addComponent(descriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
216  .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
217  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
218  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
219  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
220  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
221  .addGroup(layout.createSequentialGroup()
222  .addComponent(fullPathsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
223  .addGap(0, 115, Short.MAX_VALUE))
224  .addComponent(fullPathsScrollPane))
225  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
226  .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
227  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
228  .addComponent(jLabel2)
229  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
230  .addComponent(shouldSaveCheckBox)
231  .addGap(3, 3, 3)
232  .addComponent(shouldAlertCheckBox)
233  .addContainerGap())
234  );
235  }// </editor-fold>//GEN-END:initComponents
236 
237  private void shouldSaveCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shouldSaveCheckBoxActionPerformed
238  setOkButton();
239  }//GEN-LAST:event_shouldSaveCheckBoxActionPerformed
240 
241  private void shouldAlertCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shouldAlertCheckBoxActionPerformed
242  setOkButton();
243  }//GEN-LAST:event_shouldAlertCheckBoxActionPerformed
244 
245 
246  // Variables declaration - do not modify//GEN-BEGIN:variables
247  private javax.swing.JTextField descriptionTextField;
248  private javax.swing.JLabel fullPathsLabel;
249  private javax.swing.JScrollPane fullPathsScrollPane;
250  private javax.swing.JLabel jLabel1;
251  private javax.swing.JLabel jLabel2;
252  private javax.swing.JSeparator jSeparator1;
253  private javax.swing.JSeparator jSeparator2;
254  private javax.swing.JLabel ruleNameLabel;
255  private javax.swing.JTextField ruleNameTextField;
256  private javax.swing.JCheckBox shouldAlertCheckBox;
257  private javax.swing.JCheckBox shouldSaveCheckBox;
258  // End of variables declaration//GEN-END:variables
259 
264  void setOkButton() {
265  if (this.okButton != null) {
266  this.okButton.setEnabled(!StringUtils.isBlank(ruleNameTextField.getText()) && !StringUtils.isBlank(fullPathsTextArea.getText()) && (shouldAlertCheckBox.isSelected() || shouldSaveCheckBox.isSelected()));
267  }
268  }
269 
277  private JOptionPane getOptionPane(JComponent parent) {
278  JOptionPane pane;
279  if (!(parent instanceof JOptionPane)) {
280  pane = getOptionPane((JComponent) parent.getParent());
281  } else {
282  pane = (JOptionPane) parent;
283  }
284  return pane;
285  }
286 
293  private void setButtons(JButton ok, JButton cancel) {
294  this.okButton = ok;
295  this.cancelButton = cancel;
296  okButton.addActionListener((ActionEvent e) -> {
297  JOptionPane pane = getOptionPane(okButton);
298  pane.setValue(okButton);
299  });
300  cancelButton.addActionListener((ActionEvent e) -> {
301  JOptionPane pane = getOptionPane(cancelButton);
302  pane.setValue(cancelButton);
303  });
304  }
305 
312  private void setRule(String ruleName, LogicalImagerRule rule) {
313  ruleNameTextField.setText(ruleName);
314  descriptionTextField.setText(rule.getDescription());
315  shouldAlertCheckBox.setSelected(rule.isShouldAlert());
316  shouldSaveCheckBox.setSelected(rule.isShouldSave());
317  }
318 
326  private void setTextArea(JTextArea textArea, List<String> list) {
327  String text = "";
328  for (String s : list) {
329  text += s + System.getProperty("line.separator"); // NON-NLS
330  }
331  textArea.setText(text);
332  }
333 
342  @NbBundle.Messages({
343  "EditFullPathsRulePanel.fullPaths=Full paths",})
344  ImmutablePair<String, LogicalImagerRule> toRule() throws IOException {
345  List<String> fullPaths = EditRulePanel.validateTextList(fullPathsTextArea, Bundle.EditFullPathsRulePanel_fullPaths());
346  String ruleName = EditRulePanel.validRuleName(ruleNameTextField.getText());
347  LogicalImagerRule.Builder builder = new LogicalImagerRule.Builder();
348  builder.getShouldAlert(shouldAlertCheckBox.isSelected())
349  .getShouldSave(shouldSaveCheckBox.isSelected())
350  .getName(ruleName)
351  .getDescription(descriptionTextField.getText())
352  .getFullPaths(fullPaths);
353  LogicalImagerRule rule = builder.build();
354  return new ImmutablePair<>(ruleName, rule);
355  }
356 }

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