GUI.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. }
  306. }
  307. });
  308. /*
  309. * Delete the choosen HolonElement of the selected HolonObject
  310. */
  311. btnDelHolEL.addActionListener(new ActionListener() {
  312. public void actionPerformed(ActionEvent arg0) {
  313. if (getActualCps().getClass() == HolonObject.class) {
  314. HolonObject obj = (HolonObject) getActualCps();
  315. tempElement = getActualHolonElement(obj, yValueElements);
  316. if (tempElement != null && obj.getClass() == HolonObject.class && obj.getID() != 0) {
  317. controller.deleteElementCanvas(obj.getID(), tempElement.getEleName());
  318. refreshTableHolonElement();
  319. refreshTableProperties();
  320. }
  321. }
  322. }
  323. });
  324. /*
  325. * Communication between HolonElement Table and displayed Graph
  326. */
  327. tableHolonElement.addMouseListener(new MouseAdapter() {
  328. public void mousePressed(MouseEvent e) {
  329. HolonObject obj = (HolonObject) getActualCps();
  330. yValueElements = e.getY();
  331. HolonElement ele = getActualHolonElement(obj, yValueElements);
  332. if (e.isControlDown() && ele != null) {
  333. if (!selectedElements.contains(ele)) {
  334. selectedElements.add(ele);
  335. if (!holonEleNamesDisplayed.equals("None ")) {
  336. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  337. } else {
  338. holonEleNamesDisplayed = ele.getEleName() + " ";
  339. }
  340. unitGraph.repaintWithNewElement(selectedElements);
  341. }
  342. } else if (ele != null) {
  343. selectedElements.clear();
  344. selectedElements.add(ele);
  345. holonEleNamesDisplayed = ele.getEleName() + " ";
  346. unitGraph.repaintWithNewElement(selectedElements);
  347. } else {
  348. elementGraph.setText("None ");
  349. unitGraph.empty();
  350. }
  351. // if any HolonElement is double-clicked --> Edit instance of
  352. // selected HolonElement
  353. if (e.getClickCount() == 2) {
  354. yTHIS = e.getY();
  355. xTHIS = e.getX();
  356. }
  357. if (e.getClickCount() == 1 && ele == null) {
  358. selectedElements.clear();
  359. holonEleNamesDisplayed = "None ";
  360. }
  361. for (int i = 0; i < selectedElements.size(); i++) {
  362. if (i == 0) {
  363. System.out.println("Selected Items: " + selectedElements.get(i).getEleName());
  364. } else {
  365. System.out.println(selectedElements.get(i).getEleName());
  366. }
  367. }
  368. elementGraph.setText(holonEleNamesDisplayed);
  369. yBTHIS = e.getY();
  370. xBTHIS = e.getX();
  371. }
  372. });
  373. /*
  374. * If the HolonElement Table enters to editing instance, than is the
  375. * propertyChangeListener triggered
  376. */
  377. tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
  378. @Override
  379. public void propertyChange(PropertyChangeEvent evt) {
  380. try {
  381. int yMouse = yTHIS;
  382. int yBMouse = yBTHIS;
  383. int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
  384. int selectedValueY = (int) Math.floor(yMouse / 16);
  385. int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
  386. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  387. if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
  388. if (selectedValueBX == 3) {
  389. HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse);
  390. String newBStuff = tableModelHolonElement.getValueAt(selectedValueBY, selectedValueBX)
  391. .toString();
  392. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  393. eleBTemp.setActive(bTemp);
  394. } else {
  395. HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
  396. String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX)
  397. .toString();
  398. if (selectedValueX == 0) {
  399. eleTemp.setEleName(newStuff);
  400. } else if (selectedValueX == 1) {
  401. Float ftemp = Float.parseFloat(newStuff);
  402. eleTemp.setEnergy(ftemp);
  403. } else if (selectedValueX == 2) {
  404. Integer iTemp = Integer.parseInt(newStuff);
  405. eleTemp.setAmount(iTemp);
  406. }
  407. }
  408. refreshTableProperties();
  409. tableModelHolonElement.fireTableDataChanged();
  410. controller.calculateStateForTimeStep(model.getCurIteration());
  411. unitGraph.repaint();
  412. }
  413. } catch (Exception e) {
  414. }
  415. }
  416. });
  417. /***********************
  418. * HolonElement Properties Actions
  419. **********************/
  420. /*
  421. * If any value at the Properties Table enters to editing instance, than
  422. * is PropertyChangeListener triggered (For HolonObject, the only value
  423. * to be edited is the name and for CpsEdge the Max.flow)
  424. */
  425. tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
  426. @Override
  427. public void propertyChange(PropertyChangeEvent evt) {
  428. try {
  429. Object temp;
  430. if (getActualCps() != null) {
  431. if (getActualCps() instanceof HolonObject) {
  432. temp = tableModelProperties.getValueAt(0, 1);
  433. getActualCps().setName(temp.toString());
  434. } else if (getActualCps() instanceof HolonTransformer) {
  435. // get Info of the Properties for Transformer
  436. }
  437. } else {
  438. temp = tableModelProperties.getValueAt(2, 1);
  439. Float ftemp = Float.parseFloat(temp.toString());
  440. model.getSelectedEdge().setCapacity(ftemp);
  441. }
  442. canvas.repaint();
  443. } catch (Exception e) {
  444. }
  445. }
  446. });
  447. /***********************
  448. * HolonElement Graph Actions
  449. **********************/
  450. /*
  451. * Reset the graph of the selected HolonElement
  452. */
  453. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  454. resetGraphBtn.addActionListener(new ActionListener() {
  455. public void actionPerformed(ActionEvent arg0) {
  456. unitGraph.reset();
  457. }
  458. });
  459. /*****************************
  460. * RIGHT CONTAINER DONE
  461. *****************************/
  462. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  463. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  464. @Override
  465. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  466. boolean leaf, int row, boolean hasFocus) {
  467. JLabel label = new JLabel();
  468. Image imgR = null;
  469. if (leaf) {
  470. for (Category cat : model.getCategories()) {
  471. for (CpsObject cps : cat.getObjects()) {
  472. if (value.toString().compareTo(cps.getObjName()) == 0) {
  473. File checkPath = new File(cps.getImage());
  474. if (checkPath.exists()) {
  475. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  476. controller.getScale(), controller.getScale(), java.awt.Image.SCALE_SMOOTH);
  477. } else {
  478. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  479. .getScaledInstance(controller.getScale(), controller.getScale(),
  480. java.awt.Image.SCALE_SMOOTH);
  481. }
  482. if (imgR != null) {
  483. label.setIcon(new ImageIcon(imgR));
  484. }
  485. label.setText(cps.getName());
  486. }
  487. }
  488. }
  489. }
  490. tree.setRowHeight(model.getScale());
  491. if (hasFocus) {
  492. label.setForeground(new Color(0, 0, 255));
  493. label.setOpaque(true);
  494. }
  495. if (label.getText().length() == 0) {
  496. label.setText(value.toString());
  497. if (value.toString().compareTo("Categories") != 0) {
  498. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  499. }
  500. }
  501. return label;
  502. }
  503. };
  504. tree.setCellRenderer(customRenderer);
  505. tree.addMouseListener(new MouseAdapter() {
  506. public void mouseReleased(MouseEvent e) {
  507. try {
  508. if (dragging) {
  509. int x = (int) canvas.getMousePosition().getX();
  510. int y = (int) canvas.getMousePosition().getY();
  511. CpsObject h = null;
  512. if (tempCps.getClass() == HolonObject.class) {
  513. h = new HolonObject(tempCps);
  514. }
  515. if (tempCps.getClass() == HolonSwitch.class) {
  516. h = new HolonSwitch(tempCps);
  517. }
  518. if (tempCps.getClass() == HolonTransformer.class) {
  519. h = new HolonTransformer(tempCps);
  520. }
  521. h.setPosition(x, y);
  522. controller.addObjectCanvas(h);
  523. canvas.repaint();
  524. dragging = false;
  525. }
  526. } catch (Exception e2) {
  527. }
  528. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  529. }
  530. });
  531. popmenuEdit.add(editItem);
  532. editItem.setEnabled(false);
  533. editItem.addActionListener(new ActionListener() {
  534. @Override
  535. public void actionPerformed(ActionEvent e) {
  536. }
  537. });
  538. tree.addMouseListener(new MouseAdapter() {
  539. public void mousePressed(MouseEvent e) {
  540. try {
  541. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  542. // if an Object was selected, the porperties are shown in
  543. // the table
  544. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  545. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  546. if (selectedNode.getLevel() == 2) {
  547. CpsObject selected = controller.searchCategoryObject(selectedNode.getParent().toString(),
  548. selectedNode.toString());
  549. deleteRows();
  550. // if (selected instanceof HolonObject && selected !=
  551. // null) {
  552. // selected = (HolonObject) selected;
  553. // fillElementTable(((HolonObject)
  554. // selected).getElements());
  555. // }
  556. }
  557. if (SwingUtilities.isRightMouseButton(e)) {
  558. for (Category cat : model.getCategories()) {
  559. for (CpsObject cps : cat.getObjects()) {
  560. if (actualObjectClicked.compareTo(cps.getObjName()) == 0
  561. && !(cps instanceof HolonSwitch)) {
  562. editItem.setEnabled(true);
  563. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  564. catOfObjToBeEdited = selectedNode.getParent().toString();
  565. tempCps = cps;
  566. }
  567. }
  568. }
  569. } else {
  570. for (Category cat : model.getCategories()) {
  571. for (CpsObject cps : cat.getObjects()) {
  572. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  573. File checkPath = new File(cps.getImage());
  574. if (checkPath.exists()) {
  575. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  576. controller.getScale(), controller.getScale(),
  577. java.awt.Image.SCALE_SMOOTH);
  578. } else {
  579. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  580. .getScaledInstance(controller.getScale(), controller.getScale(),
  581. java.awt.Image.SCALE_SMOOTH);
  582. }
  583. tempCps = cps;
  584. dragging = true;
  585. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  586. "Image");
  587. frmCyberPhysical.setCursor(cursor);
  588. }
  589. }
  590. }
  591. }
  592. } catch (Exception e2) {
  593. }
  594. }
  595. });
  596. editItem.addActionListener(new ActionListener() {
  597. @Override
  598. public void actionPerformed(ActionEvent e) {
  599. // Remove the selected Object object
  600. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
  601. addObjectPopUP.setCategory(catOfObjToBeEdited);
  602. addObjectPopUP.setController(controller);
  603. addObjectPopUP.setVisible(true);
  604. }
  605. });
  606. scrollPane_1.setViewportView(tree);
  607. scrollPane_1.setColumnHeaderView(panel);
  608. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  609. toolBar.setFloatable(false);
  610. panel.add(toolBar);
  611. toolBar.add(comboBox);
  612. comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Transformer", "Switch" }));
  613. // Add Button
  614. btnAdd.addActionListener(new ActionListener() {
  615. public void actionPerformed(ActionEvent arg0) {
  616. Object nodeInfo = tree.getLastSelectedPathComponent();
  617. String selectedOption = comboBox.getSelectedItem().toString();
  618. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  619. switch (selectedOption) {
  620. case "Category":
  621. String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
  622. if (catName.length() != 0) {
  623. controller.addCategory(catName);
  624. }
  625. break;
  626. case "Object":
  627. if (selectedNode == null) {
  628. JOptionPane.showMessageDialog(new JFrame(),
  629. "Please select a Category first before adding " + selectedOption + ".");
  630. }
  631. if (selectedNode.getLevel() == 1) {
  632. addObjectPopUP = new AddObjectPopUp(false, null, null);
  633. addObjectPopUP.setVisible(true);
  634. addObjectPopUP.setController(controller);
  635. addObjectPopUP.setCategory(selectedNode.toString());
  636. }
  637. break;
  638. default:
  639. addObjectAction(selectedOption, selectedNode);
  640. break;
  641. }
  642. tree.repaint();
  643. }
  644. });
  645. /**
  646. * Pop up - About Us with some important information about the
  647. * developers, source and programming stuff
  648. */
  649. aboutUs.addMouseListener(new MouseAdapter() {
  650. @Override
  651. public void mousePressed(MouseEvent e) {
  652. aboutUsPopUp = new AboutUsPopUp();
  653. aboutUsPopUp.setVisible(true);
  654. }
  655. });
  656. /**
  657. * Update of every interaction between the user and the canvas (only on
  658. * the canvas). Basically the update of all the information concerning
  659. * the clicked HolonObject
  660. */
  661. canvas.addMouseListener(new MouseAdapter() {
  662. @Override
  663. public void mousePressed(MouseEvent e) {
  664. if (temp == null || temp.getID() != model.getSelectedObjectID()) {
  665. unitGraph.empty();
  666. elementGraph.setText("None ");
  667. }
  668. temp = getActualCps();
  669. // Update of the Information about the HolonElements - only for
  670. // if (temp instanceof HolonObject) {
  671. // refreshTableHolonElement();
  672. // }
  673. // Update of the Information about the Properties - only for
  674. // CpsObjects
  675. // Erase old data
  676. if (tableModelProperties.getRowCount() > 0) {
  677. for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
  678. tableModelProperties.removeRow(i);
  679. }
  680. }
  681. // Write new data
  682. if (temp != null) {
  683. Object[] tempName = { "Name", temp.getName() };
  684. tableModelProperties.addRow(tempName);
  685. Object[] tempId = { "ID", temp.getID() };
  686. tableModelProperties.addRow(tempId);
  687. if (temp instanceof HolonObject) {
  688. refreshTableHolonElement();
  689. Object[] tempEnergy = { "Total Energy", ((HolonObject) temp).getCurrentEnergy() };
  690. tableModelProperties.addRow(tempEnergy);
  691. } else if (temp instanceof HolonTransformer) {
  692. deleteRows();
  693. Object[] tempRatioPerc = { "Ratio Type", true };
  694. tableModelProperties.addRow(tempRatioPerc);
  695. } else if (temp instanceof HolonSwitch) {
  696. deleteRows();
  697. Object[] tempActive = { "Active", ((HolonSwitch) temp).getActiveAt()[model.getCurIteration()] };
  698. tableModelProperties.addRow(tempActive);
  699. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  700. elementGraph.setText(temp.getName());
  701. } else {
  702. deleteRows();
  703. }
  704. tableModelProperties.setCellEditable(0, 1, true);
  705. tableModelProperties.setCellEditable(2, 1, false);
  706. ArrayList<CpsEdge> temp_array = temp.getConnections();
  707. if (!temp_array.isEmpty()) {
  708. boolean first = true;
  709. for (CpsEdge temp2 : temp_array) {
  710. if (first) {
  711. first = false;
  712. if (temp.getID() != temp2.getA().getID()) {
  713. Object[] tempConnection = { temp.getName() + " is connected to",
  714. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  715. tableModelProperties.addRow(tempConnection);
  716. } else {
  717. Object[] tempConnection = { temp.getName() + " is connected to",
  718. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  719. tableModelProperties.addRow(tempConnection);
  720. }
  721. } else {
  722. if (temp.getID() != temp2.getA().getID()) {
  723. Object[] tempConnection = { "",
  724. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  725. tableModelProperties.addRow(tempConnection);
  726. } else {
  727. Object[] tempConnection = { "",
  728. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  729. tableModelProperties.addRow(tempConnection);
  730. }
  731. }
  732. }
  733. }
  734. } else if (model.getSelectedEdge() != null) {
  735. Object[] tempName = { "Name", "Edge: " + model.getSelectedEdge().getA().getName() + " to "
  736. + model.getSelectedEdge().getB().getName() };
  737. tableModelProperties.addRow(tempName);
  738. Object[] tempFlow = { "Current flow", model.getSelectedEdge().getFlow() };
  739. tableModelProperties.addRow(tempFlow);
  740. Object[] tempCapacity = { "Max. Capacity", model.getSelectedEdge().getCapacity() };
  741. tableModelProperties.addRow(tempCapacity);
  742. tableModelProperties.setCellEditable(0, 1, false);
  743. tableModelProperties.setCellEditable(2, 1, true);
  744. } else if (getActualCps() == null) {
  745. deleteRows();
  746. }
  747. }
  748. });
  749. toolBar.add(btnAdd);
  750. // Del Button
  751. btnDel.addActionListener(new ActionListener() {
  752. public void actionPerformed(ActionEvent arg0) {
  753. Object nodeInfo = tree.getLastSelectedPathComponent();
  754. if (nodeInfo != null) {
  755. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  756. String nodeName = selectedNode.getUserObject().toString();
  757. int depthOfNode = selectedNode.getLevel();
  758. switch (depthOfNode) {
  759. case 1:
  760. int dialogResult = JOptionPane.showConfirmDialog(null,
  761. "Do you realy want to delete the Category " + nodeName + "?", "Warning",
  762. JOptionPane.YES_NO_OPTION);
  763. if (dialogResult == JOptionPane.YES_OPTION) {
  764. controller.deleteCategory(nodeName);
  765. }
  766. break;
  767. case 2:
  768. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  769. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  770. break;
  771. default:
  772. JOptionPane.showMessageDialog(new JFrame(),
  773. "Please select a Category or an Object in order to delete something.");
  774. }
  775. } else {
  776. JOptionPane.showMessageDialog(new JFrame(),
  777. "Please select a Category or an Object in order to delete something.");
  778. }
  779. tree.repaint();
  780. }
  781. });
  782. toolBar.add(btnDel);
  783. frmCyberPhysical.getContentPane().add(splitPane);
  784. mntmNew.addActionListener(new ActionListener() {
  785. @Override
  786. public void actionPerformed(ActionEvent arg0) {
  787. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  788. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  789. controller.setSelectedObjectID(0);
  790. controller.setSelecteEdge(null);
  791. controller.setCurIteration(0);
  792. unitGraph.empty();
  793. elementGraph.setText("None ");
  794. canvas.tempCps = null;
  795. canvas.objectSelectionHighlighting();
  796. canvas.repaint();
  797. }
  798. });
  799. mntmOpen.addActionListener(new java.awt.event.ActionListener() {
  800. @Override
  801. public void actionPerformed(java.awt.event.ActionEvent evt) {
  802. menuFileExitActionPerformed(evt);
  803. }
  804. private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
  805. JFileChooser fileChooser = new JFileChooser();
  806. JFrame test = new JFrame();
  807. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  808. fileChooser.setFileFilter(jsonFilter);
  809. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  810. File file = fileChooser.getSelectedFile();
  811. try {
  812. controller.loadFile(file.getAbsolutePath());
  813. canvas.repaint();
  814. ArrayList<HolonElement> tempList = new ArrayList<>();
  815. for (CpsObject cps : model.getObjectsOnCanvas()) {
  816. if (cps instanceof HolonObject) {
  817. for (HolonElement h : ((HolonObject) cps).getElements()) {
  818. tempList.add(h);
  819. unitGraph.repaintWithNewElement(tempList);
  820. unitGraph.fillArrayofValue();
  821. tempList.remove(0);
  822. }
  823. } else if (cps instanceof HolonSwitch) {
  824. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  825. unitGraph.fillArrayofBooleans();
  826. }
  827. }
  828. unitGraph.empty();
  829. tree.repaint();
  830. } catch (IOException e) {
  831. // TODO Auto-generated catch block
  832. e.printStackTrace();
  833. }
  834. }
  835. }
  836. });
  837. mntmSave.addActionListener(new java.awt.event.ActionListener() {
  838. @Override
  839. public void actionPerformed(java.awt.event.ActionEvent evt) {
  840. menuSaveActionPerformed(evt);
  841. }
  842. private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
  843. JFileChooser fileChooser = new JFileChooser();
  844. JFrame test = new JFrame();
  845. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  846. fileChooser.setFileFilter(jsonFilter);
  847. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  848. String file = fileChooser.getSelectedFile().getPath();
  849. if (!file.endsWith(".json"))
  850. file += ".json";
  851. try {
  852. controller.saveFile(new File(file).getAbsolutePath());
  853. } catch (IOException e) {
  854. // TODO Auto-generated catch block
  855. e.printStackTrace();
  856. }
  857. }
  858. }
  859. });
  860. timePanel = new TimePanel(model, controller);
  861. timePanel.setBorder(null);
  862. ((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
  863. @Override
  864. public void stateChanged(ChangeEvent e) {
  865. int i = model.getCurIteration();
  866. controller.calculateStateForTimeStep(i);
  867. unitGraph.repaint();
  868. }
  869. });
  870. splitPane.setRightComponent(splitPane_1);
  871. splitPane.setDividerLocation(200);
  872. splitPane_1.setDividerLocation(500);
  873. splitPane.setLeftComponent(scrollPane_1);
  874. splitPane_1.setLeftComponent(tabbedPane);
  875. splitPane_1.setRightComponent(split_HolonEl_Pro);
  876. split_HolonEl_Pro.setDividerLocation(400);
  877. split_HolonEl_Pro.setTopComponent(split_Graph_HolonEl);
  878. split_HolonEl_Pro.setBottomComponent(scrollProperties);
  879. split_Graph_HolonEl.setDividerLocation(150);
  880. split_Graph_HolonEl.setTopComponent(scrollGraph);
  881. split_Graph_HolonEl.setBottomComponent(scrollElements);
  882. tabbedPane.setBorder(null);
  883. scrollProperties.setBorder(null);
  884. scrollGraph.setBorder(null);
  885. scrollElements.setBorder(null);
  886. splitPane.setBorder(null);
  887. splitPane_1.setBorder(null);
  888. split_HolonEl_Pro.setBorder(null);
  889. split_Graph_HolonEl.setBorder(null);
  890. scrollPane_2.setBorder(null);
  891. panel_HolonEl.setBorder(null);
  892. tableHolonElementScrollPane.setBorder(null);
  893. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  894. }
  895. /*
  896. * adds a specific object type to selected Category also handles input
  897. * windows and illegal inputs
  898. */
  899. public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  900. if (selectedNode == null) {
  901. JOptionPane.showMessageDialog(new JFrame(),
  902. "Please select a Category first before adding " + objType + ".");
  903. }
  904. // if selected node is a directory for Categories
  905. else {
  906. if (selectedNode.getLevel() == 1) {
  907. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  908. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  909. if (objname.length() != 0) {
  910. switch (objType) {
  911. case "Switch":
  912. controller.addSwitch(cat, objname);
  913. break;
  914. case "Transformer":
  915. controller.addTransformer(cat, objname);
  916. break;
  917. }
  918. }
  919. } else {
  920. JOptionPane.showMessageDialog(new JFrame(),
  921. "Objects can not be added to Objects. Please select a Category.");
  922. }
  923. }
  924. }
  925. /**
  926. * reloads the Categories from Model
  927. */
  928. public void updateCategories(final ArrayList<Category> categories) {
  929. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  930. {
  931. DefaultMutableTreeNode node_1;
  932. for (Category c : categories) {
  933. node_1 = new DefaultMutableTreeNode(c.getName());
  934. // kann eventuell umgeändert werden
  935. for (CpsObject obj : c.getObjects()) {
  936. node_1.add(new DefaultMutableTreeNode(obj.getObjName()));
  937. }
  938. add(node_1);
  939. }
  940. }
  941. }));
  942. }
  943. /**
  944. *
  945. */
  946. public void onChange(ArrayList<Category> categories) {
  947. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  948. updateCategories(categories);
  949. model.reload();
  950. }
  951. /**
  952. *
  953. * @return
  954. */
  955. public JFrame getFrmCyberPhysical() {
  956. return frmCyberPhysical;
  957. }
  958. /**
  959. * Getter for selected CpsObject
  960. *
  961. * @return selected CpsObject
  962. */
  963. public CpsObject getActualCps() {
  964. int tempID = model.getSelectedObjectID();
  965. CpsObject tempCps = controller.searchByID(tempID);
  966. return tempCps;
  967. }
  968. /**
  969. * Search for actual selected HolonElement
  970. *
  971. * @param obj
  972. * selected HolonObject
  973. * @param yValue
  974. * Y-Coord in the HolonElementsTable
  975. * @return the selected HolonElement
  976. */
  977. public HolonElement getActualHolonElement(HolonObject obj, int yValue) {
  978. final int yTemp = (int) Math.floor(yValue / 16);
  979. int rowsTotal = tableModelHolonElement.getRowCount();
  980. if (rowsTotal != 0 && rowsTotal > yTemp) {
  981. model.setSelectedHolonElement(obj.getElements().get(yTemp));
  982. return obj.getElements().get(yTemp);
  983. } else {
  984. model.setSelectedHolonElement(null);
  985. return null;
  986. }
  987. }
  988. /**
  989. * Update the HolonElement Table, that means erase all rows and add the new
  990. * rows with new data
  991. */
  992. public void refreshTableHolonElement() {
  993. // Update of the Information about the HolonElements - only for
  994. // HolonObjects
  995. deleteRows();
  996. if (getActualCps() != null) {
  997. fillElementTable(((HolonObject) getActualCps()).getElements());
  998. }
  999. tableModelHolonElement.fireTableDataChanged();
  1000. }
  1001. /**
  1002. * Erase all information of the HolonElement Model
  1003. */
  1004. public void deleteRows() {
  1005. if (tableModelHolonElement.getRowCount() > 0) {
  1006. for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
  1007. tableModelHolonElement.removeRow(i);
  1008. }
  1009. }
  1010. }
  1011. /**
  1012. * Add the Information of the given ArrayList in the HolonElement Model as
  1013. * Name,Energy and Amount
  1014. *
  1015. * @param elements
  1016. * ArrayList to be displayed
  1017. */
  1018. public void fillElementTable(ArrayList<HolonElement> elements) {
  1019. for (HolonElement he : elements) {
  1020. Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
  1021. tableModelHolonElement.addRow(temp);
  1022. }
  1023. }
  1024. /**
  1025. * Update the information concerning properties of the selected CpsObject
  1026. */
  1027. public void refreshTableProperties() {
  1028. CpsObject tempCps = getActualCps();
  1029. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  1030. tableModelProperties.removeRow(2);
  1031. Object[] tempEnergy = { "Total Energy", ((HolonObject) tempCps).getCurrentEnergy() };
  1032. tableModelProperties.insertRow(2, tempEnergy);
  1033. } else if (tempCps instanceof HolonTransformer) {
  1034. // Refresh Transformer Info
  1035. }
  1036. }
  1037. private static void addPopup(Component component, final JPopupMenu popup) {
  1038. component.addMouseListener(new MouseAdapter() {
  1039. public void mousePressed(MouseEvent e) {
  1040. if (e.isPopupTrigger()) {
  1041. showMenu(e);
  1042. }
  1043. }
  1044. public void mouseReleased(MouseEvent e) {
  1045. if (e.isPopupTrigger()) {
  1046. showMenu(e);
  1047. }
  1048. }
  1049. private void showMenu(MouseEvent e) {
  1050. popup.show(e.getComponent(), e.getX(), e.getY());
  1051. }
  1052. });
  1053. }
  1054. }