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.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   * The table model for displaying music tags from a directory.
17   * <p>
18   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
19   * @author Emil A. Lefkof III <info@melloware.com>
20   * @version 4.0
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       * Constructor that takes a collection.
35       */
36      public MusicTagTableModel() {
37          super();
38          LOG.debug("MusicTagTableModel created.");
39          this.data = null;
40      }
41  
42      /**
43       * Constructor that takes a collection.
44       */
45      public MusicTagTableModel(Object[] aData) {
46          super();
47          LOG.debug("MusicTagTableModel created.");
48          this.data = aData;
49      }
50  
51      /* (non-Javadoc)
52       * @see javax.swing.table.TableModel#getColumnCount()
53       */
54      public int getColumnCount() {
55          return columnNames.length;
56      }
57  
58      /* (non-Javadoc)
59       * @see javax.swing.table.AbstractTableModel#getColumnName(int)
60       */
61      public String getColumnName(int col) {
62          return columnNames[col];
63      }
64  
65      /**
66       * Gets the data.
67       * <p>
68       * @return Returns the data.
69       */
70      public Object[] getData() {
71          return this.data;
72      }
73  
74      /* (non-Javadoc)
75       * @see javax.swing.table.TableModel#getRowCount()
76       */
77      public int getRowCount() {
78          if (data == null) {
79              return 0;
80          } else {
81              return data.length;
82          }
83      }
84  
85      /* (non-Javadoc)
86       * @see javax.swing.table.TableModel#getValueAt(int, int)
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      * Sets the data.
135      * <p>
136      * @param aData The data to set.
137      */
138     public void setData(Object[] aData) {
139         this.data = aData;
140     }
141 
142     /* (non-Javadoc)
143      * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
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     /* (non-Javadoc)
178      * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
179      */
180     public boolean isCellEditable(int row, int col) {
181     	boolean result = false;
182         // only track, title,and comment are editable
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 }