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