Skip to content

Commit

Permalink
Remember the last selected account/portfolio by the type of PDF document
Browse files Browse the repository at this point in the history
  • Loading branch information
buchen committed Dec 15, 2015
1 parent 092cf2b commit c527a21
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

import javax.inject.Named;

import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import name.abuchen.portfolio.datatransfer.ComdirectPDFExtractor;
import name.abuchen.portfolio.datatransfer.CommerzbankPDFExctractor;
import name.abuchen.portfolio.datatransfer.ConsorsbankPDFExctractor;
Expand All @@ -18,20 +30,10 @@
import name.abuchen.portfolio.datatransfer.IBFlexStatementExtractor;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.ui.PortfolioPart;
import name.abuchen.portfolio.ui.PortfolioPlugin;
import name.abuchen.portfolio.ui.wizards.datatransfer.ImportExtractedItemsWizard;

import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class ImportPDFHandler
{
@CanExecute
Expand Down Expand Up @@ -73,8 +75,9 @@ public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
files.add(new File(fileDialog.getFilterPath(), file));

// open wizard dialog

Dialog wizwardDialog = new WizardDialog(shell, new ImportExtractedItemsWizard(client, extractor, files));
IPreferenceStore preferences = ((PortfolioPart) part.getObject()).getPreferenceStore();
Dialog wizwardDialog = new WizardDialog(shell,
new ImportExtractedItemsWizard(client, extractor, preferences, files));
wizwardDialog.open();
}
catch (IllegalArgumentException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.wizard.Wizard;

import name.abuchen.portfolio.datatransfer.Extractor;
Expand All @@ -16,14 +17,17 @@ public class ImportExtractedItemsWizard extends Wizard
{
private Client client;
private Extractor extractor;
private IPreferenceStore preferences;
private List<File> files;

private ReviewExtractedItemsPage page;

public ImportExtractedItemsWizard(Client client, Extractor extractor, List<File> files)
public ImportExtractedItemsWizard(Client client, Extractor extractor, IPreferenceStore preferences,
List<File> files)
{
this.client = client;
this.extractor = extractor;
this.preferences = preferences;
this.files = files;

setWindowTitle(Messages.PDFImportWizardTitle);
Expand All @@ -33,14 +37,16 @@ public ImportExtractedItemsWizard(Client client, Extractor extractor, List<File>
@Override
public void addPages()
{
page = new ReviewExtractedItemsPage(client, extractor, files);
page = new ReviewExtractedItemsPage(client, extractor, preferences, files);
addPage(page);
AbstractWizardPage.attachPageListenerTo(getContainer());
}

@Override
public boolean performFinish()
{
page.afterPage();

InsertAction action = new InsertAction(client);

boolean isDirty = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -14,6 +15,7 @@
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnPixelData;
Expand Down Expand Up @@ -66,6 +68,10 @@

public class ReviewExtractedItemsPage extends AbstractWizardPage implements ImportAction.Context
{
private static final String IMPORT_TARGET = "import-target"; //$NON-NLS-1$
private static final String IMPORT_TARGET_PORTFOLIO = IMPORT_TARGET + "-portfolio-"; //$NON-NLS-1$
private static final String IMPORT_TARGET_ACCOUNT = IMPORT_TARGET + "-account-"; //$NON-NLS-1$

private TableViewer tableViewer;
private TableViewer errorTableViewer;

Expand All @@ -80,16 +86,18 @@ public class ReviewExtractedItemsPage extends AbstractWizardPage implements Impo

private final Client client;
private final Extractor extractor;
private final IPreferenceStore preferences;
private List<File> files;

private List<ExtractedEntry> allEntries = new ArrayList<ExtractedEntry>();

public ReviewExtractedItemsPage(Client client, Extractor extractor, List<File> files)
public ReviewExtractedItemsPage(Client client, Extractor extractor, IPreferenceStore preferences, List<File> files)
{
super("reviewitems"); //$NON-NLS-1$

this.client = client;
this.extractor = extractor;
this.preferences = preferences;
this.files = files;

setTitle(extractor.getLabel());
Expand Down Expand Up @@ -142,7 +150,6 @@ public void createControl(Composite parent)
primaryAccount.setContentProvider(ArrayContentProvider.getInstance());
primaryAccount.setInput(client.getActiveAccounts());
primaryAccount.addSelectionChangedListener(e -> checkEntriesAndRefresh(allEntries));
cmbAccount.select(0);

lblSecondaryAccount = new Label(targetContainer, SWT.NONE);
lblSecondaryAccount.setText(Messages.LabelTransferTo);
Expand All @@ -152,7 +159,6 @@ public void createControl(Composite parent)
secondaryAccount.setContentProvider(ArrayContentProvider.getInstance());
secondaryAccount.setInput(client.getActiveAccounts());
secondaryAccount.getControl().setVisible(false);
cmbAccountTarget.select(0);

lblPrimaryPortfolio = new Label(targetContainer, SWT.NONE);
lblPrimaryPortfolio.setText(Messages.ColumnPortfolio);
Expand All @@ -161,7 +167,6 @@ public void createControl(Composite parent)
primaryPortfolio.setContentProvider(ArrayContentProvider.getInstance());
primaryPortfolio.setInput(client.getActivePortfolios());
primaryPortfolio.addSelectionChangedListener(e -> checkEntriesAndRefresh(allEntries));
cmbPortfolio.select(0);

lblSecondaryPortfolio = new Label(targetContainer, SWT.NONE);
lblSecondaryPortfolio.setText(Messages.LabelTransferTo);
Expand All @@ -171,7 +176,8 @@ public void createControl(Composite parent)
secondaryPortfolio.setContentProvider(ArrayContentProvider.getInstance());
secondaryPortfolio.setInput(client.getActivePortfolios());
secondaryPortfolio.getControl().setVisible(false);
cmbPortfolioTarget.select(0);

preselectDropDowns();

Composite compositeTable = new Composite(container, SWT.NONE);
Composite errorTable = new Composite(container, SWT.NONE);
Expand Down Expand Up @@ -228,6 +234,33 @@ public void createControl(Composite parent)
addColumnsExceptionTable(errorTableViewer, layout);
}

private void preselectDropDowns()
{
// idea: generally one type of document (i.e. from the same bank) will
// be imported into the same account

List<Account> activeAccounts = client.getActiveAccounts();
if (!activeAccounts.isEmpty())
{
String uuid = preferences.getString(IMPORT_TARGET_ACCOUNT + extractor.getClass().getSimpleName());

// do not trigger selection listener (-> do not user #setSelection)
primaryAccount.getCombo().select(IntStream.range(0, activeAccounts.size())
.filter(i -> activeAccounts.get(i).getUUID().equals(uuid)).findAny().orElse(0));
secondaryAccount.getCombo().select(0);
}

List<Portfolio> activePortfolios = client.getActivePortfolios();
if (!activePortfolios.isEmpty())
{
String uuid = preferences.getString(IMPORT_TARGET_PORTFOLIO + extractor.getClass().getSimpleName());
// do not trigger selection listener (-> do not user #setSelection)
primaryPortfolio.getCombo().select(IntStream.range(0, activePortfolios.size())
.filter(i -> activePortfolios.get(i).getUUID().equals(uuid)).findAny().orElse(0));
secondaryPortfolio.getCombo().select(0);
}
}

private void addColumnsExceptionTable(TableViewer viewer, TableColumnLayout layout)
{
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
Expand Down Expand Up @@ -471,6 +504,13 @@ protected IStatus run(IProgressMonitor monitor)
}
}

@Override
public void afterPage()
{
preferences.setValue(IMPORT_TARGET_ACCOUNT + extractor.getClass().getSimpleName(), getAccount().getUUID());
preferences.setValue(IMPORT_TARGET_PORTFOLIO + extractor.getClass().getSimpleName(), getPortfolio().getUUID());
}

private void setResults(List<ExtractedEntry> entries, List<Exception> errors)
{
checkEntries(entries);
Expand Down

0 comments on commit c527a21

Please sign in to comment.