GUI.java 38 KB

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