OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 177
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.panel;
14
 package org.openconcerto.erp.panel;
15
 
15
 
16
import org.openconcerto.erp.action.SauvegardeBaseAction;
16
import org.openconcerto.erp.action.SauvegardeBaseAction;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18
import org.openconcerto.erp.config.Gestion;
18
import org.openconcerto.erp.config.Gestion;
19
import org.openconcerto.erp.config.GestionLauncher;
19
import org.openconcerto.erp.config.GestionLauncher;
20
import org.openconcerto.erp.modules.ModuleManager;
20
import org.openconcerto.erp.modules.ModuleManager;
21
import org.openconcerto.erp.preferences.BackupNXProps;
21
import org.openconcerto.erp.preferences.BackupNXProps;
22
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.users.rights.UserRightsManager;
23
import org.openconcerto.sql.users.rights.UserRightsManager;
24
import org.openconcerto.sql.utils.BackupPanel;
24
import org.openconcerto.sql.utils.BackupPanel;
25
import org.openconcerto.ui.DefaultGridBagConstraints;
25
import org.openconcerto.ui.DefaultGridBagConstraints;
26
import org.openconcerto.ui.UserExit;
26
import org.openconcerto.ui.UserExit;
27
import org.openconcerto.utils.ExceptionHandler;
27
import org.openconcerto.utils.ExceptionHandler;
28
import org.openconcerto.utils.ProcessStreams;
28
import org.openconcerto.utils.ProcessStreams;
29
import org.openconcerto.utils.prog.VMLauncher;
29
import org.openconcerto.utils.prog.VMLauncher;
30
 
30
 
31
import java.awt.Frame;
31
import java.awt.Frame;
32
import java.awt.GridBagConstraints;
32
import java.awt.GridBagConstraints;
33
import java.awt.GridBagLayout;
33
import java.awt.GridBagLayout;
34
import java.awt.Insets;
34
import java.awt.Insets;
35
import java.awt.Window;
35
import java.awt.Window;
36
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionEvent;
37
import java.awt.event.ActionListener;
37
import java.awt.event.ActionListener;
38
import java.awt.event.WindowAdapter;
38
import java.awt.event.WindowAdapter;
39
import java.awt.event.WindowEvent;
39
import java.awt.event.WindowEvent;
40
import java.io.File;
40
import java.io.File;
41
import java.io.IOException;
41
import java.io.IOException;
42
import java.util.Set;
42
import java.util.Set;
43
import java.util.concurrent.atomic.AtomicBoolean;
43
import java.util.concurrent.atomic.AtomicBoolean;
44
 
44
 
45
import javax.swing.AbstractAction;
45
import javax.swing.AbstractAction;
46
import javax.swing.JButton;
46
import javax.swing.JButton;
47
import javax.swing.JDialog;
47
import javax.swing.JDialog;
48
import javax.swing.JFrame;
48
import javax.swing.JFrame;
49
import javax.swing.JLabel;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
50
import javax.swing.JPanel;
51
import javax.swing.SwingUtilities;
51
import javax.swing.SwingUtilities;
52
 
52
 
53
public class UserExitPanel extends JPanel {
53
public class UserExitPanel extends JPanel {
54
 
54
 
55
    static private final AtomicBoolean CLOSING = new AtomicBoolean(false);
55
    static private final AtomicBoolean CLOSING = new AtomicBoolean(false);
56
 
56
 
57
    static public final boolean isClosing() {
57
    static public final boolean isClosing() {
58
        return CLOSING.get();
58
        return CLOSING.get();
59
    }
59
    }
60
 
60
 
61
    private final UserExitConf conf;
61
    private final UserExitConf conf;
62
 
62
 
63
    public UserExitPanel(final UserExitConf conf) {
63
    public UserExitPanel(final UserExitConf conf) {
64
        if (conf == null)
64
        if (conf == null)
65
            throw new NullPointerException("Null conf");
65
            throw new NullPointerException("Null conf");
66
        this.conf = conf;
66
        this.conf = conf;
67
        this.setLayout(new GridBagLayout());
67
        this.setLayout(new GridBagLayout());
68
        final GridBagConstraints c = new DefaultGridBagConstraints();
68
        final GridBagConstraints c = new DefaultGridBagConstraints();
69
        c.anchor = GridBagConstraints.CENTER;
69
        c.anchor = GridBagConstraints.CENTER;
70
        c.insets = new Insets(2, 2, 5, 2);
70
        c.insets = new Insets(2, 2, 5, 2);
71
        c.weightx = 1;
71
        c.weightx = 1;
72
        c.gridwidth = 2;
72
        c.gridwidth = 2;
73
 
73
 
74
        String text = "Voulez-vous vraiment " + (this.conf.shouldRestart() ? "relancer " : "quitter ") + Configuration.getInstance().getAppName() + " ?";
74
        String text = "Voulez-vous vraiment " + (this.conf.shouldRestart() ? "relancer " : "quitter ") + Configuration.getInstance().getAppName() + " ?";
75
        if (this.conf.getMessage() != null) {
75
        if (this.conf.getMessage() != null) {
76
            text = this.conf.getMessage() + "<br/>" + text;
76
            text = this.conf.getMessage() + "<br/>" + text;
77
        }
77
        }
78
        final JLabel labelQuit = new JLabel("<html>" + text + "</html>");
78
        final JLabel labelQuit = new JLabel("<html>" + text + "</html>");
79
        JButton buttonCancel = new JButton("Annuler");
79
        JButton buttonCancel = new JButton("Annuler");
80
        final String verb = this.conf.shouldRestart() ? "Relancer" : "Quitter";
80
        final String verb = this.conf.shouldRestart() ? "Relancer" : "Quitter";
81
        JButton buttonExit = new JButton(verb);
81
        JButton buttonExit = new JButton(verb);
82
        this.add(labelQuit, c);
82
        this.add(labelQuit, c);
83
 
83
 
84
        c.gridy++;
84
        c.gridy++;
85
        c.fill = GridBagConstraints.NONE;
85
        c.fill = GridBagConstraints.NONE;
86
        c.gridwidth = 1;
86
        c.gridwidth = 1;
87
        this.add(buttonExit, c);
87
        this.add(buttonExit, c);
88
        final ComptaPropsConfiguration comptaPropsConfiguration = (ComptaPropsConfiguration) Configuration.getInstance();
88
        final ComptaPropsConfiguration comptaPropsConfiguration = (ComptaPropsConfiguration) Configuration.getInstance();
89
            final javax.swing.Action backupAction = createBackupAction(verb);
89
            final javax.swing.Action backupAction = createBackupAction(verb);
90
            if (backupAction != null && comptaPropsConfiguration.getRowSociete() != null) {
90
            if (backupAction != null && comptaPropsConfiguration.getRowSociete() != null) {
91
                c.gridx++;
91
                c.gridx++;
92
                this.add(new JButton(backupAction), c);
92
                this.add(new JButton(backupAction), c);
93
            }
93
            }
94
        c.gridx++;
94
        c.gridx++;
95
        this.add(buttonCancel, c);
95
        this.add(buttonCancel, c);
96
        buttonCancel.addActionListener(new ActionListener() {
96
        buttonCancel.addActionListener(new ActionListener() {
97
            public void actionPerformed(ActionEvent arg0) {
97
            public void actionPerformed(ActionEvent arg0) {
98
                // not use getRoot if frame is Jdialog modal, not return the jdialog but the root of
98
                // not use getRoot if frame is Jdialog modal, not return the jdialog but the root of
99
                // jdialog
99
                // jdialog
100
                SwingUtilities.getWindowAncestor(UserExitPanel.this).dispose();
100
                SwingUtilities.getWindowAncestor(UserExitPanel.this).dispose();
101
            }
101
            }
102
        });
102
        });
103
 
103
 
104
        final ActionListener listenerExit = new ActionListener() {
104
        final ActionListener listenerExit = new ActionListener() {
105
            public void actionPerformed(ActionEvent arg0) {
105
            public void actionPerformed(ActionEvent arg0) {
106
 
106
 
107
                closeGNx();
107
                closeGNx();
108
            }
108
            }
109
 
109
 
110
        };
110
        };
111
 
111
 
112
        buttonExit.addActionListener(listenerExit);
112
        buttonExit.addActionListener(listenerExit);
113
    }
113
    }
114
 
114
 
115
    protected javax.swing.Action createBackupAction(final String verb) {
115
    protected javax.swing.Action createBackupAction(final String verb) {
116
        if (!UserRightsManager.getCurrentUserRights().haveRight(BackupPanel.RIGHT_CODE))
116
        if (!UserRightsManager.getCurrentUserRights().haveRight(BackupPanel.RIGHT_CODE))
117
            return null;
117
            return null;
118
        return new AbstractAction("Sauvegarder et " + verb.toLowerCase()) {
118
        return new AbstractAction("Sauvegarder et " + verb.toLowerCase()) {
119
 
119
 
120
            @Override
120
            @Override
121
            public void actionPerformed(ActionEvent e) {
121
            public void actionPerformed(ActionEvent e) {
122
                JDialog frame = new JDialog();
122
                JDialog frame = new JDialog();
123
                frame.setContentPane(new BackupPanel(null, SauvegardeBaseAction.getDirs(), false, BackupNXProps.getInstance()) {
123
                frame.setContentPane(new BackupPanel(null, SauvegardeBaseAction.getDirs(), false, BackupNXProps.getInstance()) {
124
                    @Override
124
                    @Override
125
                    public void doOnClose() {
125
                    public void doOnClose() {
126
                        super.doOnClose();
126
                        super.doOnClose();
127
                        closeGNx();
127
                        closeGNx();
128
                    }
128
                    }
129
                });
129
                });
130
                frame.setTitle("Sauvegarde des données");
130
                frame.setTitle("Sauvegarde des données");
131
                // so that the application can exit
131
                // so that the application can exit
132
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
132
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
133
                // ATTN works because BackupPanel do not open a new window when doing backup
133
                // ATTN works because BackupPanel do not open a new window when doing backup
134
                frame.addWindowListener(new WindowAdapter() {
134
                frame.addWindowListener(new WindowAdapter() {
135
                    @Override
135
                    @Override
136
                    public void windowClosed(WindowEvent e) {
136
                    public void windowClosed(WindowEvent e) {
137
                        startTimeOut();
137
                        startTimeOut();
138
                    }
138
                    }
139
                });
139
                });
140
                frame.setLocationRelativeTo(null);
140
                frame.setLocationRelativeTo(null);
141
                frame.pack();
141
                frame.pack();
142
                frame.setMinimumSize(frame.getSize());
142
                frame.setMinimumSize(frame.getSize());
143
 
143
 
144
                ((Window) SwingUtilities.getRoot(UserExitPanel.this)).dispose();
144
                ((Window) SwingUtilities.getRoot(UserExitPanel.this)).dispose();
145
                frame.setModal(true);
145
                frame.setModal(true);
146
                frame.setIconImages(Gestion.getFrameIcon());
146
                frame.setIconImages(Gestion.getFrameIcon());
147
                frame.setAlwaysOnTop(true);
147
                frame.setAlwaysOnTop(true);
148
                frame.setVisible(true);
148
                frame.setVisible(true);
149
            }
149
            }
150
        };
150
        };
151
    }
151
    }
152
 
152
 
153
    private void closeGNx() {
153
    private void closeGNx() {
154
        exit(this.conf);
154
        exit(this.conf);
155
    }
155
    }
156
 
156
 
157
    static public void exit(final UserExitConf c) {
157
    static public void exit(final UserExitConf c) {
158
        assert SwingUtilities.isEventDispatchThread();
158
        assert SwingUtilities.isEventDispatchThread();
159
        if (!CLOSING.compareAndSet(false, true)) {
159
        if (!CLOSING.compareAndSet(false, true)) {
160
            // already closing
160
            // already closing
161
            return;
161
            return;
162
        }
162
        }
163
        UserExit.closeAllWindows(null);
163
        UserExit.closeAllWindows(null);
164
        try {
164
        try {
165
            // add shutdown hook first, since the user already agreed to restart
165
            // add shutdown hook first, since the user already agreed to restart
166
            Runtime.getRuntime().addShutdownHook(new Thread() {
166
            Runtime.getRuntime().addShutdownHook(new Thread() {
167
                @Override
167
                @Override
168
                public void run() {
168
                public void run() {
169
                    try {
169
                    try {
170
                        // MAYBE only run beforeShutdown() if afterWindowsClosed() is successful
170
                        // MAYBE only run beforeShutdown() if afterWindowsClosed() is successful
171
                        c.beforeShutdown();
171
                        c.beforeShutdown();
172
 
172
 
173
                        if (c.shouldRestart())
173
                        if (c.shouldRestart())
174
                            VMLauncher.restart(ProcessStreams.Action.CLOSE, GestionLauncher.class);
174
                            VMLauncher.restart(ProcessStreams.DISCARD, GestionLauncher.class);
175
                    } catch (Exception e) {
175
                    } catch (Exception e) {
176
                        // in shutdown sequence : don't use the GUI
176
                        // in shutdown sequence : don't use the GUI
177
                        e.printStackTrace();
177
                        e.printStackTrace();
178
                    }
178
                    }
179
                }
179
                }
180
            });
180
            });
181
            c.afterWindowsClosed();
181
            c.afterWindowsClosed();
182
            ModuleManager.tearDown();
182
            ModuleManager.tearDown();
183
            ComptaPropsConfiguration.closeOOConnexion();
183
            ComptaPropsConfiguration.closeOOConnexion();
184
        } catch (Exception exn) {
184
        } catch (Exception exn) {
185
            // all windows already closed
185
            // all windows already closed
186
            ExceptionHandler.handle("Erreur lors de la fermeture", exn);
186
            ExceptionHandler.handle("Erreur lors de la fermeture", exn);
187
        }
187
        }
188
        // ((JFrame) SwingUtilities.getRoot(UserExitingPanel.this)).dispose();
188
        // ((JFrame) SwingUtilities.getRoot(UserExitingPanel.this)).dispose();
189
        if (Gestion.pgFrameStart != null) {
189
        if (Gestion.pgFrameStart != null) {
190
            Gestion.pgFrameStart.setVisible(false);
190
            Gestion.pgFrameStart.setVisible(false);
191
        }
191
        }
192
        new Thread() {
192
        new Thread() {
193
            private PostgreSQLFrame pgFrame = null;
193
            private PostgreSQLFrame pgFrame = null;
194
 
194
 
195
            @Override
195
            @Override
196
            public void run() {
196
            public void run() {
197
                try {
197
                try {
198
                    Configuration.getInstance().destroy();
198
                    Configuration.getInstance().destroy();
199
                    Runtime runtime = Runtime.getRuntime();
199
                    Runtime runtime = Runtime.getRuntime();
200
 
200
 
201
                    File f = new File(".\\PostgreSQL\\bin\\");
201
                    File f = new File(".\\PostgreSQL\\bin\\");
202
                    if (f.exists()) {
202
                    if (f.exists()) {
203
                        final Process p = runtime.exec(new String[] { "cmd.exe", "/C", "stopPostGres.bat" }, null, f);
203
                        final Process p = runtime.exec(new String[] { "cmd.exe", "/C", "stopPostGres.bat" }, null, f);
204
                        SwingUtilities.invokeLater(new Runnable() {
204
                        SwingUtilities.invokeLater(new Runnable() {
205
 
205
 
206
                            @Override
206
                            @Override
207
                            public void run() {
207
                            public void run() {
208
 
208
 
209
                                try {
209
                                try {
210
                                    pgFrame = new PostgreSQLFrame("Arrêt en cours");
210
                                    pgFrame = new PostgreSQLFrame("Arrêt en cours");
211
                                    pgFrame.setVisible(true);
211
                                    pgFrame.setVisible(true);
212
                                } catch (Exception e) {
212
                                } catch (Exception e) {
213
                                    e.printStackTrace();
213
                                    e.printStackTrace();
214
                                }
214
                                }
215
                            }
215
                            }
216
                        });
216
                        });
217
                        System.err.println("Execute end postgres");
217
                        System.err.println("Execute end postgres");
218
                        ProcessStreams.handle(p, ProcessStreams.Action.REDIRECT);
218
                        ProcessStreams.handle(p, ProcessStreams.Action.REDIRECT);
219
                        p.waitFor();
219
                        p.waitFor();
220
 
220
 
221
                    }
221
                    }
222
                    ComptaPropsConfiguration.getInstanceCompta().tearDownLogging(true);
222
                    ComptaPropsConfiguration.getInstanceCompta().tearDownLogging(true);
223
                } catch (IOException e1) {
223
                } catch (IOException e1) {
224
                    // TODO Auto-generated catch block
224
                    // TODO Auto-generated catch block
225
                    e1.printStackTrace();
225
                    e1.printStackTrace();
226
                } catch (InterruptedException e) {
226
                } catch (InterruptedException e) {
227
                    // TODO Auto-generated catch block
227
                    // TODO Auto-generated catch block
228
                    e.printStackTrace();
228
                    e.printStackTrace();
229
                } finally {
229
                } finally {
230
                    SwingUtilities.invokeLater(new Runnable() {
230
                    SwingUtilities.invokeLater(new Runnable() {
231
                        public void run() {
231
                        public void run() {
232
                            if (pgFrame != null) {
232
                            if (pgFrame != null) {
233
                                pgFrame.dispose();
233
                                pgFrame.dispose();
234
                            }
234
                            }
235
                        }
235
                        }
236
                    });
236
                    });
237
                    startTimeOut();
237
                    startTimeOut();
238
                    // System.exit(0);
238
                    // System.exit(0);
239
                }
239
                }
240
            }
240
            }
241
        }.start();
241
        }.start();
242
    }
242
    }
243
 
243
 
244
    // (especially important with embedded h2, since the database is locked)
244
    // (especially important with embedded h2, since the database is locked)
245
    static private final void startTimeOut() {
245
    static private final void startTimeOut() {
246
        final Thread timeOut = new Thread(new Runnable() {
246
        final Thread timeOut = new Thread(new Runnable() {
247
            @Override
247
            @Override
248
            public void run() {
248
            public void run() {
249
                try {
249
                try {
250
                    // 5 s d'attente
250
                    // 5 s d'attente
251
                    // TODO make ModuleManager.install() atomic
251
                    // TODO make ModuleManager.install() atomic
252
                    Thread.sleep(5 * 1000);
252
                    Thread.sleep(5 * 1000);
253
                    System.err.println("Warning: Forcing exit");
253
                    System.err.println("Warning: Forcing exit");
254
                    Frame[] l = Frame.getFrames();
254
                    Frame[] l = Frame.getFrames();
255
                    for (int i = 0; i < l.length; i++) {
255
                    for (int i = 0; i < l.length; i++) {
256
                        Frame f = l[i];
256
                        Frame f = l[i];
257
                        System.err.println("Frame " + f.getName() + " " + f + " Displayable: " + f.isDisplayable() + " Valid: " + f.isValid() + " Active: " + f.isActive());
257
                        System.err.println("Frame " + f.getName() + " " + f + " Displayable: " + f.isDisplayable() + " Valid: " + f.isValid() + " Active: " + f.isActive());
258
                    }
258
                    }
259
                    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
259
                    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
260
                    for (Thread thread : threadSet) {
260
                    for (Thread thread : threadSet) {
261
                        if (!thread.isDaemon()) {
261
                        if (!thread.isDaemon()) {
262
                            System.err.println("Thread " + thread.getName() + " " + thread.getId() + " not daemon");
262
                            System.err.println("Thread " + thread.getName() + " " + thread.getId() + " not daemon");
263
                        }
263
                        }
264
                    }
264
                    }
265
                } catch (Throwable e) {
265
                } catch (Throwable e) {
266
                    e.printStackTrace();
266
                    e.printStackTrace();
267
                }
267
                }
268
                try {
268
                try {
269
                    Configuration.getInstance().destroy();
269
                    Configuration.getInstance().destroy();
270
                    ComptaPropsConfiguration.getInstanceCompta().tearDownLogging(true);
270
                    ComptaPropsConfiguration.getInstanceCompta().tearDownLogging(true);
271
                } catch (Throwable e) {
271
                } catch (Throwable e) {
272
                    e.printStackTrace();
272
                    e.printStackTrace();
273
                }
273
                }
274
 
274
 
275
                // Dans tous les cas, on arrete le programme
275
                // Dans tous les cas, on arrete le programme
276
                System.exit(1);
276
                System.exit(1);
277
            }
277
            }
278
        });
278
        });
279
        timeOut.setName("TimeOut Thread");
279
        timeOut.setName("TimeOut Thread");
280
        timeOut.setDaemon(true);
280
        timeOut.setDaemon(true);
281
        timeOut.start();
281
        timeOut.start();
282
    }
282
    }
283
}
283
}