All Classes Namespaces Files Functions Variables
Laskuri.java
Go to the documentation of this file.
1 package autolaskuri.comp;
2 
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.ResourceBundle;
8 
9 import javafx.beans.property.SimpleIntegerProperty;
10 import javafx.fxml.FXML;
11 import javafx.fxml.FXMLLoader;
12 import javafx.fxml.Initializable;
13 import javafx.scene.control.Button;
14 import javafx.scene.control.Label;
15 import javafx.scene.layout.VBox;
16 
17 /**
18  * Laskuri-komponentti
19  * @author vesal
20  * @version 6.3.2016
21  *
22  */
23 public class Laskuri extends VBox implements Initializable {
24  @SuppressWarnings("javadoc")
25  public static class Laskettava extends SimpleIntegerProperty {
26  public Laskettava(int value) { super(value); }
27  public int inc() { set(get()+1); return get(); }
28  public int reset() { set(0); return get(); }
29  }
30 
31  @SuppressWarnings("javadoc")
32  public static class Laskurit {
33  private List<Laskuri> alkiot = new ArrayList<>();
34  public void add(Laskuri alkio) { alkiot.add(alkio); }
35  public void reset() { alkiot.forEach(l -> l.reset()); }
36  }
37 
38  @FXML private Label laskuri;
39  @FXML private Button button;
40 
41  private Laskettava laskettava = new Laskettava(0);
42 
43  /** Luodaan laskuri */
44  public Laskuri() {
45  FXMLLoader loader = new FXMLLoader(getClass().getResource("LaskuriView.fxml"));
46  loader.setRoot(this);
47  loader.setController(this);
48  try {
49  loader.load();
50  }
51  catch (IOException ex) {
52  System.err.println(ex.getMessage());
53  }
54  }
55 
56 
57  @Override
58  public void initialize(URL location, ResourceBundle resources) {
59  laskuri.textProperty().bind(laskettava.asString());
60  laskuri.setOnMouseClicked(e -> laskettava.inc());
61  laskuri.setOnTouchPressed(e -> laskettava.inc());
62  button.setOnAction(e -> laskettava.inc());
63  }
64 
65 
66  /** Nollataan laskuri */
67  public void reset() { laskettava.reset(); }
68 
69  /** @return painikkeen teksti */
70  public String getCaption() { return button.getText(); }
71 
72  /** @param caption painikkeelle asetettava teksti */
73  public void setCaption(String caption) { button.setText(caption); }
74 }