1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 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 |
|
23 |
package org.jsampler.view; |
24 |
|
25 |
import javax.swing.table.AbstractTableModel; |
26 |
|
27 |
import org.jsampler.SamplerChannelModel; |
28 |
|
29 |
import org.jsampler.event.EffectSendsEvent; |
30 |
import org.jsampler.event.EffectSendsListener; |
31 |
|
32 |
import org.linuxsampler.lscp.FxSend; |
33 |
|
34 |
|
35 |
/** |
36 |
* A tabular data model for representing effect sends. |
37 |
* @author Grigor Iliev |
38 |
*/ |
39 |
public class FxSendTableModel extends AbstractTableModel { |
40 |
private SamplerChannelModel channelModel; |
41 |
|
42 |
/** |
43 |
* Creates a new instance of <code>FxSendTableModel</code>. |
44 |
* @param channelModel The <code>SamplerChannelModel</code>, which |
45 |
* effect sends should be represented by this table model. |
46 |
* @throws IllegalArgumentException If <code>channelModel</code> is <code>null</code>. |
47 |
*/ |
48 |
public |
49 |
FxSendTableModel(SamplerChannelModel channelModel) { |
50 |
if(channelModel == null) |
51 |
throw new IllegalArgumentException("channelModel should be non-null!"); |
52 |
|
53 |
this.channelModel = channelModel; |
54 |
channelModel.addEffectSendsListener(getHandler()); |
55 |
} |
56 |
|
57 |
/** |
58 |
* Gets the number of columns in the model. |
59 |
* @return The number of columns in the model. |
60 |
*/ |
61 |
public int |
62 |
getColumnCount() { return 1; } |
63 |
|
64 |
/** |
65 |
* Gets the number of rows in the model. |
66 |
* @return The number of rows in the model. |
67 |
*/ |
68 |
public int |
69 |
getRowCount() { return channelModel.getFxSendCount(); } |
70 |
|
71 |
/** |
72 |
* Gets the name of the column at <code>columnIndex</code>. |
73 |
* @return The name of the column at <code>columnIndex</code>. |
74 |
*/ |
75 |
public String |
76 |
getColumnName(int col) { return " "; } |
77 |
|
78 |
/** |
79 |
* Gets the value for the cell at <code>columnIndex</code> and |
80 |
* <code>rowIndex</code>. |
81 |
* @param row The row whose value is to be queried. |
82 |
* @param col The column whose value is to be queried. |
83 |
* @return The value for the cell at <code>columnIndex</code> and |
84 |
* <code>rowIndex</code>. |
85 |
*/ |
86 |
public Object |
87 |
getValueAt(int row, int col) { return channelModel.getFxSend(row); } |
88 |
|
89 |
/** |
90 |
* Sets the value in the cell at <code>col</code> |
91 |
* and <code>row</code> to <code>value</code>. |
92 |
*/ |
93 |
public void |
94 |
setValueAt(Object value, int row, int col) { |
95 |
if(value.toString().isEmpty()) return; |
96 |
|
97 |
FxSend fxs = channelModel.getFxSend(row); |
98 |
channelModel.setBackendFxSendName(fxs.getFxSendId(), value.toString()); |
99 |
fxs.setName(value.toString()); |
100 |
fireTableCellUpdated(row, col); |
101 |
} |
102 |
|
103 |
/** |
104 |
* Returns <code>true</code> if the cell at |
105 |
* <code>row</code> and <code>col</code> is editable. |
106 |
*/ |
107 |
public boolean |
108 |
isCellEditable(int row, int col) { |
109 |
if(col == 0) return true; |
110 |
return false; |
111 |
} |
112 |
|
113 |
/** |
114 |
* Gets the effect send at the specified position. |
115 |
* @param index The index of the effect send to be returned. |
116 |
* @return The effect send at the specified position. |
117 |
*/ |
118 |
public FxSend |
119 |
getFxSend(int index) { return channelModel.getFxSend(index); } |
120 |
|
121 |
/** |
122 |
* Gets the position of the specified effect send. |
123 |
* @param fxSend The effect send which position should be obtained. |
124 |
* @return The position of the specified effect send, or |
125 |
* <code>-1</code> if the specified effect send is not found in the table. |
126 |
*/ |
127 |
public int |
128 |
getFxSendPosition(FxSend fxSend) { |
129 |
if(fxSend == null) return -1; |
130 |
|
131 |
for(int i = 0; i < channelModel.getFxSendCount(); i++) { |
132 |
if(channelModel.getFxSend(i).getFxSendId() == fxSend.getFxSendId()) { |
133 |
return i; |
134 |
} |
135 |
} |
136 |
|
137 |
return -1; |
138 |
} |
139 |
|
140 |
private final Handler eventHandler = new Handler(); |
141 |
|
142 |
private Handler |
143 |
getHandler() { return eventHandler; } |
144 |
|
145 |
private class Handler implements EffectSendsListener { |
146 |
/** Invoked when a new effect send is added to a sampler channel. */ |
147 |
public void |
148 |
effectSendAdded(EffectSendsEvent e) { fireTableDataChanged(); } |
149 |
|
150 |
/** Invoked when an effect send is removed from a sampler channel. */ |
151 |
public void |
152 |
effectSendRemoved(EffectSendsEvent e) { fireTableDataChanged(); } |
153 |
|
154 |
/** Invoked when an effect send's setting are changed. */ |
155 |
public void |
156 |
effectSendChanged(EffectSendsEvent e) { |
157 |
for(int i = 0; i < getRowCount(); i ++) { |
158 |
if(e.getFxSend().equals(getValueAt(i, 0))) { |
159 |
fireTableRowsUpdated(i, i); |
160 |
return; |
161 |
} |
162 |
} |
163 |
} |
164 |
} |
165 |
} |