GUI.java 35 KB

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