OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 47 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
47 ilm 1
package org.openconcerto.modules.google.docs;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.io.File;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.util.Properties;
11
 
12
import javax.swing.JButton;
13
import javax.swing.JCheckBox;
14
import javax.swing.JLabel;
15
import javax.swing.JOptionPane;
16
import javax.swing.JPasswordField;
17
import javax.swing.JTextField;
18
import javax.swing.SwingConstants;
19
 
20
import org.openconcerto.erp.preferences.DefaultLocalPreferencePanel;
21
import org.openconcerto.ui.DefaultGridBagConstraints;
22
 
23
import com.google.gdata.util.AuthenticationException;
24
 
25
public class GoogleDocsPreferencePanel extends DefaultLocalPreferencePanel {
26
    public static final String SHOW_MENU = "showMenu";
27
    public static final String AUTO = "auto";
28
    public static final String ACCOUNT_PASSWORD = "accountPassword";
29
    public static final String ACCOUNT_LOGIN = "accountLogin";
30
    public static final String GOOGLE_DOCS_PROPERTIES = "googledocs.properties";
31
    final JTextField textAccountLogin = new JTextField();
32
    final JPasswordField textAccountPassword = new JPasswordField();
33
    final JCheckBox checkAuto = new JCheckBox("Sauvegarder automatiquement les documents générés");
34
    final JCheckBox checkShowMenu = new JCheckBox("Activer le menu contextuel (clic droit sur les listes)");
35
 
36
    public GoogleDocsPreferencePanel() {
37
        super("Google Docs", GOOGLE_DOCS_PROPERTIES);
38
 
39
        this.setLayout(new GridBagLayout());
40
        GridBagConstraints c = new DefaultGridBagConstraints();
41
 
42
        // Account
43
        c.gridx = 0;
44
        c.gridy++;
45
 
46
        c.fill = GridBagConstraints.HORIZONTAL;
47
        c.gridwidth = 1;
48
        c.weightx = 0;
49
        this.add(new JLabel("Identifiant Google", SwingConstants.RIGHT), c);
50
        c.gridx++;
51
        c.weightx = 1;
52
        this.add(textAccountLogin, c);
53
 
54
        c.gridx = 0;
55
        c.gridy++;
56
        c.weightx = 0;
57
        this.add(new JLabel("Mot de passe", SwingConstants.RIGHT), c);
58
        c.gridx++;
59
        c.weightx = 1;
60
        this.add(textAccountPassword, c);
61
        c.fill = GridBagConstraints.HORIZONTAL;
62
        c.gridwidth = 2;
63
        c.gridy++;
64
        c.gridx = 0;
65
        c.weightx = 0;
66
 
67
        this.add(checkAuto, c);
68
        c.gridx = 0;
69
 
70
        c.gridy++;
71
 
72
        this.add(checkShowMenu, c);
73
        c.gridx = 0;
74
        c.gridy++;
75
 
76
        final JButton bTest = new JButton("Tester les paramètres");
77
        bTest.setOpaque(false);
78
        c.fill = GridBagConstraints.NONE;
79
        c.gridx = 0;
80
        c.gridy++;
81
        c.gridwidth = 2;
82
        c.weighty = 1;
83
        c.anchor = GridBagConstraints.NORTHEAST;
84
        this.add(bTest, c);
85
        bTest.addActionListener(new ActionListener() {
86
            @Override
87
            public void actionPerformed(ActionEvent e) {
88
 
89
                final Thread t = new Thread(new Runnable() {
90
 
91
                    @Override
92
                    public void run() {
93
                        try {
94
                            final GoogleDocsUtils ll = new GoogleDocsUtils("OpenConcerto");
95
                            ll.login(textAccountLogin.getText(), new String(textAccountPassword.getPassword()));
96
                            final File f = File.createTempFile("test_ovh", "openconcerto.txt");
97
                            final FileOutputStream fOut = new FileOutputStream(f);
98
                            fOut.write("OpenConcerto".getBytes());
99
                            fOut.flush();
100
                            fOut.close();
101
                            ll.uploadFile(f, "OpenConcerto/Devis/2010", "Test Google Docs", true);
102
                            JOptionPane.showMessageDialog(GoogleDocsPreferencePanel.this, "Connexion réussie");
103
                        } catch (AuthenticationException e) {
181 ilm 104
                            e.printStackTrace();
47 ilm 105
                            JOptionPane.showMessageDialog(GoogleDocsPreferencePanel.this, "Identifiant ou mot de passe invalide");
106
                        } catch (Throwable e1) {
181 ilm 107
                            e1.printStackTrace();
47 ilm 108
                            JOptionPane.showMessageDialog(GoogleDocsPreferencePanel.this, e1.getMessage());
109
                        }
110
                    }
111
                });
112
                t.setDaemon(true);
113
                t.setName("Test Google Docs");
114
                t.start();
115
            }
116
        });
117
        textAccountLogin.setText(properties.getProperty(ACCOUNT_LOGIN, ""));
118
        textAccountPassword.setText(properties.getProperty(ACCOUNT_PASSWORD, ""));
119
        checkAuto.setSelected(properties.getProperty(AUTO, "true").equals("true"));
120
        checkShowMenu.setSelected(properties.getProperty(SHOW_MENU, "false").equals("true"));
121
 
122
    }
123
 
124
    @Override
125
    public void storeValues() {
126
        properties.setProperty(ACCOUNT_LOGIN, textAccountLogin.getText());
127
        properties.setProperty(ACCOUNT_PASSWORD, String.valueOf(textAccountPassword.getPassword()));
128
        properties.setProperty(AUTO, String.valueOf(this.checkAuto.isSelected()));
129
        properties.setProperty(SHOW_MENU, String.valueOf(this.checkShowMenu.isSelected()));
130
        super.storeValues();
131
    }
132
 
133
    @Override
134
    public void restoreToDefaults() {
135
        textAccountLogin.setText("");
136
        textAccountPassword.setText("");
137
        checkAuto.setSelected(true);
138
        checkShowMenu.setSelected(false);
139
    }
140
 
141
    public static Properties getProperties() throws IOException {
142
        return DefaultLocalPreferencePanel.getPropertiesFromFile(GOOGLE_DOCS_PROPERTIES);
143
    }
144
 
145
}