GUI.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Cursor;
  6. import java.awt.Dimension;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.beans.PropertyChangeEvent;
  15. import java.beans.PropertyChangeListener;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import javax.swing.BoxLayout;
  20. import javax.swing.DefaultComboBoxModel;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JButton;
  23. import javax.swing.JCheckBoxMenuItem;
  24. import javax.swing.JComboBox;
  25. import javax.swing.JEditorPane;
  26. import javax.swing.JFileChooser;
  27. import javax.swing.JFrame;
  28. import javax.swing.JLabel;
  29. import javax.swing.JMenu;
  30. import javax.swing.JMenuBar;
  31. import javax.swing.JMenuItem;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.JPanel;
  34. import javax.swing.JPopupMenu;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JSlider;
  37. import javax.swing.JSplitPane;
  38. import javax.swing.JTabbedPane;
  39. import javax.swing.JTable;
  40. import javax.swing.JTextField;
  41. import javax.swing.JToolBar;
  42. import javax.swing.JTree;
  43. import javax.swing.SwingUtilities;
  44. import javax.swing.border.LineBorder;
  45. import javax.swing.event.ChangeEvent;
  46. import javax.swing.event.ChangeListener;
  47. import javax.swing.filechooser.FileNameExtensionFilter;
  48. import javax.swing.table.DefaultTableModel;
  49. import javax.swing.tree.DefaultMutableTreeNode;
  50. import javax.swing.tree.DefaultTreeModel;
  51. import javax.swing.tree.TreeCellRenderer;
  52. import Interfaces.CategoryListener;
  53. import classes.Category;
  54. import classes.CpsEdge;
  55. import classes.CpsObject;
  56. import classes.HolonElement;
  57. import classes.HolonObject;
  58. import classes.HolonSwitch;
  59. import classes.HolonTransformer;
  60. import ui.controller.Control;
  61. import ui.controller.SimulationManager;
  62. import ui.model.Model;;
  63. public class GUI<E> implements CategoryListener {
  64. private JFrame frmCyberPhysical;
  65. private final JMenuBar menuBar = new JMenuBar();
  66. private final JMenu mnNewMenu = new JMenu("File");
  67. private final JMenu mnNewMenu_1 = new JMenu("Edit");
  68. private final JMenu mnNewMenu_2 = new JMenu("Options");
  69. private final JMenu mnNewMenu_3 = new JMenu("View");
  70. private final JMenu mnHelp = new JMenu("Help");
  71. private final JMenuItem mntmOpen = new JMenuItem("Open");
  72. private final JMenuItem mntmNew = new JMenuItem("New");
  73. private final JMenuItem mntmSave = new JMenuItem("Save");
  74. private final JMenuItem aboutUs = new JMenuItem("About Us");
  75. private final JMenuItem canvasSize = new JMenuItem("View Size");
  76. private final JSplitPane splitPane = new JSplitPane();
  77. private final JSplitPane splitPane_1 = new JSplitPane();
  78. private final JScrollPane scrollPane_1 = new JScrollPane();
  79. private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  80. private final JScrollPane scrollPane_2 = new JScrollPane();
  81. private JPopupMenu popmenuEdit = new JPopupMenu();
  82. private JMenuItem editItem = new JMenuItem("Edit Object");
  83. private String catOfObjToBeEdited;
  84. private final JLabel maxGraph = new JLabel("100%");
  85. private final JLabel medGraph = new JLabel("50%");
  86. private final JLabel minGraph = new JLabel("0%");
  87. private final JLabel elementGraph = new JLabel("None ");
  88. private final ArrayList<HolonElement> selectedElements = new ArrayList<HolonElement>();
  89. private String holonEleNamesDisplayed = "None ";
  90. private final JTree tree = new JTree();
  91. private final JEditorPane dtrpnHereWillBe = new JEditorPane();
  92. /******************************************
  93. ************* Right Container*************
  94. ******************************************/
  95. // Right Container: here comes the information about the HolonObject, such
  96. // as HolonElements Information, Properties and Consumption/Production graph
  97. private final JSplitPane split_HolonEl_Pro = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  98. private final JSplitPane split_Graph_HolonEl = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  99. // In this section are all the Holonelements that correspond to the clicked
  100. // HolonObject with consumption/production, name and amount.
  101. private JTable tableHolonElement = new JTable();
  102. private PropertyTable tableModelHolonElement = new PropertyTable();
  103. private final JPanel scrollElements = new JPanel();
  104. private JScrollPane tableHolonElementScrollPane = new JScrollPane();
  105. // In this section are all the properties that correspond to the clicked
  106. // HolonObject, such as connections, name, Type, etc.
  107. private JTable tableProperties = new JTable();
  108. private JPanel graphLabel = new JPanel();
  109. private DefaulTable tableModelProperties;
  110. private final JScrollPane scrollProperties = new JScrollPane();
  111. // In this section is the graph for the selected HolonElement of the clicked
  112. // HolonObject
  113. private JTable tableGraph = new JTable();
  114. private DefaultTableModel tableModelGraph = new DefaultTableModel();
  115. private final JScrollPane scrollGraph = new JScrollPane();
  116. private Model model;
  117. private final Control controller;
  118. // Pop up Windows
  119. private AddObjectPopUp addObjectPopUP;
  120. private AboutUsPopUp aboutUsPopUp;
  121. private AddElementPopUp addElementPopUp;
  122. private final JPanel panel = new JPanel();
  123. private final JPanel panel_HolonEl = new JPanel();
  124. private final JComboBox comboBox = new JComboBox();
  125. // private final JComboBox comboBoxGraph = new JComboBox();
  126. // Buttons
  127. private final JButton btnAdd = new JButton("+");
  128. private final JButton btnDel = new JButton("-");
  129. private final JButton btnAddHolEL = new JButton("+");
  130. private final JButton btnDelHolEL = new JButton("-");
  131. private final JButton resetGraphBtn = new JButton("Reset");
  132. private final JToolBar toolBar = new JToolBar();
  133. private final JToolBar toolBarHolonEl = new JToolBar();
  134. private final JToolBar toolBarGraph = new JToolBar();
  135. // variables
  136. private boolean dragging = false;
  137. private String actualObjectClicked;
  138. private Image img = null;
  139. private CpsObject tempCps = null;
  140. private HolonElement tempElement = null;
  141. private int yValueElements = 0;
  142. private MyCanvas canvas;
  143. private UnitGraph unitGraph;
  144. private final JSplitPane splitPane_3 = new JSplitPane();
  145. private final JSlider sizeSlider = new JSlider();
  146. private final JLabel lblImageSize = new JLabel("Image Size");
  147. // Time Stuff
  148. private TimePanel timePanel;
  149. private final JMenu mnAlgorithm = new JMenu("Algorithm");
  150. private final JCheckBoxMenuItem chckbxmntmUseAlgorithm = new JCheckBoxMenuItem("Use Algorithm");
  151. private final JSplitPane splitPane_2 = new JSplitPane();
  152. private final JLabel lblSelect = new JLabel("Select");
  153. private final JComboBox comboBoxAlgo = new JComboBox();
  154. private int yTHIS;
  155. private int xTHIS;
  156. private int yBTHIS;
  157. private int xBTHIS;
  158. private CpsObject temp = null;
  159. private final JButton btnTest = new JButton("test");
  160. /**
  161. * Create the application.
  162. */
  163. public GUI(Control control) {
  164. this.controller = control;
  165. this.model = control.getModel();
  166. this.canvas = new MyCanvas(model, control);
  167. this.unitGraph = new UnitGraph(model, control);
  168. control.initListener(this);
  169. initialize();
  170. updateCategories(model.getCategories());
  171. controller.setCanvas(canvas);
  172. }
  173. /**
  174. * Initialize the contents of the frame.
  175. */
  176. @SuppressWarnings({ "serial", "unchecked" })
  177. private void initialize() {
  178. frmCyberPhysical = new JFrame();
  179. frmCyberPhysical.setTitle("Cyber Physical Systems Model");
  180. frmCyberPhysical.setBounds(100, 100, 1000, 800);
  181. frmCyberPhysical.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  182. frmCyberPhysical.setJMenuBar(menuBar);
  183. frmCyberPhysical.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  184. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  185. menuBar.add(mnNewMenu);
  186. mnNewMenu.add(mntmNew);
  187. mnNewMenu.add(mntmOpen);
  188. mnNewMenu.add(mntmSave);
  189. menuBar.add(mnNewMenu_1);
  190. menuBar.add(mnNewMenu_2);
  191. menuBar.add(mnNewMenu_3);
  192. mnNewMenu_3.add(canvasSize);
  193. canvasSize.addActionListener(new ActionListener() {
  194. @Override
  195. public void actionPerformed(ActionEvent e) {
  196. JPanel myPanel = new JPanel();
  197. JTextField field1 = new JTextField("" + canvas.getWidth() + "");
  198. JTextField field2 = new JTextField("" + canvas.getHeight() + "");
  199. myPanel.add(field1);
  200. myPanel.add(field2);
  201. JOptionPane.showMessageDialog(null, myPanel);
  202. canvas.setPreferredSize(
  203. new Dimension(Integer.parseInt(field1.getText()), Integer.parseInt(field2.getText())));
  204. canvas.repaint();
  205. }
  206. });
  207. mnNewMenu_3.add(splitPane_3);
  208. sizeSlider.setMinimum(15);
  209. sizeSlider.setMaximum(115);
  210. sizeSlider.addChangeListener(new ChangeListener() {
  211. @Override
  212. public void stateChanged(ChangeEvent e) {
  213. controller.setScale(sizeSlider.getValue());
  214. tree.setRowHeight(model.getScale());
  215. canvas.objectSelectionHighlighting();
  216. canvas.repaint();
  217. }
  218. });
  219. splitPane_3.setRightComponent(sizeSlider);
  220. splitPane_3.setLeftComponent(lblImageSize);
  221. menuBar.add(mnHelp);
  222. mnHelp.add(aboutUs);
  223. menuBar.add(mnAlgorithm);
  224. mnAlgorithm.add(chckbxmntmUseAlgorithm);
  225. mnAlgorithm.add(splitPane_2);
  226. splitPane_2.setLeftComponent(lblSelect);
  227. splitPane_2.setRightComponent(comboBoxAlgo);
  228. canvas.setBackground(Color.WHITE);
  229. canvas.setPreferredSize(new Dimension(1000, 1000));
  230. JScrollPane canvasSP = new JScrollPane(canvas);
  231. canvasSP.setBorder(null);
  232. tabbedPane.addTab("Modeling", new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/home.png"))
  233. .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)), canvasSP, "Model a CPS");
  234. tabbedPane.addTab("Simulation",
  235. new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  236. .getScaledInstance(30, 30, Image.SCALE_SMOOTH)),
  237. scrollPane_2, "Simulate the CPS");
  238. dtrpnHereWillBe.setText("Here will be the Simulation");
  239. scrollPane_2.setViewportView(dtrpnHereWillBe);
  240. /********************
  241. * RIGHT CONTAINER (INFORMATION)
  242. **********************/
  243. // Set up of the HolonElements section
  244. Object[] columnNames = { "Device", "Energy", "Quantity", "Activated" };
  245. tableModelHolonElement.setColumnIdentifiers(columnNames);
  246. tableHolonElement.setBorder(null);
  247. tableHolonElement.setModel(tableModelHolonElement);
  248. tableHolonElement.setFillsViewportHeight(true);
  249. tableHolonElement.setCellSelectionEnabled(true);
  250. tableHolonElement.setColumnSelectionAllowed(true);
  251. tableHolonElementScrollPane.setViewportView(tableHolonElement);
  252. scrollElements.setLayout(new BorderLayout(0, 0));
  253. scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
  254. scrollElements.add(tableHolonElementScrollPane);
  255. panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
  256. toolBarHolonEl.add(btnAddHolEL);
  257. toolBarHolonEl.add(btnDelHolEL);
  258. toolBarHolonEl.setFloatable(false);
  259. panel_HolonEl.add(toolBarHolonEl);
  260. // Set up of the Properties section
  261. Object[] colNames = { "Field", "Information" };
  262. tableModelProperties = new DefaulTable(100, colNames.length);
  263. tableModelProperties.setColumnIdentifiers(colNames);
  264. tableProperties.setModel(tableModelProperties);
  265. tableProperties.setFillsViewportHeight(true);
  266. tableProperties.setCellSelectionEnabled(true);
  267. tableProperties.setColumnSelectionAllowed(true);
  268. scrollProperties.setViewportView(tableProperties);
  269. // Set up of the Graph section
  270. Object[] tempText = { "Here comes the graph for each clicked HolonElement" };
  271. tableModelGraph.setColumnIdentifiers(tempText);
  272. tableGraph.setModel(tableModelGraph);
  273. tableGraph.setFillsViewportHeight(true);
  274. tableGraph.setCellSelectionEnabled(true);
  275. tableGraph.setColumnSelectionAllowed(true);
  276. scrollGraph.setViewportView(unitGraph);
  277. graphLabel.setLayout(new BorderLayout(0, 10));
  278. graphLabel.add(maxGraph, BorderLayout.NORTH);
  279. graphLabel.add(medGraph, BorderLayout.CENTER);
  280. graphLabel.add(minGraph, BorderLayout.SOUTH);
  281. toolBarGraph.add(elementGraph);
  282. toolBarGraph.add(resetGraphBtn);
  283. toolBarGraph.setFloatable(false);
  284. scrollGraph.setRowHeaderView(graphLabel);
  285. scrollGraph.setColumnHeaderView(toolBarGraph);
  286. /***********************
  287. * HolonElement Table Actions
  288. **********************/
  289. /*
  290. * Add HolonElement to given HolonObject
  291. */
  292. btnAddHolEL.addActionListener(new ActionListener() {
  293. public void actionPerformed(ActionEvent arg0) {
  294. CpsObject tempCpsObject = getActualCps();
  295. if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
  296. && tempCpsObject.getID() != 0) {
  297. addElementPopUp = new AddElementPopUp();
  298. addElementPopUp.setActualCps(getActualCps());
  299. addElementPopUp.setVisible(true);
  300. HolonElement ele = addElementPopUp.getElement();
  301. controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
  302. ele.getEnergy());
  303. refreshTableHolonElement();
  304. refreshTableProperties();
  305. controller.calculateStateForTimeStep(model.getCurIteration());
  306. }
  307. }
  308. });
  309. /*
  310. * Delete the choosen HolonElement of the selected HolonObject
  311. */
  312. btnDelHolEL.addActionListener(new ActionListener() {
  313. public void actionPerformed(ActionEvent arg0) {
  314. if (getActualCps().getClass() == HolonObject.class) {
  315. HolonObject obj = (HolonObject) getActualCps();
  316. tempElement = getActualHolonElement(obj, yValueElements);
  317. if (tempElement != null && obj.getClass() == HolonObject.class && obj.getID() != 0) {
  318. controller.deleteElementCanvas(obj.getID(), tempElement.getEleName());
  319. refreshTableHolonElement();
  320. refreshTableProperties();
  321. controller.calculateStateForTimeStep(model.getCurIteration());
  322. }
  323. }
  324. }
  325. });
  326. /*
  327. * Communication between HolonElement Table and displayed Graph
  328. */
  329. tableHolonElement.addMouseListener(new MouseAdapter() {
  330. public void mousePressed(MouseEvent e) {
  331. HolonObject obj = (HolonObject) getActualCps();
  332. yValueElements = e.getY();
  333. HolonElement ele = getActualHolonElement(obj, yValueElements);
  334. if (e.isControlDown() && ele != null) {
  335. if (!selectedElements.contains(ele)) {
  336. selectedElements.add(ele);
  337. if (!holonEleNamesDisplayed.equals("None ")) {
  338. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  339. } else {
  340. holonEleNamesDisplayed = ele.getEleName() + " ";
  341. }
  342. unitGraph.repaintWithNewElement(selectedElements);
  343. }
  344. } else if (ele != null) {
  345. selectedElements.clear();
  346. selectedElements.add(ele);
  347. holonEleNamesDisplayed = ele.getEleName() + " ";
  348. unitGraph.repaintWithNewElement(selectedElements);
  349. } else {
  350. elementGraph.setText("None ");
  351. unitGraph.empty();
  352. }
  353. // if any HolonElement is double-clicked --> Edit instance of
  354. // selected HolonElement
  355. if (e.getClickCount() == 2) {
  356. yTHIS = e.getY();
  357. xTHIS = e.getX();
  358. }
  359. if (e.getClickCount() == 1 && ele == null) {
  360. selectedElements.clear();
  361. holonEleNamesDisplayed = "None ";
  362. }
  363. for (int i = 0; i < selectedElements.size(); i++) {
  364. if (i == 0) {
  365. System.out.println("Selected Items: " + selectedElements.get(i).getEleName());
  366. } else {
  367. System.out.println(selectedElements.get(i).getEleName());
  368. }
  369. }
  370. elementGraph.setText(holonEleNamesDisplayed);
  371. yBTHIS = e.getY();
  372. xBTHIS = e.getX();
  373. }
  374. });
  375. /*
  376. * If the HolonElement Table enters to editing instance, than is the
  377. * propertyChangeListener triggered
  378. */
  379. tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
  380. @Override
  381. public void propertyChange(PropertyChangeEvent evt) {
  382. try {
  383. int yMouse = yTHIS;
  384. int yBMouse = yBTHIS;
  385. int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
  386. int selectedValueY = (int) Math.floor(yMouse / 16);
  387. int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
  388. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  389. if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
  390. if (selectedValueBX == 3) {
  391. HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse);
  392. String newBStuff = tableModelHolonElement.getValueAt(selectedValueBY, selectedValueBX)
  393. .toString();
  394. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  395. eleBTemp.setActive(bTemp);
  396. } else {
  397. HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
  398. String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX)
  399. .toString();
  400. if (selectedValueX == 0) {
  401. eleTemp.setEleName(newStuff);
  402. } else if (selectedValueX == 1) {
  403. Float ftemp = Float.parseFloat(newStuff);
  404. eleTemp.setEnergy(ftemp);
  405. } else if (selectedValueX == 2) {
  406. Integer iTemp = Integer.parseInt(newStuff);
  407. eleTemp.setAmount(iTemp);
  408. }
  409. }
  410. refreshTableProperties();
  411. tableModelHolonElement.fireTableDataChanged();
  412. controller.calculateStateForTimeStep(model.getCurIteration());
  413. unitGraph.repaint();
  414. }
  415. } catch (Exception e) {
  416. }
  417. }
  418. });
  419. /***********************
  420. * HolonElement Properties Actions
  421. **********************/
  422. /*
  423. * If any value at the Properties Table enters to editing instance, than
  424. * is PropertyChangeListener triggered (For HolonObject, the only value
  425. * to be edited is the name and for CpsEdge the Max.flow)
  426. */
  427. tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
  428. @Override
  429. public void propertyChange(PropertyChangeEvent evt) {
  430. try {
  431. Object temp;
  432. if (getActualCps() != null) {
  433. if (getActualCps() instanceof HolonObject) {
  434. temp = tableModelProperties.getValueAt(0, 1);
  435. getActualCps().setName(temp.toString());
  436. } else if (getActualCps() instanceof HolonTransformer) {
  437. // get Info of the Properties for Transformer
  438. }
  439. } else {
  440. temp = tableModelProperties.getValueAt(2, 1);
  441. Float ftemp = Float.parseFloat(temp.toString());
  442. model.getSelectedEdge().setCapacity(ftemp);
  443. }
  444. canvas.repaint();
  445. } catch (Exception e) {
  446. }
  447. }
  448. });
  449. /***********************
  450. * HolonElement Graph Actions
  451. **********************/
  452. /*
  453. * Reset the graph of the selected HolonElement
  454. */
  455. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  456. resetGraphBtn.addActionListener(new ActionListener() {
  457. public void actionPerformed(ActionEvent arg0) {
  458. unitGraph.reset();
  459. }
  460. });
  461. /*****************************
  462. * RIGHT CONTAINER DONE
  463. *****************************/
  464. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  465. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  466. @Override
  467. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  468. boolean leaf, int row, boolean hasFocus) {
  469. JLabel label = new JLabel();
  470. Image imgR = null;
  471. if (leaf) {
  472. for (Category cat : model.getCategories()) {
  473. for (CpsObject cps : cat.getObjects()) {
  474. if (value.toString().compareTo(cps.getObjName()) == 0) {
  475. File checkPath = new File(cps.getImage());
  476. if (checkPath.exists()) {
  477. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  478. controller.getScale(), controller.getScale(), java.awt.Image.SCALE_SMOOTH);
  479. } else {
  480. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  481. .getScaledInstance(controller.getScale(), controller.getScale(),
  482. java.awt.Image.SCALE_SMOOTH);
  483. }
  484. if (imgR != null) {
  485. label.setIcon(new ImageIcon(imgR));
  486. }
  487. label.setText(cps.getName());
  488. }
  489. }
  490. }
  491. }
  492. tree.setRowHeight(model.getScale());
  493. if (hasFocus) {
  494. label.setForeground(new Color(0, 0, 255));
  495. label.setOpaque(true);
  496. }
  497. if (label.getText().length() == 0) {
  498. label.setText(value.toString());
  499. if (value.toString().compareTo("Categories") != 0) {
  500. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  501. }
  502. }
  503. return label;
  504. }
  505. };
  506. tree.setCellRenderer(customRenderer);
  507. tree.addMouseListener(new MouseAdapter() {
  508. public void mouseReleased(MouseEvent e) {
  509. try {
  510. if (dragging) {
  511. int x = (int) canvas.getMousePosition().getX();
  512. int y = (int) canvas.getMousePosition().getY();
  513. CpsObject h = null;
  514. if (tempCps.getClass() == HolonObject.class) {
  515. h = new HolonObject(tempCps);
  516. }
  517. if (tempCps.getClass() == HolonSwitch.class) {
  518. h = new HolonSwitch(tempCps);
  519. }
  520. if (tempCps.getClass() == HolonTransformer.class) {
  521. h = new HolonTransformer(tempCps);
  522. }
  523. h.setPosition(x, y);
  524. controller.addObjectCanvas(h);
  525. canvas.repaint();
  526. dragging = false;
  527. }
  528. } catch (Exception e2) {
  529. }
  530. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  531. }
  532. });
  533. popmenuEdit.add(editItem);
  534. editItem.setEnabled(false);
  535. editItem.addActionListener(new ActionListener() {
  536. @Override
  537. public void actionPerformed(ActionEvent e) {
  538. }
  539. });
  540. tree.addMouseListener(new MouseAdapter() {
  541. public void mousePressed(MouseEvent e) {
  542. try {
  543. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  544. // if an Object was selected, the porperties are shown in
  545. // the table
  546. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  547. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  548. if (selectedNode.getLevel() == 2) {
  549. CpsObject selected = controller.searchCategoryObject(selectedNode.getParent().toString(),
  550. selectedNode.toString());
  551. deleteRows();
  552. // if (selected instanceof HolonObject && selected !=
  553. // null) {
  554. // selected = (HolonObject) selected;
  555. // fillElementTable(((HolonObject)
  556. // selected).getElements());
  557. // }
  558. }
  559. if (SwingUtilities.isRightMouseButton(e)) {
  560. for (Category cat : model.getCategories()) {
  561. for (CpsObject cps : cat.getObjects()) {
  562. if (actualObjectClicked.compareTo(cps.getObjName()) == 0
  563. && !(cps instanceof HolonSwitch)) {
  564. editItem.setEnabled(true);
  565. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  566. catOfObjToBeEdited = selectedNode.getParent().toString();
  567. tempCps = cps;
  568. }
  569. }
  570. }
  571. } else {
  572. for (Category cat : model.getCategories()) {
  573. for (CpsObject cps : cat.getObjects()) {
  574. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  575. File checkPath = new File(cps.getImage());
  576. if (checkPath.exists()) {
  577. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  578. controller.getScale(), controller.getScale(),
  579. java.awt.Image.SCALE_SMOOTH);
  580. } else {
  581. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  582. .getScaledInstance(controller.getScale(), controller.getScale(),
  583. java.awt.Image.SCALE_SMOOTH);
  584. }
  585. tempCps = cps;
  586. dragging = true;
  587. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  588. "Image");
  589. frmCyberPhysical.setCursor(cursor);
  590. }
  591. }
  592. }
  593. }
  594. } catch (Exception e2) {
  595. }
  596. }
  597. });
  598. editItem.addActionListener(new ActionListener() {
  599. @Override
  600. public void actionPerformed(ActionEvent e) {
  601. // Remove the selected Object object
  602. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
  603. addObjectPopUP.setCategory(catOfObjToBeEdited);
  604. addObjectPopUP.setController(controller);
  605. addObjectPopUP.setVisible(true);
  606. }
  607. });
  608. scrollPane_1.setViewportView(tree);
  609. scrollPane_1.setColumnHeaderView(panel);
  610. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  611. toolBar.setFloatable(false);
  612. panel.add(toolBar);
  613. toolBar.add(comboBox);
  614. comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Transformer", "Switch" }));
  615. // Add Button
  616. btnAdd.addActionListener(new ActionListener() {
  617. public void actionPerformed(ActionEvent arg0) {
  618. Object nodeInfo = tree.getLastSelectedPathComponent();
  619. String selectedOption = comboBox.getSelectedItem().toString();
  620. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  621. switch (selectedOption) {
  622. case "Category":
  623. String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
  624. if (catName.length() != 0) {
  625. controller.addCategory(catName);
  626. }
  627. break;
  628. case "Object":
  629. if (selectedNode == null) {
  630. JOptionPane.showMessageDialog(new JFrame(),
  631. "Please select a Category first before adding " + selectedOption + ".");
  632. }
  633. if (selectedNode.getLevel() == 1) {
  634. CpsObject tmp = new HolonObject("");
  635. addObjectPopUP = new AddObjectPopUp(false, tmp, null);
  636. addObjectPopUP.setVisible(true);
  637. addObjectPopUP.setController(controller);
  638. addObjectPopUP.setCategory(selectedNode.toString());
  639. }
  640. break;
  641. default:
  642. addObjectAction(selectedOption, selectedNode);
  643. break;
  644. }
  645. tree.repaint();
  646. }
  647. });
  648. /**
  649. * Pop up - About Us with some important information about the
  650. * developers, source and programming stuff
  651. */
  652. aboutUs.addMouseListener(new MouseAdapter() {
  653. @Override
  654. public void mousePressed(MouseEvent e) {
  655. aboutUsPopUp = new AboutUsPopUp();
  656. aboutUsPopUp.setVisible(true);
  657. }
  658. });
  659. /**
  660. * Update of every interaction between the user and the canvas (only on
  661. * the canvas). Basically the update of all the information concerning
  662. * the clicked HolonObject
  663. */
  664. canvas.addMouseListener(new MouseAdapter() {
  665. @Override
  666. public void mousePressed(MouseEvent e) {
  667. if (temp == null || temp.getID() != model.getSelectedObjectID()) {
  668. unitGraph.empty();
  669. elementGraph.setText("None ");
  670. }
  671. temp = getActualCps();
  672. // Update of the Information about the HolonElements - only for
  673. // if (temp instanceof HolonObject) {
  674. // refreshTableHolonElement();
  675. // }
  676. // Update of the Information about the Properties - only for
  677. // CpsObjects
  678. // Erase old data
  679. if (tableModelProperties.getRowCount() > 0) {
  680. for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
  681. tableModelProperties.removeRow(i);
  682. }
  683. }
  684. // Write new data
  685. if (temp != null) {
  686. Object[] tempName = { "Name", temp.getName() };
  687. tableModelProperties.addRow(tempName);
  688. Object[] tempId = { "ID", temp.getID() };
  689. tableModelProperties.addRow(tempId);
  690. if (temp instanceof HolonObject) {
  691. refreshTableHolonElement();
  692. Object[] tempEnergy = { "Total Energy", ((HolonObject) temp).getCurrentEnergy() };
  693. tableModelProperties.addRow(tempEnergy);
  694. } else if (temp instanceof HolonTransformer) {
  695. deleteRows();
  696. Object[] tempRatioPerc = { "Ratio Type", true };
  697. tableModelProperties.addRow(tempRatioPerc);
  698. } else if (temp instanceof HolonSwitch) {
  699. deleteRows();
  700. Object[] tempActive = { "Active", ((HolonSwitch) temp).getActiveAt()[model.getCurIteration()] };
  701. tableModelProperties.addRow(tempActive);
  702. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  703. elementGraph.setText(temp.getName());
  704. } else {
  705. deleteRows();
  706. }
  707. tableModelProperties.setCellEditable(0, 1, true);
  708. tableModelProperties.setCellEditable(2, 1, false);
  709. ArrayList<CpsEdge> temp_array = temp.getConnections();
  710. if (!temp_array.isEmpty()) {
  711. boolean first = true;
  712. for (CpsEdge temp2 : temp_array) {
  713. if (first) {
  714. first = false;
  715. if (temp.getID() != temp2.getA().getID()) {
  716. Object[] tempConnection = { temp.getName() + " is connected to",
  717. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  718. tableModelProperties.addRow(tempConnection);
  719. } else {
  720. Object[] tempConnection = { temp.getName() + " is connected to",
  721. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  722. tableModelProperties.addRow(tempConnection);
  723. }
  724. } else {
  725. if (temp.getID() != temp2.getA().getID()) {
  726. Object[] tempConnection = { "",
  727. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  728. tableModelProperties.addRow(tempConnection);
  729. } else {
  730. Object[] tempConnection = { "",
  731. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  732. tableModelProperties.addRow(tempConnection);
  733. }
  734. }
  735. }
  736. }
  737. } else if (model.getSelectedEdge() != null) {
  738. Object[] tempName = { "Name", "Edge: " + model.getSelectedEdge().getA().getName() + " to "
  739. + model.getSelectedEdge().getB().getName() };
  740. tableModelProperties.addRow(tempName);
  741. Object[] tempFlow = { "Current flow", model.getSelectedEdge().getFlow() };
  742. tableModelProperties.addRow(tempFlow);
  743. Object[] tempCapacity = { "Max. Capacity", model.getSelectedEdge().getCapacity() };
  744. tableModelProperties.addRow(tempCapacity);
  745. tableModelProperties.setCellEditable(0, 1, false);
  746. tableModelProperties.setCellEditable(2, 1, true);
  747. } else if (getActualCps() == null) {
  748. deleteRows();
  749. }
  750. }
  751. });
  752. toolBar.add(btnAdd);
  753. // Del Button
  754. btnDel.addActionListener(new ActionListener() {
  755. public void actionPerformed(ActionEvent arg0) {
  756. Object nodeInfo = tree.getLastSelectedPathComponent();
  757. if (nodeInfo != null) {
  758. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  759. String nodeName = selectedNode.getUserObject().toString();
  760. int depthOfNode = selectedNode.getLevel();
  761. switch (depthOfNode) {
  762. case 1:
  763. int dialogResult = JOptionPane.showConfirmDialog(null,
  764. "Do you realy want to delete the Category " + nodeName + "?", "Warning",
  765. JOptionPane.YES_NO_OPTION);
  766. if (dialogResult == JOptionPane.YES_OPTION) {
  767. controller.deleteCategory(nodeName);
  768. }
  769. break;
  770. case 2:
  771. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  772. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  773. break;
  774. default:
  775. JOptionPane.showMessageDialog(new JFrame(),
  776. "Please select a Category or an Object in order to delete something.");
  777. }
  778. } else {
  779. JOptionPane.showMessageDialog(new JFrame(),
  780. "Please select a Category or an Object in order to delete something.");
  781. }
  782. tree.repaint();
  783. }
  784. });
  785. toolBar.add(btnDel);
  786. frmCyberPhysical.getContentPane().add(splitPane);
  787. mntmNew.addActionListener(new ActionListener() {
  788. @Override
  789. public void actionPerformed(ActionEvent arg0) {
  790. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  791. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  792. controller.setSelectedObjectID(0);
  793. controller.setSelecteEdge(null);
  794. controller.setCurIteration(0);
  795. unitGraph.empty();
  796. elementGraph.setText("None ");
  797. canvas.tempCps = null;
  798. canvas.objectSelectionHighlighting();
  799. canvas.repaint();
  800. }
  801. });
  802. mntmOpen.addActionListener(new java.awt.event.ActionListener() {
  803. @Override
  804. public void actionPerformed(java.awt.event.ActionEvent evt) {
  805. menuFileExitActionPerformed(evt);
  806. }
  807. private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
  808. JFileChooser fileChooser = new JFileChooser();
  809. JFrame test = new JFrame();
  810. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  811. fileChooser.setFileFilter(jsonFilter);
  812. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  813. File file = fileChooser.getSelectedFile();
  814. try {
  815. controller.loadFile(file.getAbsolutePath());
  816. canvas.repaint();
  817. ArrayList<HolonElement> tempList = new ArrayList<>();
  818. for (CpsObject cps : model.getObjectsOnCanvas()) {
  819. if (cps instanceof HolonObject) {
  820. for (HolonElement h : ((HolonObject) cps).getElements()) {
  821. tempList.add(h);
  822. unitGraph.repaintWithNewElement(tempList);
  823. unitGraph.fillArrayofValue();
  824. tempList.remove(0);
  825. }
  826. } else if (cps instanceof HolonSwitch) {
  827. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  828. unitGraph.fillArrayofBooleans();
  829. }
  830. }
  831. unitGraph.empty();
  832. tree.repaint();
  833. } catch (IOException e) {
  834. // TODO Auto-generated catch block
  835. e.printStackTrace();
  836. }
  837. }
  838. }
  839. });
  840. mntmSave.addActionListener(new java.awt.event.ActionListener() {
  841. @Override
  842. public void actionPerformed(java.awt.event.ActionEvent evt) {
  843. menuSaveActionPerformed(evt);
  844. }
  845. private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
  846. JFileChooser fileChooser = new JFileChooser();
  847. JFrame test = new JFrame();
  848. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  849. fileChooser.setFileFilter(jsonFilter);
  850. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  851. String file = fileChooser.getSelectedFile().getPath();
  852. if (!file.endsWith(".json"))
  853. file += ".json";
  854. try {
  855. controller.saveFile(new File(file).getAbsolutePath());
  856. } catch (IOException e) {
  857. // TODO Auto-generated catch block
  858. e.printStackTrace();
  859. }
  860. }
  861. }
  862. });
  863. timePanel = new TimePanel(model, controller);
  864. timePanel.setBorder(null);
  865. ((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
  866. @Override
  867. public void stateChanged(ChangeEvent e) {
  868. int i = model.getCurIteration();
  869. controller.calculateStateForTimeStep(i);
  870. unitGraph.repaint();
  871. }
  872. });
  873. splitPane.setRightComponent(splitPane_1);
  874. splitPane.setDividerLocation(200);
  875. splitPane_1.setDividerLocation(500);
  876. splitPane.setLeftComponent(scrollPane_1);
  877. splitPane_1.setLeftComponent(tabbedPane);
  878. splitPane_1.setRightComponent(split_HolonEl_Pro);
  879. split_HolonEl_Pro.setDividerLocation(400);
  880. split_HolonEl_Pro.setTopComponent(split_Graph_HolonEl);
  881. split_HolonEl_Pro.setBottomComponent(scrollProperties);
  882. split_Graph_HolonEl.setDividerLocation(150);
  883. split_Graph_HolonEl.setTopComponent(scrollGraph);
  884. split_Graph_HolonEl.setBottomComponent(scrollElements);
  885. tabbedPane.setBorder(null);
  886. scrollProperties.setBorder(null);
  887. scrollGraph.setBorder(null);
  888. scrollElements.setBorder(null);
  889. splitPane.setBorder(null);
  890. splitPane_1.setBorder(null);
  891. split_HolonEl_Pro.setBorder(null);
  892. split_Graph_HolonEl.setBorder(null);
  893. scrollPane_2.setBorder(null);
  894. panel_HolonEl.setBorder(null);
  895. tableHolonElementScrollPane.setBorder(null);
  896. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  897. }
  898. /*
  899. * adds a specific object type to selected Category also handles input
  900. * windows and illegal inputs
  901. */
  902. public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  903. if (selectedNode == null) {
  904. JOptionPane.showMessageDialog(new JFrame(),
  905. "Please select a Category first before adding " + objType + ".");
  906. }
  907. // if selected node is a directory for Categories
  908. else {
  909. if (selectedNode.getLevel() == 1) {
  910. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  911. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  912. if (objname.length() != 0) {
  913. switch (objType) {
  914. case "Switch":
  915. controller.addSwitch(cat, objname);
  916. break;
  917. case "Transformer":
  918. controller.addTransformer(cat, objname);
  919. break;
  920. }
  921. }
  922. } else {
  923. JOptionPane.showMessageDialog(new JFrame(),
  924. "Objects can not be added to Objects. Please select a Category.");
  925. }
  926. }
  927. }
  928. /**
  929. * reloads the Categories from Model
  930. */
  931. public void updateCategories(final ArrayList<Category> categories) {
  932. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  933. {
  934. DefaultMutableTreeNode node_1;
  935. for (Category c : categories) {
  936. node_1 = new DefaultMutableTreeNode(c.getName());
  937. // kann eventuell umgeändert werden
  938. for (CpsObject obj : c.getObjects()) {
  939. node_1.add(new DefaultMutableTreeNode(obj.getObjName()));
  940. }
  941. add(node_1);
  942. }
  943. }
  944. }));
  945. }
  946. /**
  947. *
  948. */
  949. public void onChange(ArrayList<Category> categories) {
  950. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  951. updateCategories(categories);
  952. model.reload();
  953. }
  954. /**
  955. *
  956. * @return
  957. */
  958. public JFrame getFrmCyberPhysical() {
  959. return frmCyberPhysical;
  960. }
  961. /**
  962. * Getter for selected CpsObject
  963. *
  964. * @return selected CpsObject
  965. */
  966. public CpsObject getActualCps() {
  967. int tempID = model.getSelectedObjectID();
  968. CpsObject tempCps = controller.searchByID(tempID);
  969. return tempCps;
  970. }
  971. /**
  972. * Search for actual selected HolonElement
  973. *
  974. * @param obj
  975. * selected HolonObject
  976. * @param yValue
  977. * Y-Coord in the HolonElementsTable
  978. * @return the selected HolonElement
  979. */
  980. public HolonElement getActualHolonElement(HolonObject obj, int yValue) {
  981. final int yTemp = (int) Math.floor(yValue / 16);
  982. int rowsTotal = tableModelHolonElement.getRowCount();
  983. if (rowsTotal != 0 && rowsTotal > yTemp) {
  984. model.setSelectedHolonElement(obj.getElements().get(yTemp));
  985. return obj.getElements().get(yTemp);
  986. } else {
  987. model.setSelectedHolonElement(null);
  988. return null;
  989. }
  990. }
  991. /**
  992. * Update the HolonElement Table, that means erase all rows and add the new
  993. * rows with new data
  994. */
  995. public void refreshTableHolonElement() {
  996. // Update of the Information about the HolonElements - only for
  997. // HolonObjects
  998. deleteRows();
  999. if (getActualCps() != null) {
  1000. fillElementTable(((HolonObject) getActualCps()).getElements());
  1001. }
  1002. tableModelHolonElement.fireTableDataChanged();
  1003. }
  1004. /**
  1005. * Erase all information of the HolonElement Model
  1006. */
  1007. public void deleteRows() {
  1008. if (tableModelHolonElement.getRowCount() > 0) {
  1009. for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
  1010. tableModelHolonElement.removeRow(i);
  1011. }
  1012. }
  1013. }
  1014. /**
  1015. * Add the Information of the given ArrayList in the HolonElement Model as
  1016. * Name,Energy and Amount
  1017. *
  1018. * @param elements
  1019. * ArrayList to be displayed
  1020. */
  1021. public void fillElementTable(ArrayList<HolonElement> elements) {
  1022. for (HolonElement he : elements) {
  1023. Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
  1024. tableModelHolonElement.addRow(temp);
  1025. }
  1026. }
  1027. /**
  1028. * Update the information concerning properties of the selected CpsObject
  1029. */
  1030. public void refreshTableProperties() {
  1031. CpsObject tempCps = getActualCps();
  1032. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  1033. tableModelProperties.removeRow(2);
  1034. Object[] tempEnergy = { "Total Energy", ((HolonObject) tempCps).getCurrentEnergy() };
  1035. tableModelProperties.insertRow(2, tempEnergy);
  1036. } else if (tempCps instanceof HolonTransformer) {
  1037. // Refresh Transformer Info
  1038. }
  1039. }
  1040. private static void addPopup(Component component, final JPopupMenu popup) {
  1041. component.addMouseListener(new MouseAdapter() {
  1042. public void mousePressed(MouseEvent e) {
  1043. if (e.isPopupTrigger()) {
  1044. showMenu(e);
  1045. }
  1046. }
  1047. public void mouseReleased(MouseEvent e) {
  1048. if (e.isPopupTrigger()) {
  1049. showMenu(e);
  1050. }
  1051. }
  1052. private void showMenu(MouseEvent e) {
  1053. popup.show(e.getComponent(), e.getX(), e.getY());
  1054. }
  1055. });
  1056. }
  1057. }