Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
KeywordSearchGlobalLanguageSettingsPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2018 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.keywordsearch;
20 
21 import java.awt.EventQueue;
22 import java.awt.GridLayout;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import javax.swing.JCheckBox;
32 import org.netbeans.spi.options.OptionsPanelController;
39 
43 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
44 class KeywordSearchGlobalLanguageSettingsPanel extends javax.swing.JPanel implements OptionsPanel {
45 
46  private final Map<String, StringExtract.StringExtractUnicodeTable.SCRIPT> scripts = new HashMap<>();
47  private ActionListener updateLanguagesAction;
48  private List<SCRIPT> toUpdate;
49 
50  KeywordSearchGlobalLanguageSettingsPanel() {
51  initComponents();
52  customizeComponents();
53  }
54 
55  private void customizeComponents() {
56  updateLanguagesAction = new ActionListener() {
57  @Override
58  public void actionPerformed(ActionEvent e) {
59  toUpdate = new ArrayList<>();
60  final int components = checkPanel.getComponentCount();
61  for (int i = 0; i < components; ++i) {
62  JCheckBox ch = (JCheckBox) checkPanel.getComponent(i);
63  if (ch.isSelected()) {
64  SCRIPT s = scripts.get(ch.getText());
65  toUpdate.add(s);
66  }
67  }
68  }
69  };
70 
71  initScriptsCheckBoxes();
72  reloadScriptsCheckBoxes();
73 
74  //allow panel to toggle its enabled status while it is open based on ingest events
75  IngestManager.getInstance().addIngestJobEventListener(new PropertyChangeListener() {
76  @Override
77  public void propertyChange(PropertyChangeEvent evt) {
78  Object source = evt.getSource();
79  if (source instanceof String && ((String) source).equals("LOCAL")) { //NON-NLS
80  EventQueue.invokeLater(() -> {
81  activateWidgets();
82  });
83  }
84  }
85  });
86  }
87 
88  private void activateScriptsCheckboxes(boolean activate) {
89  final int components = checkPanel.getComponentCount();
90  for (int i = 0; i < components; ++i) {
91  JCheckBox ch = (JCheckBox) checkPanel.getComponent(i);
92  ch.setEnabled(activate);
93  }
94  }
95 
96  private static String getLangText(SCRIPT script) {
97  StringBuilder sb = new StringBuilder();
98  sb.append(script.toString()).append(" (");
99  sb.append(script.getLanguages());
100  sb.append(")");
101  return sb.toString();
102  }
103 
104  private void initScriptsCheckBoxes() {
105  final List<StringExtract.StringExtractUnicodeTable.SCRIPT> supportedScripts = StringExtract.getSupportedScripts();
106  checkPanel.setLayout(new GridLayout(0, 1));
107  for (StringExtract.StringExtractUnicodeTable.SCRIPT s : supportedScripts) {
108  String text = getLangText(s);
109  JCheckBox ch = new JCheckBox(text);
110  ch.addActionListener(new ActionListener() {
111  @Override
112  public void actionPerformed(ActionEvent e) {
113  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
114  }
115  });
116  ch.addActionListener(updateLanguagesAction);
117  checkPanel.add(ch);
118  ch.setSelected(false);
119  scripts.put(text, s);
120  }
121  }
122 
123  private void reloadScriptsCheckBoxes() {
124  boolean utf16
125  = Boolean.parseBoolean(KeywordSearchSettings.getStringExtractOption(StringsExtractOptions.EXTRACT_UTF16.toString()));
126 
127  enableUTF16Checkbox.setSelected(utf16);
128 
129  boolean utf8
130  = Boolean.parseBoolean(KeywordSearchSettings.getStringExtractOption(StringsExtractOptions.EXTRACT_UTF8.toString()));
131  enableUTF8Checkbox.setSelected(utf8);
132 
133  final List<SCRIPT> serviceScripts = KeywordSearchSettings.getStringExtractScripts();
134  final int components = checkPanel.getComponentCount();
135 
136  for (int i = 0; i < components; ++i) {
137  JCheckBox ch = (JCheckBox) checkPanel.getComponent(i);
138 
139  StringExtract.StringExtractUnicodeTable.SCRIPT script = scripts.get(ch.getText());
140 
141  ch.setSelected(serviceScripts.contains(script));
142  }
143  }
144 
145  private void activateWidgets() {
146  reloadScriptsCheckBoxes();
147 
148  boolean utf16
149  = Boolean.parseBoolean(KeywordSearchSettings.getStringExtractOption(StringsExtractOptions.EXTRACT_UTF16.toString()));
150 
151  enableUTF16Checkbox.setSelected(utf16);
152 
153  boolean utf8
154  = Boolean.parseBoolean(KeywordSearchSettings.getStringExtractOption(StringsExtractOptions.EXTRACT_UTF8.toString()));
155  enableUTF8Checkbox.setSelected(utf8);
156  final boolean extractEnabled = utf16 || utf8;
157 
158  boolean ingestRunning = IngestManager.getInstance().isIngestRunning();
159  //enable / disable checboxes
160  activateScriptsCheckboxes(extractEnabled && !ingestRunning);
161  ingestWarningLabel.setVisible(ingestRunning);
162  enableUTF16Checkbox.setEnabled(!ingestRunning);
163  enableUTF8Checkbox.setEnabled(!ingestRunning);
164  }
165 
171  @SuppressWarnings("unchecked")
172  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
173  private void initComponents() {
174 
175  languagesLabel = new javax.swing.JLabel();
176  langPanel = new javax.swing.JScrollPane();
177  checkPanel = new javax.swing.JPanel();
178  enableUTF8Checkbox = new javax.swing.JCheckBox();
179  enableUTF16Checkbox = new javax.swing.JCheckBox();
180  ingestSettingsLabel = new javax.swing.JLabel();
181  ingestWarningLabel = new javax.swing.JLabel();
182 
183  org.openide.awt.Mnemonics.setLocalizedText(languagesLabel, org.openide.util.NbBundle.getMessage(KeywordSearchGlobalLanguageSettingsPanel.class, "KeywordSearchGlobalLanguageSettingsPanel.languagesLabel.text")); // NOI18N
184 
185  langPanel.setPreferredSize(new java.awt.Dimension(430, 361));
186 
187  checkPanel.setPreferredSize(new java.awt.Dimension(400, 361));
188 
189  javax.swing.GroupLayout checkPanelLayout = new javax.swing.GroupLayout(checkPanel);
190  checkPanel.setLayout(checkPanelLayout);
191  checkPanelLayout.setHorizontalGroup(
192  checkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
193  .addGap(0, 428, Short.MAX_VALUE)
194  );
195  checkPanelLayout.setVerticalGroup(
196  checkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
197  .addGap(0, 406, Short.MAX_VALUE)
198  );
199 
200  langPanel.setViewportView(checkPanel);
201 
202  org.openide.awt.Mnemonics.setLocalizedText(enableUTF8Checkbox, org.openide.util.NbBundle.getMessage(KeywordSearchGlobalLanguageSettingsPanel.class, "KeywordSearchGlobalLanguageSettingsPanel.enableUTF8Checkbox.text")); // NOI18N
203  enableUTF8Checkbox.addActionListener(new java.awt.event.ActionListener() {
204  public void actionPerformed(java.awt.event.ActionEvent evt) {
205  enableUTF8CheckboxActionPerformed(evt);
206  }
207  });
208 
209  org.openide.awt.Mnemonics.setLocalizedText(enableUTF16Checkbox, org.openide.util.NbBundle.getMessage(KeywordSearchGlobalLanguageSettingsPanel.class, "KeywordSearchGlobalLanguageSettingsPanel.enableUTF16Checkbox.text")); // NOI18N
210  enableUTF16Checkbox.addActionListener(new java.awt.event.ActionListener() {
211  public void actionPerformed(java.awt.event.ActionEvent evt) {
212  enableUTF16CheckboxActionPerformed(evt);
213  }
214  });
215 
216  org.openide.awt.Mnemonics.setLocalizedText(ingestSettingsLabel, org.openide.util.NbBundle.getMessage(KeywordSearchGlobalLanguageSettingsPanel.class, "KeywordSearchGlobalLanguageSettingsPanel.ingestSettingsLabel.text")); // NOI18N
217 
218  ingestWarningLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/modules/hashdatabase/warning16.png"))); // NOI18N
219  org.openide.awt.Mnemonics.setLocalizedText(ingestWarningLabel, org.openide.util.NbBundle.getMessage(KeywordSearchGlobalLanguageSettingsPanel.class, "KeywordSearchGlobalLanguageSettingsPanel.ingestWarningLabel.text")); // NOI18N
220 
221  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
222  this.setLayout(layout);
223  layout.setHorizontalGroup(
224  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
225  .addGroup(layout.createSequentialGroup()
226  .addContainerGap()
227  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
228  .addGroup(layout.createSequentialGroup()
229  .addComponent(langPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
230  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
231  .addComponent(ingestWarningLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
232  .addGroup(layout.createSequentialGroup()
233  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
234  .addComponent(languagesLabel)
235  .addGroup(layout.createSequentialGroup()
236  .addGap(16, 16, 16)
237  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
238  .addComponent(enableUTF16Checkbox)
239  .addComponent(enableUTF8Checkbox)))
240  .addComponent(ingestSettingsLabel))
241  .addGap(0, 0, Short.MAX_VALUE)))
242  .addContainerGap())
243  );
244  layout.setVerticalGroup(
245  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
246  .addGroup(layout.createSequentialGroup()
247  .addContainerGap()
248  .addComponent(ingestSettingsLabel)
249  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
250  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
251  .addGroup(layout.createSequentialGroup()
252  .addComponent(enableUTF16Checkbox)
253  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
254  .addComponent(enableUTF8Checkbox)
255  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
256  .addComponent(languagesLabel)
257  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
258  .addComponent(langPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
259  .addGroup(layout.createSequentialGroup()
260  .addGap(0, 0, Short.MAX_VALUE)
261  .addComponent(ingestWarningLabel)))
262  .addContainerGap())
263  );
264  }// </editor-fold>//GEN-END:initComponents
265 
266  private void enableUTF8CheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableUTF8CheckboxActionPerformed
267 
268  boolean selected = this.enableUTF8Checkbox.isSelected();
269 
270  activateScriptsCheckboxes(selected || this.enableUTF16Checkbox.isSelected());
271  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
272 
273  }//GEN-LAST:event_enableUTF8CheckboxActionPerformed
274 
275  private void enableUTF16CheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableUTF16CheckboxActionPerformed
276 
277  boolean selected = this.enableUTF16Checkbox.isSelected();
278 
279  activateScriptsCheckboxes(selected || this.enableUTF8Checkbox.isSelected());
280  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
281  }//GEN-LAST:event_enableUTF16CheckboxActionPerformed
282 
283  // Variables declaration - do not modify//GEN-BEGIN:variables
284  private javax.swing.JPanel checkPanel;
285  private javax.swing.JCheckBox enableUTF16Checkbox;
286  private javax.swing.JCheckBox enableUTF8Checkbox;
287  private javax.swing.JLabel ingestSettingsLabel;
288  private javax.swing.JLabel ingestWarningLabel;
289  private javax.swing.JScrollPane langPanel;
290  private javax.swing.JLabel languagesLabel;
291  // End of variables declaration//GEN-END:variables
292 
293  @Override
294  public void store() {
295  KeywordSearchSettings.setStringExtractOption(StringsExtractOptions.EXTRACT_UTF8.toString(),
296  Boolean.toString(enableUTF8Checkbox.isSelected()));
297  KeywordSearchSettings.setStringExtractOption(StringsExtractOptions.EXTRACT_UTF16.toString(),
298  Boolean.toString(enableUTF16Checkbox.isSelected()));
299 
300  if (toUpdate != null) {
301  KeywordSearchSettings.setStringExtractScripts(toUpdate);
302  }
303 
304  // This is a stop-gap way of notifying the job settings panel of potential changes.
305  XmlKeywordSearchList.getCurrent().fireLanguagesEvent(KeywordSearchList.LanguagesEvent.LANGUAGES_CHANGED);
306  }
307 
308  @Override
309  public void load() {
310  activateWidgets();
311  }
312 }

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