create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / dialogs / MotorDatabaseLoadingDialog.java
1 package net.sf.openrocket.gui.dialogs;
2
3 import java.awt.SplashScreen;
4 import java.awt.Window;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7
8 import javax.swing.JDialog;
9 import javax.swing.JLabel;
10 import javax.swing.JPanel;
11 import javax.swing.JProgressBar;
12 import javax.swing.Timer;
13
14 import net.miginfocom.swing.MigLayout;
15 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
16 import net.sf.openrocket.gui.util.GUIUtil;
17 import net.sf.openrocket.l10n.Translator;
18 import net.sf.openrocket.logging.LogHelper;
19 import net.sf.openrocket.startup.Application;
20
21 /**
22  * A progress dialog displayed while loading motors.
23  * 
24  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
25  */
26 public class MotorDatabaseLoadingDialog extends JDialog {
27         private static final LogHelper log = Application.getLogger();
28         private static final Translator trans = Application.getTranslator();
29         
30         
31         private MotorDatabaseLoadingDialog(Window parent) {
32                 //// Loading motors
33                 super(parent, trans.get("MotorDbLoadDlg.title"), ModalityType.APPLICATION_MODAL);
34                 
35                 JPanel panel = new JPanel(new MigLayout("fill"));
36                 //// Loading motors...
37                 panel.add(new JLabel(trans.get("MotorDbLoadDlg.Loadingmotors")), "wrap para");
38                 
39                 JProgressBar progress = new JProgressBar();
40                 progress.setIndeterminate(true);
41                 panel.add(progress, "growx");
42                 
43                 this.add(panel);
44                 this.pack();
45                 this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
46                 this.setLocationByPlatform(true);
47                 GUIUtil.setWindowIcons(this);
48         }
49         
50         
51         /**
52          * Check whether the motor database is loaded and block until it is.
53          * An uncloseable modal dialog window is opened while loading unless the splash screen
54          * is still being displayed.
55          * 
56          * @param parent        the parent window for the dialog, or <code>null</code>
57          */
58         public static void check(Window parent) {
59                 // TODO - ugly blind cast
60                 final ThrustCurveMotorSetDatabase db = (ThrustCurveMotorSetDatabase) Application.getMotorSetDatabase();
61                 if (db.isLoaded())
62                         return;
63                 
64                 if (SplashScreen.getSplashScreen() == null) {
65                         
66                         log.info(1, "Motor database not loaded yet, displaying dialog");
67                         
68                         final MotorDatabaseLoadingDialog dialog = new MotorDatabaseLoadingDialog(parent);
69                         
70                         final Timer timer = new Timer(100, new ActionListener() {
71                                 private int count = 0;
72                                 
73                                 @Override
74                                 public void actionPerformed(ActionEvent e) {
75                                         count++;
76                                         if (db.isLoaded()) {
77                                                 log.debug("Database loaded, closing dialog");
78                                                 dialog.setVisible(false);
79                                         } else if (count % 10 == 0) {
80                                                 log.debug("Database not loaded, count=" + count);
81                                         }
82                                 }
83                         });
84                         
85                         db.setInUse();
86                         timer.start();
87                         dialog.setVisible(true);
88                         timer.stop();
89                         
90                 } else {
91                         
92                         log.info(1, "Motor database not loaded yet, splash screen still present, delaying until loaded");
93                         
94                         db.setInUse();
95                         int count = 0;
96                         while (!db.isLoaded()) {
97                                 try {
98                                         Thread.sleep(100);
99                                 } catch (InterruptedException e) {
100                                         // No-op
101                                 }
102                                 
103                                 count++;
104                                 if (count % 10 == 0) {
105                                         log.debug("Database not loaded, count=" + count);
106                                 }
107                         }
108                         
109                 }
110                 
111                 log.info("Motor database now loaded");
112         }
113         
114 }