-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCounterTableModel.java
92 lines (76 loc) · 2.26 KB
/
WordCounterTableModel.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package at.tw.ose;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.table.AbstractTableModel;
/**
* <h1> WordCounterTableModel </h1>
*
* This class implements methods to modify the table in which the words and
* their occurences are shown.
*
* @author Werner Egermann & Thomas Jungwirth
* @version 1.0
* @since 2018-11-08
*/
public class WordCounterTableModel extends AbstractTableModel implements Serializable {
private static final long serialVersionUID = 12345678987654321L;
private String [] titles = new String [] { "Word", "Occurence"};
//private ArrayList<WordEntry> entries = new ArrayList<WordEntry>();
private LinkedHashMap<String, Long> reverseSortedMap = new LinkedHashMap<>();
@Override
public int getRowCount() {
return reverseSortedMap.size();
}
@Override
public int getColumnCount() {
return titles.length;
}
public LinkedHashMap<String, Long> getReverseSortedMap() {
return reverseSortedMap;
}
public void setReverseSortedMap(LinkedHashMap<String, Long> reverseSortedMap) {
this.reverseSortedMap = reverseSortedMap;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Set<Map.Entry<String, Long>> entry = reverseSortedMap.entrySet();
Map.Entry<String, Long> element = (new ArrayList<Map.Entry<String, Long>>(entry)).get(rowIndex);
if (entry != null)
{
switch(columnIndex)
{
case 0:
return element.getKey();
case 1:
return element.getValue();
default:
return null;
}
}
return null;
}
@Override
public String getColumnName(int column) {
return titles[column];
}
/**
*
* @param WordEntry entry
* This function adds the structure consisting of words and their occurences to the table.
* The structure is already sorted. This is the last step before showing the result of the
* WordCount program.
*/
public void addEntry(LinkedHashMap<String, Long> reverseSortedMap) {
//reverseSortedMap.add(reverseSortedMap);
this.reverseSortedMap = reverseSortedMap;
fireTableDataChanged();
}
@Override
// table is not editable, but clickable and selectable
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}