GUI.java 36 KB

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