Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CreatePersonaAccountDialog.java
Go to the documentation of this file.
1 /*
2  * Central Repository
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.centralrepository.persona;
20 
21 import java.awt.Component;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.logging.Level;
25 import javax.swing.JDialog;
26 import javax.swing.JLabel;
27 import javax.swing.JList;
28 import javax.swing.JOptionPane;
29 import javax.swing.ListCellRenderer;
30 import javax.swing.SwingUtilities;
31 import org.apache.commons.lang3.StringUtils;
32 import org.openide.util.NbBundle.Messages;
33 import org.openide.windows.WindowManager;
39 import org.sleuthkit.datamodel.InvalidAccountIDException;
40 
44 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
45 public class CreatePersonaAccountDialog extends JDialog {
46 
47  private static final Logger logger = Logger.getLogger(CreatePersonaAccountDialog.class.getName());
48 
49  private static final long serialVersionUID = 1L;
50 
51  private final TypeChoiceRenderer TYPE_CHOICE_RENDERER = new TypeChoiceRenderer();
52 
56  @Messages({"CreatePersonaAccountDialog.title.text=Create Account",})
58  super(SwingUtilities.windowForComponent(pdp),
59  Bundle.PersonaAccountDialog_title_text(),
60  ModalityType.APPLICATION_MODAL);
61 
62  initComponents();
63  typeComboBox.setRenderer(TYPE_CHOICE_RENDERER);
64  display();
65  }
66 
71  private class TypeChoiceRenderer extends JLabel implements ListCellRenderer<CentralRepoAccountType>, Serializable {
72 
73  private static final long serialVersionUID = 1L;
74 
75  @Override
76  public Component getListCellRendererComponent(
77  JList<? extends CentralRepoAccountType> list, CentralRepoAccountType value,
78  int index, boolean isSelected, boolean cellHasFocus) {
79  setText(value.getAcctType().getDisplayName());
80  return this;
81  }
82  }
83 
85  Collection<CentralRepoAccountType> allAccountTypes;
86  try {
87  allAccountTypes = CentralRepository.getInstance().getAllAccountTypes();
88  } catch (CentralRepoException e) {
89  logger.log(Level.SEVERE, "Failed to access central repository", e);
90  JOptionPane.showMessageDialog(this,
91  Bundle.PersonaAccountDialog_get_types_exception_Title(),
92  Bundle.PersonaAccountDialog_get_types_exception_msg(),
93  JOptionPane.ERROR_MESSAGE);
94  return new CentralRepoAccountType[0];
95  }
96  return allAccountTypes.toArray(new CentralRepoAccountType[0]);
97  }
98 
104  @SuppressWarnings("unchecked")
105  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
106  private void initComponents() {
107 
108  settingsPanel = new javax.swing.JPanel();
109  identiferLbl = new javax.swing.JLabel();
110  identifierTextField = new javax.swing.JTextField();
111  typeLbl = new javax.swing.JLabel();
112  typeComboBox = new javax.swing.JComboBox<>();
113  cancelBtn = new javax.swing.JButton();
114  okBtn = new javax.swing.JButton();
115 
116  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
117  setResizable(false);
118 
119  settingsPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
120 
121  org.openide.awt.Mnemonics.setLocalizedText(identiferLbl, org.openide.util.NbBundle.getMessage(CreatePersonaAccountDialog.class, "CreatePersonaAccountDialog.identiferLbl.text")); // NOI18N
122 
123  identifierTextField.setText(org.openide.util.NbBundle.getMessage(CreatePersonaAccountDialog.class, "CreatePersonaAccountDialog.identifierTextField.text")); // NOI18N
124  identifierTextField.addActionListener(new java.awt.event.ActionListener() {
125  public void actionPerformed(java.awt.event.ActionEvent evt) {
126  identifierTextFieldActionPerformed(evt);
127  }
128  });
129 
130  org.openide.awt.Mnemonics.setLocalizedText(typeLbl, org.openide.util.NbBundle.getMessage(CreatePersonaAccountDialog.class, "CreatePersonaAccountDialog.typeLbl.text")); // NOI18N
131 
132  typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(getAllAccountTypes()));
133 
134  javax.swing.GroupLayout settingsPanelLayout = new javax.swing.GroupLayout(settingsPanel);
135  settingsPanel.setLayout(settingsPanelLayout);
136  settingsPanelLayout.setHorizontalGroup(
137  settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
138  .addGroup(settingsPanelLayout.createSequentialGroup()
139  .addContainerGap()
140  .addGroup(settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
141  .addGroup(settingsPanelLayout.createSequentialGroup()
142  .addComponent(typeLbl)
143  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
144  .addComponent(typeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
145  .addGroup(settingsPanelLayout.createSequentialGroup()
146  .addComponent(identiferLbl)
147  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
148  .addComponent(identifierTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)))
149  .addContainerGap())
150  );
151  settingsPanelLayout.setVerticalGroup(
152  settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
153  .addGroup(settingsPanelLayout.createSequentialGroup()
154  .addContainerGap()
155  .addGroup(settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
156  .addComponent(identiferLbl)
157  .addComponent(identifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
158  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
159  .addGroup(settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
160  .addComponent(typeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
161  .addComponent(typeLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE))
162  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
163  );
164 
165  org.openide.awt.Mnemonics.setLocalizedText(cancelBtn, org.openide.util.NbBundle.getMessage(CreatePersonaAccountDialog.class, "CreatePersonaAccountDialog.cancelBtn.text")); // NOI18N
166  cancelBtn.setMaximumSize(new java.awt.Dimension(79, 23));
167  cancelBtn.setMinimumSize(new java.awt.Dimension(79, 23));
168  cancelBtn.setPreferredSize(new java.awt.Dimension(79, 23));
169  cancelBtn.addActionListener(new java.awt.event.ActionListener() {
170  public void actionPerformed(java.awt.event.ActionEvent evt) {
171  cancelBtnActionPerformed(evt);
172  }
173  });
174 
175  org.openide.awt.Mnemonics.setLocalizedText(okBtn, org.openide.util.NbBundle.getMessage(CreatePersonaAccountDialog.class, "CreatePersonaAccountDialog.okBtn.text")); // NOI18N
176  okBtn.addActionListener(new java.awt.event.ActionListener() {
177  public void actionPerformed(java.awt.event.ActionEvent evt) {
178  okBtnActionPerformed(evt);
179  }
180  });
181 
182  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
183  getContentPane().setLayout(layout);
184  layout.setHorizontalGroup(
185  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
186  .addGroup(layout.createSequentialGroup()
187  .addContainerGap(194, Short.MAX_VALUE)
188  .addComponent(okBtn)
189  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
190  .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
191  .addContainerGap())
192  .addComponent(settingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
193  );
194 
195  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelBtn, okBtn});
196 
197  layout.setVerticalGroup(
198  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
199  .addGroup(layout.createSequentialGroup()
200  .addContainerGap()
201  .addComponent(settingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
202  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
203  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
204  .addComponent(okBtn)
205  .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
206  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
207  );
208 
209  pack();
210  }// </editor-fold>//GEN-END:initComponents
211 
212  private void display() {
213  this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
214  setVisible(true);
215  }
216 
217  @Messages({
218  "CreatePersonaAccountDialog_error_title=Account failure",
219  "CreatePersonaAccountDialog_error_msg=Failed to create account.",
220  "CreatePersonaAccountDialog_invalid_account_Title=Invalid account identifier",
221  "CreatePersonaAccountDialog_invalid_account_msg=Account identifier is not valid.",})
223  CentralRepoAccount ret = null;
224  try {
226  if (cr != null) {
227  ret = cr.getOrCreateAccount(type, identifier);
228  }
229  } catch (CentralRepoException e) {
230  logger.log(Level.SEVERE, "Failed to create account", e);
231  JOptionPane.showMessageDialog(this,
232  Bundle.CreatePersonaAccountDialog_error_msg(),
233  Bundle.CreatePersonaAccountDialog_error_title(),
234  JOptionPane.ERROR_MESSAGE);
235  } catch (InvalidAccountIDException e) {
236  logger.log(Level.WARNING, "Invalid account identifier", e);
237  JOptionPane.showMessageDialog(this,
238  Bundle.CreatePersonaAccountDialog_invalid_account_msg(),
239  Bundle.CreatePersonaAccountDialog_invalid_account_Title(),
240  JOptionPane.ERROR_MESSAGE);
241  }
242  return ret;
243  }
244 
245  @Messages({
246  "CreatePersonaAccountDialog_success_title=Account added",
247  "CreatePersonaAccountDialog_success_msg=Account added.",
248  })
249  private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
250  if (StringUtils.isBlank(identifierTextField.getText())) {
251  JOptionPane.showMessageDialog(this,
252  Bundle.PersonaAccountDialog_identifier_empty_msg(),
253  Bundle.PersonaAccountDialog_identifier_empty_Title(),
254  JOptionPane.ERROR_MESSAGE);
255  return;
256  }
257 
259  (CentralRepoAccount.CentralRepoAccountType) typeComboBox.getSelectedItem();
260  String identifier = identifierTextField.getText();
261 
262  if (createAccount(type, identifier) != null) {
263  // show account created message
264  JOptionPane.showMessageDialog(this,
265  Bundle.CreatePersonaAccountDialog_success_msg(),
266  Bundle.CreatePersonaAccountDialog_success_title(),
267  JOptionPane.INFORMATION_MESSAGE);
268 
269  dispose();
270  }
271  }//GEN-LAST:event_okBtnActionPerformed
272 
273  private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
274  dispose();
275  }//GEN-LAST:event_cancelBtnActionPerformed
276 
277  private void identifierTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_identifierTextFieldActionPerformed
278  // TODO add your handling code here:
279  }//GEN-LAST:event_identifierTextFieldActionPerformed
280 
281  // Variables declaration - do not modify//GEN-BEGIN:variables
282  private javax.swing.JButton cancelBtn;
283  private javax.swing.JLabel identiferLbl;
284  private javax.swing.JTextField identifierTextField;
285  private javax.swing.JButton okBtn;
286  private javax.swing.JPanel settingsPanel;
288  private javax.swing.JLabel typeLbl;
289  // End of variables declaration//GEN-END:variables
290 }
Component getListCellRendererComponent(JList<?extends CentralRepoAccountType > list, CentralRepoAccountType value, int index, boolean isSelected, boolean cellHasFocus)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
javax.swing.JComboBox< org.sleuthkit.autopsy.centralrepository.datamodel.CentralRepoAccount.CentralRepoAccountType > typeComboBox
CentralRepoAccount createAccount(CentralRepoAccount.CentralRepoAccountType type, String identifier)
CentralRepoAccount getOrCreateAccount(CentralRepoAccount.CentralRepoAccountType crAccountType, String accountUniqueID)

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.