Search in sources :

Example 16 with SwingWorker

use of javax.swing.SwingWorker in project jgnash by ccavanaugh.

the class BudgetPanel method exportBudgetAction.

private void exportBudgetAction() {
    final Preferences pref = Preferences.userNodeForPackage(BudgetPanel.class);
    final ResourceBundle rb = ResourceUtils.getBundle();
    JFileChooser chooser = new JFileChooser(pref.get(CURRENT_DIR, null));
    chooser.setMultiSelectionEnabled(false);
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.addChoosableFileFilter(new FileNameExtensionFilter(rb.getString("Label.SpreadsheetFiles") + " (*.xls, *.xlsx)", "xls", "xlsx"));
    if (chooser.showSaveDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
        pref.put(CURRENT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        final File file = new File(chooser.getSelectedFile().getAbsolutePath());
        final class Export extends SwingWorker<String, Void> {

            @Override
            protected String doInBackground() throws Exception {
                UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
                return BudgetResultsExport.exportBudgetResultsModel(file.toPath(), resultsModel);
            }

            @Override
            protected void done() {
                UIApplication.getFrame().stopWaitMessage();
                try {
                    String message = get();
                    // display any errors that may have occurred
                    if (message != null) {
                        StaticUIMethods.displayError(message);
                    }
                } catch (InterruptedException | ExecutionException e) {
                    logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
                }
            }
        }
        new Export().execute();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) SwingWorker(javax.swing.SwingWorker) BudgetResultsExport(jgnash.engine.budget.BudgetResultsExport) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 17 with SwingWorker

use of javax.swing.SwingWorker in project jgnash by ccavanaugh.

the class SaveFileAsAction method saveFileAs.

/**
     * Opens a Save as Dialog. If the extension of the destination file is different than the file currently open, then
     * an attempt is made to identify the new file format and save accordingly. Otherwise, a copy of the file is made.
     */
private static void saveFileAs() {
    final ResourceBundle rb = ResourceUtils.getBundle();
    final Preferences pref = Preferences.userNodeForPackage(SaveFileAsAction.class);
    JFileChooser chooser = new JFileChooser(pref.get(CURRENT_DIR, null));
    chooser.setMultiSelectionEnabled(false);
    chooser.setDialogTitle(rb.getString("Title.SaveAs"));
    final DataStoreType[] types = DataStoreType.values();
    final String[] ext = new String[types.length];
    for (int i = 0; i < types.length; i++) {
        ext[i] = types[i].getDataStore().getFileExt();
    }
    StringBuilder description = new StringBuilder(rb.getString("Label.jGnashFiles") + " (");
    for (int i = 0; i < types.length; i++) {
        description.append("*");
        description.append(types[i].getDataStore().getFileExt());
        if (i < types.length - 1) {
            description.append(", ");
        }
    }
    description.append(')');
    chooser.addChoosableFileFilter(new DataStoreFilter(description.toString(), ext));
    if (chooser.showSaveDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
        pref.put(CURRENT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        final class SaveAs extends SwingWorker<Void, Void> {

            @Override
            protected Void doInBackground() throws Exception {
                UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
                EngineFactory.saveAs(chooser.getSelectedFile().getAbsolutePath());
                return null;
            }

            @Override
            protected void done() {
                UIApplication.getFrame().stopWaitMessage();
            }
        }
        new SaveAs().execute();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences) DataStoreType(jgnash.engine.DataStoreType)

Example 18 with SwingWorker

use of javax.swing.SwingWorker in project jgnash by ccavanaugh.

the class OpenAction method openRemote.

public static void openRemote(final String host, final int port, final char[] password) {
    final class BootEngine extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            final ResourceBundle rb = ResourceUtils.getBundle();
            UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
            logger.fine("Booting the engine");
            Thread.sleep(750);
            EngineFactory.bootClientEngine(host, port, password, EngineFactory.DEFAULT);
            logger.fine("Engine boot complete");
            return null;
        }

        @Override
        protected void done() {
            UIApplication.getFrame().stopWaitMessage();
        }
    }
    pool.execute(new BootEngine());
}
Also used : SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle)

Example 19 with SwingWorker

use of javax.swing.SwingWorker 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)

Example 20 with SwingWorker

use of javax.swing.SwingWorker in project jgnash by ccavanaugh.

the class OpenAction method openAction.

public static void openAction(final Path file, final char[] password) {
    final String database = file.toAbsolutePath().toString();
    final class BootEngine extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            final ResourceBundle rb = ResourceUtils.getBundle();
            UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
            logger.fine("Booting the engine");
            // Disk IO is heavy so delay and allow the UI to react before starting the boot operation
            Thread.sleep(750);
            if (checkAndBackupOldVersion(database, password)) {
                final Engine e = EngineFactory.bootLocalEngine(database, EngineFactory.DEFAULT, password);
                if (e != null) {
                    // prime the engine
                    e.getRootAccount();
                }
                logger.fine("Engine boot complete");
            }
            return null;
        }

        @Override
        protected void done() {
            logger.info("openAction(final File file) done");
            UIApplication.getFrame().stopWaitMessage();
        }
    }
    if (EngineFactory.doesDatabaseExist(database)) {
        try {
            if (!FileUtils.isFileLocked(database)) {
                pool.execute(new BootEngine());
            } else {
                StaticUIMethods.displayError(ResourceUtils.getString("Message.FileIsLocked"));
            }
        } catch (final IOException e) {
            logger.log(Level.SEVERE, e.toString(), e);
        }
    }
}
Also used : SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) Engine(jgnash.engine.Engine)

Aggregations

SwingWorker (javax.swing.SwingWorker)41 ExecutionException (java.util.concurrent.ExecutionException)15 IOException (java.io.IOException)14 List (java.util.List)10 ResourceBundle (java.util.ResourceBundle)10 ArrayList (java.util.ArrayList)9 Preferences (java.util.prefs.Preferences)8 File (java.io.File)6 JFileChooser (javax.swing.JFileChooser)6 JPanel (javax.swing.JPanel)6 BorderLayout (java.awt.BorderLayout)5 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)5 Engine (jgnash.engine.Engine)5 FileNotFoundException (java.io.FileNotFoundException)4 InstanceNotFoundException (javax.management.InstanceNotFoundException)4 IntrospectionException (javax.management.IntrospectionException)4 MBeanInfo (javax.management.MBeanInfo)4 ReflectionException (javax.management.ReflectionException)4 JScrollPane (javax.swing.JScrollPane)4 ResourceAccessException (org.springframework.web.client.ResourceAccessException)4