From c527a219a6010de9610ccf6ecb0c94f209fb7759 Mon Sep 17 00:00:00 2001 From: Andreas Buchen Date: Tue, 15 Dec 2015 23:00:09 +0100 Subject: [PATCH] Remember the last selected account/portfolio by the type of PDF document --- .../ui/handlers/ImportPDFHandler.java | 29 ++++++----- .../ImportExtractedItemsWizard.java | 10 +++- .../ReviewExtractedItemsPage.java | 50 +++++++++++++++++-- 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/handlers/ImportPDFHandler.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/handlers/ImportPDFHandler.java index 975f703f91..baf24c14fd 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/handlers/ImportPDFHandler.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/handlers/ImportPDFHandler.java @@ -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; @@ -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 @@ -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) diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ImportExtractedItemsWizard.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ImportExtractedItemsWizard.java index 37e8110870..d899974a1e 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ImportExtractedItemsWizard.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ImportExtractedItemsWizard.java @@ -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; @@ -16,14 +17,17 @@ public class ImportExtractedItemsWizard extends Wizard { private Client client; private Extractor extractor; + private IPreferenceStore preferences; private List files; private ReviewExtractedItemsPage page; - public ImportExtractedItemsWizard(Client client, Extractor extractor, List files) + public ImportExtractedItemsWizard(Client client, Extractor extractor, IPreferenceStore preferences, + List files) { this.client = client; this.extractor = extractor; + this.preferences = preferences; this.files = files; setWindowTitle(Messages.PDFImportWizardTitle); @@ -33,7 +37,7 @@ public ImportExtractedItemsWizard(Client client, Extractor extractor, List @Override public void addPages() { - page = new ReviewExtractedItemsPage(client, extractor, files); + page = new ReviewExtractedItemsPage(client, extractor, preferences, files); addPage(page); AbstractWizardPage.attachPageListenerTo(getContainer()); } @@ -41,6 +45,8 @@ public void addPages() @Override public boolean performFinish() { + page.afterPage(); + InsertAction action = new InsertAction(client); boolean isDirty = false; diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ReviewExtractedItemsPage.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ReviewExtractedItemsPage.java index 91ff11419d..9c57dd64c8 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ReviewExtractedItemsPage.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/datatransfer/ReviewExtractedItemsPage.java @@ -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; @@ -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; @@ -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; @@ -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 files; private List allEntries = new ArrayList(); - public ReviewExtractedItemsPage(Client client, Extractor extractor, List files) + public ReviewExtractedItemsPage(Client client, Extractor extractor, IPreferenceStore preferences, List files) { super("reviewitems"); //$NON-NLS-1$ this.client = client; this.extractor = extractor; + this.preferences = preferences; this.files = files; setTitle(extractor.getLabel()); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 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 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); @@ -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 entries, List errors) { checkEntries(entries);