1 package gnu.kawa.swingviews; 2 import gnu.kawa.models.*; 3 import javax.swing.*; 4 import java.awt.Color ; 5 6 public class SwingButton 7 extends JButton 8 implements ModelListener 9 { 10 Button model; 11 12 public SwingButton (Button model) 13 { 14 super(model.getText()); 15 setModel(new SwModel(model)); 16 this.model = model; 17 Object action = model.getAction(); 18 if (action != null) 19 addActionListener(SwingDisplay.makeActionListener(action)); 20 model.addListener(this); 21 Color fg = model.getForeground(); 22 if (fg != null) 23 super.setBackground(fg); 24 Color bg = model.getBackground(); 25 if (bg != null) 26 super.setBackground(bg); 27 } 28 29 public void setText(String text) 30 { 31 if (model == null) 32 super.setText(text); 33 else 34 model.setText(text); 35 } 36 37 public void setForeground (Color fg) 38 { 39 if (model == null) 40 super.setForeground(fg); 41 else 42 model.setForeground(fg); 43 } 44 45 public void setBackground (Color bg) 46 { 47 if (model == null) 48 super.setBackground(bg); 49 else 50 model.setBackground(bg); 51 } 52 53 public void modelUpdated (Model model, Object key) 54 { 55 if (key == "text" && model == this.model) 56 super.setText(this.model.getText()); 57 else if (key == "foreground" && model == this.model) 58 super.setForeground(this.model.getForeground()); 59 else if (key == "background" && model == this.model) 60 super.setBackground(this.model.getBackground()); 61 } 62 } 63 64 class SwModel extends DefaultButtonModel 65 { 66 Button model; 67 68 public SwModel (Button model) 69 { 70 this.model = model; 71 setActionCommand(model.getText()); 72 } 73 } 74 | Popular Tags |