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 | import java.util.HashMap; |
20 | import java.util.Iterator; |
21 | import java.util.Map; |
22 | |
23 | import javax.swing.Icon; |
24 | import javax.swing.event.ChangeEvent; |
25 | import javax.swing.event.ChangeListener; |
26 | import javax.swing.table.AbstractTableModel; |
27 | |
28 | import net.sf.jomic.tools.LocaleTools; |
29 | |
30 | /** |
31 | * A report holding the current state of a conversion of multiple comics. To simplify the |
32 | * presentation it also acts as TableModel. |
33 | * |
34 | * @author Thomas Aglassinger |
35 | */ |
36 | public class ConversionReport extends AbstractTableModel implements ChangeListener |
37 | { |
38 | public static final int ERROR_COLUMN = 5; |
39 | public static final int SOURCE_FILE_COLUMN = 1; |
40 | public static final int SOURCE_FILE_SIZE_COLUMN = 3; |
41 | public static final int STATE_COLUMN = 0; |
42 | public static final int TARGET_FILE_COLUMN = 2; |
43 | public static final int TARGET_FILE_SIZE_COLUMN = 4; |
44 | private static final int COLUMN_COUNT = ERROR_COLUMN + 1; |
45 | |
46 | private Map comicToIndexMap; |
47 | |
48 | /** |
49 | * Map to map comicToConvert to respective item in report. |
50 | */ |
51 | private Map comicToItemMap; |
52 | private Map itemToIndexMap; |
53 | private int lastItemIndex; |
54 | private LocaleTools localeTools; |
55 | |
56 | public ConversionReport() { |
57 | super(); |
58 | comicToItemMap = new HashMap(); |
59 | comicToIndexMap = new HashMap(); |
60 | itemToIndexMap = new HashMap(); |
61 | localeTools = LocaleTools.instance(); |
62 | } |
63 | |
64 | public ConversionReportItem get(ComicToConvert comicToConvert) { |
65 | assert comicToConvert != null; |
66 | ConversionReportItem result; |
67 | |
68 | synchronized (comicToItemMap) { |
69 | result = (ConversionReportItem) comicToItemMap.get(comicToConvert); |
70 | } |
71 | return result; |
72 | } |
73 | |
74 | public ConversionReportItem get(int index) { |
75 | assert index >= 0; |
76 | assert index <= lastItemIndex; |
77 | ConversionReportItem result; |
78 | |
79 | synchronized (comicToItemMap) { |
80 | result = (ConversionReportItem) comicToIndexMap.get(new Integer(index)); |
81 | } |
82 | assert result != null : "comic for index=" + index + " must be in comicToIndexMap"; |
83 | return result; |
84 | } |
85 | |
86 | public Class getColumnClass(int columnIndex) { |
87 | Class result; |
88 | |
89 | if (columnIndex == ERROR_COLUMN) { |
90 | result = Throwable.class; |
91 | } else if (columnIndex == SOURCE_FILE_COLUMN) { |
92 | result = File.class; |
93 | } else if (columnIndex == SOURCE_FILE_SIZE_COLUMN) { |
94 | result = Long.class; |
95 | } else if (columnIndex == STATE_COLUMN) { |
96 | result = Icon.class; |
97 | } else if (columnIndex == TARGET_FILE_COLUMN) { |
98 | result = File.class; |
99 | } else if (columnIndex == TARGET_FILE_SIZE_COLUMN) { |
100 | result = Long.class; |
101 | } else { |
102 | result = String.class; |
103 | } |
104 | return result; |
105 | } |
106 | |
107 | public int getColumnCount() { |
108 | return COLUMN_COUNT; |
109 | } |
110 | |
111 | public String getColumnName(int columnIndex) { |
112 | String result; |
113 | |
114 | if (columnIndex == ERROR_COLUMN) { |
115 | // TODO: think about error column; wouldn't a seperate error panel be better? |
116 | result = "Error"; |
117 | } else { |
118 | String key = null; |
119 | |
120 | if (columnIndex == SOURCE_FILE_COLUMN) { |
121 | key = "sourceComic"; |
122 | } else if (columnIndex == SOURCE_FILE_SIZE_COLUMN) { |
123 | key = "sourceLength"; |
124 | } else if (columnIndex == STATE_COLUMN) { |
125 | key = "status"; |
126 | } else if (columnIndex == TARGET_FILE_COLUMN) { |
127 | key = "targetComic"; |
128 | } else if (columnIndex == TARGET_FILE_SIZE_COLUMN) { |
129 | key = "targetLength"; |
130 | } |
131 | |
132 | if (key != null) { |
133 | result = localeTools.getMessage("dialogs.conversionReport.column." + key); |
134 | } else { |
135 | assert false : "columnIndex=" + columnIndex; |
136 | result = super.getColumnName(columnIndex); |
137 | } |
138 | } |
139 | |
140 | return result; |
141 | } |
142 | |
143 | public int getRowCount() { |
144 | return size(); |
145 | } |
146 | |
147 | public Object getValueAt(int rowIndex, int columnIndex) { |
148 | assert rowIndex >= 0; |
149 | assert rowIndex < getRowCount() : "rowIndex=" + rowIndex; |
150 | assert columnIndex >= 0; |
151 | assert columnIndex < getColumnCount() : "columnIndex=" + columnIndex; |
152 | Object result; |
153 | ConversionReportItem item = get(rowIndex); |
154 | |
155 | if (columnIndex == ERROR_COLUMN) { |
156 | if (item.getState() == ConversionReportItem.STATE_ERROR_DURING_CONVERSION) { |
157 | result = item.getError(); |
158 | } else { |
159 | result = null; |
160 | } |
161 | } else if (columnIndex == SOURCE_FILE_COLUMN) { |
162 | result = item.getSourceFile(); |
163 | } else if (columnIndex == SOURCE_FILE_SIZE_COLUMN) { |
164 | result = new Long(item.getSourceFileSize()); |
165 | } else if (columnIndex == STATE_COLUMN) { |
166 | result = item.getStateIcon(); |
167 | } else if (columnIndex == TARGET_FILE_COLUMN) { |
168 | result = item.getTargetFile(); |
169 | } else if (columnIndex == TARGET_FILE_SIZE_COLUMN) { |
170 | result = new Long(item.getTargetFileSize()); |
171 | } else { |
172 | assert false : "columnIndex=" + columnIndex; |
173 | result = null; |
174 | } |
175 | return result; |
176 | } |
177 | |
178 | public void clear() { |
179 | synchronized (comicToItemMap) { |
180 | // Remove change listeners. |
181 | Iterator itemRider = itemToIndexMap.keySet().iterator(); |
182 | |
183 | while (itemRider.hasNext()) { |
184 | ConversionReportItem item = (ConversionReportItem) itemRider.next(); |
185 | |
186 | item.removeChangeListener(this); |
187 | } |
188 | |
189 | // Forget everything. |
190 | comicToItemMap.clear(); |
191 | comicToIndexMap.clear(); |
192 | itemToIndexMap.clear(); |
193 | lastItemIndex = 0; |
194 | } |
195 | } |
196 | |
197 | public void put(ComicToConvert comicToConvert) { |
198 | assert comicToConvert != null; |
199 | ConversionReportItem reportItem = new ConversionReportItem(comicToConvert); |
200 | |
201 | synchronized (comicToItemMap) { |
202 | ConversionReportItem oldItem = get(comicToConvert); |
203 | Integer indexObject = new Integer(lastItemIndex); |
204 | |
205 | if (oldItem == null) { |
206 | comicToIndexMap.put(indexObject, reportItem); |
207 | lastItemIndex += 1; |
208 | } else { |
209 | oldItem.removeChangeListener(this); |
210 | } |
211 | comicToItemMap.put(comicToConvert, reportItem); |
212 | itemToIndexMap.put(reportItem, indexObject); |
213 | reportItem.addChangeListener(this); |
214 | } |
215 | } |
216 | |
217 | /** |
218 | * Number of items in the report. |
219 | */ |
220 | public int size() { |
221 | int result = comicToItemMap.size(); |
222 | |
223 | assert result == lastItemIndex : "comicReportMap.size must be " + lastItemIndex + " but is " + result; |
224 | return result; |
225 | } |
226 | |
227 | /** |
228 | * Called when one of the report items has changed to reflect the state in the table model. |
229 | */ |
230 | public void stateChanged(ChangeEvent event) { |
231 | assert event != null; |
232 | ConversionReportItem item = (ConversionReportItem) event.getSource(); |
233 | |
234 | assert item != null; |
235 | Integer indexObject = (Integer) itemToIndexMap.get(item); |
236 | |
237 | assert indexObject != null; |
238 | int index = indexObject.intValue(); |
239 | |
240 | fireTableRowsUpdated(index, index); |
241 | } |
242 | } |