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.ws.AmazonItem;
10  
11  /**
12   * The table model for displaying results in the web 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 WebSearchTableModel
19      extends AbstractTableModel {
20  
21      private static final Log LOG = LogFactory.getLog(WebSearchTableModel.class);
22      private Object[] data;
23      private final String[] columnNames = { "Artist", "Disc", "Year", "Tracks", "Cover" };
24  
25      /**
26       * Constructor that takes a collection.
27       */
28      public WebSearchTableModel() {
29          super();
30          LOG.debug("WebSearchTableModel created.");
31          this.data = null;
32      }
33  
34      /**
35       * Constructor that takes a collection.
36       */
37      public WebSearchTableModel(Object[] aData) {
38          super();
39          LOG.debug("WebSearchTableModel 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              AmazonItem item = (AmazonItem)data[row];
89              Object value = null;
90              switch (col) {
91                  case 0: {
92                      value = item.getArtist();
93                      break;
94                  }
95                  case 1: {
96                      value = item.getDisc();
97                      break;
98                  }
99                  case 2: {
100                     value = item.getReleaseYear();
101                     break;
102                 }
103                 case 3: {
104                     if (item.getTracks().isEmpty()) {
105                         value = "No";
106                     } else {
107                         value = "Yes";
108                     }
109                     break;
110                 }
111                 case 4: {
112                     if (StringUtils.isNotBlank(item.getBestImageUrl())) {
113                         value = Integer.toString(item.getBestImageWidth()) + "x"
114                                 + Integer.toString(item.getBestImageHeight());
115                     } else {
116                         value = "";
117                     }
118                     break;
119                 }
120                 default: {
121                     break;
122                 }
123             }
124             return (value == null) ? "" : value;
125         } catch (Exception ex) {
126             LOG.error("An unexpected error occured loading the results.");
127             return "";
128         }
129     }
130 
131     /**
132      * Sets the data.
133      * <p>
134      * @param aData The data to set.
135      */
136     public void setData(Object[] aData) {
137         this.data = aData;
138     }
139 
140     /* (non-Javadoc)
141      * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
142      */
143     public boolean isCellEditable(int row, int col) {
144         return false;
145     }
146 }