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.jgoodies.uif.util.ResourceUtils;
11  import com.melloware.jukes.db.orm.Track;
12  import com.melloware.jukes.gui.tool.Resources;
13  import com.melloware.jukes.gui.view.MainFrame;
14  import com.melloware.jukes.util.MessageUtil;
15  
16  /**
17   * The table model for displaying results in the search dialog.
18   * <p>
19   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
20   * @author Emil A. Lefkof III <info@melloware.com>
21   * @version 4.0
22   */
23  public final class SearchTableModel extends AbstractTableModel {
24  
25     private static final Log LOG = LogFactory.getLog(SearchTableModel.class);
26     private Object[] data;
27     private final String[] columnNames = { Resources.getString("label.artist"), Resources.getString("label.disc"),
28              Resources.getString("label.track"), Resources.getString("label.year"), Resources.getString("label.genre"),
29              Resources.getString("label.bitrate"), Resources.getString("label.notes") };
30  
31     /**
32      * Constructor that takes a collection.
33      */
34     public SearchTableModel() {
35        super();
36        LOG.debug("SearchTableModel created.");
37        this.data = null;
38     }
39  
40     /**
41      * Constructor that takes a collection.
42      */
43     public SearchTableModel(Object[] aData) {
44        super();
45        LOG.debug("SearchTableModel created.");
46        this.data = aData;
47     }
48  
49     /*
50      * (non-Javadoc)
51      * @see javax.swing.table.TableModel#getColumnCount()
52      */
53     public int getColumnCount() {
54        return columnNames.length;
55     }
56  
57     /*
58      * (non-Javadoc)
59      * @see javax.swing.table.AbstractTableModel#getColumnName(int)
60      */
61     @Override
62     public String getColumnName(int col) {
63        return columnNames[col];
64     }
65  
66     /**
67      * Gets the data.
68      * <p>
69      * @return Returns the data.
70      */
71     public Object[] getData() {
72        return this.data;
73     }
74  
75     /*
76      * (non-Javadoc)
77      * @see javax.swing.table.TableModel#getRowCount()
78      */
79     public int getRowCount() {
80        if (data == null) {
81           return 0;
82        } else {
83           return data.length;
84        }
85     }
86  
87     /*
88      * (non-Javadoc)
89      * @see javax.swing.table.TableModel#getValueAt(int, int)
90      */
91     public Object getValueAt(int row, int col) {
92        try {
93           if (data == null) {
94              return "";
95           }
96           if (row >= data.length) {
97              return "";
98           }
99           Track item = (Track) data[row];
100          Object value = null;
101          switch (col) {
102          case 0: {
103             value = item.getDisc().getArtist().getName();
104             break;
105          }
106          case 1: {
107             value = item.getDisc().getName();
108             break;
109          }
110          case 2: {
111             value = item.getName();
112             break;
113          }
114          case 3: {
115             value = item.getDisc().getYear();
116             break;
117          }
118          case 4: {
119             value = item.getDisc().getGenre();
120             break;
121          }
122          case 5: {
123             value = item.getBitrate();
124             break;
125          }
126          case 6: {
127             final StringBuffer sb = new StringBuffer();
128             sb.append(StringUtils.defaultIfEmpty(item.getComment(), "")).append(' ');
129             sb.append(StringUtils.defaultIfEmpty(item.getDisc().getNotes(), "")).append(' ');
130             sb.append(StringUtils.defaultIfEmpty(item.getDisc().getArtist().getNotes(), ""));
131             value = sb.toString();
132             break;
133          }
134          default: {
135             break;
136          }
137          }
138          return (value == null) ? "" : value;
139       } catch (Exception ex) {
140          final MainFrame mainFrame = (MainFrame) Application.getDefaultParentFrame();
141          final String errorMessage = ResourceUtils.getString("messages.ErrorLoadingResults");
142          MessageUtil.showError(mainFrame, errorMessage); // AZ
143          LOG.error(errorMessage);
144          return "";
145       }
146    }
147 
148    /**
149     * Sets the data.
150     * <p>
151     * @param aData The data to set.
152     */
153    public void setData(Object[] aData) {
154       this.data = aData;
155    }
156 
157    /*
158     * (non-Javadoc)
159     * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
160     */
161    @Override
162    public boolean isCellEditable(int row, int col) {
163       return false;
164    }
165 }