GUI.java 33 KB

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