View Javadoc

1   package com.melloware.jukes.gui.view.dialogs;
2   
3   import javax.swing.table.AbstractTableModel;
4   
5   import org.apache.commons.lang.StringUtils;
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   import com.melloware.jukes.db.orm.Track;
10  
11  /**
12   * The table model for displaying results in the search dialog.
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 SearchTableModel
19      extends AbstractTableModel {
20  
21      private static final Log LOG = LogFactory.getLog(SearchTableModel.class);
22      private Object[] data;
23      private final String[] columnNames = { "Artist", "Disc", "Track", "Year", "Genre", "Bitrate", "Notes" };
24  
25      /**
26       * Constructor that takes a collection.
27       */
28      public SearchTableModel() {
29          super();
30          LOG.debug("SearchTableModel created.");
31          this.data = null;
32      }
33  
34      /**
35       * Constructor that takes a collection.
36       */
37      public SearchTableModel(Object[] aData) {
38          super();
39          LOG.debug("SearchTableModel 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              Track item = (Track)data[row];
89              Object value = null;
90              switch (col) {
91                  case 0: {
92                      value = item.getDisc().getArtist().getName();
93                      break;
94                  }
95                  case 1: {
96                      value = item.getDisc().getName();
97                      break;
98                  }
99                  case 2: {
100                     value = item.getName();
101                     break;
102                 }
103                 case 3: {
104                     value = item.getDisc().getYear();
105                     break;
106                 }
107                 case 4: {
108                     value = item.getDisc().getGenre();
109                     break;
110                 }
111                 case 5: {
112                     value = item.getBitrate();
113                     break;
114                 }
115                 case 6: {
116                 	final StringBuffer sb = new StringBuffer();
117                 	sb.append(StringUtils.defaultIfEmpty(item.getComment(), "")).append(' ');
118                 	sb.append(StringUtils.defaultIfEmpty(item.getDisc().getNotes(), "")).append(' ');
119                 	sb.append(StringUtils.defaultIfEmpty(item.getDisc().getArtist().getNotes(), ""));
120                     value = sb.toString();
121                     break;
122                 }
123                 default: {
124                     break;
125                 }
126             }
127             return (value == null) ? "": value;
128         } catch (Exception ex) {
129             LOG.error("An unexpected error occured loading the results.");
130             return "";
131         }
132     }
133 
134     /**
135      * Sets the data.
136      * <p>
137      * @param aData The data to set.
138      */
139     public void setData(Object[] aData) {
140         this.data = aData;
141     }
142 
143     /* (non-Javadoc)
144      * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
145      */
146     public boolean isCellEditable(int row, int col) {
147         return false;
148     }
149 }