1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.view.panels; |
5 | |
|
6 | |
import java.awt.BorderLayout; |
7 | |
import java.awt.FlowLayout; |
8 | |
import java.awt.Point; |
9 | |
import java.awt.event.ActionEvent; |
10 | |
import java.awt.event.MouseAdapter; |
11 | |
import java.awt.event.MouseEvent; |
12 | |
import java.util.LinkedList; |
13 | |
import java.util.List; |
14 | |
|
15 | |
import javax.swing.JLabel; |
16 | |
import javax.swing.JPanel; |
17 | |
import javax.swing.JScrollPane; |
18 | |
import javax.swing.event.TreeExpansionEvent; |
19 | |
import javax.swing.event.TreeExpansionListener; |
20 | |
import javax.swing.event.TreeSelectionEvent; |
21 | |
import javax.swing.event.TreeSelectionListener; |
22 | |
import javax.swing.table.TableModel; |
23 | |
import javax.swing.tree.TreePath; |
24 | |
import javax.swing.tree.TreeSelectionModel; |
25 | |
|
26 | |
import org.homeunix.thecave.buddi.Const; |
27 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
28 | |
import org.homeunix.thecave.buddi.model.Account; |
29 | |
import org.homeunix.thecave.buddi.model.AccountType; |
30 | |
import org.homeunix.thecave.buddi.model.Document; |
31 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
32 | |
import org.homeunix.thecave.buddi.model.swing.MyAccountTreeTableModel; |
33 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
34 | |
import org.homeunix.thecave.buddi.view.MainFrame; |
35 | |
import org.homeunix.thecave.buddi.view.menu.items.EditEditTransactions; |
36 | |
import org.homeunix.thecave.buddi.view.swing.MyAccountTableAmountCellRenderer; |
37 | |
import org.homeunix.thecave.buddi.view.swing.MyAccountTreeNameCellRenderer; |
38 | |
import org.jdesktop.swingx.JXTreeTable; |
39 | |
import org.jdesktop.swingx.decorator.HighlighterFactory; |
40 | |
import org.jdesktop.swingx.table.ColumnFactory; |
41 | |
import org.jdesktop.swingx.table.TableColumnExt; |
42 | |
|
43 | |
import ca.digitalcave.moss.application.document.DocumentChangeEvent; |
44 | |
import ca.digitalcave.moss.application.document.DocumentChangeListener; |
45 | |
import ca.digitalcave.moss.common.OperatingSystemUtil; |
46 | |
import ca.digitalcave.moss.swing.MossPanel; |
47 | |
|
48 | 6338 | public class MyAccountsPanel extends MossPanel { |
49 | |
public static final long serialVersionUID = 0; |
50 | |
|
51 | |
private final JXTreeTable tree; |
52 | |
private final JLabel balanceLabel; |
53 | |
|
54 | |
private final MyAccountTreeTableModel treeTableModel; |
55 | |
|
56 | |
private final MainFrame parent; |
57 | |
|
58 | |
private final DocumentChangeListener listener; |
59 | |
|
60 | |
public MyAccountsPanel(MainFrame parent) { |
61 | 3034 | super(true); |
62 | 3034 | this.parent = parent; |
63 | 3034 | this.treeTableModel = new MyAccountTreeTableModel((Document) parent.getDocument()); |
64 | |
|
65 | 3034 | listener = new DocumentChangeListener(){ |
66 | |
public void documentChange(DocumentChangeEvent event) { |
67 | 2835 | updateContent(); |
68 | 2835 | } |
69 | |
}; |
70 | |
|
71 | 3034 | tree = new JXTreeTable(){ |
72 | |
public static final long serialVersionUID = 0; |
73 | |
@Override |
74 | |
public String getToolTipText(MouseEvent event) { |
75 | 0 | if (PrefsModel.getInstance().isShowTooltips()){ |
76 | 0 | Point p = event.getPoint(); |
77 | 0 | int rowIndex = rowAtPoint(p); |
78 | |
|
79 | 0 | if (this.getPathForRow(rowIndex) != null){ |
80 | 0 | Object[] path = this.getPathForRow(rowIndex).getPath(); |
81 | 0 | if (path != null && path.length > 0){ |
82 | 0 | Object node = path[path.length - 1]; |
83 | 0 | if (node instanceof Account) |
84 | 0 | return ((Account) node).getNotes(); |
85 | |
} |
86 | |
} |
87 | |
} |
88 | 0 | return null; |
89 | |
} |
90 | |
}; |
91 | 3034 | tree.setColumnFactory(new ColumnFactory(){ |
92 | |
@Override |
93 | |
public void configureTableColumn(TableModel arg0, TableColumnExt arg1) { |
94 | 6338 | super.configureTableColumn(arg0, arg1); |
95 | |
|
96 | 6338 | arg1.setCellRenderer(new MyAccountTableAmountCellRenderer((Document) MyAccountsPanel.this.parent.getDocument())); |
97 | 6338 | } |
98 | |
}); |
99 | 3034 | tree.setTreeTableModel(treeTableModel); |
100 | 3034 | balanceLabel = new JLabel("Change Me"); |
101 | |
|
102 | 3034 | open(); |
103 | 3034 | } |
104 | |
|
105 | |
public List<Account> getSelectedAccounts(){ |
106 | 659692 | List<Account> accounts = new LinkedList<Account>(); |
107 | |
|
108 | 659692 | for (Integer i : tree.getSelectedRows()) { |
109 | 0 | if (tree.getModel().getValueAt(i, -1) instanceof Account) |
110 | 0 | accounts.add((Account) tree.getModel().getValueAt(i, -1)); |
111 | |
} |
112 | |
|
113 | 659692 | return accounts; |
114 | |
} |
115 | |
|
116 | |
public List<AccountType> getSelectedTypes(){ |
117 | 0 | List<AccountType> types = new LinkedList<AccountType>(); |
118 | |
|
119 | 0 | for (Integer i : tree.getSelectedRows()) { |
120 | 0 | if (tree.getModel().getValueAt(i, -1) instanceof AccountType) |
121 | 0 | types.add((AccountType) tree.getModel().getValueAt(i, -1)); |
122 | |
} |
123 | |
|
124 | 0 | return types; |
125 | |
} |
126 | |
|
127 | |
public void actionPerformed(ActionEvent e) { |
128 | |
|
129 | 0 | } |
130 | |
|
131 | |
public void init() { |
132 | 3034 | super.init(); |
133 | |
|
134 | 3034 | tree.setRootVisible(false); |
135 | 3034 | tree.setShowsRootHandles(true); |
136 | 3034 | tree.setAutoResizeMode(JXTreeTable.AUTO_RESIZE_ALL_COLUMNS); |
137 | 3034 | tree.setClosedIcon(null); |
138 | 3034 | tree.setOpenIcon(null); |
139 | 3034 | tree.setLeafIcon(null); |
140 | |
|
141 | 3034 | tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
142 | 3034 | tree.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
143 | 3034 | tree.addHighlighter(HighlighterFactory.createAlternateStriping(Const.COLOR_EVEN_ROW, Const.COLOR_ODD_ROW)); |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 3034 | tree.setTreeCellRenderer(new MyAccountTreeNameCellRenderer()); |
151 | |
|
152 | 3034 | parent.getDocument().addDocumentChangeListener(listener); |
153 | |
|
154 | 3034 | JScrollPane listScroller = new JScrollPane(tree); |
155 | |
|
156 | 3034 | JPanel balanceLabelPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
157 | 3034 | balanceLabelPanel.add(balanceLabel); |
158 | |
|
159 | 3034 | JPanel listScrollerPanel = new JPanel(new BorderLayout()); |
160 | 3034 | listScrollerPanel.add(listScroller, BorderLayout.CENTER); |
161 | |
|
162 | 3034 | JPanel mainPanel = new JPanel(); |
163 | 3034 | mainPanel.setLayout(new BorderLayout()); |
164 | |
|
165 | 3034 | mainPanel.add(listScrollerPanel, BorderLayout.CENTER); |
166 | 3034 | mainPanel.add(balanceLabelPanel, BorderLayout.SOUTH); |
167 | |
|
168 | 3034 | if (OperatingSystemUtil.isMac()){ |
169 | 0 | listScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | 3034 | tree.addTreeSelectionListener(new TreeSelectionListener(){ |
181 | |
public void valueChanged(TreeSelectionEvent arg0) { |
182 | 0 | parent.updateButtons(); |
183 | 0 | } |
184 | |
}); |
185 | |
|
186 | 3034 | tree.addMouseListener(new MouseAdapter(){ |
187 | |
public void mouseClicked(MouseEvent arg0) { |
188 | 0 | if (arg0.getClickCount() >= 2) |
189 | 0 | new EditEditTransactions(parent).doClick(); |
190 | 0 | super.mouseClicked(arg0); |
191 | 0 | } |
192 | |
}); |
193 | |
|
194 | 3034 | tree.addTreeExpansionListener(new TreeExpansionListener(){ |
195 | |
public void treeCollapsed(TreeExpansionEvent event) { |
196 | 0 | Object o = event.getPath().getPath()[event.getPath().getPath().length - 1]; |
197 | 0 | if (o instanceof AccountType){ |
198 | 0 | AccountType t = (AccountType) o; |
199 | 0 | t.setExpanded(false); |
200 | |
} |
201 | 0 | } |
202 | |
public void treeExpanded(TreeExpansionEvent event) { |
203 | 3032 | Object o = event.getPath().getPath()[event.getPath().getPath().length - 1]; |
204 | 3032 | if (o instanceof AccountType){ |
205 | 0 | AccountType t = (AccountType) o; |
206 | 0 | t.setExpanded(true); |
207 | |
} |
208 | 3032 | } |
209 | |
}); |
210 | |
|
211 | 3034 | updateButtons(); |
212 | |
|
213 | |
|
214 | 3034 | this.setLayout(new BorderLayout()); |
215 | 3034 | this.add(mainPanel, BorderLayout.CENTER); |
216 | 3034 | } |
217 | |
|
218 | |
public void updateContent() { |
219 | 15154 | super.updateContent(); |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | 15154 | for (AccountType t : ((Document) parent.getDocument()).getAccountTypes()) { |
226 | 136386 | TreePath path = new TreePath(new Object[]{treeTableModel.getRoot(), t}); |
227 | 136386 | if (t.isExpanded()) |
228 | 0 | tree.expandPath(path); |
229 | |
else |
230 | 136386 | tree.collapsePath(path); |
231 | 136386 | } |
232 | |
|
233 | 15154 | long netWorth = ((Document) parent.getDocument()).getNetWorth(null); |
234 | 15154 | balanceLabel.setText(TextFormatter.getHtmlWrapper( |
235 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.NET_WORTH) |
236 | |
+ ": " |
237 | |
+ TextFormatter.getFormattedCurrency(netWorth))); |
238 | |
|
239 | 15154 | tree.invalidate(); |
240 | 15154 | } |
241 | |
|
242 | |
public void fireStructureChanged(){ |
243 | 132 | treeTableModel.fireStructureChanged(); |
244 | 132 | } |
245 | |
} |