-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleModel2.java
executable file
·82 lines (82 loc) · 2.58 KB
/
CircleModel2.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
import java.awt.event.*;
import java.util.*;
public class CircleModel2 implements CircleEvents
{ private int circleCounter;
private double radius = 20;
private boolean filled;
private boolean calculateArea;
private java.awt.Color color;
private Map<eventType, ArrayList<ActionListener>> circleHashMap =
new HashMap<eventType, ArrayList<ActionListener>>();
public CircleModel2(int circleCounter)
{ this.circleCounter = circleCounter;
for (eventType et: eventType.values())
circleHashMap.put(et, new ArrayList<ActionListener>());
}
public int getcircleCounter()
{ return circleCounter;
}
public double getRadius()
{ return radius;
}
public void setRadius(double radius)
{ this.radius = radius;
processEvent(eventType.RADIUS,
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
eventType.RADIUS.toString()));
}
public boolean isFilled()
{ return filled;
}
public void setFilled(boolean filled)
{ this.filled = filled;
processEvent(eventType.FILLED,
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
eventType.FILLED.toString()));
}
public boolean calculateArea()
{ return calculateArea;
}
public void setCalculateArea(boolean calculateArea)
{ this.calculateArea = calculateArea;
processEvent(eventType.AREA, new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, eventType.AREA.toString()));
}
public java.awt.Color getColor()
{ return color;
}
public void setColor(java.awt.Color color)
{ this.color = color;
processEvent(eventType.COLOR, new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, eventType.COLOR.toString()));
}
public synchronized void addActionListener(
ActionListener l, eventType et)
{ ArrayList<ActionListener> al;
al = circleHashMap.get(et);
if (al == null) al = new ArrayList<ActionListener>();
al.add(l);
circleHashMap.put(et, al);
}
public synchronized void removeActionListener(
ActionListener l, eventType et)
{ ArrayList<ActionListener> al;
al = circleHashMap.get(et);
if (al != null && al.contains(l)) al.remove(l);
circleHashMap.put(et, al);
}
private void processEvent(eventType et, ActionEvent e)
{ ArrayList<ActionListener> al;
synchronized (this)
{ al = circleHashMap.get(et);
if (al == null) return;
}
System.out.println("model number: " + (getcircleCounter()+1)
+ " actionCommand: " + e.getActionCommand()
+ " array size is: " + al.size());
for (int i = 0; i < al.size(); i++)
{ ActionListener listener = (ActionListener)al.get(i);
listener.actionPerformed(e);
}
}
}