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