1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.kernel.modules.tags; 22 23 import java.util.ArrayList; 24 import java.util.HashMap; 25 import java.util.List; 26 import java.util.Map; 27 28 import javax.swing.event.TableModelEvent; 29 import javax.swing.event.TableModelListener; 30 import javax.swing.table.AbstractTableModel; 31 32 import net.sourceforge.atunes.model.IFileManager; 33 import net.sourceforge.atunes.model.ILocalAudioObject; 34 import net.sourceforge.atunes.utils.I18nUtils; 35 36 /** 37 * Table model for title edition when importing CD 38 * 39 * @author alex 40 * 41 */ 42 public final class EditTitlesTableModel extends AbstractTableModel { 43 44 private static final long serialVersionUID = -4440078678648669115L; 45 46 /** The files. */ 47 private final List<ILocalAudioObject> files; 48 49 /** The new values. */ 50 private final Map<ILocalAudioObject, String> newValues; 51 52 /** The listeners. */ 53 private final List<TableModelListener> listeners; 54 55 private final IFileManager fileManager; 56 57 /** 58 * Instantiates a new edits the titles table model. 59 * 60 * @param files 61 * @param fileManager 62 */ EditTitlesTableModel(final List<ILocalAudioObject> files, final IFileManager fileManager)63 public EditTitlesTableModel(final List<ILocalAudioObject> files, 64 final IFileManager fileManager) { 65 this.files = files; 66 this.newValues = new HashMap<ILocalAudioObject, String>(); 67 this.listeners = new ArrayList<TableModelListener>(); 68 this.fileManager = fileManager; 69 } 70 71 /** 72 * Adds the listener. 73 * 74 * @param listener 75 * the listener 76 */ addListener(final TableModelListener listener)77 public void addListener(final TableModelListener listener) { 78 this.listeners.add(listener); 79 } 80 81 /* 82 * (non-Javadoc) 83 * 84 * @see javax.swing.table.TableModel#getColumnCount() 85 */ 86 @Override getColumnCount()87 public int getColumnCount() { 88 return 2; 89 } 90 91 @Override getColumnName(final int column)92 public String getColumnName(final int column) { 93 if (column == 0) { 94 return I18nUtils.getString("FILE"); 95 } 96 return I18nUtils.getString("TITLE"); 97 } 98 99 /** 100 * Gets the new values. 101 * 102 * @return the new values 103 */ getNewValues()104 public Map<ILocalAudioObject, String> getNewValues() { 105 return this.newValues; 106 } 107 108 @Override getRowCount()109 public int getRowCount() { 110 return this.files.size(); 111 } 112 113 @Override getValueAt(final int rowIndex, final int columnIndex)114 public String getValueAt(final int rowIndex, final int columnIndex) { 115 ILocalAudioObject file = this.files.get(rowIndex); 116 if (columnIndex == 0) { 117 return this.fileManager.getFileName(file); 118 } 119 if (this.newValues.containsKey(file)) { 120 return this.newValues.get(file); 121 } 122 return file.getTitle(); 123 } 124 125 @Override isCellEditable(final int rowIndex, final int columnIndex)126 public boolean isCellEditable(final int rowIndex, final int columnIndex) { 127 return columnIndex == 1; 128 } 129 130 /** 131 * Sets the titles. 132 * 133 * @param titles 134 * the new titles 135 */ setTitles(final List<String> titles)136 public void setTitles(final List<String> titles) { 137 for (int i = 0; i < this.files.size(); i++) { 138 String title = titles.size() > i ? titles.get(i) : ""; 139 this.newValues.put(this.files.get(i), title); 140 } 141 142 TableModelEvent event; 143 event = new TableModelEvent(this, TableModelEvent.ALL_COLUMNS, 144 TableModelEvent.UPDATE); 145 146 for (int i = 0; i < this.listeners.size(); i++) { 147 this.listeners.get(i).tableChanged(event); 148 } 149 } 150 151 /** 152 * Sets the value at. 153 * 154 * @param aValue 155 * the a value 156 * @param rowIndex 157 * the row index 158 * @param columnIndex 159 * the column index 160 */ 161 @Override setValueAt(final Object aValue, final int rowIndex, final int columnIndex)162 public void setValueAt(final Object aValue, final int rowIndex, 163 final int columnIndex) { 164 this.newValues.put(this.files.get(rowIndex), (String) aValue); 165 fireTableCellUpdated(rowIndex, columnIndex); 166 } 167 } 168