forked from portfolio-performance/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionTypeEditingSupport.java
255 lines (198 loc) · 9.75 KB
/
TransactionTypeEditingSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package name.abuchen.portfolio.ui.util.viewers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.AccountTransaction;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.Transaction;
import name.abuchen.portfolio.model.TransactionPair;
import name.abuchen.portfolio.ui.views.actions.ConvertBuySellToDeliveryAction;
import name.abuchen.portfolio.ui.views.actions.ConvertDeliveryToBuySellAction;
import name.abuchen.portfolio.ui.views.actions.RevertBuySellAction;
import name.abuchen.portfolio.ui.views.actions.RevertDeliveryAction;
import name.abuchen.portfolio.ui.views.actions.RevertDepositRemovalAction;
import name.abuchen.portfolio.ui.views.actions.RevertInterestAction;
import name.abuchen.portfolio.ui.views.actions.RevertTransferAction;
/**
* Creates a cell editor with a combo box of transactions types.
*/
public class TransactionTypeEditingSupport extends ColumnEditingSupport
{
private static final Object[] TRANSITIONS = new Object[] { //
PortfolioTransaction.Type.BUY, PortfolioTransaction.Type.SELL,
new Class[] { RevertBuySellAction.class },
PortfolioTransaction.Type.BUY, PortfolioTransaction.Type.DELIVERY_INBOUND,
new Class[] { ConvertBuySellToDeliveryAction.class },
PortfolioTransaction.Type.BUY, PortfolioTransaction.Type.DELIVERY_OUTBOUND,
new Class[] { RevertBuySellAction.class, ConvertBuySellToDeliveryAction.class },
PortfolioTransaction.Type.SELL, PortfolioTransaction.Type.BUY,
new Class[] { RevertBuySellAction.class },
PortfolioTransaction.Type.SELL, PortfolioTransaction.Type.DELIVERY_OUTBOUND,
new Class[] { ConvertBuySellToDeliveryAction.class },
PortfolioTransaction.Type.SELL, PortfolioTransaction.Type.DELIVERY_INBOUND,
new Class[] { RevertBuySellAction.class, ConvertBuySellToDeliveryAction.class },
PortfolioTransaction.Type.DELIVERY_INBOUND, PortfolioTransaction.Type.DELIVERY_OUTBOUND,
new Class[] { RevertDeliveryAction.class },
PortfolioTransaction.Type.DELIVERY_INBOUND, PortfolioTransaction.Type.BUY,
new Class[] { ConvertDeliveryToBuySellAction.class },
PortfolioTransaction.Type.DELIVERY_INBOUND, PortfolioTransaction.Type.SELL,
new Class[] { RevertDeliveryAction.class, ConvertDeliveryToBuySellAction.class },
PortfolioTransaction.Type.DELIVERY_OUTBOUND, PortfolioTransaction.Type.DELIVERY_INBOUND,
new Class[] { RevertDeliveryAction.class },
PortfolioTransaction.Type.DELIVERY_OUTBOUND, PortfolioTransaction.Type.SELL,
new Class[] { ConvertDeliveryToBuySellAction.class },
PortfolioTransaction.Type.DELIVERY_OUTBOUND, PortfolioTransaction.Type.BUY,
new Class[] { RevertDeliveryAction.class, ConvertDeliveryToBuySellAction.class },
PortfolioTransaction.Type.SHORT, PortfolioTransaction.Type.COVER,
new Class[] { RevertBuySellAction.class },
PortfolioTransaction.Type.COVER, PortfolioTransaction.Type.SHORT,
new Class[] { RevertBuySellAction.class },
AccountTransaction.Type.SELL, AccountTransaction.Type.BUY,
new Class[] { RevertBuySellAction.class },
AccountTransaction.Type.BUY, AccountTransaction.Type.SELL,
new Class[] { RevertBuySellAction.class },
AccountTransaction.Type.SHORT, AccountTransaction.Type.COVER,
new Class[] { RevertBuySellAction.class },
AccountTransaction.Type.COVER, AccountTransaction.Type.SHORT,
new Class[] { RevertBuySellAction.class },
AccountTransaction.Type.TRANSFER_IN, AccountTransaction.Type.TRANSFER_OUT,
new Class[] { RevertTransferAction.class },
AccountTransaction.Type.TRANSFER_OUT, AccountTransaction.Type.TRANSFER_IN,
new Class[] { RevertTransferAction.class },
AccountTransaction.Type.DEPOSIT, AccountTransaction.Type.REMOVAL,
new Class[] { RevertDepositRemovalAction.class },
AccountTransaction.Type.REMOVAL, AccountTransaction.Type.DEPOSIT,
new Class[] { RevertDepositRemovalAction.class },
AccountTransaction.Type.INTEREST, AccountTransaction.Type.INTEREST_CHARGE,
new Class[] { RevertInterestAction.class },
AccountTransaction.Type.INTEREST_CHARGE, AccountTransaction.Type.INTEREST,
new Class[] { RevertInterestAction.class } };
private final Client client;
private ComboBoxCellEditor editor;
private List<Object> comboBoxItems;
public TransactionTypeEditingSupport(Client client)
{
this.client = client;
}
/**
* Returns the owner of the transaction.
*/
private Account lookupOwner(AccountTransaction t)
{
return client.getAccounts().stream().filter(a -> a.getTransactions().contains(t)).findAny()
.orElseThrow(IllegalArgumentException::new);
}
/**
* Returns the owner of the transaction.
*/
private Portfolio lookupOwner(PortfolioTransaction t)
{
return client.getPortfolios().stream().filter(a -> a.getTransactions().contains(t)).findAny()
.orElseThrow(IllegalArgumentException::new);
}
private TransactionPair<?> getTransactionPair(Object element)
{
if (element instanceof TransactionPair<?>)
return (TransactionPair<?>) element;
Transaction t = getTransaction(element);
if (t instanceof AccountTransaction)
return new TransactionPair<>(lookupOwner((AccountTransaction) t), (AccountTransaction) t);
else if (t instanceof PortfolioTransaction)
return new TransactionPair<>(lookupOwner((PortfolioTransaction) t), (PortfolioTransaction) t);
else
throw new UnsupportedOperationException();
}
private Transaction getTransaction(Object element)
{
if (element instanceof Transaction)
return (Transaction) element;
else if (element instanceof TransactionPair<?>)
return ((TransactionPair<?>) element).getTransaction();
else
throw new UnsupportedOperationException();
}
private Enum<?> getTypeValue(Transaction t)
{
if (t instanceof AccountTransaction)
return ((AccountTransaction) t).getType();
else if (t instanceof PortfolioTransaction)
return ((PortfolioTransaction) t).getType();
else
throw new IllegalArgumentException();
}
@Override
public boolean canEdit(Object element)
{
Transaction t = getTransaction(element);
Enum<?> type = getTypeValue(t);
for (int ii = 0; ii < TRANSITIONS.length; ii += 3)
{
if (TRANSITIONS[ii] == type)
return true;
}
return false;
}
@Override
public final CellEditor createEditor(Composite composite)
{
editor = new ComboBoxCellEditor(composite, new String[0], SWT.READ_ONLY);
return editor;
}
@Override
public final void prepareEditor(Object element)
{
Transaction t = getTransaction(element);
Enum<?> type = getTypeValue(t);
comboBoxItems = new ArrayList<>();
comboBoxItems.add(type);
for (int ii = 0; ii < TRANSITIONS.length; ii += 3)
{
if (TRANSITIONS[ii] == type)
comboBoxItems.add(TRANSITIONS[ii + 1]);
}
Collections.sort(comboBoxItems, (r, l) -> r.toString().compareTo(l.toString()));
editor.setItems(comboBoxItems.stream().map(Object::toString).toArray(String[]::new));
}
@Override
public final Object getValue(Object element) throws Exception
{
Transaction t = getTransaction(element);
Enum<?> type = getTypeValue(t);
return comboBoxItems.indexOf(type);
}
@Override
public final void setValue(Object element, Object value) throws Exception
{
int index = (Integer) value;
if (index < 0 || index >= comboBoxItems.size())
return;
Transaction t = getTransaction(element);
Enum<?> oldValue = getTypeValue(t);
Enum<?> newValue = (Enum<?>) comboBoxItems.get(index);
if (newValue.equals(oldValue))
return;
TransactionPair<?> pair = getTransactionPair(element);
Class<?>[] transition = getTransition(oldValue, newValue);
for (Class<?> action : transition)
((Action) action.getDeclaredConstructor(Client.class, TransactionPair.class) //
.newInstance(client, pair)).run();
notify(element, newValue, oldValue);
}
private Class<?>[] getTransition(Enum<?> fromValue, Enum<?> toValue)
{
for (int ii = 0; ii < TRANSITIONS.length; ii += 3)
{
if (TRANSITIONS[ii] == fromValue && TRANSITIONS[ii + 1] == toValue)
return (Class<?>[]) TRANSITIONS[ii + 2];
}
throw new IllegalArgumentException();
}
}