ConstantValueDistributionHandler.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JTextField;
  8. import org.apache.commons.math3.random.RandomGenerator;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
  10. /**
  11. * Example class representing a Distribution, with only one constant value
  12. *
  13. * @author Andreas T. Meyer-Berg
  14. */
  15. public class ConstantValueDistributionHandler implements
  16. ProbabilityDistributionHandler {
  17. /**
  18. * Fixed value this distribution returns
  19. */
  20. private long fixedValue;
  21. /**
  22. * Creates a new distribution which returns the given value
  23. * @param value value to be returned
  24. */
  25. public ConstantValueDistributionHandler(long value) {
  26. fixedValue = value;
  27. }
  28. /**
  29. * Creates a default distribution, which returns 50.
  30. */
  31. public ConstantValueDistributionHandler() {
  32. fixedValue = 50;
  33. }
  34. @Override
  35. public void setRandomGenerator(RandomGenerator rng) {
  36. // Fixed Value requires no randomness
  37. }
  38. @Override
  39. public long sampleNextValue() {
  40. return fixedValue;
  41. }
  42. /**
  43. * Sets the sample value to the given one
  44. * @param value new fixed value
  45. */
  46. public void setValue(long value){
  47. this.fixedValue = value;
  48. }
  49. @Override
  50. public JPanel getConfigurationPanel() {
  51. /**
  52. * JPanel which allows configuration of this Distribution
  53. */
  54. JPanel panel = new JPanel();
  55. panel.setMinimumSize(new Dimension(90, 40));
  56. panel.setLayout(null);
  57. /**
  58. * Label
  59. */
  60. JLabel label = new JLabel("Value: ");
  61. panel.add(label);
  62. label.setLocation(20, 20);
  63. label.setSize(50,20);
  64. label.setMinimumSize(new Dimension(50, 20));
  65. /**
  66. * Textfield for changing the value
  67. */
  68. JTextField valueConfig = new JTextField(""+fixedValue);
  69. panel.add(valueConfig);
  70. valueConfig.setLocation(80, 20);
  71. valueConfig.setMinimumSize(new Dimension(32, 20));
  72. valueConfig.setSize(32, 20);
  73. valueConfig.addActionListener(a->{
  74. try {
  75. /**
  76. * Update the value
  77. */
  78. fixedValue = Long.parseLong(valueConfig.getText());
  79. valueConfig.setBackground(Color.WHITE);
  80. } catch (Exception e) {
  81. valueConfig.setBackground(Color.RED);
  82. }
  83. });
  84. return panel;
  85. }
  86. public static void main(String[] args) {
  87. ConstantValueDistributionHandler c = new ConstantValueDistributionHandler(12);
  88. JFrame test = new JFrame("test");
  89. test.setSize(400, 400);
  90. test.add(c.getConfigurationPanel());
  91. test.setEnabled(true);
  92. test.setVisible(true);
  93. }
  94. @Override
  95. public String getSimpleDescription() {
  96. return "Constant Value";
  97. }
  98. }