Autopsy  4.5.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-2017 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.awt.Dimension;
22 import java.awt.Toolkit;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.logging.Level;
26 import javax.swing.JFrame;
27 import javax.swing.JTextField;
28 import javax.swing.event.DocumentEvent;
29 import javax.swing.event.DocumentListener;
30 import org.netbeans.spi.options.OptionsPanelController;
31 import org.openide.util.NbBundle.Messages;
32 import org.openide.windows.WindowManager;
37 
41 public class AddNewOrganizationDialog extends javax.swing.JDialog {
42 
43  private static final Logger LOGGER = Logger.getLogger(AddNewOrganizationDialog.class.getName());
44  private static final long serialVersionUID = 1L;
45 
46  private final Collection<JTextField> textBoxes;
48  private boolean hasChanged;
51 
55  @Messages({"AddNewOrganizationDialog.addNewOrg.msg=Add New Organization"})
57  super((JFrame) WindowManager.getDefault().getMainWindow(),
58  Bundle.AddNewOrganizationDialog_addNewOrg_msg(),
59  true); // NON-NLS
60  textBoxes = new ArrayList<>();
61  textBoxChangedListener = new TextBoxChangedListener();
62  hasChanged = false;
63  newOrg = null;
66  organizationToEdit = null;
67  display();
68  }
69 
71  super((JFrame) WindowManager.getDefault().getMainWindow(),
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;
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  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
90  setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
91  setVisible(true);
92  }
93 
94  private void customizeComponents() {
96  enableOkButton(false);
97  }
98 
102  private void setTextBoxListeners() {
103  textBoxes.add(tfOrganizationName);
104  textBoxes.add(tfPocEmail);
105  textBoxes.add(tfPocPhone);
106  textBoxes.add(tfPocName);
107  addDocumentListeners(textBoxes, textBoxChangedListener);
108  }
109 
116  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
117  textFields.forEach((textField) -> {
118  textField.getDocument().addDocumentListener(listener);
119  });
120  }
121 
128  private boolean requiredFieldsArePopulated() {
129  return !tfOrganizationName.getText().trim().isEmpty();
130  }
131 
137  @Messages({"AddNewOrganizationDialog.validation.incompleteFields=Organization Name is required."})
138  private boolean checkFields() {
139  boolean result = true;
140 
141  boolean isPopulated = requiredFieldsArePopulated();
142 
143  if (!isPopulated) {
144  // We don't even have everything filled out
145  result = false;
146  lbWarningMsg.setText(Bundle.AddNewOrganizationDialog_validation_incompleteFields());
147  }
148  return result;
149  }
150 
156  public boolean valid() {
157  lbWarningMsg.setText("");
158 
159  return enableOkButton(checkFields());
160  }
161 
169  private boolean enableOkButton(Boolean enable) {
170  bnOK.setEnabled(enable);
171  return enable;
172  }
173 
178  private class TextBoxChangedListener implements DocumentListener {
179 
180  @Override
181  public void changedUpdate(DocumentEvent e) {
182  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
183  valid();
184  }
185 
186  @Override
187  public void insertUpdate(DocumentEvent e) {
188  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
189  valid();
190  }
191 
192  @Override
193  public void removeUpdate(DocumentEvent e) {
194  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
195  valid();
196  }
197  }
198 
199  public boolean isChanged() {
200  return hasChanged;
201  }
202 
204  return newOrg;
205  }
206 
212  @SuppressWarnings("unchecked")
213  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
214  private void initComponents() {
215 
216  bnOK = new javax.swing.JButton();
217  bnCancel = new javax.swing.JButton();
218  lbOrganizationName = new javax.swing.JLabel();
219  lbPocHeading = new javax.swing.JLabel();
220  lbPocName = new javax.swing.JLabel();
221  lbPocEmail = new javax.swing.JLabel();
222  lbPocPhone = new javax.swing.JLabel();
223  tfPocName = new javax.swing.JTextField();
224  tfPocEmail = new javax.swing.JTextField();
225  tfPocPhone = new javax.swing.JTextField();
226  tfOrganizationName = new javax.swing.JTextField();
227  lbWarningMsg = new javax.swing.JLabel();
228 
229  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
230 
231  org.openide.awt.Mnemonics.setLocalizedText(bnOK, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.bnOK.text")); // NOI18N
232  bnOK.addActionListener(new java.awt.event.ActionListener() {
233  public void actionPerformed(java.awt.event.ActionEvent evt) {
234  bnOKActionPerformed(evt);
235  }
236  });
237 
238  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.bnCancel.text")); // NOI18N
239  bnCancel.addActionListener(new java.awt.event.ActionListener() {
240  public void actionPerformed(java.awt.event.ActionEvent evt) {
242  }
243  });
244 
245  org.openide.awt.Mnemonics.setLocalizedText(lbOrganizationName, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbOrganizationName.text")); // NOI18N
246 
247  org.openide.awt.Mnemonics.setLocalizedText(lbPocHeading, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocHeading.text")); // NOI18N
248 
249  org.openide.awt.Mnemonics.setLocalizedText(lbPocName, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocName.text")); // NOI18N
250 
251  org.openide.awt.Mnemonics.setLocalizedText(lbPocEmail, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocEmail.text")); // NOI18N
252 
253  org.openide.awt.Mnemonics.setLocalizedText(lbPocPhone, org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.lbPocPhone.text")); // NOI18N
254 
255  tfPocName.setToolTipText(org.openide.util.NbBundle.getMessage(AddNewOrganizationDialog.class, "AddNewOrganizationDialog.tfName.tooltip")); // NOI18N
256 
257  lbWarningMsg.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
258  lbWarningMsg.setForeground(new java.awt.Color(255, 0, 0));
259 
260  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
261  getContentPane().setLayout(layout);
262  layout.setHorizontalGroup(
263  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
264  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
265  .addContainerGap(258, Short.MAX_VALUE)
266  .addComponent(bnOK)
267  .addGap(18, 18, 18)
268  .addComponent(bnCancel)
269  .addGap(12, 12, 12))
270  .addGroup(layout.createSequentialGroup()
271  .addGap(39, 39, 39)
272  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
273  .addComponent(lbPocEmail)
274  .addComponent(lbPocName)
275  .addComponent(lbPocPhone))
276  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
277  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
278  .addComponent(tfPocPhone)
279  .addComponent(tfPocName)
280  .addComponent(tfPocEmail))
281  .addContainerGap())
282  .addGroup(layout.createSequentialGroup()
283  .addGap(18, 18, 18)
284  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
285  .addGroup(layout.createSequentialGroup()
286  .addComponent(lbOrganizationName, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
287  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
288  .addComponent(tfOrganizationName))
289  .addGroup(layout.createSequentialGroup()
290  .addComponent(lbPocHeading)
291  .addGap(0, 0, Short.MAX_VALUE))
292  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
293  .addContainerGap())
294  );
295  layout.setVerticalGroup(
296  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
297  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
298  .addContainerGap()
299  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
300  .addComponent(lbOrganizationName, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
301  .addComponent(tfOrganizationName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
302  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
303  .addComponent(lbPocHeading)
304  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
305  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
306  .addComponent(lbPocName)
307  .addComponent(tfPocName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
308  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
309  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
310  .addComponent(lbPocEmail)
311  .addComponent(tfPocEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
312  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
313  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
314  .addComponent(lbPocPhone)
315  .addComponent(tfPocPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
316  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
317  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
318  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
319  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
320  .addComponent(bnOK)
321  .addComponent(bnCancel))
322  .addContainerGap())
323  );
324 
325  pack();
326  }// </editor-fold>//GEN-END:initComponents
327 
328  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
329  dispose();
330  }//GEN-LAST:event_bnCancelActionPerformed
331 
332  @Messages({"AddNewOrganizationDialog.bnOk.addFailed.text=Failed to add new organization."})
333  private void bnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOKActionPerformed
334 
335  try {
336  EamDb dbManager = EamDb.getInstance();
337  if (organizationToEdit != null) {
338  //check if new name exists with ID other than the one in use here
339  newOrg = new EamOrganization(organizationToEdit.getOrgID(),
340  tfOrganizationName.getText(),
341  tfPocName.getText(),
342  tfPocEmail.getText(),
343  tfPocPhone.getText());
344  dbManager.updateOrganization(newOrg);
345  } else {
346  newOrg = new EamOrganization(
347  tfOrganizationName.getText(),
348  tfPocName.getText(),
349  tfPocEmail.getText(),
350  tfPocPhone.getText());
351  newOrg.setOrgID((int)dbManager.newOrganization(newOrg));
352  }
353  hasChanged = true;
354  dispose();
355  } catch (EamDbException ex) {
356  lbWarningMsg.setText(Bundle.AddNewOrganizationDialog_bnOk_addFailed_text());
357  LOGGER.log(Level.SEVERE, "Failed adding new organization.", ex);
358  }
359  }//GEN-LAST:event_bnOKActionPerformed
360 
361 
362  // Variables declaration - do not modify//GEN-BEGIN:variables
363  private javax.swing.JButton bnCancel;
364  private javax.swing.JButton bnOK;
365  private javax.swing.JLabel lbOrganizationName;
366  private javax.swing.JLabel lbPocEmail;
367  private javax.swing.JLabel lbPocHeading;
368  private javax.swing.JLabel lbPocName;
369  private javax.swing.JLabel lbPocPhone;
370  private javax.swing.JLabel lbWarningMsg;
371  private javax.swing.JTextField tfOrganizationName;
372  private javax.swing.JTextField tfPocEmail;
373  private javax.swing.JTextField tfPocName;
374  private javax.swing.JTextField tfPocPhone;
375  // End of variables declaration//GEN-END:variables
376 }
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: Tue Feb 20 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.