Skip to content

Commit

Permalink
First SWidgets example: Spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Blackheath committed Jan 21, 2015
1 parent 315eb5b commit 47f9d81
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 0 deletions.
2 changes: 2 additions & 0 deletions book/swidgets/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.class
*.jar
49 changes: 49 additions & 0 deletions book/swidgets/java/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project default="compile">

<target name="clean">
<delete dir="swidgets/build"/>
<delete file="swidgets/swidgets.jar"/>
<delete>
<fileset dir="examples" includes="*.class"/>
</delete>
</target>

<target name="compile">
<subant buildpath="../../../java"/>
<mkdir dir="swidgets/build/src"/>
<javac debug="true" srcdir="swidgets/src" destdir="swidgets/build/src" source="1.8" target="1.8">
<classpath>
<pathelement path="../../../java/sodium/sodium.jar"/>
</classpath>
</javac>
</target>

<target name="jar" depends="compile">
<mkdir dir="swidgets/build/jar" />
<jar destfile="swidgets/swidgets.jar" basedir="swidgets/build/src">
<manifest>
<attribute name="Main-Class" value="oata.HelloWorld" />
</manifest>
</jar>
</target>

<target name="compile-examples" depends="jar">
<javac debug="true" srcdir="examples" destdir="examples" source="1.8" target="1.8">
<classpath>
<pathelement path="swidgets/swidgets.jar"/>
<pathelement path="../../../java/sodium/sodium.jar"/>
</classpath>
</javac>
</target>

<target name="example1" depends="compile-examples">
<java classname="Example1" fork="true">
<classpath>
<pathelement path="examples"/>
<pathelement path="swidgets/swidgets.jar"/>
<pathelement path="../../../java/sodium/sodium.jar"/>
</classpath>
</java>
</target>

</project>
30 changes: 30 additions & 0 deletions book/swidgets/java/examples/Example1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import sodium.*;

public class Example1 {
public static void main(String[] args) {
JFrame view = new JFrame("Example1") {
public Dimension getPreferredSize() {
return new Dimension(400, 160);
}
};
Transaction.runVoid(() -> {
view.setLayout(new FlowLayout());

SSpinner spnr = new SSpinner();
view.add(spnr);

view.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
view.pack();
});
view.setVisible(true);
}
}

59 changes: 59 additions & 0 deletions book/swidgets/java/examples/SSpinner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import javax.swing.*;
import java.awt.*;
import swidgets.*;
import sodium.*;

public class SSpinner extends JComponent
{
SSpinner()
{
StreamLoop<String> sSetText = new StreamLoop<>();
STextField textField = new STextField("0", sSetText, 5);
this.text = textField.text;
SButton up = new SButton("+");
SButton down = new SButton("-");

GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 2;
gridbag.setConstraints(textField, c);
add(textField);
c.gridwidth = 1;
c.gridheight = 1;
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(up, c);
add(up);
c.gridx = 1;
c.gridy = 1;
gridbag.setConstraints(down, c);
add(down);

Stream<Integer> sUpDelta = up.sClicked.map(u -> 1);
Stream<Integer> sDownDelta = down.sClicked.map(u -> -1);
Stream<Integer> sDelta = sUpDelta.merge(sDownDelta);
sSetText.loop(
sDelta.snapshot(
textField.text,
(delta, txt) -> {
try {
return Integer.toString(Integer.parseInt(txt) + delta);
}
catch (NumberFormatException e) {
System.err.println("can't parse "+txt);
return txt;
}
}
)
);
}

public final Cell<String> text;
}

23 changes: 23 additions & 0 deletions book/swidgets/java/swidgets/src/swidgets/SButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package swidgets;

import sodium.*;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SButton extends JButton
{
public SButton(String label)
{
super(label);
StreamSink<Unit> sClickedSink = new StreamSink<>();
this.sClicked = sClickedSink;
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sClickedSink.send(Unit.UNIT);
}
});
}

public final Stream<Unit> sClicked;
}
56 changes: 56 additions & 0 deletions book/swidgets/java/swidgets/src/swidgets/STextField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package swidgets;

import sodium.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.util.concurrent.ArrayBlockingQueue;

public class STextField extends JTextField
{
public STextField(String initText, Stream<String> sText, int width)
{
super(initText, width);

allow = sText.map(u -> 1).merge(sDecrement).accum(0, (d, b) -> b + d).map(b -> b == 0);

final StreamSink<String> sUserText = new StreamSink<String>();
this.text = sUserText.gate(allow).merge(sText).hold(initText);
DocumentListener dl = new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
update();
}
public void removeUpdate(DocumentEvent e) {
update();
}
public void insertUpdate(DocumentEvent e) {
update();
}

public void update() {
sUserText.send(getText());
}
};

getDocument().addDocumentListener(dl);

l = sText.listen(text -> {
SwingUtilities.invokeLater(() -> {
setText(text);
sDecrement.send(-1);
});
});
}

private final StreamSink<Integer> sDecrement = new StreamSink<>();
private final Cell<Boolean> allow;
private final Listener l;
public final Cell<String> text;

public void removeNotify() {
l.unlisten();
super.removeNotify();
}
}

0 comments on commit 47f9d81

Please sign in to comment.