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.jgoodies.uif.application.Application;
10  import com.melloware.jukes.gui.tool.Resources;
11  import com.melloware.jukes.gui.view.MainFrame;
12  import com.melloware.jukes.util.MessageUtil;
13  import com.melloware.jukes.ws.AmazonItem;
14  
15  /**
16   * The table model for displaying results in the web search dialog.
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 WebSearchTableModel
23      extends AbstractTableModel {
24  
25      private static final Log LOG = LogFactory.getLog(WebSearchTableModel.class);
26      private Object[] data;
27      private final String[] columnNames = { Resources.getString("label.artist"),
28      		Resources.getString("label.disc"), Resources.getString("label.year"),
29      		Resources.getString("label.tracks"), Resources.getString("label.cover") };
30  
31      /**
32       * Constructor that takes a collection.
33       */
34      public WebSearchTableModel() {
35          super();
36          LOG.debug("WebSearchTableModel created.");
37          this.data = null;
38      }
39  
40      /**
41       * Constructor that takes a collection.
42       */
43      public WebSearchTableModel(Object[] aData) {
44          super();
45          LOG.debug("WebSearchTableModel created.");
46          this.data = aData;
47      }
48  
49      /* (non-Javadoc)
50       * @see javax.swing.table.TableModel#getColumnCount()
51       */
52      public int getColumnCount() {
53          return columnNames.length;
54      }
55  
56      /* (non-Javadoc)
57       * @see javax.swing.table.AbstractTableModel#getColumnName(int)
58       */
59      public String getColumnName(int col) {
60          return columnNames[col];
61      }
62  
63      /**
64       * Gets the data.
65       * <p>
66       * @return Returns the data.
67       */
68      public Object[] getData() {
69          return this.data;
70      }
71  
72      /* (non-Javadoc)
73       * @see javax.swing.table.TableModel#getRowCount()
74       */
75      public int getRowCount() {
76          if (data == null) {
77              return 0;
78          } else {
79              return data.length;
80          }
81      }
82  
83      /* (non-Javadoc)
84       * @see javax.swing.table.TableModel#getValueAt(int, int)
85       */
86      public Object getValueAt(int row, int col) {
87          try {
88              if (data == null) {
89                  return "";
90              }
91              if (row >= data.length) {
92                  return "";
93              }
94              AmazonItem item = (AmazonItem)data[row];
95              Object value = null;
96              switch (col) {
97                  case 0: {
98                      value = item.getArtist();
99                      break;
100                 }
101                 case 1: {
102                     value = item.getDisc();
103                     break;
104                 }
105                 case 2: {
106                     value = item.getReleaseYear();
107                     break;
108                 }
109                 case 3: {
110                     if (item.getTracks().isEmpty()) {
111                         value = "No";
112                     } else {
113                         value = "Yes";
114                     }
115                     break;
116                 }
117                 case 4: {
118                     if (StringUtils.isNotBlank(item.getBestImageUrl())) {
119                         value = Integer.toString(item.getBestImageWidth()) + "x"
120                                 + Integer.toString(item.getBestImageHeight());
121                     } else {
122                         value = "";
123                     }
124                     break;
125                 }
126                 default: {
127                     break;
128                 }
129             }
130             return (value == null) ? "" : value;
131         } catch (Exception ex) {
132         	final MainFrame mainFrame = (MainFrame) Application.getDefaultParentFrame();
133        	    final String errorMessage = Resources.getString("messages.ErrorLoadingResults");
134     	    MessageUtil.showError(mainFrame, errorMessage);
135             LOG.error(errorMessage);
136             return "";
137         }
138     }
139 
140     /**
141      * Sets the data.
142      * <p>
143      * @param aData The data to set.
144      */
145     public void setData(Object[] aData) {
146         this.data = aData;
147     }
148 
149     /* (non-Javadoc)
150      * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
151      */
152     public boolean isCellEditable(int row, int col) {
153         return false;
154     }
155 }