apple2-image-encoder/src/main/java/a2geek/apple2/image/encoder/ui/EncoderTableModel.java

103 lines
2.7 KiB
Java
Raw Normal View History

2016-09-13 03:21:41 +00:00
package a2geek.apple2.image.encoder.ui;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import a2geek.apple2.image.encoder.A2Image;
import a2geek.apple2.image.encoder.encode.A2Encoder;
2017-07-09 21:07:08 +00:00
import a2geek.apple2.image.encoder.encode.A2EncoderFactory;
2016-09-13 03:21:41 +00:00
import a2geek.apple2.image.encoder.util.ProgressListener;
/**
* Provide a TableModel for the encoding table. This TableModel also
* handles actually performing the compression.
*
* @author a2geek@users.noreply.github.com
*/
@SuppressWarnings("serial")
public class EncoderTableModel extends AbstractTableModel {
2017-07-09 21:07:08 +00:00
private final List<A2Encoder> results = new ArrayList<>();
2016-09-13 03:21:41 +00:00
private byte[] data = null;
private String[] headers = new String[] { "Type", "Original", "Compressed", "%" };
/**
* Construct the TableModel and setup all the A2Encoders.
*/
public EncoderTableModel(A2Image image, int maxSize, ProgressListener listener) {
this.data = image.getBytes();
2017-07-09 21:07:08 +00:00
2016-09-13 03:21:41 +00:00
int count = 0;
2017-07-09 21:07:08 +00:00
int size = A2EncoderFactory.getEncoders().size();
for (A2Encoder encoder : A2EncoderFactory.getEncoders()) {
2016-09-13 03:21:41 +00:00
try {
2017-07-09 21:07:08 +00:00
if (listener != null && listener.isCancelled()) return;
2016-09-13 03:21:41 +00:00
count++;
2017-07-09 21:07:08 +00:00
if (listener != null) listener.update(count, size, encoder.getTitle());
encoder.encode(image, maxSize);
results.add(encoder);
2016-09-13 03:21:41 +00:00
} catch (Throwable t) {
2017-07-09 21:07:08 +00:00
// Ignoring these as some are expected to throw errors if boundaries are missed
2016-09-13 03:21:41 +00:00
}
}
}
/**
* Answer with the name of the column.
*/
public String getColumnName(int column) {
return headers[column];
}
/**
* Answer with the number of rows.
*/
public int getRowCount() {
2017-07-09 21:07:08 +00:00
return results.size();
2016-09-13 03:21:41 +00:00
}
/**
* Answer with the number of columns.
*/
public int getColumnCount() {
return headers.length;
}
/**
* Answer with a specific cell value in the table.
*/
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
2017-07-09 21:07:08 +00:00
return results.get(rowIndex).getTitle();
2016-09-13 03:21:41 +00:00
case 1:
return data.length;
case 2:
2017-07-09 21:07:08 +00:00
return results.get(rowIndex).getSize();
2016-09-13 03:21:41 +00:00
case 3:
2017-07-09 21:07:08 +00:00
return (results.get(rowIndex).getSize() * 100) / data.length;
2016-09-13 03:21:41 +00:00
default:
return "Unknown";
}
}
/**
* Anwwer with the specific Class to indicate formatting in the cell.
*/
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return Integer.class;
case 3:
return Integer.class;
default:
return Object.class;
}
}
/**
* Answer with the specific A2Encoder.
*/
public A2Encoder getSelectedEncoder(int rowIndex) {
2017-07-09 21:07:08 +00:00
return results.get(rowIndex);
2016-09-13 03:21:41 +00:00
}
}