public class StudentEntry2 implements TableEntry
1:
2: package adapter;
3:
4: import java.util.Comparator;
5:
6: /**
7: * Adapter design pattern
8: */
9: public class StudentEntry2 implements TableEntry {
10:
11: // position of each column
12: public static final int ID_COLUMN = 0;
13: public static final int FIRST_NAME_COLUMN = 1;
14: public static final int LAST_NAME_COLUMN = 2;
15: public static final int STREET_ADDRESS_COLUMN = 3;
16: public static final int STATE_COLUMN = 4;
17: public static final int CITY_COLUMN = 5;
18: public static final int COUNTRY_COLUMN = 6;
19: public static final int POSTAL_CODE_COLUMN = 7;
20: public static final int TELEPHONE_COLUMN = 8;
21: public static final int GPA_COLUMN = 9;
22: public static final int TOTAL_CREDITS_COLUMN = 10;
23:
24: public static final String[] columnNames = {
25: "ID",
26: "First Name",
27: "Last Name",
28: "Street Address",
29: "State",
30: "City",
31: "Country",
32: "Postal Code",
33: "Telephone",
34: "GPA",
35: "Total Credits",
36: };
37:
38: public static final String[] columnTips = {
39: "ID",
40: "First Name",
41: "Last Name",
42: "Street Address",
43: "State",
44: "City",
45: "Country",
46: "Postal Code",
47: "Telephone",
48: "GPA",
49: "Total Credits",
50: };
51:
52: public static final Comparator[] comparators = {
53: new StudentEntryComparator(ID_COLUMN),
54: new StudentEntryComparator(FIRST_NAME_COLUMN),
55: new StudentEntryComparator(LAST_NAME_COLUMN),
56: new StudentEntryComparator(STREET_ADDRESS_COLUMN),
57: new StudentEntryComparator(STATE_COLUMN),
58: new StudentEntryComparator(CITY_COLUMN),
59: new StudentEntryComparator(COUNTRY_COLUMN),
60: new StudentEntryComparator(POSTAL_CODE_COLUMN),
61: new StudentEntryComparator(TELEPHONE_COLUMN),
62: new StudentEntryComparator(GPA_COLUMN),
63: new StudentEntryComparator(TOTAL_CREDITS_COLUMN),
64: };
65:
66: public StudentEntry2(String ID,
67: String firstName,
68: String lastName,
69: String streetAddress,
70: String state,
71: String city,
72: String country,
73: String postalCode,
74: String telephone,
75: float GPA,
76: int totalCredits) {
77: student = new Student(ID, firstName, lastName,
78: streetAddress, state, city, country, postalCode,
79: telephone, GPA, totalCredits);
80: }
81:
82: public StudentEntry2(Student student) {
83: this.student= student;
84: }
85:
86: public int getColumnCount() {
87: return columnNames.length;
88: }
89:
90: public String getColumnName(int col) {
91: if (col >= 0 &&
92: col < columnNames.length) {
93: return columnNames[col];
94: }
95: return null;
96: }
97:
98: public Object getColumnValue(int col) {
99: if (student != null &&
100: col >= 0 &&
101: col < columnNames.length) {
102: switch (col) {
103: case ID_COLUMN: return student.getID();
104: case FIRST_NAME_COLUMN: return student.getFirstName();
105: case LAST_NAME_COLUMN: return student.getLastName();
106: case STREET_ADDRESS_COLUMN: return student.getStreetAddress();
107: case STATE_COLUMN: return student.getState();
108: case CITY_COLUMN: return student.getCity();
109: case COUNTRY_COLUMN: return student.getCountry();
110: case POSTAL_CODE_COLUMN: return student.getPostalCode();
111: case TELEPHONE_COLUMN: return student.getTelephone();
112: case GPA_COLUMN: return new Float(student.getGPA());
113: case TOTAL_CREDITS_COLUMN: return new Integer(student.getTotalCredits());
114: }
115: }
116: return null;
117: }
118:
119: public String getColumnTip(int col) {
120: if (col >= 0 &&
121: col < columnTips.length) {
122: return columnTips[col];
123: }
124: return null;
125: }
126:
127: public Class getColumnClass(int col) {
128: if (col == GPA_COLUMN) {
129: return Float.class;
130: } else if (col == TOTAL_CREDITS_COLUMN) {
131: return Integer.class;
132: } else {
133: return String.class;
134: }
135: }
136:
137: public Comparator getColumnComparator(int col) {
138: if (col >= 0 &&
139: col < comparators.length) {
140: return comparators[col];
141: }
142: return null;
143: }
144:
145: public int getColumnWidth(int col) {
146: return -1;
147: }
148:
149: protected Student student;
150:
151: static public class StudentEntryComparator implements Comparator {
152:
153: public StudentEntryComparator(int col) {
154: this.col = col;
155: }
156:
157: public int compare(Object o1, Object o2) {
158: if (o1 != null &&
159: o2 != null &&
160: o1 instanceof StudentEntry2 &&
161: o2 instanceof StudentEntry2) {
162: StudentEntry2 e1 = (StudentEntry2) o1;
163: StudentEntry2 e2 = (StudentEntry2) o2;
164: if (col == GPA_COLUMN) {
165: return (int) (e1.student.getGPA() * 1000 - e2.student.getGPA() * 1000);
166: } else if (col == TOTAL_CREDITS_COLUMN) {
167: return (e1.student.getTotalCredits() - e2.student.getTotalCredits());
168: } else {
169: return ((String) e1.getColumnValue(col)).compareTo(e2.getColumnValue(col));
170: }
171: }
172: return 0;
173: }
174:
175: protected int col;
176: }
177:
178: }