-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestoreObjectState.java
209 lines (150 loc) · 5.64 KB
/
RestoreObjectState.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* restoreObject state class restore the specific object that his details saved in memento object and repaint the aquarium
* @author Adir Zoari 203002753
* @author Idan Levi 305562431
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class RestoreObjectState extends JDialog implements ActionListener, ItemListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private String[] types = {"Swimmable types","Planets types"};
private JComboBox<String> cmb_types; // ComboBox colors types
private Iterator <Swimmable>itrAnimals;
private Iterator <Immobile>itrPlants;
private JPanel DialogPanel,buttonsPanel,comboPanel,reportsPanel; // create panels to the dialog
private JButton b_restoreObject,b_cancel;
private SwimmableReport swReport;
private PlanetReport plReport;
private CareTaker carTakerMemento;
private HashMap<Integer, Memento> swimmableMementoList;
private HashMap<Integer, Memento> plantesMementoList;
private HashSet<Swimmable> animalsSet=new HashSet<Swimmable>(); //hash set for swimmable
private HashSet<Immobile> planetSet=new HashSet<Immobile>(); //hash set for swimmable
private AquaPanel panel;
public RestoreObjectState(AquaPanel panel,CareTaker carTakerMemento)
{
super();
//settings restore object state
setTitle("Momento Design Pattern - Restore Object state");
setSize(650,300);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
//this.carTakerMemento=new CareTaker();
this.carTakerMemento=carTakerMemento;
this.panel=panel;
this.swimmableMementoList=new HashMap<Integer, Memento>(carTakerMemento.getSwimmableMementoList());
this.plantesMementoList=new HashMap<Integer, Memento>(carTakerMemento.getPlantesMementoList());
this.swReport=new SwimmableReport(swimmableMementoList,2);
this.plReport=new PlanetReport(plantesMementoList,2);
this.animalsSet=new HashSet<Swimmable>(panel.getSwimmableSet());
this.planetSet=new HashSet<Immobile>(panel.getPlanetSet());
setPanel();
add(DialogPanel); // add the panel to the JDialog dialogTable
setVisible(true);// show the dialog
}
public void setPanel() {
DialogPanel=new JPanel(new BorderLayout());
buttonsPanel = new JPanel(new FlowLayout());
comboPanel=new JPanel(new FlowLayout());
reportsPanel=new JPanel();
comboPanel.setSize(new Dimension(300,100));
cmb_types=new JComboBox<String>(types);
b_restoreObject=new JButton("Restore Object");
b_cancel=new JButton("Cancel");
buttonsPanel.add(b_restoreObject);
buttonsPanel.add(b_cancel);
buttonsPanel.setVisible(true);
reportsPanel.add(swReport);
reportsPanel.add(plReport);
plReport.setVisible(false);
swReport.setVisible(true);
comboPanel.add(cmb_types);
DialogPanel.add(comboPanel,BorderLayout.PAGE_START);
DialogPanel.add(buttonsPanel,BorderLayout.SOUTH);
DialogPanel.add(reportsPanel);
cmb_types.addItemListener(this);
b_restoreObject.addActionListener(this);
b_cancel.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e1) {
if(e1.getSource()==b_restoreObject)
{
Swimmable swimObj=null;
Immobile immoObj=null;
//Memento memObj=null;
int idObject;
if(swReport.getTypeAnimal()=="Fish" || swReport.getTypeAnimal()=="Jellyfish"){
idObject=swReport.getIDobject();
swimObj = getSwimById(swReport.getIDobject());
swimObj.setState(swimmableMementoList.get(idObject).getCol(), swimmableMementoList.get(idObject).getSize(),swimmableMementoList.get(idObject).getXfront(), swimmableMementoList.get(idObject).getYfront(),swimmableMementoList.get(idObject).getHorSpeed(),swimmableMementoList.get(idObject).getVerSpeed(),swimmableMementoList.get(idObject).getX_dir(),swimmableMementoList.get(idObject).getY_dir());
JOptionPane.showMessageDialog(null,swimObj.getAnimalName() + " " + swimObj.getID()+ " state restored!");
panel.repaint();
}
if(plReport.getTypeAnimal()=="Laminaria" || plReport.getTypeAnimal()=="Zostera"){
idObject=plReport.getIDobject();
immoObj = getImmoById(plReport.getIDobject());
immoObj.setState(plantesMementoList.get(idObject).getCol(), plantesMementoList.get(idObject).getSize(), plantesMementoList.get(idObject).getXfront(), plantesMementoList.get(idObject).getYfront());
JOptionPane.showMessageDialog(null,immoObj.getPlanetName() + " " + immoObj.getID()+ " state restored!");
panel.repaint();
}
dispose();
}
else if(e1.getSource()==b_cancel)
{
dispose();
}
}
private Immobile getImmoById(int idObject) {
itrPlants=planetSet.iterator();
while(itrPlants.hasNext())
{
if(itrPlants.next().getID()==idObject)
return itrPlants.next();
}
return null;
}
private Swimmable getSwimById(int idObject) {
itrAnimals=animalsSet.iterator();
while(itrAnimals.hasNext())
{
Swimmable sw=itrAnimals.next();
if(sw.getID()==idObject)
{
return sw;
}
}
return null;
}
@Override
public void itemStateChanged(ItemEvent e2) {
if(e2.getItem()=="Swimmable types")
{
swReport.setVisible(true);
plReport.setVisible(false);
System.out.println("sw");
}
if(e2.getItem()=="Planets types")
{
swReport.setVisible(false);
plReport.setVisible(true);
System.out.println("pl");
}
}
}