StatPanel2.java 26 KB

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