summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/table/LectureTable.java
blob: ae29304a186efbfe19f4ddc93aa7228f3142e4e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package org.openslx.dozmod.gui.control.table;

import java.awt.Component;

import javax.swing.JTable;

import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.dozmod.thrift.Sorters;
import org.openslx.dozmod.thrift.cache.UserCache;
import org.openslx.dozmod.util.FormatHelper;

@SuppressWarnings("serial")
public class LectureTable extends ListTable<LectureSummary> {

	private static String[] columnNames = { "Name", "Besitzer", "Startzeit", "Endzeit" };
	private static Class<?>[] columnClasses = { String.class, String.class, Long.class, Long.class };


	public LectureTable() {
		super(columnNames, columnClasses);
		this.setDefaultRenderer(Object.class, new LectureTableRenderer());

		//  --------- set comparators for different columns ------------------------------
		// first column doesn't have to be set, because standard comparator already compares strings
		// second column compares the owner
		getRowSorter().setComparator(1, Sorters.userNameById);
		// third and fourth column are both longs (last update and size), use default comparator
		// --------- end of comparators -------------------------------------------------
	}

	@Override
	protected Object getValueAtInternal(int rowIndex, int columnIndex) {
		LectureSummary row = getModelRow(rowIndex);
		if (columnIndex == 0)
			return row.getLectureName();
		if (columnIndex == 1)
			return row.getOwnerId();
		if (columnIndex == 2)
			return row.getStartTime();
		if (columnIndex == 3)
			return row.getEndTime();
		throw new IndexOutOfBoundsException();
	}
}

/**
 * This renderer formats the given value to a readable string for the table cell, depending on the column
 */
@SuppressWarnings("serial")
class LectureTableRenderer extends TableRenderer {
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
			boolean hasFocus, int row, int column) {
		if (value == null)
			value = "-";
		if (column == 1)
			value = FormatHelper.userName(UserCache.find((String) value));
		if (column == 2)
			value = FormatHelper.longDate((Long) value);
		if (column == 3)
			value = FormatHelper.longDate((Long) value);
		if( column > 3 || column < 0)
			throw new IndexOutOfBoundsException();

		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		return this;
	}
}