Autopsy  4.19.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
RuleSetPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.modules.yara.ui;
20 
21 import java.awt.Component;
22 import java.awt.event.ActionListener;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import javax.swing.DefaultListCellRenderer;
27 import javax.swing.DefaultListModel;
28 import javax.swing.JList;
29 import javax.swing.ListSelectionModel;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
33 
37 public final class RuleSetPanel extends javax.swing.JPanel {
38 
39  private static final long serialVersionUID = 1L;
40 
41  private final DefaultListModel<RuleSet> listModel;
42  private final JList<RuleSet> ruleSetList;
43 
44  public RuleSetPanel() {
46 
47  // Creating and initializing JList here to better take
48  // advantace of JList use of generics.
49  ruleSetList = new JList<>();
50  listModel = new DefaultListModel<>();
51  scrollPane.setViewportView(ruleSetList);
52  ruleSetList.setModel(listModel);
53  ruleSetList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
54  ruleSetList.setCellRenderer(new RuleSetRenderer());
55  ruleSetList.addListSelectionListener(new ListSelectionListener() {
56  @Override
57  public void valueChanged(ListSelectionEvent e) {
58  deleteButton.setEnabled(getSelectedRule() != null);
59  }
60  });
61 
62  deleteButton.setEnabled(false);
63  }
64 
70  void addSetList(List<RuleSet> newSetList) {
71  // Put the list into alphectical order.
72  List<RuleSet> list = new ArrayList<>();
73  list.addAll(newSetList);
74  Collections.sort(list);
75 
76  listModel.clear();
77 
78  for (RuleSet set : list) {
79  listModel.addElement(set);
80  }
81  }
82 
88  void addRuleSet(RuleSet set) {
89  // This will assure that the new item
90  // appears in the correct location.
91  List<RuleSet> list = Collections.list(listModel.elements());
92  list.add(set);
93 
94  addSetList(list);
95 
96  ruleSetList.setSelectedValue(set, true);
97  }
98 
104  void removeRuleSet(RuleSet set) {
105  listModel.removeElement(set);
106  }
107 
113  void addNewRuleListener(ActionListener listener) {
114  newButton.addActionListener(listener);
115  }
116 
122  void addDeleteRuleListener(ActionListener listener) {
123  deleteButton.addActionListener(listener);
124  }
125 
131  void addListSelectionListener(ListSelectionListener listener) {
132  ruleSetList.addListSelectionListener(listener);
133  }
134 
140  RuleSet getSelectedRule() {
141  return ruleSetList.getSelectedValue();
142  }
143 
147  private final class RuleSetRenderer extends DefaultListCellRenderer {
148 
149  private static final long serialVersionUID = 1L;
150 
151  @Override
152  public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
153  super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
154 
155  if (value instanceof RuleSet) {
156  RuleSet set = (RuleSet) value;
157  setText(set.getName());
158  setToolTipText(set.getName());
159  }
160 
161  return this;
162  }
163  }
164 
170  @SuppressWarnings("unchecked")
171  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
172  private void initComponents() {
173  java.awt.GridBagConstraints gridBagConstraints;
174 
175  javax.swing.JTextPane descriptionField = new javax.swing.JTextPane();
176  javax.swing.JLabel ruleSetListLabel = new javax.swing.JLabel();
177  scrollPane = new javax.swing.JScrollPane();
178  javax.swing.JPanel buttonPanel = new javax.swing.JPanel();
179  newButton = new javax.swing.JButton();
180  deleteButton = new javax.swing.JButton();
181 
182  setLayout(new java.awt.GridBagLayout());
183 
184  descriptionField.setEditable(false);
185  descriptionField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
186  descriptionField.setText(org.openide.util.NbBundle.getMessage(RuleSetPanel.class, "RuleSetPanel.descriptionField.text")); // NOI18N
187  descriptionField.setOpaque(false);
188  gridBagConstraints = new java.awt.GridBagConstraints();
189  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
190  gridBagConstraints.weightx = 1.0;
191  add(descriptionField, gridBagConstraints);
192 
193  org.openide.awt.Mnemonics.setLocalizedText(ruleSetListLabel, org.openide.util.NbBundle.getMessage(RuleSetPanel.class, "RuleSetPanel.ruleSetListLabel.text")); // NOI18N
194  gridBagConstraints = new java.awt.GridBagConstraints();
195  gridBagConstraints.gridx = 0;
196  gridBagConstraints.gridy = 1;
197  gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
198  gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
199  add(ruleSetListLabel, gridBagConstraints);
200  gridBagConstraints = new java.awt.GridBagConstraints();
201  gridBagConstraints.gridx = 0;
202  gridBagConstraints.gridy = 2;
203  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
204  gridBagConstraints.weightx = 1.0;
205  gridBagConstraints.weighty = 1.0;
206  gridBagConstraints.insets = new java.awt.Insets(5, 0, 10, 0);
207  add(scrollPane, gridBagConstraints);
208 
209  buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 5, 0));
210 
211  newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/add16.png"))); // NOI18N
212  org.openide.awt.Mnemonics.setLocalizedText(newButton, org.openide.util.NbBundle.getMessage(RuleSetPanel.class, "RuleSetPanel.newButton.text")); // NOI18N
213  buttonPanel.add(newButton);
214 
215  deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/delete16.png"))); // NOI18N
216  org.openide.awt.Mnemonics.setLocalizedText(deleteButton, org.openide.util.NbBundle.getMessage(RuleSetPanel.class, "RuleSetPanel.deleteButton.text")); // NOI18N
217  buttonPanel.add(deleteButton);
218 
219  gridBagConstraints = new java.awt.GridBagConstraints();
220  gridBagConstraints.gridx = 0;
221  gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
222  add(buttonPanel, gridBagConstraints);
223  }// </editor-fold>//GEN-END:initComponents
224 
225 
226  // Variables declaration - do not modify//GEN-BEGIN:variables
227  private javax.swing.JButton deleteButton;
228  private javax.swing.JButton newButton;
229  private javax.swing.JScrollPane scrollPane;
230  // End of variables declaration//GEN-END:variables
231 }
Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus)
final DefaultListModel< RuleSet > listModel

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.