GUI.java 40 KB

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