StatisticPanel.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. package ui.view;
  2. import DataSets.GraphDataSet;
  3. import DataSets.PropertyDataSet;
  4. import classes.*;
  5. import interfaces.GraphListener;
  6. import ui.controller.Control;
  7. import ui.controller.SingletonControl;
  8. import javax.swing.*;
  9. import javax.swing.border.EmptyBorder;
  10. import javax.swing.border.LineBorder;
  11. import javax.swing.event.DocumentEvent;
  12. import javax.swing.event.DocumentListener;
  13. import javax.swing.tree.DefaultMutableTreeNode;
  14. import javax.swing.tree.DefaultTreeModel;
  15. import java.awt.*;
  16. import java.util.ArrayList;
  17. import java.util.Enumeration;
  18. import java.util.Hashtable;
  19. import java.util.Random;
  20. public class StatisticPanel extends JSplitPane implements GraphListener {
  21. public static final String AVG_INACTIVE_ELEMENTS_HOLON = "average of inactive elements per Holon";
  22. public static final String AVG_PRODUCTION_HOLON = "average production per Holon";
  23. // for tracked objects
  24. public static final String TOT_PROD_OBJ = "total production";
  25. public static final String TOT_CONS_OBJ = "total consumption";
  26. public static final String NR_ACTIVE_ELEMENTS = "active elements";
  27. public static final String SW_ACTIVE = "active";
  28. public static final String TOT_PROD_GRID = "total Grid production";
  29. public static final String TOT_CONS_GRID = "total Grid consumption";
  30. private static final String MAIN_GRID = "Main Grid";
  31. private static final String HOLON = "Holons";
  32. // Property Strings
  33. // for whole grid
  34. private static final String TOT_PROD_HOLON = "total production";
  35. private static final String TOT_CONS_HOLON = "total consumption";
  36. private static final String TOT_WASTED_HOLON = "total wasted energy";
  37. private static final String SUPPLIED_OBJ = "supplied objects' percentage";
  38. private static final String NOT_SUPPLIED_OBJ = "not supplied objects' percentage";
  39. private static final String PART_SUPPLIED_OBJ = "partially supplied objects' percentage";
  40. private static final String NR_HOLONS = "nr. of Holons";
  41. private static final String NR_CLOSED_SWITCHES = "nr. of closed switches";
  42. private static final String AVG_OBJ_IN_HOLONS = "average amount of objects";
  43. private static final String AVG_ELEM_IN_HOLONS = "average amount of elements";
  44. private static final String AVG_PRODS_IN_HOLONS = "average amount of producers";
  45. private static final String AVG_CONS_ENERGY_IN_HOLONS = "average consumed energy";
  46. private static final String AVG_WASTED_ENERGY_HOLONS = "average wasted energy";
  47. private static final String NR_BROKEN_EDGES = "nr. of broken edges";
  48. private static final String PROD_CONS_RATIO = "producer/consumer ratio";
  49. private static final String AVG_CLOSED_SW_HOLON = "average of closed switches per Holon";
  50. private static final String AVG_ACTIVE_ELEMENTS_HOLON = "average of active elements per Holon";
  51. private DefaultTreeModel treeModel;
  52. private DefaultMutableTreeNode objectsNode;
  53. private DefaultMutableTreeNode mainGrid;
  54. private DefaultMutableTreeNode holonNode;
  55. private DefaultMutableTreeNode switchesNode;
  56. private DefaultMutableTreeNode groupNode;
  57. private Hashtable<String, GraphDataSet> objectHashtable;
  58. private Hashtable<String, StatisticGraphPanel> graphHashtable;
  59. private Hashtable<String, PropertyDataSet> holonHashtable;
  60. private Hashtable<String, Integer> propValTable;
  61. //contains information about a selected property
  62. private PropertyDataSet currentProperty = new PropertyDataSet();
  63. private Color defaultGraphColor = Color.RED;
  64. private Control controller;
  65. private JPanel graphPanel;
  66. //WindowBuilder Components
  67. private JTree objectTree;
  68. private JTextField graphNrTxtField;
  69. private JTextField redField;
  70. private JTextField greenField;
  71. private JTextField blueField;
  72. private JPanel colorPanel;
  73. private JComboBox colorComboBox;
  74. private JLabel showObjectlbl;
  75. private JLabel showPropertylbl;
  76. private JButton btnAdd;
  77. public StatisticPanel(Control cont) {
  78. setBorder(null);
  79. setMaximumSize(new Dimension(0, 0));
  80. setPreferredSize(new Dimension(0, 0));
  81. setMinimumSize(new Dimension(0, 0));
  82. this.setDividerLocation(180);
  83. setContinuousLayout(true);
  84. this.controller = cont;
  85. objectHashtable = new Hashtable<>();
  86. graphHashtable = new Hashtable<>();
  87. controller.setGraphTable(graphHashtable);
  88. holonHashtable = new Hashtable<>();
  89. holonHashtable.put(TOT_PROD_HOLON, new PropertyDataSet("", defaultGraphColor));
  90. holonHashtable.put(TOT_CONS_HOLON, new PropertyDataSet("", defaultGraphColor));
  91. holonHashtable.put(TOT_WASTED_HOLON, new PropertyDataSet("", defaultGraphColor));
  92. holonHashtable.put(SUPPLIED_OBJ, new PropertyDataSet("", defaultGraphColor));
  93. holonHashtable.put(NOT_SUPPLIED_OBJ, new PropertyDataSet("", defaultGraphColor));
  94. holonHashtable.put(PART_SUPPLIED_OBJ, new PropertyDataSet("", defaultGraphColor));
  95. holonHashtable.put(NR_HOLONS, new PropertyDataSet("", defaultGraphColor));
  96. holonHashtable.put(NR_CLOSED_SWITCHES, new PropertyDataSet("", defaultGraphColor));
  97. holonHashtable.put(AVG_OBJ_IN_HOLONS, new PropertyDataSet("", defaultGraphColor));
  98. holonHashtable.put(AVG_ELEM_IN_HOLONS, new PropertyDataSet("", defaultGraphColor));
  99. holonHashtable.put(AVG_PRODS_IN_HOLONS, new PropertyDataSet("", defaultGraphColor));
  100. holonHashtable.put(AVG_CONS_ENERGY_IN_HOLONS, new PropertyDataSet("", defaultGraphColor));
  101. holonHashtable.put(AVG_WASTED_ENERGY_HOLONS, new PropertyDataSet("", defaultGraphColor));
  102. holonHashtable.put(NR_BROKEN_EDGES, new PropertyDataSet("", defaultGraphColor));
  103. holonHashtable.put(PROD_CONS_RATIO, new PropertyDataSet("", defaultGraphColor));
  104. holonHashtable.put(AVG_CLOSED_SW_HOLON, new PropertyDataSet("", defaultGraphColor));
  105. holonHashtable.put(AVG_ACTIVE_ELEMENTS_HOLON, new PropertyDataSet("", defaultGraphColor));
  106. holonHashtable.put(AVG_INACTIVE_ELEMENTS_HOLON, new PropertyDataSet("", defaultGraphColor));
  107. holonHashtable.put(AVG_PRODUCTION_HOLON, new PropertyDataSet("", defaultGraphColor));
  108. //propValTable associates the Strings to the numbers
  109. propValTable = new Hashtable<>();
  110. propValTable.put(TOT_PROD_OBJ, TrackedDataSet.PRODUCTION);
  111. propValTable.put(TOT_CONS_OBJ, TrackedDataSet.CONSUMPTION);
  112. propValTable.put(TOT_WASTED_HOLON, TrackedDataSet.WASTED_ENERGY);
  113. propValTable.put(NR_ACTIVE_ELEMENTS, TrackedDataSet.ACTIVATED_ELEMENTS);
  114. propValTable.put(SW_ACTIVE, TrackedDataSet.ON_OFF);
  115. propValTable.put(TOT_PROD_HOLON, TrackedDataSet.TOTAL_PRODUCTION);
  116. propValTable.put(TOT_CONS_HOLON, TrackedDataSet.TOTAL_CONSUMPTION);
  117. propValTable.put(SUPPLIED_OBJ, TrackedDataSet.PERCENT_SUPPLIED);
  118. propValTable.put(NOT_SUPPLIED_OBJ, TrackedDataSet.PERCENT_NOT_SUPPLIED);
  119. propValTable.put(PART_SUPPLIED_OBJ, TrackedDataSet.PERCENT_PARTIAL_SUPPLIED);
  120. propValTable.put(TOT_PROD_GRID, TrackedDataSet.GROUP_PRODUCTION);
  121. propValTable.put(TOT_CONS_GRID, TrackedDataSet.GROUP_CONSUMPTION);
  122. propValTable.put(NR_HOLONS, TrackedDataSet.AMOUNT_HOLONS);
  123. propValTable.put(NR_CLOSED_SWITCHES, TrackedDataSet.AMOUNT_CLOSED_SWITCHES);
  124. propValTable.put(AVG_OBJ_IN_HOLONS, TrackedDataSet.AVG_AMOUNT_OBJECTS_IN_HOLONS);
  125. propValTable.put(AVG_ELEM_IN_HOLONS, TrackedDataSet.AVG_AMOUNT_ELEMENTS_IN_HOLONS);
  126. propValTable.put(AVG_PRODS_IN_HOLONS, TrackedDataSet.AVG_AMOUNT_PRODUCERS_IN_HOLONS);
  127. propValTable.put(AVG_CONS_ENERGY_IN_HOLONS, TrackedDataSet.AVG_CONSUMED_ENERGY_IN_HOLONS);
  128. propValTable.put(AVG_WASTED_ENERGY_HOLONS, TrackedDataSet.AVG_WASTED_ENERGY_IN_HOLONS);
  129. propValTable.put(NR_BROKEN_EDGES, TrackedDataSet.AMOUNT_BROKEN_EDGES);
  130. propValTable.put(PROD_CONS_RATIO, TrackedDataSet.RATIO_PRODUCERS_CONSUMERS);
  131. propValTable.put(AVG_CLOSED_SW_HOLON, TrackedDataSet.AVG_AMOUNT_CLOSED_SWITCHES_IN_HOLONS);
  132. propValTable.put(AVG_ACTIVE_ELEMENTS_HOLON, TrackedDataSet.AVG_AMOUNT_ACTIVE_ELEMENTS_IN_HOLONS);
  133. propValTable.put(AVG_INACTIVE_ELEMENTS_HOLON, TrackedDataSet.AVG_AMOUNT_INACTIVE_ELEMENTS_IN_HOLONS);
  134. propValTable.put(AVG_PRODUCTION_HOLON, TrackedDataSet.AVG_PRODUCED_ENERGY_IN_HOLONS);
  135. JScrollPane graphScrollPane = new JScrollPane();
  136. graphScrollPane.setBorder(null);
  137. setRightComponent(graphScrollPane);
  138. graphPanel = new JPanel();
  139. graphPanel.setLayout(new BoxLayout(graphPanel, BoxLayout.Y_AXIS));
  140. graphPanel.revalidate();
  141. graphPanel.updateUI();
  142. graphScrollPane.setViewportView(graphPanel);
  143. JSplitPane splitPane = new JSplitPane();
  144. splitPane.setResizeWeight(0.5);
  145. splitPane.setBorder(null);
  146. splitPane.setPreferredSize(new Dimension(0, 0));
  147. splitPane.setMinimumSize(new Dimension(0, 0));
  148. splitPane.setMaximumSize(new Dimension(0, 0));
  149. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  150. setLeftComponent(splitPane);
  151. //========================== TREE STRUCTURE ===============================//
  152. JScrollPane treeScrollPane = new JScrollPane();
  153. treeScrollPane.setBorder(null);
  154. objectTree = new JTree();
  155. objectTree.setBorder(new EmptyBorder(0, 0, 0, 0));
  156. treeModel = (DefaultTreeModel) objectTree.getModel();
  157. DefaultMutableTreeNode root = new DefaultMutableTreeNode("Statistics");
  158. mainGrid = new DefaultMutableTreeNode(MAIN_GRID);
  159. mainGrid.add(new DefaultMutableTreeNode(TOT_PROD_HOLON));
  160. mainGrid.add(new DefaultMutableTreeNode(TOT_CONS_HOLON));
  161. mainGrid.add(new DefaultMutableTreeNode(TOT_WASTED_HOLON));
  162. mainGrid.add(new DefaultMutableTreeNode(SUPPLIED_OBJ));
  163. mainGrid.add(new DefaultMutableTreeNode(NOT_SUPPLIED_OBJ));
  164. mainGrid.add(new DefaultMutableTreeNode(PART_SUPPLIED_OBJ));
  165. mainGrid.add(new DefaultMutableTreeNode(PROD_CONS_RATIO));
  166. mainGrid.add(new DefaultMutableTreeNode(NR_BROKEN_EDGES));
  167. mainGrid.add(new DefaultMutableTreeNode(NR_CLOSED_SWITCHES));
  168. holonNode = new DefaultMutableTreeNode(HOLON);
  169. holonNode.add(new DefaultMutableTreeNode(NR_HOLONS));
  170. holonNode.add(new DefaultMutableTreeNode(AVG_OBJ_IN_HOLONS));
  171. holonNode.add(new DefaultMutableTreeNode(AVG_ELEM_IN_HOLONS));
  172. holonNode.add(new DefaultMutableTreeNode(AVG_WASTED_ENERGY_HOLONS));
  173. holonNode.add(new DefaultMutableTreeNode(AVG_CONS_ENERGY_IN_HOLONS));
  174. holonNode.add(new DefaultMutableTreeNode(AVG_PRODS_IN_HOLONS));
  175. holonNode.add(new DefaultMutableTreeNode(AVG_CLOSED_SW_HOLON));
  176. holonNode.add(new DefaultMutableTreeNode(AVG_ACTIVE_ELEMENTS_HOLON));
  177. holonNode.add(new DefaultMutableTreeNode(AVG_INACTIVE_ELEMENTS_HOLON));
  178. holonNode.add(new DefaultMutableTreeNode(AVG_PRODUCTION_HOLON));
  179. objectsNode = new DefaultMutableTreeNode("Objects");
  180. objectsNode.add(new DefaultMutableTreeNode("empty"));
  181. switchesNode = new DefaultMutableTreeNode("Switches");
  182. switchesNode.add(new DefaultMutableTreeNode("empty"));
  183. groupNode = new DefaultMutableTreeNode("Groups");
  184. groupNode.add(new DefaultMutableTreeNode("empty"));
  185. treeModel.setRoot(root);
  186. root.add(mainGrid);
  187. root.add(holonNode);
  188. root.add(objectsNode);
  189. root.add(switchesNode);
  190. root.add(groupNode);
  191. objectTree.setModel(treeModel);
  192. treeScrollPane.setViewportView(objectTree);
  193. JPopupMenu popupMenu = new JPopupMenu();
  194. addPopup(objectTree, popupMenu);
  195. JMenuItem mntmUntrack = new JMenuItem("Untrack");
  196. mntmUntrack.addActionListener(actionEvent -> {
  197. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) objectTree
  198. .getLastSelectedPathComponent();
  199. if (selectedNode.getLevel() == 2 && !selectedNode.getParent().toString().equals("whole Holon")) {
  200. String object = selectedNode.toString();
  201. controller.removeTrackingObj(objectHashtable.get(object).getObject());
  202. }
  203. });
  204. popupMenu.add(mntmUntrack);
  205. objectTree.addTreeSelectionListener(treeSelectionEvent -> {
  206. resetFields();
  207. showObjectlbl.setText("...");
  208. //showPropertylbl.setText("...");
  209. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) objectTree
  210. .getLastSelectedPathComponent();
  211. if (selectedNode == null) {
  212. return;
  213. } else {
  214. if (selectedNode.getLevel() == 0) {
  215. disableFields();
  216. }
  217. if (selectedNode.getLevel() == 1) {
  218. disableFields();
  219. currentProperty = null;
  220. graphNrTxtField.setText("");
  221. showObjectlbl.setText(selectedNode.toString());
  222. }
  223. if (selectedNode.getLevel() == 2) {
  224. if (selectedNode.getParent().toString().equals(MAIN_GRID)
  225. || selectedNode.getParent().toString().equals(HOLON)) {
  226. enableFields();
  227. String object = selectedNode.getParent().toString();
  228. String property = selectedNode.toString();
  229. currentProperty = holonHashtable.get(property);
  230. Color color = currentProperty.getColor();
  231. redField.setText(Integer.toString(color.getRed()));
  232. greenField.setText(Integer.toString(color.getGreen()));
  233. blueField.setText(Integer.toString(color.getBlue()));
  234. showPropertylbl.setText(property);
  235. showObjectlbl.setText(object);
  236. graphNrTxtField.setText(currentProperty.getAssignedGraph());
  237. colorPanel.setBackground(color);
  238. } else {
  239. disableFields();
  240. currentProperty = null;
  241. graphNrTxtField.setText("");
  242. showObjectlbl.setText(selectedNode.toString());
  243. }
  244. }
  245. if (selectedNode.getLevel() == 3) {
  246. enableFields();
  247. String object = selectedNode.getParent().toString();
  248. String property = selectedNode.toString();
  249. currentProperty = objectHashtable.get(object).getPropertytTable().get(property);
  250. Color color = currentProperty.getColor();
  251. redField.setText(Integer.toString(color.getRed()));
  252. greenField.setText(Integer.toString(color.getGreen()));
  253. blueField.setText(Integer.toString(color.getBlue()));
  254. showObjectlbl.setText(object);
  255. showPropertylbl.setText(property);
  256. graphNrTxtField.setText(currentProperty.getAssignedGraph());
  257. colorPanel.setBackground(color);
  258. }
  259. }
  260. });
  261. splitPane.setLeftComponent(treeScrollPane);
  262. repaintTree();
  263. //=========================== TREE STRUCTURE END ==========================//
  264. //================= COLOR COMBOBOX ===========================//
  265. colorComboBox = new JComboBox();
  266. colorComboBox.addItem("Red");
  267. colorComboBox.addItem("Blue");
  268. colorComboBox.addItem("Green");
  269. colorComboBox.addItem("Yellow");
  270. colorComboBox.addItem("Orange");
  271. colorComboBox.addItem("Cyan");
  272. colorComboBox.addItem("Magenta");
  273. colorComboBox.addItem("Pink");
  274. colorComboBox.addItem("Gray");
  275. colorComboBox.addItem("Random");
  276. colorComboBox.addItemListener(itemEvent -> {
  277. String colorName = (String) colorComboBox.getSelectedItem();
  278. Color tmpColor = defaultGraphColor;
  279. switch (colorName) {
  280. case "Red":
  281. colorChanged(tmpColor);
  282. break;
  283. case "Blue":
  284. tmpColor = Color.BLUE;
  285. colorChanged(tmpColor);
  286. break;
  287. case "Green":
  288. tmpColor = Color.GREEN;
  289. colorChanged(tmpColor);
  290. break;
  291. case "Yellow":
  292. tmpColor = Color.YELLOW;
  293. colorChanged(tmpColor);
  294. break;
  295. case "Orange":
  296. tmpColor = Color.ORANGE;
  297. colorChanged(tmpColor);
  298. break;
  299. case "Cyan":
  300. tmpColor = Color.CYAN;
  301. colorChanged(tmpColor);
  302. break;
  303. case "Magenta":
  304. tmpColor = Color.MAGENTA;
  305. colorChanged(tmpColor);
  306. break;
  307. case "Pink":
  308. tmpColor = Color.PINK;
  309. colorChanged(tmpColor);
  310. break;
  311. case "Gray":
  312. tmpColor = Color.GRAY;
  313. colorChanged(tmpColor);
  314. break;
  315. case "Random":
  316. Random rdm = new Random();
  317. tmpColor = new Color(rdm.nextInt(255), rdm.nextInt(255), rdm.nextInt(255));
  318. }
  319. redField.setText(Integer.toString(tmpColor.getRed()));
  320. greenField.setText(Integer.toString(tmpColor.getGreen()));
  321. blueField.setText(Integer.toString(tmpColor.getBlue()));
  322. });
  323. // ==================== COLORBOX END ==========================//
  324. // ====================RED TEXTFIELD===========================//
  325. redField = new JTextField();
  326. redField.setColumns(10);
  327. redField.getDocument().addDocumentListener(new DocumentListener() {
  328. /*
  329. * if textField for Red changes, changes will applied in the
  330. * DataStructure "currentProperty" if Value is legit
  331. */
  332. @Override
  333. public void insertUpdate(DocumentEvent e) {
  334. int tmp = -1;
  335. try {
  336. tmp = Integer.parseInt(redField.getText());
  337. } catch (NumberFormatException e1) {
  338. }
  339. if (tmp > -1 && tmp <= 255) {
  340. if (currentProperty != null) {
  341. Color oldColor = currentProperty.getColor();
  342. Color color = new Color(tmp, oldColor.getGreen(), oldColor.getBlue());
  343. currentProperty.setColor(color);
  344. colorChanged(color);
  345. }
  346. }
  347. }
  348. @Override
  349. public void removeUpdate(DocumentEvent e) {
  350. int tmp = -1;
  351. try {
  352. tmp = Integer.parseInt(redField.getText());
  353. } catch (NumberFormatException e1) {
  354. }
  355. if (tmp > -1 && tmp <= 255) {
  356. if (currentProperty != null) {
  357. Color oldColor = currentProperty.getColor();
  358. Color color = new Color(tmp, oldColor.getGreen(), oldColor.getBlue());
  359. currentProperty.setColor(color);
  360. colorChanged(color);
  361. }
  362. }
  363. }
  364. @Override
  365. public void changedUpdate(DocumentEvent e) {
  366. }
  367. });
  368. // ======================RED TEXTFIELD END==========================//
  369. // ======================GREEN TEXTFIELD============================//
  370. greenField = new JTextField();
  371. greenField.setColumns(10);
  372. greenField.getDocument().addDocumentListener(new DocumentListener() {
  373. /*
  374. * if textField for Red changes, changes will applied in the
  375. * DataStructure "currentProperty" if Value is legit
  376. */
  377. @Override
  378. public void insertUpdate(DocumentEvent e) {
  379. int tmp = -1;
  380. try {
  381. tmp = Integer.parseInt(greenField.getText());
  382. } catch (NumberFormatException e1) {
  383. }
  384. if (tmp > -1 && tmp <= 255) {
  385. if (currentProperty != null) {
  386. Color oldColor = currentProperty.getColor();
  387. Color color = new Color(oldColor.getRed(), tmp, oldColor.getBlue());
  388. currentProperty.setColor(color);
  389. colorChanged(color);
  390. }
  391. }
  392. }
  393. @Override
  394. public void removeUpdate(DocumentEvent e) {
  395. int tmp = -1;
  396. try {
  397. tmp = Integer.parseInt(greenField.getText());
  398. } catch (NumberFormatException e1) {
  399. }
  400. if (tmp > -1 && tmp <= 255) {
  401. if (currentProperty != null) {
  402. Color oldColor = currentProperty.getColor();
  403. Color color = new Color(oldColor.getRed(), tmp, oldColor.getBlue());
  404. currentProperty.setColor(color);
  405. colorChanged(color);
  406. }
  407. }
  408. }
  409. @Override
  410. public void changedUpdate(DocumentEvent e) {
  411. }
  412. });
  413. // ======================GREEN TEXTFIELD END========================//
  414. // ======================BLUE TEXTFIELD=============================//
  415. blueField = new JTextField();
  416. blueField.setColumns(10);
  417. blueField.getDocument().addDocumentListener(new DocumentListener() {
  418. /*
  419. * if textField for Red changes, changes will applied in the
  420. * DataStructure "currentProperty" if Value is legit
  421. */
  422. @Override
  423. public void insertUpdate(DocumentEvent e) {
  424. int tmp = -1;
  425. try {
  426. tmp = Integer.parseInt(blueField.getText());
  427. } catch (NumberFormatException e1) {
  428. }
  429. if (tmp > -1 && tmp <= 255) {
  430. if (currentProperty != null) {
  431. Color oldColor = currentProperty.getColor();
  432. Color color = new Color(oldColor.getRed(), oldColor.getGreen(), tmp);
  433. currentProperty.setColor(color);
  434. colorChanged(color);
  435. }
  436. }
  437. }
  438. @Override
  439. public void removeUpdate(DocumentEvent e) {
  440. int tmp = -1;
  441. try {
  442. tmp = Integer.parseInt(blueField.getText());
  443. } catch (NumberFormatException e1) {
  444. }
  445. if (tmp > -1 && tmp <= 255) {
  446. if (currentProperty != null) {
  447. Color oldColor = currentProperty.getColor();
  448. Color color = new Color(oldColor.getRed(), oldColor.getGreen(), tmp);
  449. currentProperty.setColor(color);
  450. colorChanged(color);
  451. }
  452. }
  453. }
  454. @Override
  455. public void changedUpdate(DocumentEvent e) {
  456. }
  457. });
  458. // ======================BLUE TEXTFIELD END=========================//
  459. // ======================GRAPH NR TEXTFIELD=========================//
  460. graphNrTxtField = new JTextField();
  461. graphNrTxtField.setColumns(10);
  462. graphNrTxtField.getDocument().addDocumentListener(new DocumentListener() {
  463. /*
  464. * if textField for Red changes, changes will applied in the
  465. * DataStructure "currentProperty" if Value is legit
  466. */
  467. @Override
  468. public void insertUpdate(DocumentEvent e) {
  469. if (currentProperty != null) {
  470. currentProperty.setGraph(graphNrTxtField.getText());
  471. }
  472. }
  473. @Override
  474. public void removeUpdate(DocumentEvent e) {
  475. if (currentProperty != null) {
  476. currentProperty.setGraph(graphNrTxtField.getText());
  477. }
  478. }
  479. @Override
  480. public void changedUpdate(DocumentEvent e) {
  481. }
  482. });
  483. // ======================= GRAPH NR TEXTFIELD END==================//
  484. //======================== ADD BUTTON =============================//
  485. JButton btnAdd = new JButton("Add");
  486. btnAdd.addActionListener(actionEvent -> {
  487. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) objectTree
  488. .getLastSelectedPathComponent();
  489. if (selectedNode != null) {
  490. int nodeLevel = selectedNode.getLevel();
  491. String parentString = selectedNode.getParent().toString();
  492. if (nodeLevel == 3 ||
  493. (nodeLevel == 2 && (parentString.equals(HOLON) || parentString.equals(MAIN_GRID)))) {
  494. StatisticGraphPanel tmp;
  495. if (graphNrTxtField.getText().length() > 0) {
  496. if (!graphHashtable.containsKey(graphNrTxtField.getText())
  497. && graphNrTxtField.getText().length() > 0) {
  498. tmp = new StatisticGraphPanel(controller.getModel(), controller,
  499. graphNrTxtField.getText(), graphHashtable);
  500. graphPanel.add(tmp);
  501. graphPanel.add(Box.createRigidArea(new Dimension(50, 50)));
  502. graphPanel.revalidate();
  503. graphPanel.updateUI();
  504. graphHashtable.put(graphNrTxtField.getText(), tmp);
  505. }
  506. String object = selectedNode.getParent().toString();
  507. String property = selectedNode.toString();
  508. AbstractCanvasObject absCps = null;
  509. if (!object.equals(MAIN_GRID) && !object.equals(HOLON)) {
  510. GraphDataSet dataSet = objectHashtable.get(object);
  511. absCps = dataSet.getObject();
  512. }
  513. TrackedDataSet tds = new TrackedDataSet(absCps, propValTable.get(property),
  514. currentProperty.getColor(),cont.getModel().getIterations());
  515. graphHashtable.get(graphNrTxtField.getText()).addObject(tds);
  516. }
  517. }
  518. }
  519. });
  520. //============================== ADD BUTTON END ==============================//
  521. //=========================== WINDOWBUILDER COMPONENTS ====================//
  522. JScrollPane scrollPane = new JScrollPane();
  523. scrollPane.setBorder(null);
  524. splitPane.setRightComponent(scrollPane);
  525. JPanel editPanel = new JPanel();
  526. //editPanel.setBounds(0,0,600,600);
  527. editPanel.setPreferredSize(new Dimension(170, 240));
  528. //scrollPane.setBounds(0,0,600,600);
  529. scrollPane.setViewportView(editPanel);
  530. editPanel.setLayout(null);
  531. JLabel lblObject = new JLabel("Object(s):");
  532. lblObject.setBounds(10, 11, 59, 20);
  533. editPanel.add(lblObject);
  534. showObjectlbl = new JLabel("...");
  535. showObjectlbl.setBounds(69, 11, 101, 20);
  536. editPanel.add(showObjectlbl);
  537. JLabel lblProperty = new JLabel("Property:");
  538. lblProperty.setBounds(10, 36, 59, 20);
  539. editPanel.add(lblProperty);
  540. showPropertylbl = new JLabel("...");
  541. showPropertylbl.setBounds(69, 36, 101, 20);
  542. editPanel.add(showPropertylbl);
  543. JLabel lblGraph = new JLabel("Graph:");
  544. lblGraph.setBounds(10, 61, 49, 23);
  545. editPanel.add(lblGraph);
  546. graphNrTxtField.setColumns(10);
  547. graphNrTxtField.setBounds(69, 61, 101, 23);
  548. editPanel.add(graphNrTxtField);
  549. JLabel lblColor = new JLabel("Color:");
  550. lblColor.setBounds(10, 95, 49, 23);
  551. editPanel.add(lblColor);
  552. colorComboBox.setBounds(69, 95, 101, 23);
  553. editPanel.add(colorComboBox);
  554. JLabel lblR = new JLabel("R");
  555. lblR.setBounds(10, 139, 11, 14);
  556. editPanel.add(lblR);
  557. redField.setColumns(10);
  558. redField.setBounds(22, 136, 37, 20);
  559. editPanel.add(redField);
  560. JLabel lblG = new JLabel("G");
  561. lblG.setBounds(68, 139, 11, 14);
  562. editPanel.add(lblG);
  563. greenField.setColumns(10);
  564. greenField.setBounds(79, 136, 37, 20);
  565. editPanel.add(greenField);
  566. JLabel lblB = new JLabel("B");
  567. lblB.setBounds(126, 139, 10, 14);
  568. editPanel.add(lblB);
  569. blueField.setColumns(10);
  570. blueField.setBounds(133, 136, 37, 20);
  571. editPanel.add(blueField);
  572. colorPanel = new JPanel();
  573. colorPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
  574. colorPanel.setBackground(defaultGraphColor);
  575. colorPanel.setBounds(10, 164, 59, 38);
  576. editPanel.add(colorPanel);
  577. btnAdd.setBounds(10, 213, 59, 23);
  578. editPanel.add(btnAdd);
  579. //============================= WINDOWBUILDER COMPONENTS END =================//
  580. }
  581. //=============================== END CONSTRUCTOR ==============================//
  582. private static void addPopup(Component component, final JPopupMenu popup) {
  583. }
  584. //=============================== METHODS ======================================//
  585. @Override
  586. public void repaintTree() {
  587. treeModel.reload();
  588. }
  589. @Override
  590. public void addTrackedObject(ArrayList<AbstractCanvasObject> hlList) {
  591. objectsNode.removeAllChildren();
  592. switchesNode.removeAllChildren();
  593. groupNode.removeAllChildren();
  594. objectHashtable.clear();
  595. if (hlList.size() > 0 && hlList != null) {
  596. for (AbstractCanvasObject abs : hlList) {
  597. String name = abs.getName() + " " + abs.getId();
  598. DefaultMutableTreeNode tmp = new DefaultMutableTreeNode(name);
  599. Hashtable<String, PropertyDataSet> tmpHash = new Hashtable<>();
  600. //HolonObjects
  601. if (abs instanceof HolonObject) {
  602. tmp.add(new DefaultMutableTreeNode(TOT_PROD_OBJ));
  603. tmp.add(new DefaultMutableTreeNode(TOT_CONS_OBJ));
  604. tmp.add(new DefaultMutableTreeNode(NR_ACTIVE_ELEMENTS));
  605. tmpHash.put(TOT_PROD_OBJ, new PropertyDataSet());
  606. tmpHash.put(TOT_CONS_OBJ, new PropertyDataSet());
  607. tmpHash.put(NR_ACTIVE_ELEMENTS, new PropertyDataSet());
  608. objectsNode.add(tmp);
  609. }
  610. //HolonSwitches
  611. if (abs instanceof HolonSwitch) {
  612. tmp.add(new DefaultMutableTreeNode(SW_ACTIVE));
  613. tmpHash.put(SW_ACTIVE, new PropertyDataSet());
  614. switchesNode.add(tmp);
  615. }
  616. //NodesOfNodes
  617. if (abs instanceof GroupNode) {
  618. tmp.add(new DefaultMutableTreeNode(TOT_PROD_GRID));
  619. tmp.add(new DefaultMutableTreeNode(TOT_CONS_GRID));
  620. tmpHash.put(TOT_PROD_GRID, new PropertyDataSet());
  621. tmpHash.put(TOT_CONS_GRID, new PropertyDataSet());
  622. groupNode.add(tmp);
  623. }
  624. GraphDataSet gS = new GraphDataSet(abs, tmpHash);
  625. objectHashtable.put(name, gS);
  626. }
  627. }
  628. if (objectsNode.getChildCount() == 0) {
  629. objectsNode.add(new DefaultMutableTreeNode("empty"));
  630. }
  631. if (switchesNode.getChildCount() == 0) {
  632. switchesNode.add(new DefaultMutableTreeNode("empty"));
  633. }
  634. if (groupNode.getChildCount() == 0) {
  635. groupNode.add(new DefaultMutableTreeNode("empty"));
  636. }
  637. }
  638. private void colorChanged(Color color) {
  639. colorPanel.setBackground(color);
  640. }
  641. private void resetFields() {
  642. colorPanel.setBackground(defaultGraphColor);
  643. redField.setText("");
  644. greenField.setText("");
  645. blueField.setText("");
  646. colorComboBox.setSelectedIndex(0);
  647. }
  648. private void disableFields() {
  649. redField.setEnabled(false);
  650. greenField.setEnabled(false);
  651. blueField.setEnabled(false);
  652. graphNrTxtField.setEnabled(false);
  653. colorComboBox.setEnabled(false);
  654. colorPanel.setBackground(Color.LIGHT_GRAY);
  655. }
  656. private void enableFields() {
  657. redField.setEnabled(true);
  658. greenField.setEnabled(true);
  659. blueField.setEnabled(true);
  660. graphNrTxtField.setEnabled(true);
  661. colorComboBox.setEnabled(true);
  662. colorPanel.setBackground(defaultGraphColor);
  663. }
  664. void repaintGraphs() {
  665. for (StatisticGraphPanel sg : graphHashtable.values()) {
  666. sg.calcMaximum();
  667. sg.addValues();
  668. sg.repaint();
  669. }
  670. }
  671. JPanel getGraphPanel() {
  672. return graphPanel;
  673. }
  674. public void updateIterations() {
  675. for(Enumeration<StatisticGraphPanel> dataEnum=graphHashtable.elements();dataEnum.hasMoreElements();){
  676. dataEnum.nextElement().getStatGraph().updateIterations();
  677. }
  678. }
  679. }