@@ -0,0 +1,227 @@ | |||
/* | |||
* 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; | |||
public class Datenmodell implements Runnable | |||
{ | |||
//Möglichkeit 1 bei Stopp keine Updates mehr an Anzeige | |||
private int wert; | |||
private volatile boolean laufend; | |||
private SubmissionPublisher<Integer> iPublisher; | |||
private SubmissionPublisher<Boolean> bPublisher; | |||
private ExecutorService eService; | |||
private boolean darfupdaten =true; | |||
public Datenmodell() | |||
{ | |||
wert = 1; | |||
laufend = false; | |||
iPublisher = new SubmissionPublisher<>(); | |||
bPublisher = new SubmissionPublisher<>(); | |||
eService = Executors.newSingleThreadExecutor(); | |||
} | |||
public void start() | |||
{ | |||
laufend = true; | |||
darfupdaten = true; | |||
eService.submit(this); | |||
} | |||
public void stop() | |||
{ | |||
laufend = false; | |||
darfupdaten =false; | |||
} | |||
public void addWertSubscription(Subscriber<Integer> subscriber) | |||
{ | |||
iPublisher.subscribe(subscriber); | |||
} | |||
public void addZustandSubscription(Subscriber<Boolean> subscriber) | |||
{ | |||
bPublisher.subscribe(subscriber); | |||
} | |||
@Override | |||
public void run() | |||
{ | |||
while (laufend) | |||
{ | |||
try | |||
{ | |||
Thread.sleep(50); | |||
} | |||
catch (Exception e) | |||
{ | |||
System.err.println(e); | |||
} | |||
if(wert==6){ | |||
wert=0; | |||
} | |||
wert++; | |||
if(darfupdaten){ | |||
iPublisher.submit(wert); | |||
} | |||
} | |||
} | |||
// Möglichkeit 2 zerstören und neuerstellen des Thread-Objektes | |||
// private int wert; | |||
// private volatile boolean laufend; | |||
// private SubmissionPublisher<Integer> iPublisher; | |||
// private SubmissionPublisher<Boolean> bPublisher; | |||
// private ExecutorService eService; | |||
// | |||
// public Datenmodell() | |||
// { | |||
// wert = 1; | |||
// laufend = false; | |||
// iPublisher = new SubmissionPublisher<>(); | |||
// bPublisher = new SubmissionPublisher<>(); | |||
// | |||
// } | |||
// | |||
// public void start() | |||
// { | |||
// laufend = true; | |||
// eService = Executors.newSingleThreadExecutor(); | |||
// eService.submit(this); | |||
// } | |||
// | |||
// public void stop() | |||
// { | |||
// laufend = false; | |||
// eService = null; | |||
// } | |||
// | |||
// public void addWertSubscription(Subscriber<Integer> subscriber) | |||
// { | |||
// iPublisher.subscribe(subscriber); | |||
// } | |||
// | |||
// public void addZustandSubscription(Subscriber<Boolean> subscriber) | |||
// { | |||
// bPublisher.subscribe(subscriber); | |||
// } | |||
// | |||
// @Override | |||
// public void run() | |||
// { | |||
// while (laufend) | |||
// { | |||
// try | |||
// { | |||
// Thread.sleep(50); | |||
// } | |||
// catch (Exception e) | |||
// { | |||
// System.err.println(e); | |||
// } | |||
// if(wert==6){ | |||
// wert=0; | |||
// } | |||
// wert++; | |||
// iPublisher.submit(wert); | |||
// | |||
// | |||
// } | |||
// } | |||
//Ende Möglichkeit 2 | |||
// Möglichkeit 3 wait und notify | |||
// private int wert; | |||
// private volatile boolean await; | |||
// private SubmissionPublisher<Integer> iPublisher; | |||
// private SubmissionPublisher<Boolean> bPublisher; | |||
// private ExecutorService eService; | |||
// | |||
// public Datenmodell() | |||
// { | |||
// wert = 1; | |||
// await = false; | |||
// iPublisher = new SubmissionPublisher<>(); | |||
// bPublisher = new SubmissionPublisher<>(); | |||
// eService = Executors.newSingleThreadExecutor(); | |||
// } | |||
// | |||
// public synchronized void start() | |||
// { | |||
// if(await){ | |||
// eService.notify(); | |||
// } | |||
// eService.submit(this); | |||
// } | |||
// | |||
// public synchronized void stop() | |||
// { | |||
// try{ | |||
// eService.wait(); | |||
// | |||
// await = true; | |||
// }catch(Exception ex){ | |||
// System.out.println(ex); | |||
// } | |||
// } | |||
// | |||
// public void addWertSubscription(Subscriber<Integer> subscriber) | |||
// { | |||
// iPublisher.subscribe(subscriber); | |||
// } | |||
// | |||
// public void addZustandSubscription(Subscriber<Boolean> subscriber) | |||
// { | |||
// bPublisher.subscribe(subscriber); | |||
// } | |||
// | |||
// @Override | |||
// public void run() | |||
// { | |||
// while(true){ | |||
// try | |||
// { | |||
// Thread.sleep(50); | |||
// } | |||
// catch (Exception e) | |||
// { | |||
// System.err.println(e); | |||
// } | |||
// if(wert==6){ | |||
// wert=0; | |||
// } | |||
// wert++; | |||
// iPublisher.submit(wert); | |||
// | |||
// } | |||
// } | |||
} | |||
@@ -0,0 +1,47 @@ | |||
/* | |||
* 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() | |||
{ | |||
Datenmodell model = new Datenmodell(); | |||
SubscriberView view = new SubscriberView(); | |||
StartStopController controller = new StartStopController(view, model); | |||
WertAdapter wAdapter = new WertAdapter(view, model); | |||
ZustandsAdapter zAdapter = new ZustandsAdapter(view, model); | |||
controller.registerEvents(); | |||
wAdapter.einschreiben(); | |||
zAdapter.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(); | |||
} | |||
} |
@@ -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 Datenmodell model; | |||
public StartStopController(SubscriberView view, Datenmodell 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(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,63 @@ | |||
<?xml version="1.0" encoding="UTF-8" ?> | |||
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> | |||
<Properties> | |||
<Property name="defaultCloseOperation" type="int" value="3"/> | |||
</Properties> | |||
<SyntheticProperties> | |||
<SyntheticProperty name="formSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,38,0,0,1,109"/> | |||
<SyntheticProperty name="formSizePolicy" type="int" value="0"/> | |||
<SyntheticProperty name="generateSize" type="boolean" value="true"/> | |||
<SyntheticProperty name="generateCenter" type="boolean" value="true"/> | |||
</SyntheticProperties> | |||
<AuxValues> | |||
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> | |||
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> | |||
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> | |||
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> | |||
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> | |||
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> | |||
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> | |||
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> | |||
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> | |||
</AuxValues> | |||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> | |||
<SubComponents> | |||
<Container class="javax.swing.JPanel" name="jPanel1"> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
<BorderConstraints direction="First"/> | |||
</Constraint> | |||
</Constraints> | |||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/> | |||
<SubComponents> | |||
<Component class="javax.swing.JButton" name="btnStart"> | |||
<Properties> | |||
<Property name="text" type="java.lang.String" value="Start"/> | |||
</Properties> | |||
</Component> | |||
<Component class="javax.swing.JButton" name="btnStop"> | |||
<Properties> | |||
<Property name="text" type="java.lang.String" value="Stopp"/> | |||
</Properties> | |||
</Component> | |||
</SubComponents> | |||
</Container> | |||
<Component class="javax.swing.JLabel" name="lblZahl"> | |||
<Properties> | |||
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> | |||
<Font name="Ubuntu" size="60" style="0"/> | |||
</Property> | |||
<Property name="horizontalAlignment" type="int" value="0"/> | |||
<Property name="text" type="java.lang.String" value="1"/> | |||
</Properties> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
<BorderConstraints direction="Center"/> | |||
</Constraint> | |||
</Constraints> | |||
</Component> | |||
</SubComponents> | |||
</Form> |
@@ -0,0 +1,140 @@ | |||
/* | |||
* 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; | |||
} | |||
/** | |||
* @return the pbStatus | |||
*/ | |||
/** | |||
* 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") | |||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//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); | |||
}// </editor-fold>//GEN-END:initComponents | |||
/** | |||
* @param args the command line arguments | |||
*/ | |||
public static void main(String args[]) | |||
{ | |||
/* Set the Nimbus look and feel */ | |||
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |||
/* 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); | |||
} | |||
//</editor-fold> | |||
/* 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 | |||
} |
@@ -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<Integer> | |||
{ | |||
private SubscriberView view; | |||
private Datenmodell model; | |||
private Subscription subscription; | |||
public WertAdapter(SubscriberView view, Datenmodell 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() | |||
{ | |||
} | |||
} |
@@ -0,0 +1,57 @@ | |||
/* | |||
* 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 ZustandsAdapter implements Subscriber<Boolean> | |||
{ | |||
private SubscriberView view; | |||
private Datenmodell model; | |||
private Subscription subscription; | |||
public ZustandsAdapter(SubscriberView view, Datenmodell model) | |||
{ | |||
this.view = view; | |||
this.model = model; | |||
} | |||
public void einschreiben() | |||
{ | |||
model.addZustandSubscription(this); | |||
} | |||
@Override | |||
public void onSubscribe(Subscription subscription) | |||
{ | |||
this.subscription = subscription; | |||
subscription.request(1); | |||
} | |||
@Override | |||
public void onNext(Boolean item) | |||
{ | |||
subscription.request(1); | |||
} | |||
@Override | |||
public void onError(Throwable throwable) | |||
{ | |||
} | |||
@Override | |||
public void onComplete() | |||
{ | |||
} | |||
} |