1 package com.melloware.jukes.gui.view.dialogs;
2
3 import javax.swing.table.AbstractTableModel;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import com.melloware.jukes.file.tag.MusicTag;
9
10
11
12
13
14
15
16
17
18 public final class MusicTagTableModel
19 extends AbstractTableModel {
20
21 private static final Log LOG = LogFactory.getLog(MusicTagTableModel.class);
22 private Object[] data;
23 private final String[] columnNames = { "Track", "Title", "Comment", "Bitrate", "File" };
24
25
26
27
28 public MusicTagTableModel() {
29 super();
30 LOG.debug("MusicTagTableModel created.");
31 this.data = null;
32 }
33
34
35
36
37 public MusicTagTableModel(Object[] aData) {
38 super();
39 LOG.debug("MusicTagTableModel created.");
40 this.data = aData;
41 }
42
43
44
45
46 public int getColumnCount() {
47 return columnNames.length;
48 }
49
50
51
52
53 public String getColumnName(int col) {
54 return columnNames[col];
55 }
56
57
58
59
60
61
62 public Object[] getData() {
63 return this.data;
64 }
65
66
67
68
69 public int getRowCount() {
70 if (data == null) {
71 return 0;
72 } else {
73 return data.length;
74 }
75 }
76
77
78
79
80 public Object getValueAt(int row, int col) {
81 try {
82 if (data == null) {
83 return "";
84 }
85 if (row >= data.length) {
86 return "";
87 }
88 final MusicTag item = (MusicTag)data[row];
89 Object value = null;
90 switch (col) {
91 case 0: {
92 value = item.getTrack();
93 break;
94 }
95 case 1: {
96 value = item.getTitle();
97 break;
98 }
99 case 2: {
100 value = item.getComment();
101 break;
102 }
103 case 3: {
104 value = " " + item.getBitRateAsString() + " ";
105 break;
106 }
107 case 4: {
108 value = item.getAbsolutePath();
109 break;
110 }
111 default: {
112 break;
113 }
114 }
115 return (value == null) ? "" : value;
116 } catch (Exception ex) {
117 LOG.error("An unexpected error occured loading the results.", ex);
118 return "";
119 }
120 }
121
122
123
124
125
126
127 public void setData(Object[] aData) {
128 this.data = aData;
129 }
130
131
132
133
134 public void setValueAt(Object aValue, int aRowIndex, int aColumnIndex) {
135 if (aRowIndex >= data.length) {
136 return;
137 }
138 final MusicTag tag = (MusicTag)data[aRowIndex];
139 String value = aValue.toString();
140 try {
141 switch (aColumnIndex) {
142 case 0: {
143 tag.setTrack(value);
144 break;
145 }
146 case 1: {
147 tag.setTitle(value);
148 break;
149 }
150 case 2: {
151 tag.setComment(value);
152 break;
153 }
154 default: {
155 break;
156 }
157 }
158 } catch (Exception ex) {
159 LOG.error("An unexpected error occured editing the table: \n\n" + ex.getMessage());
160 }
161 }
162
163
164
165
166 public boolean isCellEditable(int row, int col) {
167 boolean result = false;
168
169 switch (col) {
170 case 0:
171 case 1:
172 case 2: {
173 result = true;
174 break;
175 }
176 default: {
177 result = false;
178 }
179 }
180 return result;
181 }
182 }