/* * * Panbox - encryption for cloud storage * Copyright (C) 2014-2015 by Fraunhofer SIT and Sirrix AG * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Additonally, third party code may be provided with notices and open source * licenses from communities and third parties that govern the use of those * portions, and any licenses granted hereunder do not alter any rights and * obligations you may have under such open source licenses, however, the * disclaimer of warranty and limitation of liability provisions of the GPLv3 * will apply to all the product. * */ package org.panbox.desktop.common.gui; import org.panbox.Settings; import org.panbox.desktop.common.PanboxClient; import org.panbox.desktop.common.gui.addressbook.ContactListModel; import org.panbox.desktop.common.gui.addressbook.PanboxGUIContact; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.ResourceBundle; /** * @author palige * * Dialog for exporting one or more identities to a file */ public class ExportIdentitiesWoPINDialog extends javax.swing.JDialog { private static final ResourceBundle bundle = ResourceBundle.getBundle( "org.panbox.desktop.common.gui.Messages", Settings.getInstance() .getLocale()); /** * */ private static final long serialVersionUID = -1160716976951996434L; private final SimpleDateFormat df; private final PanboxClient client; private final List<PanboxGUIContact> contacts; private final ContactListModel contactModel; /** * Creates new form ExportIdentitiesDialog */ public ExportIdentitiesWoPINDialog(PanboxClient client, List<PanboxGUIContact> contacts) { super(client.getMainWindow()); this.client = client; this.contacts = contacts; this.df = new SimpleDateFormat("yyyy-MM-dd"); this.contactModel = new ContactListModel(); for (PanboxGUIContact c : contacts) { this.contactModel.addElement(c); } initComponents(); DocumentListener d = new DocumentListener() { @Override public void removeUpdate(DocumentEvent e) { checkExportability(); } @Override public void insertUpdate(DocumentEvent e) { checkExportability(); } @Override public void changedUpdate(DocumentEvent e) { checkExportability(); } }; fileLocTextField.getDocument().addDocumentListener(d); } /** * enables export button if all prerequisites for export are being met. * minimum PIN length is set to 4 characters. */ private void checkExportability() { if ((fileLocTextField.getText() == null) || fileLocTextField.getText().length() == 0) { exportButton.setEnabled(false); } else { exportButton.setEnabled(true); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { fileLocLabel = new javax.swing.JLabel(); fileLocTextField = new javax.swing.JTextField(); fileLocButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); exportButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/panbox/desktop/common/gui/Messages"); // NOI18N setTitle(bundle.getString("ExportIdentitiesDialog.title")); // NOI18N setModal(true); setResizable(false); fileLocLabel.setText(bundle.getString("ExportIdentitiesDialog.fileLocLabel")); // NOI18N fileLocTextField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fileLocTextFieldActionPerformed(evt); } }); fileLocButton.setText(bundle.getString("ExportIdentitiesDialog.fileLocButton")); // NOI18N fileLocButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fileLocButtonActionPerformed(evt); } }); cancelButton.setText(bundle.getString("ExportIdentitiesDialog.cancelButton")); // NOI18N cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); exportButton.setText(bundle.getString("ExportIdentitiesDialog.exportButton")); // NOI18N exportButton.setEnabled(false); exportButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exportButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(fileLocLabel) .addGap(13, 13, 13) .addComponent(fileLocTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(fileLocButton)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(exportButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelButton))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(fileLocLabel) .addComponent(fileLocTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(fileLocButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(exportButton) .addComponent(cancelButton)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed this.dispose(); }// GEN-LAST:event_cancelButtonActionPerformed private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exportButtonActionPerformed File vcardFile = new File(fileLocTextField.getText()); if (vcardFile.exists()) { int res = JOptionPane.showConfirmDialog(this, bundle.getString("file.exists.do.you.want.to.overwrite"), bundle.getString("file.exists"), JOptionPane.YES_NO_OPTION); if (res == JOptionPane.NO_OPTION) { return; } } else { try { vcardFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } if (!vcardFile.canWrite()) { JOptionPane.showMessageDialog(this, bundle.getString("cannot.write.file"), bundle.getString("error"), JOptionPane.ERROR_MESSAGE); } else { client.exportContacts(contacts, vcardFile); this.dispose(); } }// GEN-LAST:event_exportButtonActionPerformed private void fileLocTextFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_fileLocTextFieldActionPerformed // currently no implementation is needed here. }// GEN-LAST:event_fileLocTextFieldActionPerformed private void fileLocButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_fileLocButtonActionPerformed JFileChooser fileChooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Vcard file (.vcf)", "vcf"); fileChooser.setFileFilter(filter); fileChooser.setMultiSelectionEnabled(false); fileChooser.setSelectedFile(new File(System.getProperty("user.home") + File.separator + client.getIdentity().getFirstName().toLowerCase().replaceAll("\\s","") + "-" + client.getIdentity().getName().toLowerCase().replaceAll("\\s","") + "-" + df.format(new Date()) + ".vcf")); if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { String path = fileChooser.getSelectedFile().getAbsolutePath(); if (!path.endsWith(".vcf")) { path += ".vcf"; } fileLocTextField.setText(path); } }// GEN-LAST:event_fileLocButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancelButton; private javax.swing.JButton exportButton; private javax.swing.JButton fileLocButton; private javax.swing.JLabel fileLocLabel; private javax.swing.JTextField fileLocTextField; // End of variables declaration//GEN-END:variables }