forked from john-bai/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediator.java
More file actions
143 lines (117 loc) · 2.85 KB
/
Copy pathMediator.java
File metadata and controls
143 lines (117 loc) · 2.85 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package designpatterns;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author jtherrell
*/
interface Mediator {
public void widgetChanged(Widget changedWidget);
}
class FontDialogMediator implements Mediator {
private Button ok;
private Button cancel;
private ListBox fontList;
private EntryField fontName;
private Log log;
public FontDialogMediator(Log log) {
ok = new Button(this, "OK", log);
cancel = new Button(this, "CANCEL", log);
fontList = new ListBox(this, log);
List<String> fonts = new ArrayList<String>();
fonts.add("Helvetica");
fonts.add("Myriad Pro");
fonts.add("Times New Roman");
fontList.setList(fonts);
fontName = new EntryField(this, log);
this.log = log;
}
@Override
public void widgetChanged(Widget changedWidget) {
if (changedWidget == fontList) {
log.append("updating fontName field ");
fontName.setText(fontList.getSelection());
} else if (changedWidget == ok) {
log.append("OK ");
} else if (changedWidget == cancel) {
log.append("CANCEL ");
}
}
public Widget[] getWidgets() {
Widget[] widgets = {ok, cancel, fontList, fontName};
return widgets;
}
}
abstract class Widget {
private Mediator mediator;
protected Log log;
public void changed() {
mediator.widgetChanged(this);
}
abstract public void handleMouse(MouseEvent e);
public Widget(Mediator mediator, Log log) {
this.mediator = mediator;
this.log = log;
}
}
class ListBox extends Widget {
private List<String> listItems;
private int currentSelectionIndex;
public ListBox(Mediator mediator, Log log) {
super(mediator, log);
listItems = new ArrayList<String>();
currentSelectionIndex = 0;
}
@Override
public void handleMouse(MouseEvent e) {
log.append("ListBox changed ");
//pretend the selected item in the list changed
changed();
}
public String getSelection() {
return listItems.get(currentSelectionIndex);
}
public void setList(List<String> listItems) {
this.listItems = listItems;
}
}
class EntryField extends Widget {
private String entryText;
public EntryField(Mediator mediator, Log log) {
super(mediator, log);
entryText = "";
}
public String getText() {
return entryText;
}
public void setText(String text) {
entryText = text;
}
@Override
public void handleMouse(MouseEvent e) {
log.append("EntryField changed ");
//pretend the selected item in the list changed
changed();
}
}
class Button extends Widget {
private String buttonLabel;
public Button(Mediator mediator, String label, Log log) {
super(mediator, log);
buttonLabel = label;
}
public void setLabel(String label) {
buttonLabel = label;
}
@Override
public void handleMouse(MouseEvent e) {
log.append("Button changed ");
//pretend the button was clicked
changed();
}
}
class MouseEvent{}