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.OrchestraListModel; |
28 |
import org.jsampler.OrchestraModel; |
29 |
|
30 |
import org.jsampler.event.ListEvent; |
31 |
import org.jsampler.event.ListListener; |
32 |
import org.jsampler.event.OrchestraAdapter; |
33 |
import org.jsampler.event.OrchestraEvent; |
34 |
|
35 |
import static org.jsampler.JSI18n.i18n; |
36 |
|
37 |
|
38 |
/** |
39 |
* A tabular data model for representing orchestras. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public class OrchestraTableModel extends AbstractTableModel { |
43 |
private final OrchestraListModel orchestraListModel; |
44 |
|
45 |
/** |
46 |
* Creates a new instance of <code>OrchestraTableModel</code>. |
47 |
* @param orchestraListModel The <code>OrchestraListModel</code>, |
48 |
* which this table model should represent. |
49 |
* @throws IllegalArgumentException If <code>orchestraListModel</code> |
50 |
* is <code>null</code>. |
51 |
*/ |
52 |
public |
53 |
OrchestraTableModel(OrchestraListModel orchestraListModel) { |
54 |
if(orchestraListModel == null) throw new IllegalArgumentException ( |
55 |
"orchestraListModel should be non-null!" |
56 |
); |
57 |
|
58 |
this.orchestraListModel = orchestraListModel; |
59 |
orchestraListModel.addOrchestraListListener(getHandler()); |
60 |
|
61 |
for(int i = 0; i < orchestraListModel.getOrchestraCount(); i++) { |
62 |
orchestraListModel.getOrchestra(i).addOrchestraListener(getHandler()); |
63 |
} |
64 |
orchestraListModel.addOrchestraListListener(getHandler()); |
65 |
} |
66 |
|
67 |
/** |
68 |
* Gets the number of columns in the model. |
69 |
* @return The number of columns in the model. |
70 |
*/ |
71 |
public int |
72 |
getColumnCount() { return 1; } |
73 |
|
74 |
/** |
75 |
* Gets the number of rows in the model. |
76 |
* @return The number of rows in the model. |
77 |
*/ |
78 |
public int |
79 |
getRowCount() { return orchestraListModel.getOrchestraCount(); } |
80 |
|
81 |
/** |
82 |
* Gets the name of the column at <code>columnIndex</code>. |
83 |
* @return The name of the column at <code>columnIndex</code>. |
84 |
*/ |
85 |
public String |
86 |
getColumnName(int col) { return i18n.getLabel("OrchestraTableModel.orchestras"); } |
87 |
|
88 |
/** |
89 |
* Gets the value for the cell at <code>columnIndex</code> and |
90 |
* <code>rowIndex</code>. |
91 |
* @param row The row whose value is to be queried. |
92 |
* @param col The column whose value is to be queried. |
93 |
* @return The value for the cell at <code>columnIndex</code> and |
94 |
* <code>rowIndex</code>. |
95 |
*/ |
96 |
public Object |
97 |
getValueAt(int row, int col) { |
98 |
return orchestraListModel.getOrchestra(row); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Sets the value in the cell at <code>col</code> |
103 |
* and <code>row</code> to <code>value</code>. |
104 |
*/ |
105 |
public void |
106 |
setValueAt(Object value, int row, int col) { |
107 |
orchestraListModel.getOrchestra(row).setName(value.toString()); |
108 |
fireTableCellUpdated(row, col); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Returns <code>true</code> if the cell at |
113 |
* <code>row</code> and <code>col</code> is editable. |
114 |
*/ |
115 |
public boolean |
116 |
isCellEditable(int row, int col) { return false; } |
117 |
|
118 |
/** |
119 |
* Gets the <code>OrchestraListModel</code>, represented by this table model. |
120 |
*/ |
121 |
public OrchestraListModel |
122 |
getOrchestraListModel() { return orchestraListModel; } |
123 |
|
124 |
private final Handler eventHandler = new Handler(); |
125 |
|
126 |
private Handler |
127 |
getHandler() { return eventHandler; } |
128 |
|
129 |
private class Handler extends OrchestraAdapter implements ListListener<OrchestraModel> { |
130 |
/** Invoked when an orchestra is added to the orchestra list. */ |
131 |
public void |
132 |
entryAdded(ListEvent<OrchestraModel> e) { |
133 |
e.getEntry().addOrchestraListener(getHandler()); |
134 |
fireTableDataChanged(); |
135 |
} |
136 |
|
137 |
/** Invoked when an orchestra is removed from the orchestra list. */ |
138 |
public void |
139 |
entryRemoved(ListEvent<OrchestraModel> e) { |
140 |
e.getEntry().removeOrchestraListener(getHandler()); |
141 |
fireTableDataChanged(); |
142 |
} |
143 |
|
144 |
/** Invoked when the name of orchestra is changed. */ |
145 |
public void |
146 |
nameChanged(OrchestraEvent e) { |
147 |
OrchestraModel m = (OrchestraModel)e.getSource(); |
148 |
int idx = orchestraListModel.getOrchestraIndex(m); |
149 |
fireTableRowsUpdated(idx, idx); |
150 |
} |
151 |
|
152 |
/** Invoked when the description of orchestra is changed. */ |
153 |
public void |
154 |
descriptionChanged(OrchestraEvent e) { } |
155 |
|
156 |
|
157 |
} |
158 |
} |