Search in sources :

Example 1 with OpenDatabaseDialog

use of jgnash.ui.components.OpenDatabaseDialog in project jgnash by ccavanaugh.

the class OpenAction method openAction.

public static void openAction() {
    final class BootEngine extends SwingWorker<Void, Void> {

        private final OpenDatabaseDialog dialog;

        private BootEngine(final OpenDatabaseDialog dialog) {
            this.dialog = dialog;
        }

        @Override
        protected Void doInBackground() throws Exception {
            final ResourceBundle rb = ResourceUtils.getBundle();
            UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
            EngineFactory.closeEngine(EngineFactory.DEFAULT);
            Engine engine = null;
            final char[] password = dialog.getPassword();
            if (dialog.isRemote()) {
                final String host = dialog.getHost();
                final int port = dialog.getPort();
                engine = EngineFactory.bootClientEngine(host, port, password, EngineFactory.DEFAULT);
                if (engine == null) {
                    remoteConnectionFailed = true;
                }
            } else {
                try {
                    if (FileUtils.isFileLocked(dialog.getDatabasePath())) {
                        StaticUIMethods.displayError(ResourceUtils.getString("Message.FileIsLocked"));
                    } else if (checkAndBackupOldVersion(dialog.getDatabasePath(), password)) {
                        engine = EngineFactory.bootLocalEngine(dialog.getDatabasePath(), EngineFactory.DEFAULT, password);
                    }
                } catch (final Exception e) {
                    StaticUIMethods.displayError(e.getLocalizedMessage());
                }
            }
            if (engine != null) {
                // prime the engine
                engine.getRootAccount();
            }
            return null;
        }

        @Override
        protected void done() {
            logger.info("openAction() done");
            UIApplication.getFrame().stopWaitMessage();
            if (remoteConnectionFailed) {
                StaticUIMethods.displayError(ResourceUtils.getString("Message.Error.ServerConnection"));
            }
        }
    }
    EventQueue.invokeLater(() -> {
        final OpenDatabaseDialog d = new OpenDatabaseDialog(UIApplication.getFrame());
        d.setDatabasePath(EngineFactory.getLastDatabase());
        d.setPort(EngineFactory.getLastPort());
        d.setHost(EngineFactory.getLastHost());
        d.setRemote(EngineFactory.getLastRemote());
        d.setVisible(true);
        if (d.getResult()) {
            pool.execute(new BootEngine(d));
        }
    });
}
Also used : SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) OpenDatabaseDialog(jgnash.ui.components.OpenDatabaseDialog) Engine(jgnash.engine.Engine) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IOException (java.io.IOException)1 ResourceBundle (java.util.ResourceBundle)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SwingWorker (javax.swing.SwingWorker)1 Engine (jgnash.engine.Engine)1 OpenDatabaseDialog (jgnash.ui.components.OpenDatabaseDialog)1