| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.comic; |
| 17 | |
| 18 | import java.io.File; |
| 19 | |
| 20 | import javax.swing.table.AbstractTableModel; |
| 21 | |
| 22 | import net.sf.jomic.tools.LocaleTools; |
| 23 | import net.sf.jomic.tools.StringTools; |
| 24 | |
| 25 | /** |
| 26 | * Table model to hold details about all comic images. The model has the follwing rows: |
| 27 | * <ol> |
| 28 | * <li> image file name |
| 29 | * <li> relative path in comic archive |
| 30 | * <li> width (if available) |
| 31 | * <li> height (if available) |
| 32 | * <li> error (if available) |
| 33 | * </ol> |
| 34 | * |
| 35 | * |
| 36 | * @author Thomas Aglassinger |
| 37 | */ |
| 38 | public class ComicInfoTableModel extends AbstractTableModel |
| 39 | { |
| 40 | /** |
| 41 | * Column that contains the error information. |
| 42 | */ |
| 43 | public static final int COLUMN_ERROR = 4; |
| 44 | |
| 45 | private static final Class[] CLASSES = |
| 46 | new Class[]{String.class, String.class, Integer.class, Integer.class, Throwable.class}; |
| 47 | private static final int COLUMN_HEIGHT = 3; |
| 48 | private static final int COLUMN_IMAGE = 0; |
| 49 | private static final int COLUMN_PATH = 1; |
| 50 | private static final int COLUMN_WIDTH = 2; |
| 51 | |
| 52 | private ComicModel comicModel; |
| 53 | private StringTools stringTools; |
| 54 | |
| 55 | /** |
| 56 | * Create a new empty ComicInfoModel. |
| 57 | */ |
| 58 | public ComicInfoTableModel() { |
| 59 | this(null); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Create a new ComicInfoModel from an existing ComicModel. |
| 64 | * |
| 65 | * @param newComicModel Description of the parameter |
| 66 | */ |
| 67 | public ComicInfoTableModel(ComicModel newComicModel) { |
| 68 | stringTools = StringTools.instance(); |
| 69 | comicModel = newComicModel; |
| 70 | } |
| 71 | |
| 72 | public Class getColumnClass(int column) { |
| 73 | assertColumnIsValid(column); |
| 74 | return CLASSES[column]; |
| 75 | } |
| 76 | |
| 77 | public int getColumnCount() { |
| 78 | return CLASSES.length; |
| 79 | } |
| 80 | |
| 81 | public String getColumnName(int column) { |
| 82 | assertColumnIsValid(column); |
| 83 | |
| 84 | String result = null; |
| 85 | String columnTitleKey = null; |
| 86 | |
| 87 | if (column == COLUMN_IMAGE) { |
| 88 | columnTitleKey = "image"; |
| 89 | } else if (column == COLUMN_PATH) { |
| 90 | columnTitleKey = "path"; |
| 91 | } else if (column == COLUMN_WIDTH) { |
| 92 | columnTitleKey = "width"; |
| 93 | } else if (column == COLUMN_HEIGHT) { |
| 94 | columnTitleKey = "height"; |
| 95 | } else if (column == COLUMN_ERROR) { |
| 96 | columnTitleKey = "error"; |
| 97 | } else { |
| 98 | assert false : "column=" + column; |
| 99 | } |
| 100 | if (columnTitleKey != null) { |
| 101 | result = LocaleTools.instance().getMessage("tables.comicInfo." + columnTitleKey); |
| 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | public int getRowCount() { |
| 107 | int result; |
| 108 | |
| 109 | if (comicModel == null) { |
| 110 | result = 0; |
| 111 | } else { |
| 112 | result = comicModel.getImageCount(); |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | public Object getValueAt(int row, int column) { |
| 118 | assert comicModel != null; |
| 119 | assertRowIsValid(row); |
| 120 | assertColumnIsValid(column); |
| 121 | |
| 122 | Object result = null; |
| 123 | ComicImage comicImage = comicModel.getComicImage(row); |
| 124 | |
| 125 | if (column == COLUMN_IMAGE) { |
| 126 | result = comicImage.getFile().getName(); |
| 127 | } else if (column == COLUMN_PATH) { |
| 128 | String cachDirPath = comicModel.getCacheDir().getAbsolutePath(); |
| 129 | String imagePath = comicImage.getFile().getParent(); |
| 130 | String relativePath; |
| 131 | |
| 132 | if (imagePath.startsWith(cachDirPath)) { |
| 133 | relativePath = stringTools.trimPrefix(imagePath, cachDirPath); |
| 134 | relativePath = stringTools.trimPrefix(relativePath, File.separator); |
| 135 | } else { |
| 136 | relativePath = imagePath; |
| 137 | } |
| 138 | result = relativePath; |
| 139 | } else if (column == COLUMN_WIDTH) { |
| 140 | if (!comicImage.hasError()) { |
| 141 | result = new Integer(comicImage.getWidth()); |
| 142 | } |
| 143 | } else if (column == COLUMN_HEIGHT) { |
| 144 | if (!comicImage.hasError()) { |
| 145 | result = new Integer(comicImage.getHeight()); |
| 146 | } |
| 147 | } else if (column == COLUMN_ERROR) { |
| 148 | if (comicImage.hasError()) { |
| 149 | result = comicImage.getError(); |
| 150 | } |
| 151 | } else { |
| 152 | assert false : "column=" + column; |
| 153 | } |
| 154 | return result; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Return <code>false</code> to prevent all cells to be editable. |
| 159 | */ |
| 160 | public boolean isCellEditable(int row, int column) { |
| 161 | assertColumnIsValid(column); |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | private void assertColumnIsValid(int column) { |
| 166 | assert column < getColumnCount() : "column " + column + " must be less than " + getColumnCount(); |
| 167 | } |
| 168 | |
| 169 | private void assertRowIsValid(int row) { |
| 170 | assert row < getRowCount() : "row " + row + " must be less than " + getRowCount(); |
| 171 | } |
| 172 | } |