summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/LightedPushButton.java
blob: 1d63754b800225d54798b6ce70fcf711358310d8 (plain)
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
/* LightedPushButton.java */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.beans.*;

public class LightedPushButton
extends Canvas
implements Serializable
{
  //---Instanzvariablen----------------------------------------
  protected Color                 linecolor;
  protected Color                 framecolor;
  protected Color                 lightoncolor;
  protected Color                 lightoffcolor;
  protected boolean               lighton;
  transient protected PropertyChangeSupport pchglisteners;
  transient protected VetoableChangeSupport vchglisteners;

  //---Methoden------------------------------------------------
  public LightedPushButton()
  {
    linecolor     = Color.black;
    framecolor    = Color.darkGray;
    lightoncolor  = Color.red;
    lightoffcolor = new Color(127, 0, 0); //dark red
    lighton       = false;
    initTransientState();
  }

  //---Zustandsumschaltung Licht an/aus---
  public void setLightOn(boolean on)
  throws PropertyVetoException
  {
    boolean oldvalue = this.lighton;
    vchglisteners.fireVetoableChange("lighton", oldvalue, on);
    this.lighton = on;
    if (oldvalue != on) {
      repaint();
    }
    pchglisteners.firePropertyChange("lighton", oldvalue, on);
  }

  public boolean getLightOn()
  {
    return this.lighton;
  }

  //---Verwaltung der PropertyChangeListener---
  public void addPropertyChangeListener(PropertyChangeListener l)
  {
    pchglisteners.addPropertyChangeListener(l);
  }

  public void removePropertyChangeListener(PropertyChangeListener l)
  {
    pchglisteners.removePropertyChangeListener(l);
  }

  //---Verwaltung der VetoableChangeListener---
  public void addVetoableChangeListener(VetoableChangeListener l)
  {
    vchglisteners.addVetoableChangeListener(l);
  }

  public void removeVetoableChangeListener(VetoableChangeListener l)
  {
    vchglisteners.removeVetoableChangeListener(l);
  }

  //---Implementierung der Oberfl�che---
  public void paint(Graphics g)
  {
    int width = getSize().width;
    int height = getSize().height;
    //Rahmen
    g.setColor(framecolor);
    g.fillOval(0, 0, width, height);
    //Beleuchtung
    g.setColor(lighton ? lightoncolor : lightoffcolor);
    g.fillOval(4, 4, width - 8, height - 8);
    //Konturlinien
    g.setColor(linecolor);
    g.drawOval(0, 0, width - 1, height - 1);
    g.drawOval(3, 3, width - 7, height - 7);
  }

  public Dimension getPreferredSize()
  {
    return new Dimension(32, 32);
  }

  public Dimension getMinimumSize()
  {
    return new Dimension(16, 16);
  }

  //---Private Klassen---------------------------------------
  /**
   * Initialisierung der nicht-persistenten Instanzvariablen.
   */
  private void initTransientState()
  {
    pchglisteners = new PropertyChangeSupport(this);
    vchglisteners = new VetoableChangeSupport(this);
    addMouseListener(new MouseClickAdapter());
  }

  /**
   * Wird �berlagert, um nach dem Deserialisieren den transienten
   * Zustand zu initialisieren.
   */
  private void readObject(ObjectInputStream stream)
  throws IOException, ClassNotFoundException
  {
    stream.defaultReadObject();
    initTransientState();
  }

  //---Lokale Klassen----------------------------------------
  class MouseClickAdapter
  extends MouseAdapter
  {
    public void mouseClicked(MouseEvent event)
    {
      try {
        setLightOn(!getLightOn());
      } catch (PropertyVetoException e) {
        //no change if vetoed
      }
    }
  }
}