StatisticPanel.java 33 KB

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