Autopsy  4.6.0
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 public class AddNewOrganizationDialog extends javax.swing.JDialog {
40 
41  private static final Logger LOGGER = Logger.getLogger(AddNewOrganizationDialog.class.getName());
42  private static final long serialVersionUID = 1L;
43 
44  private final Collection<JTextField> textBoxes;
46  private boolean hasChanged;
49 
53  @Messages({"AddNewOrganizationDialog.addNewOrg.msg=Add New Organization"})
55  super((JFrame) WindowManager.getDefault().getMainWindow(),
56  Bundle.AddNewOrganizationDialog_addNewOrg_msg(),
57  true); // NON-NLS
58  textBoxes = new ArrayList<>();
59  textBoxChangedListener = new TextBoxChangedListener();
60  hasChanged = false;
61  newOrg = null;
64  organizationToEdit = null;
65  display();
66  }
67 
69  super((JFrame) WindowManager.getDefault().getMainWindow(),
70  Bundle.AddNewOrganizationDialog_addNewOrg_msg(),
71  true); // NON-NLS
72  organizationToEdit = orgToEdit;
73  textBoxes = new ArrayList<>();
74  textBoxChangedListener = new TextBoxChangedListener();
75  hasChanged = false;
76  newOrg = null;
79  tfOrganizationName.setText(orgToEdit.getName());
80  tfPocName.setText(orgToEdit.getPocName());
81  tfPocEmail.setText(orgToEdit.getPocEmail());
82  tfPocPhone.setText(orgToEdit.getPocPhone());
83  display();
84  }
85 
86  private void display() {
87  this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
88  setVisible(true);
89  }
90 
91  private void customizeComponents() {
93  enableOkButton(false);
94  }
95 
99  private void setTextBoxListeners() {
100  textBoxes.add(tfOrganizationName);
101  textBoxes.add(tfPocEmail);
102  textBoxes.add(tfPocPhone);
103  textBoxes.add(tfPocName);
104  addDocumentListeners(textBoxes, textBoxChangedListener);
105  }
106 
113  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
114  textFields.forEach((textField) -> {
115  textField.getDocument().addDocumentListener(listener);
116  });
117  }
118 
125  private boolean requiredFieldsArePopulated() {
126  return !tfOrganizationName.getText().trim().isEmpty();
127  }
128 
134  @Messages({"AddNewOrganizationDialog.validation.incompleteFields=Organization Name is required."})
135  private boolean checkFields() {
136  boolean result = true;
137 
138  boolean isPopulated = requiredFieldsArePopulated();
139 
140  if (!isPopulated) {
141  // We don't even have everything filled out
142  result = false;
143  lbWarningMsg.setText(Bundle.AddNewOrganizationDialog_validation_incompleteFields());
144  }
145  return result;
146  }
147 
153  public boolean valid() {
154  lbWarningMsg.setText("");
155 
156  return enableOkButton(checkFields());
157  }
158 
166  private boolean enableOkButton(Boolean enable) {
167  bnOK.setEnabled(enable);
168  return enable;
169  }
170 
175  private class TextBoxChangedListener implements DocumentListener {
176 
177  @Override
178  public void changedUpdate(DocumentEvent e) {
179  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
180  valid();
181  }
182 
183  @Override
184  public void insertUpdate(DocumentEvent e) {
185  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
186  valid();
187  }
188 
189  @Override
190  public void removeUpdate(DocumentEvent e) {
191  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
192  valid();
193  }
194  }
195 
196  public boolean isChanged() {
197  return hasChanged;
198  }
199 
201  return newOrg;
202  }
203 
209  @SuppressWarnings("unchecked")
210  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
211  private void initComponents() {
212 
213  bnOK = new javax.swing.JButton();
214  bnCancel = new javax.swing.JButton();
215  lbOrganizationName = new javax.swing.JLabel();
216  lbPocHeading = new javax.swing.JLabel();
217  lbPocName = new javax.swing.JLabel();
218  lbPocEmail = new javax.swing.JLabel();
219  lbPocPhone = new javax.swing.JLabel();
220  tfPocName = new javax.swing.JTextField();
221  tfPocEmail = new javax.swing.JTextField();
222  tfPocPhone = new javax.swing.JTextField();
223  tfOrganizationName = new javax.swing.JTextField();
224  lbWarningMsg = new javax.swing.JLabel();
225 
226  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
227 
228  org.openide.awt.Mnemonics.setLocalizedText(bnOK, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.bnOK.text")); // NOI18N
229  bnOK.addActionListener(new java.awt.event.ActionListener() {
230  public void actionPerformed(java.awt.event.ActionEvent evt) {
231  bnOKActionPerformed(evt);
232  }
233  });
234 
235  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.bnCancel.text")); // NOI18N
236  bnCancel.addActionListener(new java.awt.event.ActionListener() {
237  public void actionPerformed(java.awt.event.ActionEvent evt) {
239  }
240  });
241 
242  org.openide.awt.Mnemonics.setLocalizedText(lbOrganizationName, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbOrganizationName.text")); // NOI18N
243 
244  org.openide.awt.Mnemonics.setLocalizedText(lbPocHeading, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocHeading.text")); // NOI18N
245 
246  org.openide.awt.Mnemonics.setLocalizedText(lbPocName, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocName.text")); // NOI18N
247 
248  org.openide.awt.Mnemonics.setLocalizedText(lbPocEmail, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocEmail.text")); // NOI18N
249 
250  org.openide.awt.Mnemonics.setLocalizedText(lbPocPhone, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocPhone.text")); // NOI18N
251 
252  tfPocName.setToolTipText(org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.tfName.tooltip")); // NOI18N
253 
254  lbWarningMsg.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
255  lbWarningMsg.setForeground(new java.awt.Color(255, 0, 0));
256 
257  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
258  getContentPane().setLayout(layout);
259  layout.setHorizontalGroup(
260  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
261  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
262  .addContainerGap(258, Short.MAX_VALUE)
263  .addComponent(bnOK)
264  .addGap(18, 18, 18)
265  .addComponent(bnCancel)
266  .addGap(12, 12, 12))
267  .addGroup(layout.createSequentialGroup()
268  .addGap(39, 39, 39)
269  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
270  .addComponent(lbPocEmail)
271  .addComponent(lbPocName)
272  .addComponent(lbPocPhone))
273  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
274  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
275  .addComponent(tfPocPhone)
276  .addComponent(tfPocName)
277  .addComponent(tfPocEmail))
278  .addContainerGap())
279  .addGroup(layout.createSequentialGroup()
280  .addGap(18, 18, 18)
281  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
282  .addGroup(layout.createSequentialGroup()
283  .addComponent(lbOrganizationName)
284  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
285  .addComponent(tfOrganizationName))
286  .addGroup(layout.createSequentialGroup()
287  .addComponent(lbPocHeading)
288  .addGap(0, 0, Short.MAX_VALUE))
289  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
290  .addContainerGap())
291  );
292  layout.setVerticalGroup(
293  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
294  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
295  .addContainerGap()
296  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
297  .addComponent(lbOrganizationName)
298  .addComponent(tfOrganizationName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
299  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
300  .addComponent(lbPocHeading)
301  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
302  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
303  .addComponent(lbPocName)
304  .addComponent(tfPocName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
305  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
306  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
307  .addComponent(lbPocEmail)
308  .addComponent(tfPocEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
309  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
310  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
311  .addComponent(lbPocPhone)
312  .addComponent(tfPocPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
313  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
314  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
315  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
316  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
317  .addComponent(bnOK)
318  .addComponent(bnCancel))
319  .addContainerGap())
320  );
321 
322  pack();
323  }// </editor-fold>//GEN-END:initComponents
324 
325  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
326  dispose();
327  }//GEN-LAST:event_bnCancelActionPerformed
328 
329  @Messages({"AddNewOrganizationDialog.bnOk.addFailed.text=Failed to add new organization."})
330  private void bnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOKActionPerformed
331 
332  try {
333  EamDb dbManager = EamDb.getInstance();
334  if (organizationToEdit != null) {
335  //check if new name exists with ID other than the one in use here
336  newOrg = new EamOrganization(organizationToEdit.getOrgID(),
337  tfOrganizationName.getText(),
338  tfPocName.getText(),
339  tfPocEmail.getText(),
340  tfPocPhone.getText());
341  dbManager.updateOrganization(newOrg);
342  } else {
343  newOrg = new EamOrganization(
344  tfOrganizationName.getText(),
345  tfPocName.getText(),
346  tfPocEmail.getText(),
347  tfPocPhone.getText());
348  newOrg.setOrgID((int)dbManager.newOrganization(newOrg));
349  }
350  hasChanged = true;
351  dispose();
352  } catch (EamDbException ex) {
353  lbWarningMsg.setText(Bundle.AddNewOrganizationDialog_bnOk_addFailed_text());
354  LOGGER.log(Level.SEVERE, "Failed adding new organization.", ex);
355  }
356  }//GEN-LAST:event_bnOKActionPerformed
357 
358 
359  // Variables declaration - do not modify//GEN-BEGIN:variables
360  private javax.swing.JButton bnCancel;
361  private javax.swing.JButton bnOK;
362  private javax.swing.JLabel lbOrganizationName;
363  private javax.swing.JLabel lbPocEmail;
364  private javax.swing.JLabel lbPocHeading;
365  private javax.swing.JLabel lbPocName;
366  private javax.swing.JLabel lbPocPhone;
367  private javax.swing.JLabel lbWarningMsg;
368  private javax.swing.JTextField tfOrganizationName;
369  private javax.swing.JTextField tfPocEmail;
370  private javax.swing.JTextField tfPocName;
371  private javax.swing.JTextField tfPocPhone;
372  // End of variables declaration//GEN-END:variables
373 }
void updateOrganization(EamOrganization updatedOrganization)
static void addDocumentListeners(Collection< JTextField > textFields, TextBoxChangedListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.