From 09a1340b2b28de9ff3e313c5e9765834bf5cbf72 Mon Sep 17 00:00:00 2001 From: marku Date: Wed, 6 Nov 2019 11:54:29 +0100 Subject: [PATCH] 1 - First Try --- src/subscriber/Datenmodell1.java | 78 ++++++++++++++ src/subscriber/Datenmodell2.java | 73 +++++++++++++ src/subscriber/Start.java | 44 ++++++++ src/subscriber/StartStopController.java | 45 ++++++++ src/subscriber/SubscriberView.form | 63 +++++++++++ src/subscriber/SubscriberView.java | 135 ++++++++++++++++++++++++ src/subscriber/WertAdapter.java | 58 ++++++++++ 7 files changed, 496 insertions(+) create mode 100644 src/subscriber/Datenmodell1.java create mode 100644 src/subscriber/Datenmodell2.java create mode 100644 src/subscriber/Start.java create mode 100644 src/subscriber/StartStopController.java create mode 100644 src/subscriber/SubscriberView.form create mode 100644 src/subscriber/SubscriberView.java create mode 100644 src/subscriber/WertAdapter.java diff --git a/src/subscriber/Datenmodell1.java b/src/subscriber/Datenmodell1.java new file mode 100644 index 0000000..4041af4 --- /dev/null +++ b/src/subscriber/Datenmodell1.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package subscriber; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.SubmissionPublisher; + +/** + * + * @author le + */ +public class Datenmodell1 implements Runnable +{ + private int faktor; + private int wert; + public boolean active; + private volatile boolean laufend; + private SubmissionPublisher iPublisher; + private ExecutorService eService; + + public Datenmodell1() + { + faktor = 10; + wert = 1; + laufend = false; + iPublisher = new SubmissionPublisher<>(); + eService = Executors.newSingleThreadExecutor(); + laufend = true; + eService.submit(this); + + active = false; + } + + public void start(){ + active = true; + } + + public void stop() + { + active = false; + } + + public void addWertSubscription(Subscriber subscriber) + { + iPublisher.subscribe(subscriber); + } + + @Override + public void run() + { + while (laufend) + { + try + { + Thread.sleep(5); + } + catch (Exception e) + { + System.err.println(e); + } + wert = calculateWert(); + System.out.println(wert); // just for displaying that the thread is running + if (active){ + iPublisher.submit(wert); + } + } + } + + public int calculateWert(){ + return (int)(Math.random()*6) + 1; + } +} diff --git a/src/subscriber/Datenmodell2.java b/src/subscriber/Datenmodell2.java new file mode 100644 index 0000000..eba1911 --- /dev/null +++ b/src/subscriber/Datenmodell2.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package subscriber; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.SubmissionPublisher; + +/** + * + * @author le + */ +public class Datenmodell2 implements Runnable +{ + private int faktor; + private int wert; + private volatile boolean laufend; + private SubmissionPublisher iPublisher; + private ExecutorService eService; + + public Datenmodell2() + { + faktor = 10; + wert = 1; + laufend = false; + iPublisher = new SubmissionPublisher<>(); + eService = Executors.newSingleThreadExecutor(); + } + + public void start() + { + laufend = true; + eService.submit(this); + } + + public void stop() + { + laufend = false; + } + + public void addWertSubscription(Subscriber subscriber) + { + iPublisher.subscribe(subscriber); + } + + @Override + public void run() + { + while (laufend) + { + try + { + Thread.sleep(5); + } + catch (Exception e) + { + System.err.println(e); + } + wert = calculateWert(); + System.out.println(wert); // just for displaying that the thread is running + iPublisher.submit(wert); + } + } + + public int calculateWert(){ + return (int)(Math.random()*6) + 1; + } +} diff --git a/src/subscriber/Start.java b/src/subscriber/Start.java new file mode 100644 index 0000000..2d4c5d1 --- /dev/null +++ b/src/subscriber/Start.java @@ -0,0 +1,44 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package subscriber; + +import javax.swing.JOptionPane; +import javax.swing.UIManager; + +/** + * Builder Class + * @author le + */ +public class Start +{ + public Start() + { + Datenmodell3 model = new Datenmodell3(); + SubscriberView view = new SubscriberView(); + StartStopController controller = new StartStopController(view, model); + WertAdapter wAdapter = new WertAdapter(view, model); + controller.registerEvents(); + wAdapter.einschreiben(); + view.setVisible(true); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception ex) + { + JOptionPane.showMessageDialog(null, ex.toString()); + } + new Start(); + } +} diff --git a/src/subscriber/StartStopController.java b/src/subscriber/StartStopController.java new file mode 100644 index 0000000..4b4cb26 --- /dev/null +++ b/src/subscriber/StartStopController.java @@ -0,0 +1,45 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package subscriber; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * + * @author le + */ +public class StartStopController implements ActionListener +{ + private SubscriberView view; + private Datenmodell3 model; + + public StartStopController(SubscriberView view, Datenmodell3 model) + { + this.view = view; + this.model = model; + } + + public void registerEvents() + { + view.getBtnStart().addActionListener(this); + view.getBtnStop().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent evt) + { + if (evt.getSource() == view.getBtnStart()) + { + model.start(); + } + else + { + model.stop(); + } + } +} diff --git a/src/subscriber/SubscriberView.form b/src/subscriber/SubscriberView.form new file mode 100644 index 0000000..833ad5a --- /dev/null +++ b/src/subscriber/SubscriberView.form @@ -0,0 +1,63 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/subscriber/SubscriberView.java b/src/subscriber/SubscriberView.java new file mode 100644 index 0000000..b807026 --- /dev/null +++ b/src/subscriber/SubscriberView.java @@ -0,0 +1,135 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package subscriber; + +/** + * + * @author le + */ +public class SubscriberView extends javax.swing.JFrame +{ + /** + * @return the btnStart + */ + public javax.swing.JButton getBtnStart() + { + return btnStart; + } + + /** + * @return the btnStop + */ + public javax.swing.JButton getBtnStop() + { + return btnStop; + } + + /** + * @return the lblZahl + */ + public javax.swing.JLabel getLblZahl() + { + return lblZahl; + } + + /** + * Creates new form SubscriberView + */ + public SubscriberView() + { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() + { + + jPanel1 = new javax.swing.JPanel(); + btnStart = new javax.swing.JButton(); + btnStop = new javax.swing.JButton(); + lblZahl = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + btnStart.setText("Start"); + jPanel1.add(btnStart); + + btnStop.setText("Stopp"); + jPanel1.add(btnStop); + + getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START); + + lblZahl.setFont(new java.awt.Font("Ubuntu", 0, 60)); // NOI18N + lblZahl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + lblZahl.setText("1"); + getContentPane().add(lblZahl, java.awt.BorderLayout.CENTER); + + setSize(new java.awt.Dimension(365, 294)); + setLocationRelativeTo(null); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) + { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try + { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) + { + if ("Nimbus".equals(info.getName())) + { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } + catch (ClassNotFoundException ex) + { + java.util.logging.Logger.getLogger(SubscriberView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (InstantiationException ex) + { + java.util.logging.Logger.getLogger(SubscriberView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (IllegalAccessException ex) + { + java.util.logging.Logger.getLogger(SubscriberView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (javax.swing.UnsupportedLookAndFeelException ex) + { + java.util.logging.Logger.getLogger(SubscriberView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() + { + public void run() + { + new SubscriberView().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton btnStart; + private javax.swing.JButton btnStop; + private javax.swing.JPanel jPanel1; + private javax.swing.JLabel lblZahl; + // End of variables declaration//GEN-END:variables +} diff --git a/src/subscriber/WertAdapter.java b/src/subscriber/WertAdapter.java new file mode 100644 index 0000000..2e07fc6 --- /dev/null +++ b/src/subscriber/WertAdapter.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package subscriber; + +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.Flow.Subscription; + +/** + * + * @author le + */ +public class WertAdapter implements Subscriber +{ + private SubscriberView view; + private Datenmodell3 model; + private Subscription subscription; + + public WertAdapter(SubscriberView view, Datenmodell3 model) + { + this.view = view; + this.model = model; + } + + public void einschreiben() + { + model.addWertSubscription(this); + } + + @Override + public void onSubscribe(Subscription subscription) + { + this.subscription = subscription; + subscription.request(1); + } + + @Override + public void onNext(Integer item) + { + view.getLblZahl().setText(String.valueOf(item)); + subscription.request(1); + } + + @Override + public void onError(Throwable throwable) + { + } + + @Override + public void onComplete() + { + } + + +}