01: package gnu.kawa.swingviews;
02:
03: import gnu.kawa.models.*;
04: import javax.swing.*;
05: import java.awt.Color;
06:
07: public class SwingButton extends JButton implements ModelListener {
08: Button model;
09:
10: public SwingButton(Button model) {
11: super (model.getText());
12: setModel(new SwModel(model));
13: this .model = model;
14: Object action = model.getAction();
15: if (action != null)
16: addActionListener(SwingDisplay.makeActionListener(action));
17: model.addListener(this );
18: Color fg = model.getForeground();
19: if (fg != null)
20: super .setBackground(fg);
21: Color bg = model.getBackground();
22: if (bg != null)
23: super .setBackground(bg);
24: }
25:
26: public void setText(String text) {
27: if (model == null)
28: super .setText(text);
29: else
30: model.setText(text);
31: }
32:
33: public void setForeground(Color fg) {
34: if (model == null)
35: super .setForeground(fg);
36: else
37: model.setForeground(fg);
38: }
39:
40: public void setBackground(Color bg) {
41: if (model == null)
42: super .setBackground(bg);
43: else
44: model.setBackground(bg);
45: }
46:
47: public void modelUpdated(Model model, Object key) {
48: if (key == "text" && model == this .model)
49: super .setText(this .model.getText());
50: else if (key == "foreground" && model == this .model)
51: super .setForeground(this .model.getForeground());
52: else if (key == "background" && model == this .model)
53: super .setBackground(this .model.getBackground());
54: }
55: }
56:
57: class SwModel extends DefaultButtonModel {
58: Button model;
59:
60: public SwModel(Button model) {
61: this.model = model;
62: setActionCommand(model.getText());
63: }
64: }
|