SplitPane.java 29 KB

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