1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2006 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.DefaultOrchestraModel; |
28 |
import org.jsampler.OrchestraModel; |
29 |
|
30 |
import org.jsampler.event.OrchestraEvent; |
31 |
import org.jsampler.event.OrchestraListener; |
32 |
|
33 |
|
34 |
/** |
35 |
* A tabular data model for representing instruments. |
36 |
* @author Grigor Iliev |
37 |
*/ |
38 |
public class InstrumentTableModel extends AbstractTableModel { |
39 |
private OrchestraModel orchestraModel; |
40 |
|
41 |
/** |
42 |
* Creates a new instance of <code>InstrumentTableModel</code>. |
43 |
*/ |
44 |
public |
45 |
InstrumentTableModel() { this(new DefaultOrchestraModel()); } |
46 |
|
47 |
/** |
48 |
* Creates a new instance of <code>InstrumentTableModel</code>. |
49 |
* @param orchestraModel The <code>OrchestraModel</code>, |
50 |
* which this table model should represent. |
51 |
* @throws IllegalArgumentException If <code>orchestraModel</code> is <code>null</code>. |
52 |
*/ |
53 |
public |
54 |
InstrumentTableModel(OrchestraModel orchestraModel) { |
55 |
setOrchestraModel(orchestraModel); |
56 |
} |
57 |
|
58 |
/** |
59 |
* Gets the number of columns in the model. |
60 |
* @return The number of columns in the model. |
61 |
*/ |
62 |
public int |
63 |
getColumnCount() { return 1; } |
64 |
|
65 |
/** |
66 |
* Gets the number of rows in the model. |
67 |
* @return The number of rows in the model. |
68 |
*/ |
69 |
public int |
70 |
getRowCount() { return orchestraModel.getInstrumentCount(); } |
71 |
|
72 |
/** |
73 |
* Gets the name of the column at <code>columnIndex</code>. |
74 |
* @return The name of the column at <code>columnIndex</code>. |
75 |
*/ |
76 |
public String |
77 |
getColumnName(int col) { |
78 |
String s = orchestraModel.getName(); |
79 |
if(s == null || s.length() == 0) return " "; |
80 |
return s; |
81 |
} |
82 |
|
83 |
/** |
84 |
* Gets the value for the cell at <code>columnIndex</code> and |
85 |
* <code>rowIndex</code>. |
86 |
* @param row The row whose value is to be queried. |
87 |
* @param col The column whose value is to be queried. |
88 |
* @return The value for the cell at <code>columnIndex</code> and |
89 |
* <code>rowIndex</code>. |
90 |
*/ |
91 |
public Object |
92 |
getValueAt(int row, int col) { |
93 |
return orchestraModel.getInstrument(row); |
94 |
} |
95 |
|
96 |
/** |
97 |
* Sets the value in the cell at <code>col</code> |
98 |
* and <code>row</code> to <code>value</code>. |
99 |
*/ |
100 |
public void |
101 |
setValueAt(Object value, int row, int col) { |
102 |
orchestraModel.getInstrument(row).setName(value.toString()); |
103 |
fireTableCellUpdated(row, col); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Returns <code>true</code> if the cell at |
108 |
* <code>row</code> and <code>col</code> is editable. |
109 |
*/ |
110 |
public boolean |
111 |
isCellEditable(int row, int col) { return false; } |
112 |
|
113 |
/** |
114 |
* Gets the <code>OrchestraModel</code>, represented by this table model. |
115 |
*/ |
116 |
public OrchestraModel |
117 |
getOrchestraModel() { return orchestraModel; } |
118 |
|
119 |
/** |
120 |
* Sets the <code>OrchestraModel</code>, represented by this table model. |
121 |
* @param orchestraModel The new <code>OrchestraModel</code>, |
122 |
* represented by this table model. |
123 |
* @throws IllegalArgumentException If <code>orchestraModel</code> is <code>null</code>. |
124 |
*/ |
125 |
public void |
126 |
setOrchestraModel(OrchestraModel orchestraModel) { |
127 |
if(orchestraModel == null) |
128 |
throw new IllegalArgumentException("orchestraModel should be non-null!"); |
129 |
|
130 |
if(getOrchestraModel() != null) |
131 |
getOrchestraModel().removeOrchestraListener(getHandler()); |
132 |
|
133 |
this.orchestraModel = orchestraModel; |
134 |
orchestraModel.addOrchestraListener(getHandler()); |
135 |
|
136 |
fireTableStructureChanged(); |
137 |
fireTableDataChanged(); |
138 |
} |
139 |
|
140 |
private final Handler eventHandler = new Handler(); |
141 |
|
142 |
private Handler |
143 |
getHandler() { return eventHandler; } |
144 |
|
145 |
private class Handler implements OrchestraListener { |
146 |
/** Invoked when the name of orchestra is changed. */ |
147 |
public void |
148 |
nameChanged(OrchestraEvent e) { fireTableStructureChanged(); } |
149 |
|
150 |
/** Invoked when the description of orchestra is changed. */ |
151 |
public void |
152 |
descriptionChanged(OrchestraEvent e) { fireTableDataChanged(); } |
153 |
|
154 |
/** Invoked when an instrument is added to the orchestra. */ |
155 |
public void |
156 |
instrumentAdded(OrchestraEvent e) { fireTableDataChanged(); } |
157 |
|
158 |
/** Invoked when an instrument is removed from the orchestra. */ |
159 |
public void |
160 |
instrumentRemoved(OrchestraEvent e) { fireTableDataChanged(); } |
161 |
|
162 |
/** Invoked when the settings of an instrument are changed. */ |
163 |
public void |
164 |
instrumentChanged(OrchestraEvent e) { fireTableDataChanged(); } |
165 |
} |
166 |
} |