1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 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.CC; |
28 |
import org.jsampler.MidiInstrumentMap; |
29 |
import org.jsampler.SamplerModel; |
30 |
|
31 |
import org.jsampler.event.ListEvent; |
32 |
import org.jsampler.event.ListListener; |
33 |
import org.jsampler.event.MidiInstrumentMapEvent; |
34 |
import org.jsampler.event.MidiInstrumentMapListener; |
35 |
|
36 |
import static org.jsampler.JSI18n.i18n; |
37 |
|
38 |
/** |
39 |
* A tabular data model for representing MIDI instrument maps. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public class MidiMapTableModel extends AbstractTableModel { |
43 |
private final MidiMapTable table; |
44 |
/** |
45 |
* Creates a new instance of <code>MidiMapTableModel</code>. |
46 |
*/ |
47 |
public |
48 |
MidiMapTableModel(MidiMapTable table) { |
49 |
this.table = table; |
50 |
SamplerModel sm = CC.getSamplerModel(); |
51 |
|
52 |
for(int i = 0; i < sm.getMidiInstrumentMapCount(); i++) { |
53 |
sm.getMidiInstrumentMap(i).addMidiInstrumentMapListener(getHandler()); |
54 |
} |
55 |
|
56 |
sm.addMidiInstrumentMapListListener(getHandler()); |
57 |
|
58 |
} |
59 |
|
60 |
/** |
61 |
* Gets the number of columns in the model. |
62 |
* @return The number of columns in the model. |
63 |
*/ |
64 |
@Override |
65 |
public int |
66 |
getColumnCount() { return 1; } |
67 |
|
68 |
/** |
69 |
* Gets the number of rows in the model. |
70 |
* @return The number of rows in the model. |
71 |
*/ |
72 |
@Override |
73 |
public int |
74 |
getRowCount() { return CC.getSamplerModel().getMidiInstrumentMapCount(); } |
75 |
|
76 |
/** |
77 |
* Gets the name of the column at <code>columnIndex</code>. |
78 |
* @return The name of the column at <code>columnIndex</code>. |
79 |
*/ |
80 |
@Override |
81 |
public String |
82 |
getColumnName(int col) { return i18n.getLabel("MidiMapTableModel.title"); } |
83 |
|
84 |
/** |
85 |
* Gets the value for the cell at <code>columnIndex</code> and |
86 |
* <code>rowIndex</code>. |
87 |
* @param row The row whose value is to be queried. |
88 |
* @param col The column whose value is to be queried. |
89 |
* @return The value for the cell at <code>columnIndex</code> and |
90 |
* <code>rowIndex</code>. |
91 |
*/ |
92 |
@Override |
93 |
public Object |
94 |
getValueAt(int row, int col) { |
95 |
return CC.getSamplerModel().getMidiInstrumentMap(row); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Sets the value in the cell at <code>col</code> |
100 |
* and <code>row</code> to <code>value</code>. |
101 |
*/ |
102 |
@Override |
103 |
public void |
104 |
setValueAt(Object value, int row, int col) { |
105 |
|
106 |
fireTableCellUpdated(row, col); |
107 |
} |
108 |
|
109 |
/** |
110 |
* Returns <code>true</code> if the cell at |
111 |
* <code>row</code> and <code>col</code> is editable. |
112 |
*/ |
113 |
@Override |
114 |
public boolean |
115 |
isCellEditable(int row, int col) { return false; } |
116 |
|
117 |
|
118 |
private final Handler eventHandler = new Handler(); |
119 |
|
120 |
private Handler |
121 |
getHandler() { return eventHandler; } |
122 |
|
123 |
private class Handler implements ListListener<MidiInstrumentMap>, MidiInstrumentMapListener { |
124 |
/** Invoked when an orchestra is added to the orchestra list. */ |
125 |
@Override |
126 |
public void |
127 |
entryAdded(ListEvent<MidiInstrumentMap> e) { |
128 |
e.getEntry().addMidiInstrumentMapListener(getHandler()); |
129 |
fireTableDataChanged(); |
130 |
table.setSelectedMidiInstrumentMap(e.getEntry()); |
131 |
} |
132 |
|
133 |
/** Invoked when an orchestra is removed from the orchestra list. */ |
134 |
@Override |
135 |
public void |
136 |
entryRemoved(ListEvent<MidiInstrumentMap> e) { |
137 |
e.getEntry().removeMidiInstrumentMapListener(getHandler()); |
138 |
fireTableDataChanged(); |
139 |
} |
140 |
|
141 |
@Override |
142 |
public void |
143 |
nameChanged(MidiInstrumentMapEvent e) { |
144 |
MidiInstrumentMap m = (MidiInstrumentMap)e.getSource(); |
145 |
int idx = CC.getSamplerModel().getMidiInstrumentMapIndex(m); |
146 |
fireTableRowsUpdated(idx, idx); |
147 |
} |
148 |
|
149 |
@Override |
150 |
public void |
151 |
instrumentAdded(MidiInstrumentMapEvent e) { } |
152 |
|
153 |
@Override |
154 |
public void |
155 |
instrumentRemoved(MidiInstrumentMapEvent e) { } |
156 |
} |
157 |
} |