StatPanel2.java 27 KB

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