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();
}
}
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();
}
}
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());
}
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));
}
});
}
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);
}
}
}
Aggregations