StatisticPanel.java 32 KB

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