/*
*
* 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.csp.gui.dropbox;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.panbox.Settings;
import org.panbox.core.csp.CSPAdapterFactory;
import org.panbox.core.csp.StorageBackendType;
import org.panbox.desktop.common.gui.DropboxSettingsPanel;
import org.panbox.desktop.common.vfs.backend.dropbox.DropboxAPIIntegration;
import org.panbox.desktop.common.vfs.backend.dropbox.DropboxAdapterFactory;
import org.panbox.desktop.common.vfs.backend.dropbox.DropboxClientIntegration;
public class DropboxWizardDialog extends javax.swing.JDialog {
private static final long serialVersionUID = -4810796993171348135L;
private final static Logger logger = Logger
.getLogger(DropboxSettingsPanel.class);
private static final ResourceBundle bundle = ResourceBundle.getBundle(
"org.panbox.desktop.common.gui.Messages", Settings.getInstance()
.getLocale());
private DropboxAdapterFactory dropboxAdapterFactory;
private String dropboxSyncDirPath;
private String accessToken;
/**
* Creates new form DropboxWizardDialog
*/
public DropboxWizardDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
this.dropboxAdapterFactory = (DropboxAdapterFactory) CSPAdapterFactory
.getInstance(StorageBackendType.DROPBOX);
initComponents();
this.setLocationRelativeTo(null);
DropboxClientIntegration dcIntegration = (DropboxClientIntegration) dropboxAdapterFactory
.getClientAdapter();
try {
dropboxSyncDirPath = dcIntegration.readSyncDirFromMetadata()
.getAbsolutePath();
if( dropboxSyncDirPath != null ) {
syncDirTextField.setText(dropboxSyncDirPath);
}
} catch (IOException e) {
// if dropbox path could not be determined automatically we will
// need to set it manually!
}
}
/**
* 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() {
infoLabel = new javax.swing.JLabel();
dropboxSettingsPanel = new javax.swing.JPanel();
syncDirTextField = new javax.swing.JTextField();
syncDirChooseButton = new javax.swing.JButton();
accessTokenTextField = new javax.swing.JTextField();
accessTokenRefreshButton = new javax.swing.JButton();
syncDirLabel = new javax.swing.JLabel();
accessTokenLabel = new javax.swing.JLabel();
saveButton = new javax.swing.JButton();
skipButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle(bundle.getString("dropboxWizard.title")); // NOI18N
infoLabel.setText(bundle.getString("dropboxWizard.infoText")); // NOI18N
dropboxSettingsPanel.setBorder(javax.swing.BorderFactory
.createTitledBorder(bundle
.getString("dropboxWizard.groupTitle"))); // NOI18N
syncDirChooseButton.setText(bundle
.getString("DropboxSettingsPanel.syncDirButton")); // NOI18N
syncDirChooseButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
syncDirChooseButtonActionPerformed(evt);
}
});
accessTokenTextField.setEditable(false);
accessTokenRefreshButton.setText(bundle
.getString("DropboxSettingsPanel.accessTokenRefreshButton")); // NOI18N
accessTokenRefreshButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
accessTokenRefreshButtonActionPerformed(evt);
}
});
syncDirLabel.setText(bundle
.getString("DropboxSettingsPanel.syncDirLabel")); // NOI18N
accessTokenLabel.setText(bundle
.getString("DropboxSettingsPanel.accessTokenLabel")); // NOI18N
javax.swing.GroupLayout dropboxSettingsPanelLayout = new javax.swing.GroupLayout(
dropboxSettingsPanel);
dropboxSettingsPanel.setLayout(dropboxSettingsPanelLayout);
dropboxSettingsPanelLayout
.setHorizontalGroup(dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
dropboxSettingsPanelLayout
.createSequentialGroup()
.addContainerGap()
.addGroup(
dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(
syncDirLabel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
accessTokenLabel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
syncDirTextField,
javax.swing.GroupLayout.DEFAULT_SIZE,
387,
Short.MAX_VALUE)
.addComponent(
accessTokenTextField))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(
accessTokenRefreshButton,
javax.swing.GroupLayout.DEFAULT_SIZE,
90,
Short.MAX_VALUE)
.addComponent(
syncDirChooseButton,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addContainerGap()));
dropboxSettingsPanelLayout
.setVerticalGroup(dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
dropboxSettingsPanelLayout
.createSequentialGroup()
.addContainerGap()
.addGroup(
dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
syncDirTextField,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
syncDirChooseButton)
.addComponent(
syncDirLabel))
.addGap(18, 18, 18)
.addGroup(
dropboxSettingsPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
accessTokenTextField,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
accessTokenRefreshButton)
.addComponent(
accessTokenLabel))
.addContainerGap(
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
saveButton.setText(bundle.getString("dropboxWizard.saveButton")); // NOI18N
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
skipButton.setText(bundle.getString("dropboxWizard.noThanksButton")); // NOI18N
skipButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
skipButtonActionPerformed(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)
.addComponent(
infoLabel,
javax.swing.GroupLayout.PREFERRED_SIZE,
0, Short.MAX_VALUE)
.addComponent(
dropboxSettingsPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0,
0,
Short.MAX_VALUE)
.addComponent(
skipButton)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
saveButton)))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addComponent(infoLabel,
javax.swing.GroupLayout.DEFAULT_SIZE,
203, Short.MAX_VALUE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dropboxSettingsPanel,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(saveButton)
.addComponent(skipButton))
.addContainerGap()));
pack();
}// </editor-fold>//GEN-END:initComponents
private void syncDirChooseButtonActionPerformed(
java.awt.event.ActionEvent evt) {// GEN-FIRST:event_syncDirChooseButtonActionPerformed
DropboxClientIntegration dcIntegration = (DropboxClientIntegration) dropboxAdapterFactory
.getClientAdapter();
File syncDir = null;
try {
if (dcIntegration.isClientInstalled()) {
syncDir = dcIntegration.readSyncDirFromMetadata();
} else {
logger.error("No dropbox installation found!");
JOptionPane
.showMessageDialog(
this,
bundle.getString("DropboxSettingsPanel.noDropboxInstallationMsg"),
bundle.getString("DropboxSettingsPanel.noDropboxInstallationTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (IOException e) {
logger.error(
"Error while trying to resolve dropbox synchronization directory!",
e);
}
// if syncDir *may* be null at this point
JFileChooser fileChooser = null;
if (syncDir == null) {
fileChooser = new JFileChooser();
} else {
fileChooser = new JFileChooser(syncDir.getParent());
}
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setSelectedFile(syncDir);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File sel = fileChooser.getSelectedFile();
if ((sel != null) && (sel.isDirectory()) && (sel.canRead())) {
syncDirTextField.setText(sel.getAbsolutePath());
}
}
}// GEN-LAST:event_syncDirChooseButtonActionPerformed
private void accessTokenRefreshButtonActionPerformed(
java.awt.event.ActionEvent evt) {// GEN-FIRST:event_accessTokenRefreshButtonActionPerformed
try {
String res = DropboxAPIIntegration.authenticate();
if ((res != null) && (res.length() > 0)) {
accessTokenTextField.setText(res);
}
} catch (Exception e) {
logger.error(
"Unable to obtain dropbox access token due to exception.",
e);
}
}// GEN-LAST:event_accessTokenRefreshButtonActionPerformed
private void skipButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_skipButtonActionPerformed
dropboxSyncDirPath = null;
accessToken = null;
dispose();
}// GEN-LAST:event_skipButtonActionPerformed
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveButtonActionPerformed
dropboxSyncDirPath = syncDirTextField.getText();
accessToken = accessTokenTextField.getText();
if (dropboxSyncDirPath == null
|| !new File(dropboxSyncDirPath).exists()
|| accessToken == null) {
JOptionPane.showMessageDialog(null,
bundle.getString("dropboxWizard.WrongInputMsg"),
bundle.getString("dropboxWizard.WrongInputTitle"),
JOptionPane.ERROR_MESSAGE);
} else {
dispose();
}
}// GEN-LAST:event_saveButtonActionPerformed
public String getDropboxSyncDirPath() {
return dropboxSyncDirPath;
}
public String getAccessToken() {
return accessToken;
}
public void setDropboxSyncDirPath(String dropboxSyncDirPath) {
this.dropboxSyncDirPath = dropboxSyncDirPath;
syncDirTextField.setText(dropboxSyncDirPath);
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
accessTokenTextField.setText(accessToken);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel accessTokenLabel;
private javax.swing.JButton accessTokenRefreshButton;
private javax.swing.JTextField accessTokenTextField;
private javax.swing.JPanel dropboxSettingsPanel;
private javax.swing.JLabel infoLabel;
private javax.swing.JButton saveButton;
private javax.swing.JButton skipButton;
private javax.swing.JButton syncDirChooseButton;
private javax.swing.JLabel syncDirLabel;
private javax.swing.JTextField syncDirTextField;
// End of variables declaration//GEN-END:variables
}