1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2011 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
package org.jsampler.view; |
23 |
|
24 |
import java.beans.PropertyChangeEvent; |
25 |
import java.beans.PropertyChangeListener; |
26 |
import javax.swing.table.AbstractTableModel; |
27 |
import org.jsampler.AudioDeviceModel; |
28 |
import org.jsampler.event.ListEvent; |
29 |
import org.jsampler.event.ListListener; |
30 |
import static org.jsampler.view.SamplerTreeModel.*; |
31 |
|
32 |
/** |
33 |
* |
34 |
* @author Grigor Iliev |
35 |
*/ |
36 |
public class SamplerTableModel extends AbstractTableModel { |
37 |
private TreeNodeBase node; |
38 |
|
39 |
|
40 |
/** Creates a new instance of <code>SamplerTableModel</code>. */ |
41 |
public |
42 |
SamplerTableModel() { |
43 |
this(null); |
44 |
} |
45 |
|
46 |
/** Creates a new instance of <code>SamplerTableModel</code>. */ |
47 |
public |
48 |
SamplerTableModel(TreeNodeBase node) { |
49 |
this.node = node; |
50 |
|
51 |
} |
52 |
|
53 |
public TreeNodeBase |
54 |
getNode() { return node; } |
55 |
|
56 |
public void |
57 |
setNode(TreeNodeBase node) { |
58 |
if(this.node != null) removeListeners(this.node); |
59 |
this.node = node; |
60 |
if(node != null) addListeners(node); |
61 |
fireTableStructureChanged(); |
62 |
fireTableDataChanged(); |
63 |
} |
64 |
|
65 |
private void |
66 |
addListeners(TreeNodeBase node) { |
67 |
node.addPropertyChangeListener(getHandler()); |
68 |
} |
69 |
|
70 |
private void |
71 |
removeListeners(TreeNodeBase node) { |
72 |
node.addPropertyChangeListener(getHandler()); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Gets the number of columns in the model. |
77 |
* @return The number of columns in the model. |
78 |
*/ |
79 |
@Override |
80 |
public int |
81 |
getColumnCount() { return node == null ? 1 : node.getColumnCount(); } |
82 |
|
83 |
/** |
84 |
* Gets the name of the column at <code>columnIndex</code>. |
85 |
* @return The name of the column at <code>columnIndex</code>. |
86 |
*/ |
87 |
@Override |
88 |
public String |
89 |
getColumnName(int col) { return node == null ? " " : node.getColumnName(col); } |
90 |
|
91 |
/** |
92 |
* Gets the number of rows in the model. |
93 |
* @return The number of rows in the model. |
94 |
*/ |
95 |
@Override |
96 |
public int |
97 |
getRowCount() { return node == null ? 0 : node.getRowCount(); } |
98 |
|
99 |
/** |
100 |
* Gets the value for the cell at <code>columnIndex</code> and |
101 |
* <code>rowIndex</code>. |
102 |
* @param row The row whose value is to be queried. |
103 |
* @param col The column whose value is to be queried. |
104 |
* @return The value for the cell at <code>columnIndex</code> and |
105 |
* <code>rowIndex</code>. |
106 |
*/ |
107 |
@Override |
108 |
public Object |
109 |
getValueAt(int row, int col) { |
110 |
return node == null ? null : node.getValueAt(row, col); |
111 |
} |
112 |
|
113 |
public TreeNodeBase |
114 |
getNodeAt(int idx) { |
115 |
if(node == null) return null; |
116 |
if(idx >= node.getChildCount()) return null; |
117 |
return node.getChildAt(idx); |
118 |
} |
119 |
|
120 |
|
121 |
private final EventHandler eventHandler = new EventHandler(); |
122 |
|
123 |
private EventHandler |
124 |
getHandler() { return eventHandler; } |
125 |
|
126 |
private class EventHandler implements PropertyChangeListener { |
127 |
@Override |
128 |
public void |
129 |
propertyChange(PropertyChangeEvent e) { |
130 |
if(e.getPropertyName() == "SamplerTreeModel.update") { |
131 |
fireTableDataChanged(); |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
AudioDeviceListListener audioDeviceListListener = new AudioDeviceListListener(); |
137 |
|
138 |
private class AudioDeviceListListener implements ListListener<AudioDeviceModel> { |
139 |
/** Invoked when a new entry is added to a list. */ |
140 |
public void |
141 |
entryAdded(ListEvent<AudioDeviceModel> e) { fireTableDataChanged(); } |
142 |
|
143 |
/** Invoked when an entry is removed from a list. */ |
144 |
public void |
145 |
entryRemoved(ListEvent<AudioDeviceModel> e) { fireTableDataChanged(); } |
146 |
} |
147 |
} |