View Javadoc

1   package ch.braincell.stickbackup.gui.table;
2   
3   import java.awt.Color;
4   import java.io.File;
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import javax.swing.table.AbstractTableModel;
11  
12  import ch.braincell.stickbackup.config.Config;
13  
14  public class FileTableModel extends AbstractTableModel {
15  
16  	/***
17  	 * generated serialVersionUID.
18  	 */
19  	private static final long serialVersionUID = 1L;
20  
21  	private Config config = Config.getInstance();
22  
23  	private String[] columns = new String[2];
24  
25  	private List<FileData[]> rows = new ArrayList<FileData[]>();
26  
27  	private Map<File, FileRow> searchRow = new HashMap<File, FileRow>();
28  
29  	public int getColumnCount() {
30  		return columns.length;
31  	}
32  
33  	@Override
34  	public String getColumnName(int column) {
35  		return columns[column];
36  	}
37  
38  	public int getRowCount() {
39  		return rows.size();
40  	}
41  
42  	public Object getValueAt(int rowIndex, int columnIndex) {
43  		return rows.get(rowIndex)[columnIndex];
44  	}
45  
46  	// TODO: die Farbe muss in's Modell --> FileData
47  	public void addFiles(File source, File backup) {
48  		Color sourcecolor = null == backup ? config.getSourceColor() : config
49  				.getChangedColor();
50  		Color backupcolor = null == source ? config.getDestinationColor()
51  				: config.getChangedColor();
52  		FileData sourcedata = new FileData(source, sourcecolor);
53  		FileData backupData = new FileData(backup, backupcolor);
54  		FileData[] files = { sourcedata, backupData };
55  		rows.add(files);
56  		if (null != source)
57  			searchRow.put(source, new FileRow(rows.size() - 1, 0));
58  		if (null != backup)
59  			searchRow.put(backup, new FileRow(rows.size() - 1, 1));
60  		this.fireTableRowsInserted(rows.size(), rows.size());
61  	}
62  
63  	public void setWorkingColor(File file) {
64  		FileRow row = searchRow.get(file);
65  		if (null != row) {
66  			FileData data = rows.get(row.getRow())[row.getColumn()];
67  			data.setColor(config.getWorkingColor());
68  			fireTableCellUpdated(row.getRow(), row.getColumn());
69  		}
70  		else {
71  			// TODO: Logging is here...
72  			System.err.println("Row not found for file " + file);
73  		}
74  	}
75  
76  	public void setFinishedColor(File file) {
77  		FileRow row = searchRow.get(file);
78  		if (null != row) {
79  			FileData data = rows.get(row.getRow())[row.getColumn()];
80  			data.setColor(config.getFinishedColor());
81  			fireTableCellUpdated(row.getRow(), row.getColumn());
82  		}
83  		else {
84  			// TODO: Logging is here...
85  			System.err.println("Row not found for file " + file);
86  		}
87  	}
88  
89  	public void clear(String sourceColumnName, String backupColumnName) {
90  		rows.clear();
91  		searchRow.clear();
92  		columns[0] = sourceColumnName;
93  		columns[1] = backupColumnName;
94  		this.fireTableStructureChanged();
95  	}
96  
97  	public class FileRow {
98  		private final int row;
99  
100 		private final int column;
101 
102 		public FileRow(int row, int column) {
103 			this.row = row;
104 			this.column = column;
105 		}
106 
107 		public int getColumn() {
108 			return column;
109 		}
110 
111 		public int getRow() {
112 			return row;
113 		}
114 	}
115 
116 	class FileData {
117 		private final File file;
118 
119 		private Color color;
120 
121 		public FileData(File file, Color color) {
122 			this.file = file;
123 			this.color = color;
124 		}
125 
126 		public File getFile() {
127 			return file;
128 		}
129 
130 		public Color getColor() {
131 			return color;
132 		}
133 
134 		public void setColor(Color color) {
135 			this.color = color;
136 		}
137 	}
138 }