View Javadoc

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   * The table model for displaying music tags from a directory.
13   * <p>
14   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
15   * @author Emil A. Lefkof III <info@melloware.com>
16   * @version 4.0
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       * Constructor that takes a collection.
27       */
28      public MusicTagTableModel() {
29          super();
30          LOG.debug("MusicTagTableModel created.");
31          this.data = null;
32      }
33  
34      /**
35       * Constructor that takes a collection.
36       */
37      public MusicTagTableModel(Object[] aData) {
38          super();
39          LOG.debug("MusicTagTableModel created.");
40          this.data = aData;
41      }
42  
43      /* (non-Javadoc)
44       * @see javax.swing.table.TableModel#getColumnCount()
45       */
46      public int getColumnCount() {
47          return columnNames.length;
48      }
49  
50      /* (non-Javadoc)
51       * @see javax.swing.table.AbstractTableModel#getColumnName(int)
52       */
53      public String getColumnName(int col) {
54          return columnNames[col];
55      }
56  
57      /**
58       * Gets the data.
59       * <p>
60       * @return Returns the data.
61       */
62      public Object[] getData() {
63          return this.data;
64      }
65  
66      /* (non-Javadoc)
67       * @see javax.swing.table.TableModel#getRowCount()
68       */
69      public int getRowCount() {
70          if (data == null) {
71              return 0;
72          } else {
73              return data.length;
74          }
75      }
76  
77      /* (non-Javadoc)
78       * @see javax.swing.table.TableModel#getValueAt(int, int)
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      * Sets the data.
124      * <p>
125      * @param aData The data to set.
126      */
127     public void setData(Object[] aData) {
128         this.data = aData;
129     }
130 
131     /* (non-Javadoc)
132      * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
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     /* (non-Javadoc)
164      * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
165      */
166     public boolean isCellEditable(int row, int col) {
167     	boolean result = false;
168         // only track, title,and comment are editable
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 }