StatisticPanel.java 28 KB

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