summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java')
-rw-r--r--Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java114
1 files changed, 114 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java b/Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java
new file mode 100644
index 0000000..ddc8a87
--- /dev/null
+++ b/Master/Reference Architectures and Patterns/hjp5/examples/VetoSwitch.java
@@ -0,0 +1,114 @@
+/* VetoSwitch.java */
+
+import java.awt.*;
+import java.awt.event.*;
+import java.io.*;
+import java.beans.*;
+
+public class VetoSwitch
+extends Canvas
+implements Serializable, VetoableChangeListener
+{
+ //---Instanzvariablen----------------------------------------
+ protected Color linecolor;
+ protected boolean vetoallchanges;
+
+ //---Methoden------------------------------------------------
+ public VetoSwitch()
+ {
+ this.linecolor = Color.black;
+ this.vetoallchanges = false;
+ initTransientState();
+ }
+
+ //---Konturenfarbe---
+ public void setLineColor(Color color)
+ {
+ this.linecolor = color;
+ }
+
+ public Color getLineColor()
+ {
+ return this.linecolor;
+ }
+
+ //---Zustandsumschaltung Licht an/aus---
+ public void setVetoAllChanges(boolean b)
+ {
+ if (this.vetoallchanges != b) {
+ this.vetoallchanges = b;
+ repaint();
+ }
+ }
+
+ public boolean getVetoAllChanges()
+ {
+ return this.vetoallchanges;
+ }
+
+ //---Veto---
+ public void vetoableChange(PropertyChangeEvent e)
+ throws PropertyVetoException
+ {
+ if (this.vetoallchanges) {
+ throw new PropertyVetoException("!!!VETO!!!", e);
+ }
+ }
+
+ //---Implementierung der Oberfläche---
+ public void paint(Graphics g)
+ {
+ int width = getSize().width;
+ int height = getSize().height;
+ g.setColor(linecolor);
+ g.drawRect(0, 0, width - 1, height - 1);
+ g.drawLine(width * 1 / 8, height / 2, width * 3 / 8, height / 2);
+ g.drawLine(width * 5 / 8, height / 2, width * 7 / 8, height / 2);
+ g.fillRect(width * 3 / 8 - 1, height / 2 - 1, 3, 3);
+ g.fillRect(width * 5 / 8 - 1, height / 2 - 1, 3, 3);
+ if (this.vetoallchanges) {
+ //draw open connection
+ g.drawLine(width * 3 / 8, height / 2, width * 5 / 8, height / 4);
+ } else {
+ //draw short-cutted connection
+ g.drawLine(width * 3 / 8, height / 2, width * 5 / 8, height / 2);
+ }
+ }
+
+ public Dimension getPreferredSize()
+ {
+ return new Dimension(60, 20);
+ }
+
+ public Dimension getMinimumSize()
+ {
+ return new Dimension(28, 10);
+ }
+
+ //---Private Klassen----------------------------------------
+ private void initTransientState()
+ {
+ 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)
+ {
+ setVetoAllChanges(!getVetoAllChanges());
+ }
+ }
+} \ No newline at end of file