Skip to content

Commit

Permalink
Fixed newly created security being added to watchlist of different file
Browse files Browse the repository at this point in the history
The problem was that the posted event lacked information about the
client context in which the security was created. As a result, all open
watchlists were erroneously adding the security to themselves.

This change addresses the issue by creating an event that includes the
client context. Additionally, the change is using the already existing
event code for editing an existing security.

Issue: #3452 #3453
Co-authored-by: OnkelDok <90478568+OnkelDok@users.noreply.github.com>
Co-authored-by: Andreas Buchen <andreas.buchen@gmail.com>
  • Loading branch information
OnkelDok and buchen committed Jul 22, 2023
1 parent 4050dd2 commit 2a4ea16
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
Expand Up @@ -30,11 +30,6 @@ interface File // NOSONAR
String REMOVED = "file/removed"; //$NON-NLS-1$
}

interface Domain // NOSONAR
{
String SECURITY_CREATED = "domain/security-created"; //$NON-NLS-1$
}

interface ExchangeRates // NOSONAR
{
String LOADED = "exchangeRates/loaded"; //$NON-NLS-1$
Expand Down
Expand Up @@ -35,6 +35,8 @@
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import name.abuchen.portfolio.events.ChangeEventConstants;
import name.abuchen.portfolio.events.SecurityCreatedEvent;
import name.abuchen.portfolio.model.Classification;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Exchange;
Expand Down Expand Up @@ -126,10 +128,15 @@ private void openEditDialog(AbstractFinanceView view, Security newSecurity)
view.getClient().markDirty();
new UpdateQuotesJob(view.getClient(), newSecurity).schedule();

broker.post(UIConstants.Event.Domain.SECURITY_CREATED, newSecurity);
postSecurityCreatedEvent(view.getClient(), newSecurity);
}
}

private void postSecurityCreatedEvent(Client client, Security security)
{
broker.post(ChangeEventConstants.Security.CREATED, new SecurityCreatedEvent(client, security));
}

private void createNewCryptocurrency(AbstractFinanceView view)
{
try
Expand Down Expand Up @@ -226,7 +233,7 @@ private void createNewConsumerPriceIndex(AbstractFinanceView view)
view.getClient().addSecurity(newSecurity);
view.getClient().markDirty();
new UpdateQuotesJob(view.getClient(), newSecurity).schedule();
broker.post(UIConstants.Event.Domain.SECURITY_CREATED, newSecurity);
postSecurityCreatedEvent(view.getClient(), newSecurity);
}
}
}
Expand Down
Expand Up @@ -35,6 +35,8 @@
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Text;

import name.abuchen.portfolio.events.ChangeEventConstants;
import name.abuchen.portfolio.events.SecurityCreatedEvent;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.LimitPrice;
import name.abuchen.portfolio.model.PortfolioTransaction;
Expand Down Expand Up @@ -92,8 +94,6 @@ public void menuAboutToShow(IMenuManager manager)
UIConstants.Command.NEW_DOMAIN_ELEMENT, UIConstants.Parameter.TYPE,
DomainElement.CONSUMER_PRICE_INDEX.name()));



manager.add(new Separator());

manager.add(new Action(Messages.SecurityMenuImportCSV)
Expand Down Expand Up @@ -381,16 +381,19 @@ public void setup(@Named(UIConstants.Parameter.VIEW_PARAMETER) Predicate<Securit

@Inject
@Optional
public void onSecurityCreated(@UIEventTopic(UIConstants.Event.Domain.SECURITY_CREATED) Security newSecurity)
public void onSecurityCreated(@UIEventTopic(ChangeEventConstants.Security.CREATED) SecurityCreatedEvent event)
{
if (!event.appliesTo(getClient()))
return; // if security was created by other client, ignore event

if (watchlist != null)
{
watchlist.addSecurity(newSecurity);
watchlist.addSecurity(event.getSecurity());
getClient().touch();
}

setSecurityTableInput();
securities.getTableViewer().setSelection(new StructuredSelection(newSecurity), true);
securities.getTableViewer().setSelection(new StructuredSelection(event.getSecurity()), true);
}

@Override
Expand Down
@@ -1,6 +1,7 @@
package name.abuchen.portfolio.events;

import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.snapshot.filter.ReadOnlyClient;

public class ChangeEvent
{
Expand Down Expand Up @@ -28,6 +29,6 @@ public Object getSubject()
*/
public boolean appliesTo(Client client)
{
return this.client.equals(client);
return ReadOnlyClient.unwrap(this.client).equals(ReadOnlyClient.unwrap(client));
}
}
Expand Up @@ -4,6 +4,7 @@ public interface ChangeEventConstants
{
interface Security // NOSONAR
{
String CREATED = "security/created"; //$NON-NLS-1$
String EDITED = "security/edited"; //$NON-NLS-1$
}
}
@@ -0,0 +1,17 @@
package name.abuchen.portfolio.events;

import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Security;

public class SecurityCreatedEvent extends ChangeEvent
{
public SecurityCreatedEvent(Client client, Security security)
{
super(client, security);
}

public Security getSecurity()
{
return (Security) getSubject();
}
}

0 comments on commit 2a4ea16

Please sign in to comment.