GUI.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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.searchObject(selectedNode.getParent().toString(),
  492. selectedNode.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. tree.repaint();
  569. }
  570. });
  571. /**
  572. * Pop up - About Us with some important information about the
  573. * developers, source and programming stuff
  574. */
  575. aboutUs.addMouseListener(new MouseAdapter() {
  576. @Override
  577. public void mousePressed(MouseEvent e) {
  578. aboutUsPopUp = new AboutUsPopUp();
  579. aboutUsPopUp.setVisible(true);
  580. }
  581. });
  582. /**
  583. * Update of every interaction between the user and the canvas (only on
  584. * the canvas). Basically the update of all the information concerning
  585. * the clicked HolonObject
  586. */
  587. canvas.addMouseListener(new MouseAdapter() {
  588. @Override
  589. public void mousePressed(MouseEvent e) {
  590. if (temp == null || temp.getID() != model.getSelectedObjectID()) {
  591. unitGraph.empty();
  592. }
  593. temp = getActualCps();
  594. // Update of the Information about the HolonElements - only for
  595. if (temp instanceof HolonObject) {
  596. refreshTableHolonElement();
  597. }
  598. // Update of the Information about the Properties - only for
  599. // CpsObjects
  600. // Erase old data
  601. if (tableModelProperties.getRowCount() > 0) {
  602. for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
  603. tableModelProperties.removeRow(i);
  604. }
  605. }
  606. // Write new data
  607. if (temp != null) {
  608. Object[] tempName = { "Name", temp.getName() };
  609. tableModelProperties.addRow(tempName);
  610. Object[] tempId = { "ID", temp.getID() };
  611. tableModelProperties.addRow(tempId);
  612. if (temp instanceof HolonObject) {
  613. refreshTableHolonElement();
  614. Object[] tempEnergy = { "Total Energy", ((HolonObject) temp).getCurrentEnergy() };
  615. tableModelProperties.addRow(tempEnergy);
  616. }
  617. tableModelProperties.setCellEditable(0, 1, true);
  618. tableModelProperties.setCellEditable(2, 1, false);
  619. ArrayList<CpsEdge> temp_array = temp.getConnections();
  620. if (!temp_array.isEmpty()) {
  621. boolean first = true;
  622. for (CpsEdge temp2 : temp_array) {
  623. if (first) {
  624. first = false;
  625. if (temp.getID() != temp2.getA().getID()) {
  626. Object[] tempConnection = { temp.getName() + " is connected to",
  627. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  628. tableModelProperties.addRow(tempConnection);
  629. } else {
  630. Object[] tempConnection = { temp.getName() + " is connected to",
  631. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  632. tableModelProperties.addRow(tempConnection);
  633. }
  634. } else {
  635. if (temp.getID() != temp2.getA().getID()) {
  636. Object[] tempConnection = { "",
  637. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  638. tableModelProperties.addRow(tempConnection);
  639. } else {
  640. Object[] tempConnection = { "",
  641. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  642. tableModelProperties.addRow(tempConnection);
  643. }
  644. }
  645. }
  646. }
  647. } else if (model.getSelectedEdge() != null) {
  648. Object[] tempName = { "Name", "Edge: " + model.getSelectedEdge().getA().getName() + " to "
  649. + model.getSelectedEdge().getB().getName() };
  650. tableModelProperties.addRow(tempName);
  651. Object[] tempFlow = { "Current flow", model.getSelectedEdge().getFlow() };
  652. tableModelProperties.addRow(tempFlow);
  653. Object[] tempCapacity = { "Max. Capacity", model.getSelectedEdge().getCapacity() };
  654. tableModelProperties.addRow(tempCapacity);
  655. tableModelProperties.setCellEditable(0, 1, false);
  656. tableModelProperties.setCellEditable(2, 1, true);
  657. } else if (getActualCps() == null) {
  658. deleteRows();
  659. }
  660. }
  661. });
  662. toolBar.add(btnAdd);
  663. // Del Button
  664. btnDel.addActionListener(new ActionListener() {
  665. public void actionPerformed(ActionEvent arg0) {
  666. Object nodeInfo = tree.getLastSelectedPathComponent();
  667. if (nodeInfo != null) {
  668. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  669. String nodeName = selectedNode.getUserObject().toString();
  670. int depthOfNode = selectedNode.getLevel();
  671. switch (depthOfNode) {
  672. case 1:
  673. int dialogResult = JOptionPane.showConfirmDialog(null,
  674. "Do you realy want to delete the Category " + nodeName + "?", "Warning",
  675. JOptionPane.YES_NO_OPTION);
  676. if (dialogResult == JOptionPane.YES_OPTION) {
  677. controller.deleteCategory(nodeName);
  678. }
  679. break;
  680. case 2:
  681. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  682. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  683. break;
  684. default:
  685. JOptionPane.showMessageDialog(new JFrame(),
  686. "Please select a Category or an Object in order to delete something.");
  687. }
  688. } else {
  689. JOptionPane.showMessageDialog(new JFrame(),
  690. "Please select a Category or an Object in order to delete something.");
  691. }
  692. tree.repaint();
  693. }
  694. });
  695. toolBar.add(btnDel);
  696. frmCyberPhysical.getContentPane().add(splitPane);
  697. mntmOpen.addActionListener(new java.awt.event.ActionListener() {
  698. @Override
  699. public void actionPerformed(java.awt.event.ActionEvent evt) {
  700. menuFileExitActionPerformed(evt);
  701. }
  702. private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
  703. JFileChooser fileChooser = new JFileChooser();
  704. JFrame test = new JFrame();
  705. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  706. File file = fileChooser.getSelectedFile();
  707. try {
  708. controller.loadFile(file.getAbsolutePath());
  709. canvas.repaint();
  710. tree.repaint();
  711. } catch (IOException e) {
  712. // TODO Auto-generated catch block
  713. e.printStackTrace();
  714. }
  715. }
  716. }
  717. });
  718. mntmSave.addActionListener(new java.awt.event.ActionListener() {
  719. @Override
  720. public void actionPerformed(java.awt.event.ActionEvent evt) {
  721. menuSaveActionPerformed(evt);
  722. }
  723. private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
  724. JFileChooser fileChooser = new JFileChooser();
  725. JFrame test = new JFrame();
  726. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  727. File file = fileChooser.getSelectedFile();
  728. try {
  729. controller.saveFile(file.getAbsolutePath());
  730. } catch (IOException e) {
  731. // TODO Auto-generated catch block
  732. e.printStackTrace();
  733. }
  734. }
  735. }
  736. });
  737. timePanel = new TimePanel(model, controller);
  738. timePanel.setBorder(null);
  739. ((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
  740. @Override
  741. public void stateChanged(ChangeEvent e) {
  742. unitGraph.repaint();
  743. }
  744. });
  745. splitPane.setRightComponent(splitPane_1);
  746. splitPane.setDividerLocation(200);
  747. splitPane_1.setDividerLocation(500);
  748. splitPane.setLeftComponent(scrollPane_1);
  749. splitPane_1.setLeftComponent(tabbedPane);
  750. splitPane_1.setRightComponent(split_HolonEl_Pro);
  751. split_HolonEl_Pro.setDividerLocation(600);
  752. split_HolonEl_Pro.setTopComponent(split_Graph_HolonEl);
  753. split_HolonEl_Pro.setBottomComponent(scrollProperties);
  754. split_Graph_HolonEl.setDividerLocation(150);
  755. split_Graph_HolonEl.setTopComponent(scrollGraph);
  756. split_Graph_HolonEl.setBottomComponent(scrollElements);
  757. tabbedPane.setBorder(null);
  758. scrollProperties.setBorder(null);
  759. scrollGraph.setBorder(null);
  760. scrollElements.setBorder(null);
  761. splitPane.setBorder(null);
  762. splitPane_1.setBorder(null);
  763. split_HolonEl_Pro.setBorder(null);
  764. split_Graph_HolonEl.setBorder(null);
  765. scrollPane_2.setBorder(null);
  766. panel_HolonEl.setBorder(null);
  767. tableHolonElementScrollPane.setBorder(null);
  768. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  769. }
  770. /*
  771. * adds a specific object type to selected Category also handles input
  772. * windows and illegal inputs
  773. */
  774. public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  775. if (selectedNode == null) {
  776. JOptionPane.showMessageDialog(new JFrame(),
  777. "Please select a Category first before adding " + objType + ".");
  778. }
  779. // if selected node is a directory for Categories
  780. else {
  781. if (selectedNode.getLevel() == 1) {
  782. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  783. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  784. if (objname.length() != 0) {
  785. switch (objType) {
  786. case "Switch":
  787. controller.addSwitch(cat, objname);
  788. break;
  789. case "Transformer":
  790. controller.addTransformer(cat, objname);
  791. break;
  792. }
  793. }
  794. } else {
  795. JOptionPane.showMessageDialog(new JFrame(),
  796. "Objects can not be added to Objects. Please select a Category.");
  797. }
  798. }
  799. }
  800. /**
  801. * reloads the Categories from Model
  802. */
  803. public void updateCategories(final ArrayList<Category> categories) {
  804. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  805. {
  806. DefaultMutableTreeNode node_1;
  807. for (Category c : categories) {
  808. node_1 = new DefaultMutableTreeNode(c.getName());
  809. // kann eventuell umgeändert werden
  810. for (CpsObject obj : c.getObjects()) {
  811. node_1.add(new DefaultMutableTreeNode(obj.getObjName()));
  812. }
  813. add(node_1);
  814. }
  815. }
  816. }));
  817. }
  818. /**
  819. *
  820. */
  821. public void onChange(ArrayList<Category> categories) {
  822. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  823. updateCategories(categories);
  824. model.reload();
  825. }
  826. /**
  827. *
  828. * @return
  829. */
  830. public JFrame getFrmCyberPhysical() {
  831. return frmCyberPhysical;
  832. }
  833. /**
  834. * Getter for selected CpsObject
  835. *
  836. * @return selected CpsObject
  837. */
  838. public CpsObject getActualCps() {
  839. int tempID = model.getSelectedObjectID();
  840. CpsObject tempCps = controller.searchByID(tempID);
  841. return tempCps;
  842. }
  843. /**
  844. * Search for actual selected HolonElement
  845. *
  846. * @param obj
  847. * selected HolonObject
  848. * @param yValue
  849. * Y-Coord in the HolonElementsTable
  850. * @return the selected HolonElement
  851. */
  852. public HolonElement getActualHolonElement(HolonObject obj, int yValue) {
  853. final int yTemp = (int) Math.floor(yValue / 16);
  854. int rowsTotal = tableModelHolonElement.getRowCount();
  855. if (rowsTotal != 0 && rowsTotal > yTemp) {
  856. model.setSelectedHolonElement(obj.getElements().get(yTemp));
  857. return obj.getElements().get(yTemp);
  858. } else {
  859. model.setSelectedHolonElement(null);
  860. return null;
  861. }
  862. }
  863. /**
  864. * Update the HolonElement Table, that means erase all rows and add the new
  865. * rows with new data
  866. */
  867. public void refreshTableHolonElement() {
  868. // Update of the Information about the HolonElements - only for
  869. // HolonObjects
  870. deleteRows();
  871. if (getActualCps() != null) {
  872. fillElementTable(((HolonObject) getActualCps()).getElements());
  873. }
  874. tableModelHolonElement.fireTableDataChanged();
  875. }
  876. /**
  877. * Erase all information of the HolonElement Model
  878. */
  879. public void deleteRows() {
  880. if (tableModelHolonElement.getRowCount() > 0) {
  881. for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
  882. tableModelHolonElement.removeRow(i);
  883. }
  884. }
  885. }
  886. /**
  887. * Add the Information of the given ArrayList in the HolonElement Model as
  888. * Name,Energy and Amount
  889. *
  890. * @param elements
  891. * ArrayList to be displayed
  892. */
  893. public void fillElementTable(ArrayList<HolonElement> elements) {
  894. for (HolonElement he : elements) {
  895. Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
  896. tableModelHolonElement.addRow(temp);
  897. }
  898. }
  899. /**
  900. * Update the information concerning properties of the selected CpsObject
  901. */
  902. public void refreshTableProperties() {
  903. CpsObject tempCps = getActualCps();
  904. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  905. tableModelProperties.removeRow(2);
  906. Object[] tempEnergy = { "Total Energy", ((HolonObject) tempCps).getCurrentEnergy() };
  907. tableModelProperties.insertRow(2, tempEnergy);
  908. }
  909. }
  910. private static void addPopup(Component component, final JPopupMenu popup) {
  911. component.addMouseListener(new MouseAdapter() {
  912. public void mousePressed(MouseEvent e) {
  913. if (e.isPopupTrigger()) {
  914. showMenu(e);
  915. }
  916. }
  917. public void mouseReleased(MouseEvent e) {
  918. if (e.isPopupTrigger()) {
  919. showMenu(e);
  920. }
  921. }
  922. private void showMenu(MouseEvent e) {
  923. popup.show(e.getComponent(), e.getX(), e.getY());
  924. }
  925. });
  926. }
  927. }