-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwimmableReport.java
139 lines (106 loc) · 4.26 KB
/
SwimmableReport.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Swimmable report class is report that display the report of all the swimmable in the aquarium
* @author Adir Zoari 203002753
* @author Idan Levi 305562431
*/
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class SwimmableReport extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private String typeAnimal=null,idOb;
private int idObject=0,totalEatCounter=0;
private HashSet<Swimmable> animalsSet=new HashSet<Swimmable>(); //hash set for swimmable
private HashMap<Integer, Memento> swimmableMementoList;
private Set set;
private Iterator <Swimmable>itrAnimals;
private JTable table;
private AquaPanel panel;
private JPanel tablePanel=new JPanel();
private int optionTable;
public void setDialog()
{
setSize(650,300);
setLayout(new BorderLayout());
}
public SwimmableReport(AquaPanel panel,HashSet<Swimmable> animalsSet,int optionTable)
{
//settings SwimmableReport
setDialog();
this.panel=panel;
this.animalsSet=new HashSet<Swimmable>(animalsSet);
this.optionTable=optionTable;
//set panel
SettingTable();
add(tablePanel);
}
public SwimmableReport(HashMap<Integer, Memento> swimmableMementoList,int optionTable)
{
setDialog();
this.swimmableMementoList=new HashMap<Integer, Memento>(swimmableMementoList);
//System.out.println(swimmableMementoList.size());
//System.out.println(swimmableMementoList.get(0).getSize());
this.optionTable=optionTable;
SettingTable();
add(tablePanel);
}
public void SettingTable()
{
tablePanel=new JPanel();
String[] columns=null;
if(optionTable==1){
columns=new String[]{"","ID","Animal","Color","Size","Hor.Speed","Ver.speed","Eat counter","Hunger status"}; // columns
}
else if(optionTable==2){
columns=new String[]{"","ID","Animal","Color","Size","x_front,y_front","Hor.Speed","Ver.speed","Eat counter","Hunger status"}; // columns
}
DefaultTableModel tableModel = new DefaultTableModel(columns,0);
table=new JTable (tableModel);
table.setPreferredScrollableViewportSize(new Dimension(600,150));
if(optionTable==1){
itrAnimals= animalsSet.iterator();
while(itrAnimals.hasNext()){
Swimmable sw = itrAnimals.next();
String[] swimm=new String[]{"",String.valueOf(sw.getID()),sw.getAnimalName(),sw.getColor(),String.valueOf(sw.getSize()),String.valueOf(sw.getHorSpeed()),String.valueOf(sw.getVerSpeed()),String.valueOf(sw.getEatCount()),sw.getHungerState()};
totalEatCounter+=sw.getEatCount(); //sum all eat count of the swimmable
tableModel.addRow(swimm);
}
}
if(optionTable==2){
set=swimmableMementoList.entrySet();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
Map.Entry mentry = (Map.Entry)iterator.next();
Memento sw = (Memento) mentry.getValue();
String[] swimm=new String[]{"",String.valueOf(sw.getID()),sw.getAnimalName(),sw.getColor(),String.valueOf(sw.getSize()),String.valueOf(sw.getXfront()),String.valueOf(sw.getYfront()),String.valueOf(sw.getHorSpeed()),String.valueOf(sw.getVerSpeed()),sw.getHungerStatus()};
tableModel.addRow(swimm);
}
}
table.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = table.rowAtPoint(evt.getPoint());
int col = table.columnAtPoint(evt.getPoint());
if (row >= 0 && col >= 0) {
idOb=(String) table.getValueAt(row, 1); //for check
typeAnimal=(String) (table.getValueAt(row, 2));
idObject=Integer.valueOf(idOb);
}
}
});
JScrollPane pane=new JScrollPane(table);
tablePanel.add(pane); // add the table to the panel
}
public int getIDobject(){return idObject;}
public String getTypeAnimal(){return typeAnimal;}
}