Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CTOptionsPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2023 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 com.basistech.df.cybertriage.autopsy.ctoptions;
20 
22 import java.awt.Dimension;
23 import java.awt.GridBagConstraints;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.Collection;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 import javax.swing.JPanel;
32 import org.netbeans.spi.options.OptionsPanelController;
33 import org.openide.util.Lookup;
36 
41 
42  private static final int MAX_SUBPANEL_WIDTH = 650;
43 
44  private static final Logger logger = Logger.getLogger(CTOptionsPanel.class.getName());
45 
46  private final List<CTOptionsSubPanel> subPanels;
47 
52  public CTOptionsPanel() {
54  Collection<? extends CTOptionsSubPanel> coll = Lookup.getDefault().lookupAll(CTOptionsSubPanel.class);
55  Stream<? extends CTOptionsSubPanel> panelStream = coll != null ? coll.stream() : Stream.empty();
56  this.subPanels = panelStream
57  .map(panel -> {
58  try {
59  // lookup is returning singleton instances which means this panel gets messed up when accessed
60  // from multiple places because the panel's children are being added to a different CTOptionsPanel
61  return (CTOptionsSubPanel) panel.getClass().getConstructor().newInstance();
62  } catch (Exception ex) {
63  return null;
64  }
65  })
66  .filter(item -> item != null)
67  .sorted(Comparator.comparing(p -> p.getClass().getSimpleName().toUpperCase()).reversed())
68  .collect(Collectors.toList());
69  addSubOptionsPanels(this.subPanels);
70  }
71 
72  private void addSubOptionsPanels(List<CTOptionsSubPanel> subPanels) {
73  GridBagConstraints disclaimerConstraints = new GridBagConstraints();
74  disclaimerConstraints.gridx = 0;
75  disclaimerConstraints.gridy = 0;
76  disclaimerConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
77  disclaimerConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
78  disclaimerConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
79  disclaimerConstraints.weighty = 0;
80  disclaimerConstraints.weightx = 0;
81 
82  for (int i = 0; i < subPanels.size(); i++) {
83  CTOptionsSubPanel subPanel = subPanels.get(i);
84 
85  subPanel.addPropertyChangeListener(new PropertyChangeListener() {
86  @Override
87  public void propertyChange(PropertyChangeEvent evt) {
88  if (evt.getPropertyName().equals(OptionsPanelController.PROP_CHANGED)) {
89  CTOptionsPanel.this.firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
90  }
91  }
92  });
93 
94  GridBagConstraints gridBagConstraints = new GridBagConstraints();
95  gridBagConstraints.gridx = 0;
96  gridBagConstraints.gridy = i + 1;
97  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
98  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
99  gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
100  gridBagConstraints.weighty = 0;
101  gridBagConstraints.weightx = 0;
102 
103  contentPane.add(subPanel, gridBagConstraints);
104  }
105 
106  GridBagConstraints verticalConstraints = new GridBagConstraints();
107  verticalConstraints.gridx = 0;
108  verticalConstraints.gridy = subPanels.size() + 1;
109  verticalConstraints.weighty = 1;
110  verticalConstraints.weightx = 0;
111 
112  JPanel verticalSpacer = new JPanel();
113 
114  verticalSpacer.setMinimumSize(new Dimension(MAX_SUBPANEL_WIDTH, 0));
115  verticalSpacer.setPreferredSize(new Dimension(MAX_SUBPANEL_WIDTH, 0));
116  verticalSpacer.setMaximumSize(new Dimension(MAX_SUBPANEL_WIDTH, Short.MAX_VALUE));
117  contentPane.add(verticalSpacer, verticalConstraints);
118 
119  GridBagConstraints horizontalConstraints = new GridBagConstraints();
120  horizontalConstraints.gridx = 1;
121  horizontalConstraints.gridy = 0;
122  horizontalConstraints.weighty = 0;
123  horizontalConstraints.weightx = 1;
124 
125  JPanel horizontalSpacer = new JPanel();
126  contentPane.add(horizontalSpacer, horizontalConstraints);
127  }
128 
134  @SuppressWarnings("unchecked")
135  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
136  private void initComponents() {
137 
138  javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane();
139  contentPane = new javax.swing.JPanel();
140 
141  setLayout(new java.awt.BorderLayout());
142 
143  contentPane.setLayout(new java.awt.GridBagLayout());
144  scrollPane.setViewportView(contentPane);
145 
146  add(scrollPane, java.awt.BorderLayout.CENTER);
147  }// </editor-fold>//GEN-END:initComponents
148 
149  @Override
150  public void saveSettings() {
151  subPanels.forEach(panel -> panel.saveSettings());
152  }
153 
154  public void loadSavedSettings() {
155  subPanels.forEach(panel -> panel.loadSettings());
156  }
157 
158  public boolean valid() {
159  return subPanels.stream().allMatch(panel -> panel.valid());
160  }
161 
162 
163  // Variables declaration - do not modify//GEN-BEGIN:variables
164  private javax.swing.JPanel contentPane;
165  // End of variables declaration//GEN-END:variables
166 }
void addSubOptionsPanels(List< CTOptionsSubPanel > subPanels)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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.