Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddNewOrganizationDialog.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-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.centralrepository.optionspanel;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.logging.Level;
24 import javax.swing.JFrame;
25 import javax.swing.JTextField;
26 import javax.swing.event.DocumentEvent;
27 import javax.swing.event.DocumentListener;
28 import org.netbeans.spi.options.OptionsPanelController;
29 import org.openide.util.NbBundle.Messages;
30 import org.openide.windows.WindowManager;
35 
39 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
40 class AddNewOrganizationDialog extends javax.swing.JDialog {
41 
42  private static final Logger logger = Logger.getLogger(AddNewOrganizationDialog.class.getName());
43  private static final long serialVersionUID = 1L;
44 
45  private final Collection<JTextField> textBoxes;
46  private final TextBoxChangedListener textBoxChangedListener;
47  private boolean hasChanged;
48  private CentralRepoOrganization newOrg;
49  private final CentralRepoOrganization organizationToEdit;
50 
54  @Messages({"AddNewOrganizationDialog.addNewOrg.msg=Add New Organization"})
55  AddNewOrganizationDialog(javax.swing.JDialog parent) {
56  super(parent,
57  Bundle.AddNewOrganizationDialog_addNewOrg_msg(),
58  true); // NON-NLS
59  textBoxes = new ArrayList<>();
60  textBoxChangedListener = new TextBoxChangedListener();
61  hasChanged = false;
62  newOrg = null;
63  initComponents();
64  customizeComponents();
65  organizationToEdit = null;
66  display();
67  }
68 
69  // populates the dialog with existing case information to edit
70  public AddNewOrganizationDialog(javax.swing.JDialog parent, CentralRepoOrganization orgToEdit) {
71  super(parent,
72  Bundle.AddNewOrganizationDialog_addNewOrg_msg(),
73  true); // NON-NLS
74  organizationToEdit = orgToEdit;
75  textBoxes = new ArrayList<>();
76  textBoxChangedListener = new TextBoxChangedListener();
77  hasChanged = false;
78  newOrg = null;
79  initComponents();
80  customizeComponents();
81  tfOrganizationName.setText(orgToEdit.getName());
82  tfPocName.setText(orgToEdit.getPocName());
83  tfPocEmail.setText(orgToEdit.getPocEmail());
84  tfPocPhone.setText(orgToEdit.getPocPhone());
85  display();
86  }
87 
88  private void display() {
89  this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
90  setVisible(true);
91  }
92 
93  private void customizeComponents() {
94  setTextBoxListeners();
95  enableOkButton(false);
96  }
97 
101  private void setTextBoxListeners() {
102  textBoxes.add(tfOrganizationName);
103  textBoxes.add(tfPocEmail);
104  textBoxes.add(tfPocPhone);
105  textBoxes.add(tfPocName);
106  addDocumentListeners(textBoxes, textBoxChangedListener);
107  }
108 
115  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
116  textFields.forEach((textField) -> {
117  textField.getDocument().addDocumentListener(listener);
118  });
119  }
120 
127  private boolean requiredFieldsArePopulated() {
128  return !tfOrganizationName.getText().trim().isEmpty();
129  }
130 
136  @Messages({"AddNewOrganizationDialog.validation.incompleteFields=Organization Name is required."})
137  private boolean checkFields() {
138  boolean result = true;
139 
140  boolean isPopulated = requiredFieldsArePopulated();
141 
142  if (!isPopulated) {
143  // We don't even have everything filled out
144  result = false;
145  lbWarningMsg.setText(Bundle.AddNewOrganizationDialog_validation_incompleteFields());
146  }
147  return result;
148  }
149 
155  public boolean valid() {
156  lbWarningMsg.setText("");
157 
158  return enableOkButton(checkFields());
159  }
160 
168  private boolean enableOkButton(Boolean enable) {
169  bnOK.setEnabled(enable);
170  return enable;
171  }
172 
177  private class TextBoxChangedListener implements DocumentListener {
178 
179  @Override
180  public void changedUpdate(DocumentEvent e) {
181  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
182  valid();
183  }
184 
185  @Override
186  public void insertUpdate(DocumentEvent e) {
187  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
188  valid();
189  }
190 
191  @Override
192  public void removeUpdate(DocumentEvent e) {
193  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
194  valid();
195  }
196  }
197 
202  public boolean isChanged() {
203  return hasChanged;
204  }
205 
211  public CentralRepoOrganization getNewOrg() {
212  return newOrg;
213  }
214 
220  @SuppressWarnings("unchecked")
221  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
222  private void initComponents() {
223 
224  bnOK = new javax.swing.JButton();
225  bnCancel = new javax.swing.JButton();
226  lbOrganizationName = new javax.swing.JLabel();
227  lbPocHeading = new javax.swing.JLabel();
228  lbPocName = new javax.swing.JLabel();
229  lbPocEmail = new javax.swing.JLabel();
230  lbPocPhone = new javax.swing.JLabel();
231  tfPocName = new javax.swing.JTextField();
232  tfPocEmail = new javax.swing.JTextField();
233  tfPocPhone = new javax.swing.JTextField();
234  tfOrganizationName = new javax.swing.JTextField();
235  lbWarningMsg = new javax.swing.JLabel();
236 
237  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
238 
239  org.openide.awt.Mnemonics.setLocalizedText(bnOK, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.bnOK.text")); // NOI18N
240  bnOK.addActionListener(new java.awt.event.ActionListener() {
241  public void actionPerformed(java.awt.event.ActionEvent evt) {
242  bnOKActionPerformed(evt);
243  }
244  });
245 
246  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.bnCancel.text")); // NOI18N
247  bnCancel.addActionListener(new java.awt.event.ActionListener() {
248  public void actionPerformed(java.awt.event.ActionEvent evt) {
249  bnCancelActionPerformed(evt);
250  }
251  });
252 
253  org.openide.awt.Mnemonics.setLocalizedText(lbOrganizationName, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbOrganizationName.text")); // NOI18N
254 
255  org.openide.awt.Mnemonics.setLocalizedText(lbPocHeading, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocHeading.text")); // NOI18N
256 
257  org.openide.awt.Mnemonics.setLocalizedText(lbPocName, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocName.text")); // NOI18N
258 
259  org.openide.awt.Mnemonics.setLocalizedText(lbPocEmail, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocEmail.text")); // NOI18N
260 
261  org.openide.awt.Mnemonics.setLocalizedText(lbPocPhone, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocPhone.text")); // NOI18N
262 
263  tfPocName.setToolTipText(org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.tfName.tooltip")); // NOI18N
264 
265  lbWarningMsg.setFont(lbWarningMsg.getFont().deriveFont(lbWarningMsg.getFont().getStyle() | java.awt.Font.BOLD, lbWarningMsg.getFont().getSize()+1));
266  lbWarningMsg.setForeground(new java.awt.Color(255, 0, 0));
267 
268  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
269  getContentPane().setLayout(layout);
270  layout.setHorizontalGroup(
271  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
272  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
273  .addContainerGap(258, Short.MAX_VALUE)
274  .addComponent(bnOK)
275  .addGap(18, 18, 18)
276  .addComponent(bnCancel)
277  .addGap(12, 12, 12))
278  .addGroup(layout.createSequentialGroup()
279  .addGap(39, 39, 39)
280  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
281  .addComponent(lbPocEmail)
282  .addComponent(lbPocName)
283  .addComponent(lbPocPhone))
284  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
285  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
286  .addComponent(tfPocPhone)
287  .addComponent(tfPocName)
288  .addComponent(tfPocEmail))
289  .addContainerGap())
290  .addGroup(layout.createSequentialGroup()
291  .addGap(18, 18, 18)
292  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293  .addGroup(layout.createSequentialGroup()
294  .addComponent(lbOrganizationName)
295  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
296  .addComponent(tfOrganizationName))
297  .addGroup(layout.createSequentialGroup()
298  .addComponent(lbPocHeading)
299  .addGap(0, 0, Short.MAX_VALUE))
300  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
301  .addContainerGap())
302  );
303  layout.setVerticalGroup(
304  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
305  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
306  .addContainerGap()
307  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
308  .addComponent(lbOrganizationName)
309  .addComponent(tfOrganizationName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
310  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
311  .addComponent(lbPocHeading)
312  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
313  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
314  .addComponent(lbPocName)
315  .addComponent(tfPocName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
316  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
317  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
318  .addComponent(lbPocEmail)
319  .addComponent(tfPocEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
320  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
321  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
322  .addComponent(lbPocPhone)
323  .addComponent(tfPocPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
324  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
325  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
326  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
327  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
328  .addComponent(bnOK)
329  .addComponent(bnCancel))
330  .addContainerGap())
331  );
332 
333  pack();
334  }// </editor-fold>//GEN-END:initComponents
335 
336  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
337  dispose();
338  }//GEN-LAST:event_bnCancelActionPerformed
339 
340  @Messages({"AddNewOrganizationDialog.bnOk.addFailed.text=Failed to add new organization."})
341  private void bnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOKActionPerformed
342 
343  try {
344  CentralRepository dbManager = CentralRepository.getInstance();
345  if (organizationToEdit != null) {
346  // make a copy in case the update fails
347  newOrg = dbManager.getOrganizationByID(organizationToEdit.getOrgID());
348  newOrg.setName(tfOrganizationName.getText());
349  newOrg.setPocName(tfPocName.getText());
350  newOrg.setPocEmail(tfPocEmail.getText());
351  newOrg.setPocPhone(tfPocPhone.getText());
352  dbManager.updateOrganization(newOrg);
353  } else {
354  newOrg = new CentralRepoOrganization(
355  tfOrganizationName.getText(),
356  tfPocName.getText(),
357  tfPocEmail.getText(),
358  tfPocPhone.getText());
359  newOrg = dbManager.newOrganization(newOrg);
360  }
361  hasChanged = true;
362  dispose();
363  } catch (CentralRepoException ex) {
364  lbWarningMsg.setText(Bundle.AddNewOrganizationDialog_bnOk_addFailed_text());
365  logger.log(Level.SEVERE, "Failed adding new organization.", ex);
366  newOrg = null;
367  }
368  }//GEN-LAST:event_bnOKActionPerformed
369 
370 
371  // Variables declaration - do not modify//GEN-BEGIN:variables
372  private javax.swing.JButton bnCancel;
373  private javax.swing.JButton bnOK;
374  private javax.swing.JLabel lbOrganizationName;
375  private javax.swing.JLabel lbPocEmail;
376  private javax.swing.JLabel lbPocHeading;
377  private javax.swing.JLabel lbPocName;
378  private javax.swing.JLabel lbPocPhone;
379  private javax.swing.JLabel lbWarningMsg;
380  private javax.swing.JTextField tfOrganizationName;
381  private javax.swing.JTextField tfPocEmail;
382  private javax.swing.JTextField tfPocName;
383  private javax.swing.JTextField tfPocPhone;
384  // End of variables declaration//GEN-END:variables
385 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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