StatPanel2.java 27 KB

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