CertainTrustSimpleHTI.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * CertainTrust SDK
  3. *
  4. * Implements the computational trust model "CertainTrust"
  5. * in Java.
  6. * See <http://www.tk.informatik.tu-darmstadt.de/de/research/smart-security-and-trust/> for further details.
  7. *
  8. *
  9. * Telecooperation Department, Technische Universität Darmstadt
  10. * <http://www.tk.informatik.tu-darmstadt.de/>
  11. *
  12. * Prof. Dr. Max Mühlhäuser <max@informatik.tu-darmstadt.de>
  13. * Florian Volk <florian.volk@cased.de>
  14. *
  15. *
  16. * @author David Kalnischkies
  17. * @author Florian Volk
  18. * @version 1.0
  19. */
  20. /* This Source Code Form is subject to the terms of the Mozilla Public
  21. * License, v. 2.0. If a copy of the MPL was not distributed with this
  22. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  23. package CertainTrust;
  24. import java.awt.BorderLayout;
  25. import java.awt.GridLayout;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.FocusEvent;
  29. import java.awt.event.FocusListener;
  30. import java.text.DecimalFormat;
  31. import java.text.NumberFormat;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import java.util.Observable;
  35. import java.util.Observer;
  36. import javax.swing.BoxLayout;
  37. import javax.swing.JLabel;
  38. import javax.swing.JPanel;
  39. import javax.swing.JTextField;
  40. import javax.swing.border.EmptyBorder;
  41. /**
  42. * The CertainTrustHTI includes a formular to change
  43. * the C, T and F values of the given CertainTrust
  44. * element and a canvas displaying the expectation
  45. * calculated from these values
  46. */
  47. public class CertainTrustSimpleHTI extends JPanel implements Observer {
  48. private static final long serialVersionUID = -1194437596328826222L;
  49. /**
  50. * Creates a new HTI to display a CertainTrustSimple element
  51. *
  52. * @param certainlogic element to display
  53. */
  54. public CertainTrustSimpleHTI(CertainTrustSimple certainlogic) {
  55. setup(certainlogic, null);
  56. }
  57. /**
  58. * Creates a new HTI to display a CertainTrustSimple element
  59. *
  60. * Supported config options are:
  61. * - label.lang: language for the text elements (available: "de" and "en" (default))
  62. * - label.f, label.c, label.t and label.e to set the text explicitly
  63. * - readonly: to effect the complete HTI (value either "true" or "false") and
  64. * - readonly.f, readonly.c, readonly.t, readonly.e to set explicitly
  65. * See also {@link CertainTrustCanvas#CertainTrustCanvas(CertainTrust, Map)} for
  66. * additional options which are accepted by the canvas element
  67. *
  68. * @param certainlogic element to display
  69. * @param config to use for this HTI
  70. */
  71. public CertainTrustSimpleHTI(CertainTrustSimple certainlogic, Map<String, String> config) {
  72. setup(certainlogic, config);
  73. }
  74. private CertainTrustSimple cl;
  75. private JTextField f;
  76. private JTextField c;
  77. private JTextField t;
  78. private JTextField e;
  79. private CertainTrustSimpleCanvas ctc;
  80. private Map<String,String> config;
  81. protected CertainTrustSimpleCanvas getCanvas() { return ctc; }
  82. private static JTextField appendInput(JPanel p, String name, char mnemonic, double value, String readOnly, CertainTrustActionFocusListener listener) {
  83. JLabel l = new JLabel(name);
  84. l.setBorder(new EmptyBorder(0, 0, 0, (int) (l.getFont().getSize() * 1.5)));
  85. final JTextField f = new JTextField(CertainTrustSimpleHTI.formatFloat(value), 4);
  86. if (readOnly.compareToIgnoreCase("true") == 0) {
  87. f.setEditable(false);
  88. f.setFocusable(false);
  89. }
  90. else if (readOnly.compareToIgnoreCase("false") != 0)
  91. throw new IllegalArgumentException("Input element " + name + " has no valid readonly setting");
  92. else if (listener != null) {
  93. f.addActionListener(listener);
  94. f.addFocusListener(listener);
  95. }
  96. l.setLabelFor(f);
  97. if (mnemonic != '\0')
  98. l.setDisplayedMnemonic(mnemonic);
  99. p.add(l);
  100. p.add(f);
  101. return f;
  102. }
  103. private abstract class CertainTrustActionFocusListener implements ActionListener, FocusListener {
  104. @Override
  105. public void focusGained(FocusEvent arg0) {}
  106. @Override
  107. public void focusLost(FocusEvent arg0) {
  108. update();
  109. }
  110. @Override
  111. public void actionPerformed(ActionEvent arg0) {
  112. update();
  113. }
  114. abstract void update();
  115. /**
  116. * parseInput tries to convert a textual input into a floating point number in [0;1]
  117. * It accepts both "." and "," as decimal delimiters.
  118. * If the conversion fails, a default value is used.
  119. * @param input the textual input to convert into a floating point number
  120. * @param defaultValue the default value used in case the conversion fails
  121. * @return the floating point value generated from the textual input
  122. */
  123. protected double parseInput(String input, double defaultValue) {
  124. double result;
  125. try {
  126. // first, replace the first "," with a "." to understand German-language style floating point notation
  127. String rawInput = input.replaceFirst(",", ".");
  128. result = Double.parseDouble(rawInput);
  129. // check, if the value in within our bounds of [0;1]
  130. if ((1 < result) || (0 > result)) {
  131. result = defaultValue;
  132. }
  133. }
  134. catch (NumberFormatException e) {
  135. result = defaultValue;
  136. }
  137. //------------------modified by Debashis------------//
  138. if(result == 1){
  139. result = 0.999;
  140. }
  141. return result;
  142. }
  143. }
  144. private void setup(CertainTrustSimple certainlogic, Map<String,String> config) {
  145. cl = certainlogic;
  146. if (config != null)
  147. this.config = config;
  148. else
  149. this.config = new HashMap<String,String>();
  150. if (this.config.containsKey("label.lang") == false)
  151. this.config.put("label.lang", "en");
  152. if (this.config.get("label.lang") == "de") {
  153. if (this.config.containsKey("label.f") == false)
  154. this.config.put("label.f", "Initialwert");
  155. if (this.config.containsKey("label.t") == false)
  156. this.config.put("label.t", "Vertrauen");
  157. if (this.config.containsKey("label.c") == false)
  158. this.config.put("label.c", "Sicherheit");
  159. if (this.config.containsKey("label.e") == false)
  160. this.config.put("label.e", "Erwartung");
  161. } else {
  162. if (this.config.containsKey("label.f") == false)
  163. this.config.put("label.f", "Init. value");
  164. if (this.config.containsKey("label.t") == false)
  165. this.config.put("label.t", "Trust");
  166. if (this.config.containsKey("label.c") == false)
  167. this.config.put("label.c", "Certainty");
  168. if (this.config.containsKey("label.e") == false)
  169. this.config.put("label.e", "Expectation");
  170. }
  171. if (this.config.containsKey("readonly") == false) {
  172. if (this.config.containsKey("readonly.f") == false)
  173. this.config.put("readonly.f", "false");
  174. if (this.config.containsKey("readonly.t") == false)
  175. this.config.put("readonly.t", "false");
  176. if (this.config.containsKey("readonly.c") == false)
  177. this.config.put("readonly.c", "false");
  178. if (this.config.containsKey("readonly.e") == false)
  179. this.config.put("readonly.e", "true");
  180. } else {
  181. String readOnly = this.config.get("readonly");
  182. if (this.config.containsKey("readonly.f") == false)
  183. this.config.put("readonly.f", readOnly);
  184. if (this.config.containsKey("readonly.t") == false)
  185. this.config.put("readonly.t", readOnly);
  186. if (this.config.containsKey("readonly.c") == false)
  187. this.config.put("readonly.c", readOnly);
  188. if (this.config.containsKey("readonly.e") == false)
  189. this.config.put("readonly.e", readOnly);
  190. }
  191. JPanel inputs = new JPanel(new GridLayout(5,2));
  192. f = appendInput(inputs, this.config.get("label.f"), 'f', cl.getF(), this.config.get("readonly.f"),
  193. new CertainTrustActionFocusListener() {
  194. @Override
  195. void update() {
  196. cl.setF(this.parseInput(f.getText(), cl.getF()));
  197. }
  198. });
  199. t = appendInput(inputs, this.config.get("label.t"), 't', cl.getT(), this.config.get("readonly.t"),
  200. new CertainTrustActionFocusListener() {
  201. @Override
  202. void update() {
  203. cl.setTC(this.parseInput(t.getText(), cl.getT()),
  204. cl.getC());
  205. }
  206. });
  207. c = appendInput(inputs, this.config.get("label.c"), 'c', cl.getC(), this.config.get("readonly.c"),
  208. new CertainTrustActionFocusListener() {
  209. @Override
  210. void update() {
  211. cl.setTC(cl.getT(),
  212. this.parseInput(c.getText(), cl.getC()));
  213. }
  214. });
  215. JLabel ignore0 = new JLabel("");
  216. inputs.add(ignore0);
  217. JLabel ignore1 = new JLabel("");
  218. inputs.add(ignore1);
  219. e = appendInput(inputs, this.config.get("label.e"),'\0', cl.getExpectation(), this.config.get("readonly.e"), null);
  220. this.add(inputs);
  221. JPanel jp = new JPanel();
  222. jp.setLayout(new BoxLayout(jp, BoxLayout.PAGE_AXIS));
  223. JPanel jp1 = new JPanel();
  224. JLabel certain = new JLabel(this.config.get("label.c") + "\u00a0\u2192");
  225. certain.setUI(new VerticalLabelUI());
  226. jp1.add(certain);
  227. ctc = new CertainTrustSimpleCanvas(cl, this.config);
  228. ctc.repaint();
  229. jp1.add(ctc);
  230. JPanel jp2 = new JPanel(new BorderLayout());
  231. JLabel origin = new JLabel("0");
  232. int fontsize = certain.getFont().getSize();
  233. origin.setBorder(new EmptyBorder(0, fontsize, 0, fontsize));
  234. jp2.add(origin, BorderLayout.LINE_START);
  235. JLabel trust = new JLabel(this.config.get("label.t") + "\u00a0\u2192");
  236. jp2.add(trust, BorderLayout.CENTER);
  237. jp.add(jp1);
  238. jp.add(jp2);
  239. this.add(jp);
  240. this.setVisible(true);
  241. cl.addObserver(this);
  242. }
  243. @Override
  244. public void update(Observable ignored0, Object ignored1) {
  245. f.setText(CertainTrustSimpleHTI.formatFloat(cl.getF()));
  246. t.setText(CertainTrustSimpleHTI.formatFloat(cl.getT()));
  247. c.setText(CertainTrustSimpleHTI.formatFloat(cl.getC()));
  248. e.setText(CertainTrustSimpleHTI.formatFloat(cl.getExpectation()));
  249. }
  250. /**
  251. * Rounds a number to at most 3 decimal places and converts it to a string
  252. * @param value the number to round
  253. * @return the rounded value as string
  254. */
  255. static private String formatFloat(double value) {
  256. // the explicit type cast from Long to double is required, otherwise rounded will always be 0
  257. double rounded = ((double)Math.round(value * 1000) / 1000);
  258. // Use NumerFormat to omit trailing 0s as those which Double.toString() produces
  259. NumberFormat formatter = new DecimalFormat("0.###");
  260. String result = formatter.format(rounded);
  261. // for compatibility with the JS version, our decimal delimiter *always* is "."
  262. result = result.replaceFirst(",", ".");
  263. return result;
  264. }
  265. }