GUI.java 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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.KeyEvent;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import java.beans.PropertyChangeEvent;
  16. import java.beans.PropertyChangeListener;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import javax.swing.AbstractAction;
  22. import javax.swing.ActionMap;
  23. import javax.swing.BoxLayout;
  24. import javax.swing.DefaultComboBoxModel;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.InputMap;
  27. import javax.swing.JButton;
  28. import javax.swing.JCheckBoxMenuItem;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JComponent;
  31. import javax.swing.JDialog;
  32. import javax.swing.JEditorPane;
  33. import javax.swing.JFileChooser;
  34. import javax.swing.JFrame;
  35. import javax.swing.JLabel;
  36. import javax.swing.JMenu;
  37. import javax.swing.JMenuBar;
  38. import javax.swing.JMenuItem;
  39. import javax.swing.JOptionPane;
  40. import javax.swing.JPanel;
  41. import javax.swing.JPopupMenu;
  42. import javax.swing.JScrollPane;
  43. import javax.swing.JSlider;
  44. import javax.swing.JSplitPane;
  45. import javax.swing.JTabbedPane;
  46. import javax.swing.JTable;
  47. import javax.swing.JTextField;
  48. import javax.swing.JToolBar;
  49. import javax.swing.JTree;
  50. import javax.swing.KeyStroke;
  51. import javax.swing.SwingUtilities;
  52. import javax.swing.border.LineBorder;
  53. import javax.swing.event.ChangeEvent;
  54. import javax.swing.event.ChangeListener;
  55. import javax.swing.filechooser.FileNameExtensionFilter;
  56. import javax.swing.table.DefaultTableModel;
  57. import javax.swing.tree.DefaultMutableTreeNode;
  58. import javax.swing.tree.DefaultTreeModel;
  59. import javax.swing.tree.TreeCellRenderer;
  60. import Interfaces.CategoryListener;
  61. import classes.Category;
  62. import classes.CpsEdge;
  63. import classes.CpsObject;
  64. import classes.HolonElement;
  65. import classes.HolonObject;
  66. import classes.HolonSwitch;
  67. import classes.HolonTransformer;
  68. import ui.controller.Control;
  69. import ui.model.Model;;
  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 String catOfObjToBeEdited;
  91. private final JLabel maxGraph = new JLabel("100%");
  92. private final JLabel medGraph = new JLabel("50%");
  93. private final JLabel minGraph = new JLabel("0%");
  94. private final JLabel elementGraph = new JLabel("None ");
  95. private final ArrayList<HolonElement> selectedElements = new ArrayList<HolonElement>();
  96. private String holonEleNamesDisplayed = "None ";
  97. HashMap<Integer, ArrayList<HolonElement>> eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  98. private final JTree tree = new JTree();
  99. private final JEditorPane dtrpnHereWillBe = new JEditorPane();
  100. /******************************************
  101. ************* Right Container*************
  102. ******************************************/
  103. // Right Container: here comes the information about the HolonObject, such
  104. // as HolonElements Information, Properties and Consumption/Production graph
  105. private final JSplitPane split_HolonEl_Pro = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  106. private final JSplitPane split_Graph_HolonEl = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  107. // In this section are all the Holonelements that correspond to the clicked
  108. // HolonObject with consumption/production, name and amount.
  109. private JTable tableHolonElement = new JTable();
  110. private PropertyTable tableModelHolonElementMulti = new PropertyTable();
  111. private PropertyTable tableModelHolonElementSingle = new PropertyTable();
  112. private final JPanel scrollElements = new JPanel();
  113. private JScrollPane tableHolonElementScrollPane = new JScrollPane();
  114. // In this section are all the properties that correspond to the clicked
  115. // HolonObject, such as connections, name, Type, etc.
  116. private JTable tableProperties = new JTable();
  117. private JPanel graphLabel = new JPanel();
  118. private DefaulTable tableModelProperties;
  119. private final JScrollPane scrollProperties = new JScrollPane();
  120. // In this section is the graph for the selected HolonElement of the clicked
  121. // HolonObject
  122. private JTable tableGraph = new JTable();
  123. private DefaultTableModel tableModelGraph = new DefaultTableModel();
  124. private final JScrollPane scrollGraph = new JScrollPane();
  125. private Model model;
  126. private final Control controller;
  127. // Pop up Windows
  128. private AddObjectPopUp addObjectPopUP;
  129. private AboutUsPopUp aboutUsPopUp;
  130. private AddElementPopUp addElementPopUp;
  131. private final JPanel panel = new JPanel();
  132. private final JPanel panel_HolonEl = new JPanel();
  133. private final JComboBox comboBox = new JComboBox();
  134. // private final JComboBox comboBoxGraph = new JComboBox();
  135. // Buttons
  136. private final JButton btnAdd = new JButton("+");
  137. private final JButton btnDel = new JButton("-");
  138. private final JButton btnAddHolEL = new JButton("+");
  139. private final JButton btnDelHolEL = new JButton("-");
  140. private final JButton resetGraphBtn = new JButton("Reset");
  141. private final JToolBar toolBar = new JToolBar();
  142. private final JToolBar toolBarHolonEl = new JToolBar();
  143. private final JToolBar toolBarGraph = new JToolBar();
  144. // variables
  145. private boolean dragging = false;
  146. private String actualObjectClicked;
  147. private Image img = null;
  148. private CpsObject tempCps = null;
  149. private HolonElement tempElement = null;
  150. private int yValueElements = 0;
  151. private MyCanvas canvas;
  152. private UnitGraph unitGraph;
  153. private final JSplitPane splitPane_3 = new JSplitPane();
  154. private final JSlider sizeSlider = new JSlider();
  155. private final JLabel lblImageSize = new JLabel("Image Size");
  156. // Time Stuff
  157. private TimePanel timePanel;
  158. private final JMenu mnAlgorithm = new JMenu("Algorithm");
  159. private final JCheckBoxMenuItem chckbxmntmUseAlgorithm = new JCheckBoxMenuItem("Use Algorithm");
  160. private final JSplitPane splitPane_2 = new JSplitPane();
  161. private final JLabel lblSelect = new JLabel("Select");
  162. private final JComboBox comboBoxAlgo = new JComboBox();
  163. private int yTHIS;
  164. private int xTHIS;
  165. private int yBTHIS;
  166. private int xBTHIS;
  167. private CpsObject temp = null;
  168. private final JButton btnTest = new JButton("test");
  169. private final JMenuItem mntmUndo = new JMenuItem("Undo");
  170. private final JMenuItem mntmRedo = new JMenuItem("Redo");
  171. private final JMenuItem mntmEditEdges = new JMenuItem("Edit Edges");
  172. private final JMenuItem mntmFindReplace = new JMenuItem("Find/ Replace");
  173. private final JMenuItem mntmEditShowedInformation = new JMenuItem("Edit showed Information");
  174. private final JMenuItem mntmResetCategory = new JMenuItem("Reset Categories");
  175. /**
  176. * Create the application.
  177. */
  178. public GUI(Control control) {
  179. this.controller = control;
  180. this.model = control.getModel();
  181. this.canvas = new MyCanvas(model, control);
  182. this.unitGraph = new UnitGraph(model, control);
  183. control.initListener(this);
  184. initialize();
  185. updateCategories(model.getCategories());
  186. controller.setCanvas(canvas);
  187. }
  188. /**
  189. * Initialize the contents of the frame.
  190. */
  191. @SuppressWarnings({ "serial", "unchecked" })
  192. private void initialize() {
  193. frmCyberPhysical = new JFrame();
  194. frmCyberPhysical.setTitle("Cyber Physical Systems Model");
  195. frmCyberPhysical.setBounds(100, 100, 1000, 800);
  196. frmCyberPhysical.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  197. frmCyberPhysical.addWindowListener(new java.awt.event.WindowAdapter() {
  198. @Override
  199. public void windowClosing(java.awt.event.WindowEvent windowEvent) {
  200. if (JOptionPane.showConfirmDialog(frmCyberPhysical, "Are you sure you want to exit?",
  201. "Cyber Physical Systems Model", JOptionPane.YES_NO_OPTION,
  202. JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
  203. deleteDirectory(new File(System.getProperty("user.home") + "/HolonGUI/Autosave"));
  204. System.exit(0);
  205. }
  206. }
  207. private void deleteDirectory(File path) {
  208. if (path.exists()) {
  209. File[] files = path.listFiles();
  210. for (int i = 0; i < files.length; i++) {
  211. if (files[i].isDirectory()) {
  212. deleteDirectory(files[i]);
  213. } else {
  214. files[i].delete();
  215. }
  216. }
  217. path.delete();
  218. }
  219. }
  220. });
  221. JPanel contentPane = (JPanel) frmCyberPhysical.getContentPane();
  222. int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
  223. InputMap inputMap = contentPane.getInputMap(condition);
  224. ActionMap actionMap = contentPane.getActionMap();
  225. String cntrlZDown = "controlZ";
  226. inputMap.put(KeyStroke.getKeyStroke("control Z"), cntrlZDown);
  227. actionMap.put(cntrlZDown, new AbstractAction() {
  228. @Override
  229. public void actionPerformed(ActionEvent e) {
  230. try {
  231. controller.loadFile(controller.getUndoSave());
  232. canvas.repaint();
  233. ArrayList<HolonElement> tempList = new ArrayList<>();
  234. for (CpsObject cps : model.getObjectsOnCanvas()) {
  235. if (cps instanceof HolonObject) {
  236. for (HolonElement h : ((HolonObject) cps).getElements()) {
  237. tempList.add(h);
  238. unitGraph.repaintWithNewElement(tempList);
  239. unitGraph.fillArrayofValue();
  240. tempList.remove(0);
  241. }
  242. } else if (cps instanceof HolonSwitch) {
  243. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  244. unitGraph.fillArrayofBooleans();
  245. }
  246. }
  247. unitGraph.empty();
  248. } catch (IOException f) {
  249. // TODO Auto-generated catch block
  250. f.printStackTrace();
  251. }
  252. }
  253. });
  254. String cntrlYDown = "controlY";
  255. inputMap.put(KeyStroke.getKeyStroke("control Y"), cntrlYDown);
  256. actionMap.put(cntrlYDown, new AbstractAction() {
  257. @Override
  258. public void actionPerformed(ActionEvent e) {
  259. try {
  260. controller.loadFile(controller.getRedoSave());
  261. canvas.repaint();
  262. ArrayList<HolonElement> tempList = new ArrayList<>();
  263. for (CpsObject cps : model.getObjectsOnCanvas()) {
  264. if (cps instanceof HolonObject) {
  265. for (HolonElement h : ((HolonObject) cps).getElements()) {
  266. tempList.add(h);
  267. unitGraph.repaintWithNewElement(tempList);
  268. unitGraph.fillArrayofValue();
  269. tempList.remove(0);
  270. }
  271. } else if (cps instanceof HolonSwitch) {
  272. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  273. unitGraph.fillArrayofBooleans();
  274. }
  275. }
  276. unitGraph.empty();
  277. } catch (IOException f) {
  278. // TODO Auto-generated catch block
  279. f.printStackTrace();
  280. }
  281. }
  282. });
  283. String delDown = "delete";
  284. inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false), delDown);
  285. actionMap.put(delDown, new AbstractAction() {
  286. @Override
  287. public void actionPerformed(ActionEvent e) {
  288. for (CpsObject cps : model.getSelectedCpsObjects()) {
  289. controller.delCanvasObject(cps);
  290. }
  291. model.getSelectedCpsObjects().clear();
  292. /*
  293. * controller.setSelectedObjectID(0); for (int i =
  294. * tableModelHolonElementMulti.getRowCount() - 1; i > -1; i--) {
  295. * tableModelHolonElementMulti.removeRow(i); } for (int i =
  296. * tableModelHolonElementSingle.getRowCount() - 1; i > -1; i--)
  297. * { tableModelHolonElementSingle.removeRow(i); } for (int i =
  298. * tableModelProperties.getRowCount() - 1; i > -1; i--) {
  299. * tableModelProperties.removeRow(i); }
  300. */
  301. canvas.repaint();
  302. unitGraph.empty();
  303. }
  304. });
  305. String cntrlCDown = "controlC";
  306. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  307. actionMap.put(cntrlCDown, new AbstractAction() {
  308. @Override
  309. public void actionPerformed(ActionEvent e) {
  310. if (!model.getSelectedCpsObjects().isEmpty()) {
  311. System.out.println("copy");
  312. controller.copyObjects();
  313. if (!model.getClipboradObjects().isEmpty()) {
  314. canvas.itemPaste.setEnabled(true);
  315. }
  316. }
  317. }
  318. });
  319. String cntrlVDown = "controlV";
  320. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  321. actionMap.put(cntrlVDown, new AbstractAction() {
  322. @Override
  323. public void actionPerformed(ActionEvent e) {
  324. controller.pasteObjects(canvas.getMousePosition());
  325. canvas.repaint();
  326. }
  327. });
  328. String cntrlXDown = "controlX";
  329. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  330. actionMap.put(cntrlXDown, new AbstractAction() {
  331. @Override
  332. public void actionPerformed(ActionEvent e) {
  333. if (!model.getSelectedCpsObjects().isEmpty()) {
  334. controller.cutObjects();
  335. if (!model.getClipboradObjects().isEmpty()) {
  336. canvas.itemPaste.setEnabled(true);
  337. }
  338. canvas.repaint();
  339. System.out.println("cut");
  340. }
  341. }
  342. });
  343. frmCyberPhysical.setJMenuBar(menuBar);
  344. frmCyberPhysical.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  345. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  346. menuBar.add(mnNewMenu);
  347. mnNewMenu.add(mntmNew);
  348. mnNewMenu.add(mntmOpen);
  349. mnNewMenu.add(mntmSave);
  350. menuBar.add(mnNewMenu_1);
  351. mnNewMenu_1.add(mntmUndo);
  352. mnNewMenu_1.add(mntmRedo);
  353. mntmFindReplace.addActionListener(new ActionListener() {
  354. public void actionPerformed(ActionEvent e) {
  355. try {
  356. searchPopUp dialog = new searchPopUp(controller, canvas);
  357. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  358. dialog.setVisible(true);
  359. } catch (Exception e1) {
  360. e1.printStackTrace();
  361. }
  362. }
  363. });
  364. mnNewMenu_1.add(mntmFindReplace);
  365. mntmEditShowedInformation.addActionListener(new ActionListener() {
  366. public void actionPerformed(ActionEvent e) {
  367. try {
  368. ShowedInformationPopUp dialog = new ShowedInformationPopUp(canvas);
  369. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  370. dialog.setVisible(true);
  371. } catch (Exception e1) {
  372. e1.printStackTrace();
  373. }
  374. }
  375. });
  376. mnNewMenu_1.add(mntmEditShowedInformation);
  377. menuBar.add(mnNewMenu_2);
  378. mntmEditEdges.addActionListener(new ActionListener() {
  379. public void actionPerformed(ActionEvent e) {
  380. EditEdgesPopUp edgePopUp = new EditEdgesPopUp();
  381. edgePopUp.setCanvas(canvas);
  382. edgePopUp.setController(controller);
  383. edgePopUp.setVisible(true);
  384. }
  385. });
  386. mnNewMenu_2.add(mntmEditEdges);
  387. mnNewMenu_2.add(mntmResetCategory);
  388. mntmResetCategory.addActionListener(new ActionListener() {
  389. @Override
  390. public void actionPerformed(ActionEvent e) {
  391. ArrayList<Category> cat = model.getCategories();
  392. while (!cat.isEmpty()) {
  393. controller.deleteCategory(cat.get(0).getName());
  394. ;
  395. }
  396. controller.resetCategorys();
  397. tree.repaint();
  398. }
  399. });
  400. menuBar.add(mnNewMenu_3);
  401. mnNewMenu_3.add(canvasSize);
  402. canvasSize.addActionListener(new ActionListener() {
  403. @Override
  404. public void actionPerformed(ActionEvent e) {
  405. JPanel myPanel = new JPanel();
  406. JTextField field1 = new JTextField("" + canvas.getWidth() + "");
  407. JTextField field2 = new JTextField("" + canvas.getHeight() + "");
  408. myPanel.add(field1);
  409. myPanel.add(field2);
  410. JOptionPane.showMessageDialog(null, myPanel);
  411. canvas.setPreferredSize(
  412. new Dimension(Integer.parseInt(field1.getText()), Integer.parseInt(field2.getText())));
  413. canvas.repaint();
  414. }
  415. });
  416. mnNewMenu_3.add(splitPane_3);
  417. sizeSlider.setMinimum(15);
  418. sizeSlider.setMaximum(115);
  419. sizeSlider.addChangeListener(new ChangeListener() {
  420. @Override
  421. public void stateChanged(ChangeEvent e) {
  422. controller.setScale(sizeSlider.getValue());
  423. tree.setRowHeight(model.getScale());
  424. canvas.objectSelectionHighlighting();
  425. canvas.repaint();
  426. }
  427. });
  428. splitPane_3.setRightComponent(sizeSlider);
  429. splitPane_3.setLeftComponent(lblImageSize);
  430. menuBar.add(mnHelp);
  431. mnHelp.add(aboutUs);
  432. menuBar.add(mnAlgorithm);
  433. mnAlgorithm.add(chckbxmntmUseAlgorithm);
  434. mnAlgorithm.add(splitPane_2);
  435. splitPane_2.setLeftComponent(lblSelect);
  436. splitPane_2.setRightComponent(comboBoxAlgo);
  437. canvas.setBackground(Color.WHITE);
  438. canvas.setPreferredSize(new Dimension(1000, 1000));
  439. JScrollPane canvasSP = new JScrollPane(canvas);
  440. canvasSP.setBorder(null);
  441. tabbedPane.addTab("Modeling", new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/home.png"))
  442. .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)), canvasSP, "Model a CPS");
  443. tabbedPane.addTab("Simulation",
  444. new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  445. .getScaledInstance(30, 30, Image.SCALE_SMOOTH)),
  446. scrollPane_2, "Simulate the CPS");
  447. dtrpnHereWillBe.setText("Here will be the Simulation");
  448. scrollPane_2.setViewportView(dtrpnHereWillBe);
  449. /********************
  450. * RIGHT CONTAINER (INFORMATION)
  451. **********************/
  452. // Set up of the HolonElements section
  453. Object[] columnNamesMulti = { "Object", "Device", "Energy", "Quantity", "Activated" };
  454. tableModelHolonElementMulti.setColumnIdentifiers(columnNamesMulti);
  455. Object[] columnNamesSingle = { "Device", "Energy", "Quantity", "Activated" };
  456. tableModelHolonElementSingle.setColumnIdentifiers(columnNamesSingle);
  457. tableHolonElement.setBorder(null);
  458. tableHolonElement.setFillsViewportHeight(true);
  459. tableHolonElement.setCellSelectionEnabled(true);
  460. tableHolonElement.setColumnSelectionAllowed(true);
  461. tableHolonElementScrollPane.setViewportView(tableHolonElement);
  462. scrollElements.setLayout(new BorderLayout(0, 0));
  463. scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
  464. scrollElements.add(tableHolonElementScrollPane);
  465. panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
  466. toolBarHolonEl.add(btnAddHolEL);
  467. toolBarHolonEl.add(btnDelHolEL);
  468. toolBarHolonEl.setFloatable(false);
  469. panel_HolonEl.add(toolBarHolonEl);
  470. // Set up of the Properties section
  471. Object[] colNames = { "Field", "Information" };
  472. tableModelProperties = new DefaulTable(100, colNames.length);
  473. tableModelProperties.setColumnIdentifiers(colNames);
  474. tableProperties.setModel(tableModelProperties);
  475. tableProperties.setFillsViewportHeight(true);
  476. tableProperties.setCellSelectionEnabled(true);
  477. tableProperties.setColumnSelectionAllowed(true);
  478. scrollProperties.setViewportView(tableProperties);
  479. // Set up of the Graph section
  480. Object[] tempText = { "Here comes the graph for each clicked HolonElement" };
  481. tableModelGraph.setColumnIdentifiers(tempText);
  482. tableGraph.setModel(tableModelGraph);
  483. tableGraph.setFillsViewportHeight(true);
  484. tableGraph.setCellSelectionEnabled(true);
  485. tableGraph.setColumnSelectionAllowed(true);
  486. scrollGraph.setViewportView(unitGraph);
  487. graphLabel.setLayout(new BorderLayout(0, 10));
  488. graphLabel.add(maxGraph, BorderLayout.NORTH);
  489. graphLabel.add(medGraph, BorderLayout.CENTER);
  490. graphLabel.add(minGraph, BorderLayout.SOUTH);
  491. toolBarGraph.add(elementGraph);
  492. toolBarGraph.add(resetGraphBtn);
  493. toolBarGraph.setFloatable(false);
  494. scrollGraph.setRowHeaderView(graphLabel);
  495. scrollGraph.setColumnHeaderView(toolBarGraph);
  496. /***********************
  497. * HolonElement Table Actions
  498. **********************/
  499. /*
  500. * Add HolonElement to given HolonObject
  501. */
  502. btnAddHolEL.addActionListener(new ActionListener() {
  503. public void actionPerformed(ActionEvent arg0) {
  504. if (model.getSelectedCpsObjects().size() == 1) {
  505. CpsObject tempCpsObject = getActualCps();
  506. if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
  507. && tempCpsObject.getID() != 0) {
  508. addElementPopUp = new AddElementPopUp();
  509. addElementPopUp.setActualCps(getActualCps());
  510. addElementPopUp.setVisible(true);
  511. HolonElement ele = addElementPopUp.getElement();
  512. controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
  513. ele.getEnergy());
  514. refreshTableHolonElement();
  515. refreshTableProperties();
  516. controller.calculateStateForTimeStep(model.getCurIteration());
  517. }
  518. }
  519. }
  520. });
  521. /*
  522. * Delete the choosen HolonElement of the selected HolonObject
  523. */
  524. btnDelHolEL.addActionListener(new ActionListener() {
  525. public void actionPerformed(ActionEvent arg0) {
  526. if (model.getSelectedCpsObjects().size() == 1) {
  527. if (getActualCps().getClass() == HolonObject.class) {
  528. HolonObject obj = (HolonObject) getActualCps();
  529. for (HolonElement e : selectedElements) {
  530. controller.deleteElementCanvas(obj.getID(), e.getEleName());
  531. refreshTableHolonElement();
  532. refreshTableProperties();
  533. controller.calculateStateForTimeStep(model.getCurIteration());
  534. // Names displayed in graph are not updated
  535. }
  536. }
  537. } else if (model.getSelectedCpsObjects().size() > 1) {
  538. for (Integer i : eleToDelete.keySet()) {
  539. for (HolonElement e : eleToDelete.get(i)) {
  540. controller.deleteElementCanvas(i, e.getEleName());
  541. }
  542. }
  543. refreshTableHolonElement();
  544. refreshTableProperties();
  545. }
  546. }
  547. });
  548. /*
  549. * Communication between HolonElement Table and displayed Graph
  550. */
  551. tableHolonElement.addMouseListener(new MouseAdapter() {
  552. public void mousePressed(MouseEvent e) {
  553. HolonObject obj = (HolonObject) getActualCps();
  554. yValueElements = e.getY();
  555. HolonElement ele = null;
  556. if (model.getSelectedCpsObjects().size() == 1) {
  557. ele = getActualHolonElement(obj, yValueElements, 0);
  558. } else {
  559. ele = getActualHolonElement(null, yValueElements, 0);
  560. }
  561. if (e.isControlDown() && ele != null) {
  562. if (!selectedElements.contains(ele)) {
  563. selectedElements.add(ele);
  564. if (!holonEleNamesDisplayed.equals("None ")) {
  565. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  566. } else {
  567. holonEleNamesDisplayed = ele.getEleName() + " ";
  568. }
  569. unitGraph.repaintWithNewElement(selectedElements);
  570. }
  571. getActualHolonElement(null, yValueElements, 2);
  572. } else if (ele != null) {
  573. selectedElements.clear();
  574. selectedElements.add(ele);
  575. getActualHolonElement(null, yValueElements, 1);
  576. holonEleNamesDisplayed = ele.getEleName() + " ";
  577. unitGraph.repaintWithNewElement(selectedElements);
  578. } else {
  579. elementGraph.setText("None ");
  580. unitGraph.empty();
  581. }
  582. // if any HolonElement is double-clicked --> Edit instance of
  583. // selected HolonElement
  584. if (e.getClickCount() == 2) {
  585. yTHIS = e.getY();
  586. xTHIS = e.getX();
  587. }
  588. if (e.getClickCount() == 1 && ele == null) {
  589. selectedElements.clear();
  590. holonEleNamesDisplayed = "None ";
  591. }
  592. // for (int i = 0; i < selectedElements.size(); i++) {
  593. // if (i == 0) {
  594. // System.out.println("Selected Items: " +
  595. // selectedElements.get(i).getEleName());
  596. // } else {
  597. // System.out.println(selectedElements.get(i).getEleName());
  598. // }
  599. // }
  600. elementGraph.setText(holonEleNamesDisplayed);
  601. yBTHIS = e.getY();
  602. xBTHIS = e.getX();
  603. }
  604. });
  605. /*
  606. * If the HolonElement Table enters to editing instance, than is the
  607. * propertyChangeListener triggered
  608. */
  609. tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
  610. @Override
  611. public void propertyChange(PropertyChangeEvent evt) {
  612. try {
  613. int yMouse = yTHIS;
  614. int yBMouse = yBTHIS;
  615. int selectedValueY = (int) Math.floor(yMouse / 16);
  616. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  617. if (model.getSelectedCpsObjects().size() > 1) {
  618. // Multiple Selection
  619. int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 5));
  620. int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 5));
  621. if (getHolonObj(yMouse) != null) {
  622. if (selectedValueBX == 4) {
  623. HolonElement eleBTemp = getActualHolonElement(null, yBMouse, 0);
  624. String newBStuff = tableModelHolonElementMulti
  625. .getValueAt(selectedValueBY, selectedValueBX).toString();
  626. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  627. eleBTemp.setActive(bTemp);
  628. } else {
  629. HolonElement eleTemp = getActualHolonElement(null, yMouse, 0);
  630. String newStuff = tableModelHolonElementMulti.getValueAt(selectedValueY, selectedValueX)
  631. .toString();
  632. // Small bug, when it comes to edit the name of
  633. // the HolonElement
  634. if (selectedValueX == 1) {
  635. eleTemp.setEleName(newStuff);
  636. } else if (selectedValueX == 2) {
  637. Float ftemp = Float.parseFloat(newStuff);
  638. eleTemp.setEnergy(ftemp);
  639. } else if (selectedValueX == 3) {
  640. Integer iTemp = Integer.parseInt(newStuff);
  641. eleTemp.setAmount(iTemp);
  642. }
  643. }
  644. }
  645. } else if (model.getSelectedCpsObjects().size() == 1) {
  646. // Single Selection
  647. int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
  648. int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
  649. if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
  650. if (selectedValueBX == 3) {
  651. HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse, 0);
  652. String newBStuff = tableModelHolonElementSingle
  653. .getValueAt(selectedValueBY, selectedValueBX).toString();
  654. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  655. eleBTemp.setActive(bTemp);
  656. } else {
  657. HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse, 0);
  658. String newStuff = tableModelHolonElementSingle
  659. .getValueAt(selectedValueY, selectedValueX).toString();
  660. // Small bug, when it comes to edit the name of
  661. // the HolonElement
  662. if (selectedValueX == 0) {
  663. eleTemp.setEleName(newStuff);
  664. } else if (selectedValueX == 1) {
  665. Float ftemp = Float.parseFloat(newStuff);
  666. eleTemp.setEnergy(ftemp);
  667. } else if (selectedValueX == 2) {
  668. Integer iTemp = Integer.parseInt(newStuff);
  669. eleTemp.setAmount(iTemp);
  670. }
  671. }
  672. refreshTableProperties();
  673. tableModelHolonElementSingle.fireTableDataChanged();
  674. controller.calculateStateForTimeStep(model.getCurIteration());
  675. unitGraph.repaint();
  676. }
  677. }
  678. } catch (Exception e) {
  679. }
  680. }
  681. });
  682. /***********************
  683. * HolonElement Properties Actions
  684. **********************/
  685. /*
  686. * If any value at the Properties Table enters to editing instance, than
  687. * is PropertyChangeListener triggered (For HolonObject, the only value
  688. * to be edited is the name and for CpsEdge the Max.flow)
  689. */
  690. tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
  691. @Override
  692. public void propertyChange(PropertyChangeEvent evt) {
  693. try {
  694. Object temp;
  695. if (getActualCps() != null) {
  696. if (getActualCps() instanceof HolonObject) {
  697. temp = tableModelProperties.getValueAt(0, 1);
  698. getActualCps().setName(temp.toString());
  699. } else if (getActualCps() instanceof HolonTransformer) {
  700. // get Info of the Properties for Transformer
  701. }
  702. } else {
  703. temp = tableModelProperties.getValueAt(2, 1);
  704. Float ftemp = Float.parseFloat(temp.toString());
  705. model.getSelectedEdge().setCapacity(ftemp);
  706. }
  707. canvas.repaint();
  708. } catch (Exception e) {
  709. }
  710. }
  711. });
  712. /***********************
  713. * HolonElement Graph Actions
  714. **********************/
  715. /*
  716. * Reset the graph of the selected HolonElement
  717. */
  718. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  719. resetGraphBtn.addActionListener(new ActionListener() {
  720. public void actionPerformed(ActionEvent arg0) {
  721. unitGraph.reset();
  722. }
  723. });
  724. /*****************************
  725. * RIGHT CONTAINER DONE
  726. *****************************/
  727. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  728. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  729. @Override
  730. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  731. boolean leaf, int row, boolean hasFocus) {
  732. JLabel label = new JLabel();
  733. Image imgR = null;
  734. if (leaf) {
  735. for (Category cat : model.getCategories()) {
  736. for (CpsObject cps : cat.getObjects()) {
  737. if (value.toString().compareTo(cps.getObjName()) == 0) {
  738. File checkPath = new File(cps.getImage());
  739. if (checkPath.exists()) {
  740. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  741. controller.getScale(), controller.getScale(), java.awt.Image.SCALE_SMOOTH);
  742. } else {
  743. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  744. .getScaledInstance(controller.getScale(), controller.getScale(),
  745. java.awt.Image.SCALE_SMOOTH);
  746. }
  747. if (imgR != null) {
  748. label.setIcon(new ImageIcon(imgR));
  749. }
  750. label.setText(cps.getName());
  751. }
  752. }
  753. }
  754. }
  755. tree.setRowHeight(model.getScale());
  756. if (hasFocus) {
  757. label.setForeground(new Color(0, 0, 255));
  758. label.setOpaque(true);
  759. }
  760. if (label.getText().length() == 0) {
  761. label.setText(value.toString());
  762. if (value.toString().compareTo("Categories") != 0) {
  763. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  764. }
  765. }
  766. return label;
  767. }
  768. };
  769. tree.setCellRenderer(customRenderer);
  770. tree.addMouseListener(new MouseAdapter() {
  771. public void mouseReleased(MouseEvent e) {
  772. try {
  773. if (dragging) {
  774. int x = (int) canvas.getMousePosition().getX();
  775. int y = (int) canvas.getMousePosition().getY();
  776. CpsObject h = null;
  777. if (tempCps.getClass() == HolonObject.class) {
  778. h = new HolonObject(tempCps);
  779. }
  780. if (tempCps.getClass() == HolonSwitch.class) {
  781. h = new HolonSwitch(tempCps);
  782. }
  783. if (tempCps.getClass() == HolonTransformer.class) {
  784. h = new HolonTransformer(tempCps);
  785. }
  786. h.setPosition(x, y);
  787. controller.addObjectCanvas(h);
  788. canvas.repaint();
  789. dragging = false;
  790. }
  791. } catch (Exception e2) {
  792. }
  793. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  794. }
  795. });
  796. popmenuEdit.add(editItem);
  797. editItem.setEnabled(false);
  798. editItem.addActionListener(new ActionListener() {
  799. @Override
  800. public void actionPerformed(ActionEvent e) {
  801. }
  802. });
  803. tree.addMouseListener(new MouseAdapter() {
  804. public void mousePressed(MouseEvent e) {
  805. try {
  806. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  807. // if an Object was selected, the porperties are shown in
  808. // the table
  809. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  810. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  811. if (selectedNode.getLevel() == 2) {
  812. CpsObject selected = controller.searchCategoryObject(selectedNode.getParent().toString(),
  813. selectedNode.toString());
  814. deleteRows(tableModelHolonElementSingle);
  815. deleteRows(tableModelHolonElementMulti);
  816. // if (selected instanceof HolonObject && selected !=
  817. // null) {
  818. // selected = (HolonObject) selected;
  819. // fillElementTable(((HolonObject)
  820. // selected).getElements());
  821. // }
  822. }
  823. if (SwingUtilities.isRightMouseButton(e)) {
  824. for (Category cat : model.getCategories()) {
  825. for (CpsObject cps : cat.getObjects()) {
  826. if (actualObjectClicked.compareTo(cps.getObjName()) == 0
  827. && !(cps instanceof HolonSwitch)) {
  828. editItem.setEnabled(true);
  829. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  830. catOfObjToBeEdited = selectedNode.getParent().toString();
  831. tempCps = cps;
  832. }
  833. }
  834. }
  835. } else {
  836. for (Category cat : model.getCategories()) {
  837. for (CpsObject cps : cat.getObjects()) {
  838. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  839. File checkPath = new File(cps.getImage());
  840. if (checkPath.exists()) {
  841. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  842. controller.getScale(), controller.getScale(),
  843. java.awt.Image.SCALE_SMOOTH);
  844. } else {
  845. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  846. .getScaledInstance(controller.getScale(), controller.getScale(),
  847. java.awt.Image.SCALE_SMOOTH);
  848. }
  849. tempCps = cps;
  850. dragging = true;
  851. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  852. "Image");
  853. frmCyberPhysical.setCursor(cursor);
  854. }
  855. }
  856. }
  857. }
  858. } catch (Exception e2) {
  859. }
  860. }
  861. });
  862. editItem.addActionListener(new ActionListener() {
  863. @Override
  864. public void actionPerformed(ActionEvent e) {
  865. // Remove the selected Object object
  866. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
  867. addObjectPopUP.setCategory(catOfObjToBeEdited);
  868. addObjectPopUP.setController(controller);
  869. addObjectPopUP.setVisible(true);
  870. }
  871. });
  872. scrollPane_1.setViewportView(tree);
  873. scrollPane_1.setColumnHeaderView(panel);
  874. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  875. toolBar.setFloatable(false);
  876. panel.add(toolBar);
  877. toolBar.add(comboBox);
  878. comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Transformer", "Switch" }));
  879. // Add Button
  880. btnAdd.addActionListener(new ActionListener() {
  881. public void actionPerformed(ActionEvent arg0) {
  882. Object nodeInfo = tree.getLastSelectedPathComponent();
  883. String selectedOption = comboBox.getSelectedItem().toString();
  884. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  885. switch (selectedOption) {
  886. case "Category":
  887. String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
  888. if (catName.length() != 0) {
  889. controller.addCategory(catName);
  890. }
  891. break;
  892. case "Object":
  893. if (selectedNode == null) {
  894. JOptionPane.showMessageDialog(new JFrame(),
  895. "Please select a Category first before adding " + selectedOption + ".");
  896. }
  897. if (selectedNode.getLevel() == 1) {
  898. CpsObject tmp = new HolonObject("");
  899. addObjectPopUP = new AddObjectPopUp(false, tmp, null);
  900. addObjectPopUP.setVisible(true);
  901. addObjectPopUP.setController(controller);
  902. addObjectPopUP.setCategory(selectedNode.toString());
  903. }
  904. break;
  905. default:
  906. addObjectAction(selectedOption, selectedNode);
  907. break;
  908. }
  909. tree.repaint();
  910. }
  911. });
  912. /**
  913. * Pop up - About Us with some important information about the
  914. * developers, source and programming stuff
  915. */
  916. aboutUs.addMouseListener(new MouseAdapter() {
  917. @Override
  918. public void mousePressed(MouseEvent e) {
  919. aboutUsPopUp = new AboutUsPopUp();
  920. aboutUsPopUp.setVisible(true);
  921. }
  922. });
  923. /**
  924. * Update of every interaction between the user and the canvas (only on
  925. * the canvas). Basically the update of all the information concerning
  926. * the clicked HolonObject
  927. */
  928. canvas.addMouseListener(new MouseAdapter() {
  929. @Override
  930. public void mousePressed(MouseEvent e) {
  931. selectedElements.clear();
  932. holonEleNamesDisplayed = "None ";
  933. if (temp == null || temp.getID() != model.getSelectedObjectID()) {
  934. unitGraph.empty();
  935. elementGraph.setText("None ");
  936. }
  937. temp = getActualCps();
  938. // Update of the Information about the HolonElements - only for
  939. // if (temp instanceof HolonObject) {
  940. // refreshTableHolonElement();
  941. // }
  942. // Update of the Information about the Properties - only for
  943. // CpsObjects
  944. // Erase old data
  945. if (tableModelProperties.getRowCount() > 0) {
  946. for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
  947. tableModelProperties.removeRow(i);
  948. }
  949. }
  950. if (e.isControlDown() && temp != null) {
  951. if (model.getSelectedCpsObjects().contains(temp)) {
  952. controller.deleteSelectedObject(temp);
  953. } else {
  954. controller.addSelectedObject(temp);
  955. }
  956. }
  957. // } else if (e.isShiftDown() && model.getSelectedEdge() !=
  958. // null) {
  959. // selectedObjects.add(model.getSelectedEdge());
  960. // }
  961. // boolean nothingImportant = true;
  962. // for (CpsObject c : model.getSelectedCpsObjects()) {
  963. // if (nothingImportant) {
  964. // System.out.println("Elements: " + c.getName() + " and ID: " +
  965. // c.getID());
  966. // nothingImportant = false;
  967. // } else {
  968. // System.out.println(c.getName() + " and ID: " + c.getID());
  969. // }
  970. // }
  971. // Write new data
  972. if (temp != null) {
  973. Object[] tempName = { "Name", temp.getName() };
  974. tableModelProperties.addRow(tempName);
  975. Object[] tempId = { "ID", temp.getID() };
  976. tableModelProperties.addRow(tempId);
  977. if (temp instanceof HolonObject) {
  978. refreshTableHolonElement();
  979. Object[] tempEnergy = { "Total Energy", ((HolonObject) temp).getCurrentEnergy() };
  980. tableModelProperties.addRow(tempEnergy);
  981. // } else if (temp instanceof HolonTransformer) {
  982. // deleteRows();
  983. // Object[] tempRatioPerc = { "Ratio Type", true };
  984. // tableModelProperties.addRow(tempRatioPerc);
  985. } else if (temp instanceof HolonSwitch) {
  986. deleteRows(tableModelHolonElementSingle);
  987. deleteRows(tableModelHolonElementMulti);
  988. Object[] tempActive = { "Active", ((HolonSwitch) temp).getActiveAt()[model.getCurIteration()] };
  989. tableModelProperties.addRow(tempActive);
  990. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  991. elementGraph.setText(temp.getName());
  992. } else {
  993. deleteRows(tableModelHolonElementSingle);
  994. deleteRows(tableModelHolonElementMulti);
  995. }
  996. tableModelProperties.setCellEditable(0, 1, true);
  997. tableModelProperties.setCellEditable(2, 1, false);
  998. ArrayList<CpsEdge> temp_array = temp.getConnections();
  999. if (!temp_array.isEmpty()) {
  1000. boolean first = true;
  1001. for (CpsEdge temp2 : temp_array) {
  1002. if (first) {
  1003. first = false;
  1004. if (temp.getID() != temp2.getA().getID()) {
  1005. Object[] tempConnection = { temp.getName() + " is connected to",
  1006. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  1007. tableModelProperties.addRow(tempConnection);
  1008. } else {
  1009. Object[] tempConnection = { temp.getName() + " is connected to",
  1010. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  1011. tableModelProperties.addRow(tempConnection);
  1012. }
  1013. } else {
  1014. if (temp.getID() != temp2.getA().getID()) {
  1015. Object[] tempConnection = { "",
  1016. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  1017. tableModelProperties.addRow(tempConnection);
  1018. } else {
  1019. Object[] tempConnection = { "",
  1020. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  1021. tableModelProperties.addRow(tempConnection);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. } else if (model.getSelectedEdge() != null) {
  1027. Object[] tempName = { "Name", "Edge: " + model.getSelectedEdge().getA().getName() + " to "
  1028. + model.getSelectedEdge().getB().getName() };
  1029. tableModelProperties.addRow(tempName);
  1030. Object[] tempFlow = { "Current flow", model.getSelectedEdge().getFlow() };
  1031. tableModelProperties.addRow(tempFlow);
  1032. Object[] tempCapacity = { "Max. Capacity", model.getSelectedEdge().getCapacity() };
  1033. tableModelProperties.addRow(tempCapacity);
  1034. tableModelProperties.setCellEditable(0, 1, false);
  1035. tableModelProperties.setCellEditable(2, 1, true);
  1036. } else if (getActualCps() == null) {
  1037. deleteRows(tableModelHolonElementSingle);
  1038. deleteRows(tableModelHolonElementMulti);
  1039. }
  1040. if (model.getSelectedCpsObjects().size() > 1) {
  1041. tableHolonElement.setModel(tableModelHolonElementMulti);
  1042. } else if (model.getSelectedCpsObjects().size() == 1) {
  1043. tableHolonElement.setModel(tableModelHolonElementSingle);
  1044. }
  1045. }
  1046. @Override
  1047. public void mouseReleased(MouseEvent e) {
  1048. refreshTableHolonElement();
  1049. refreshTableProperties();
  1050. if (model.getSelectedCpsObjects().size() > 1) {
  1051. tableHolonElement.setModel(tableModelHolonElementMulti);
  1052. } else if (model.getSelectedCpsObjects().size() == 1) {
  1053. tableHolonElement.setModel(tableModelHolonElementSingle);
  1054. }
  1055. }
  1056. });
  1057. toolBar.add(btnAdd);
  1058. // Del Button
  1059. btnDel.addActionListener(new ActionListener() {
  1060. public void actionPerformed(ActionEvent arg0) {
  1061. Object nodeInfo = tree.getLastSelectedPathComponent();
  1062. if (nodeInfo != null) {
  1063. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1064. String nodeName = selectedNode.getUserObject().toString();
  1065. int depthOfNode = selectedNode.getLevel();
  1066. switch (depthOfNode) {
  1067. case 1:
  1068. int dialogResult = JOptionPane.showConfirmDialog(null,
  1069. "Do you realy want to delete the Category " + nodeName + "?", "Warning",
  1070. JOptionPane.YES_NO_OPTION);
  1071. if (dialogResult == JOptionPane.YES_OPTION) {
  1072. controller.deleteCategory(nodeName);
  1073. }
  1074. break;
  1075. case 2:
  1076. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  1077. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  1078. break;
  1079. default:
  1080. JOptionPane.showMessageDialog(new JFrame(),
  1081. "Please select a Category or an Object in order to delete something.");
  1082. }
  1083. } else {
  1084. JOptionPane.showMessageDialog(new JFrame(),
  1085. "Please select a Category or an Object in order to delete something.");
  1086. }
  1087. tree.repaint();
  1088. }
  1089. });
  1090. toolBar.add(btnDel);
  1091. frmCyberPhysical.getContentPane().add(splitPane);
  1092. mntmNew.addActionListener(new ActionListener() {
  1093. @Override
  1094. public void actionPerformed(ActionEvent arg0) {
  1095. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  1096. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  1097. controller.setSelectedObjectID(0);
  1098. controller.setSelecteEdge(null);
  1099. controller.setCurIteration(0);
  1100. unitGraph.empty();
  1101. elementGraph.setText("None ");
  1102. canvas.tempCps = null;
  1103. canvas.objectSelectionHighlighting();
  1104. canvas.repaint();
  1105. }
  1106. });
  1107. mntmOpen.addActionListener(new java.awt.event.ActionListener() {
  1108. @Override
  1109. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1110. menuFileExitActionPerformed(evt);
  1111. }
  1112. private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
  1113. JFileChooser fileChooser = new JFileChooser();
  1114. JFrame test = new JFrame();
  1115. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  1116. fileChooser.setFileFilter(jsonFilter);
  1117. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  1118. File file = fileChooser.getSelectedFile();
  1119. try {
  1120. controller.loadFile(file.getAbsolutePath());
  1121. canvas.repaint();
  1122. ArrayList<HolonElement> tempList = new ArrayList<>();
  1123. for (CpsObject cps : model.getObjectsOnCanvas()) {
  1124. if (cps instanceof HolonObject) {
  1125. for (HolonElement h : ((HolonObject) cps).getElements()) {
  1126. tempList.add(h);
  1127. unitGraph.repaintWithNewElement(tempList);
  1128. unitGraph.fillArrayofValue();
  1129. tempList.remove(0);
  1130. }
  1131. } else if (cps instanceof HolonSwitch) {
  1132. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  1133. unitGraph.fillArrayofBooleans();
  1134. }
  1135. }
  1136. unitGraph.empty();
  1137. tree.repaint();
  1138. } catch (IOException e) {
  1139. // TODO Auto-generated catch block
  1140. e.printStackTrace();
  1141. }
  1142. }
  1143. }
  1144. });
  1145. mntmSave.addActionListener(new java.awt.event.ActionListener() {
  1146. @Override
  1147. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1148. menuSaveActionPerformed(evt);
  1149. }
  1150. private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
  1151. JFileChooser fileChooser = new JFileChooser();
  1152. JFrame test = new JFrame();
  1153. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  1154. fileChooser.setFileFilter(jsonFilter);
  1155. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  1156. String file = fileChooser.getSelectedFile().getPath();
  1157. if (!file.endsWith(".json"))
  1158. file += ".json";
  1159. try {
  1160. controller.saveFile(new File(file).getAbsolutePath());
  1161. } catch (IOException e) {
  1162. // TODO Auto-generated catch block
  1163. e.printStackTrace();
  1164. }
  1165. }
  1166. }
  1167. });
  1168. mntmUndo.addActionListener(new java.awt.event.ActionListener() {
  1169. @Override
  1170. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1171. menuUndoActionPerformed(evt);
  1172. }
  1173. private void menuUndoActionPerformed(java.awt.event.ActionEvent evt) {
  1174. try {
  1175. controller.loadFile(controller.getUndoSave());
  1176. canvas.repaint();
  1177. ArrayList<HolonElement> tempList = new ArrayList<>();
  1178. for (CpsObject cps : model.getObjectsOnCanvas()) {
  1179. if (cps instanceof HolonObject) {
  1180. for (HolonElement h : ((HolonObject) cps).getElements()) {
  1181. tempList.add(h);
  1182. unitGraph.repaintWithNewElement(tempList);
  1183. unitGraph.fillArrayofValue();
  1184. tempList.remove(0);
  1185. }
  1186. } else if (cps instanceof HolonSwitch) {
  1187. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  1188. unitGraph.fillArrayofBooleans();
  1189. }
  1190. }
  1191. unitGraph.empty();
  1192. } catch (IOException e) {
  1193. // TODO Auto-generated catch block
  1194. e.printStackTrace();
  1195. }
  1196. }
  1197. });
  1198. mntmRedo.addActionListener(new java.awt.event.ActionListener() {
  1199. @Override
  1200. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1201. menuUndoActionPerformed(evt);
  1202. }
  1203. private void menuUndoActionPerformed(java.awt.event.ActionEvent evt) {
  1204. try {
  1205. controller.loadFile(controller.getRedoSave());
  1206. canvas.repaint();
  1207. ArrayList<HolonElement> tempList = new ArrayList<>();
  1208. for (CpsObject cps : model.getObjectsOnCanvas()) {
  1209. if (cps instanceof HolonObject) {
  1210. for (HolonElement h : ((HolonObject) cps).getElements()) {
  1211. tempList.add(h);
  1212. unitGraph.repaintWithNewElement(tempList);
  1213. unitGraph.fillArrayofValue();
  1214. tempList.remove(0);
  1215. }
  1216. } else if (cps instanceof HolonSwitch) {
  1217. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  1218. unitGraph.fillArrayofBooleans();
  1219. }
  1220. }
  1221. unitGraph.empty();
  1222. } catch (IOException e) {
  1223. // TODO Auto-generated catch block
  1224. e.printStackTrace();
  1225. }
  1226. }
  1227. });
  1228. timePanel = new TimePanel(model, controller);
  1229. timePanel.setBorder(null);
  1230. ((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
  1231. @Override
  1232. public void stateChanged(ChangeEvent e) {
  1233. int i = model.getCurIteration();
  1234. controller.calculateStateForTimeStep(i);
  1235. unitGraph.repaint();
  1236. }
  1237. });
  1238. splitPane.setRightComponent(splitPane_1);
  1239. splitPane.setDividerLocation(200);
  1240. splitPane_1.setDividerLocation(500);
  1241. splitPane.setLeftComponent(scrollPane_1);
  1242. splitPane_1.setLeftComponent(tabbedPane);
  1243. splitPane_1.setRightComponent(split_HolonEl_Pro);
  1244. split_HolonEl_Pro.setDividerLocation(400);
  1245. split_HolonEl_Pro.setTopComponent(split_Graph_HolonEl);
  1246. split_HolonEl_Pro.setBottomComponent(scrollProperties);
  1247. split_Graph_HolonEl.setDividerLocation(150);
  1248. split_Graph_HolonEl.setTopComponent(scrollGraph);
  1249. split_Graph_HolonEl.setBottomComponent(scrollElements);
  1250. tabbedPane.setBorder(null);
  1251. scrollProperties.setBorder(null);
  1252. scrollGraph.setBorder(null);
  1253. scrollElements.setBorder(null);
  1254. splitPane.setBorder(null);
  1255. splitPane_1.setBorder(null);
  1256. split_HolonEl_Pro.setBorder(null);
  1257. split_Graph_HolonEl.setBorder(null);
  1258. scrollPane_2.setBorder(null);
  1259. panel_HolonEl.setBorder(null);
  1260. tableHolonElementScrollPane.setBorder(null);
  1261. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  1262. }
  1263. /*
  1264. * adds a specific object type to selected Category also handles input
  1265. * windows and illegal inputs
  1266. */
  1267. public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  1268. if (selectedNode == null) {
  1269. JOptionPane.showMessageDialog(new JFrame(),
  1270. "Please select a Category first before adding " + objType + ".");
  1271. }
  1272. // if selected node is a directory for Categories
  1273. else {
  1274. if (selectedNode.getLevel() == 1) {
  1275. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  1276. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  1277. if (objname.length() != 0) {
  1278. switch (objType) {
  1279. case "Switch":
  1280. controller.addSwitch(cat, objname);
  1281. break;
  1282. case "Transformer":
  1283. controller.addTransformer(cat, objname);
  1284. break;
  1285. }
  1286. }
  1287. } else {
  1288. JOptionPane.showMessageDialog(new JFrame(),
  1289. "Objects can not be added to Objects. Please select a Category.");
  1290. }
  1291. }
  1292. }
  1293. /**
  1294. * reloads the Categories from Model
  1295. */
  1296. public void updateCategories(final ArrayList<Category> categories) {
  1297. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  1298. {
  1299. DefaultMutableTreeNode node_1;
  1300. for (Category c : categories) {
  1301. node_1 = new DefaultMutableTreeNode(c.getName());
  1302. // kann eventuell umgeändert werden
  1303. for (CpsObject obj : c.getObjects()) {
  1304. node_1.add(new DefaultMutableTreeNode(obj.getObjName()));
  1305. }
  1306. add(node_1);
  1307. }
  1308. }
  1309. }));
  1310. }
  1311. /**
  1312. *
  1313. */
  1314. public void onChange(ArrayList<Category> categories) {
  1315. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  1316. updateCategories(categories);
  1317. model.reload();
  1318. }
  1319. /**
  1320. *
  1321. * @return
  1322. */
  1323. public JFrame getFrmCyberPhysical() {
  1324. return frmCyberPhysical;
  1325. }
  1326. /**
  1327. * Getter for selected CpsObject
  1328. *
  1329. * @return selected CpsObject
  1330. */
  1331. public CpsObject getActualCps() {
  1332. int tempID = model.getSelectedObjectID();
  1333. CpsObject tempCps = controller.searchByID(tempID);
  1334. return tempCps;
  1335. }
  1336. /**
  1337. * Search for actual selected HolonElement
  1338. *
  1339. * @param obj
  1340. * selected HolonObject
  1341. * @param yValue
  1342. * Y-Coord in the HolonElementsTable
  1343. * @param toMultiHash
  1344. * 0 means no MultiSelection, 1 means MultiSelection without
  1345. * Control, 2 means MultiSelection with Control
  1346. * @return the selected HolonElement
  1347. */
  1348. public HolonElement getActualHolonElement(HolonObject obj, int yValue, int toMultiHash) {
  1349. final int yTemp = (int) Math.floor(yValue / 16);
  1350. int rowsTotal = tableModelHolonElementSingle.getRowCount();
  1351. if (obj == null) {
  1352. rowsTotal = tableModelHolonElementMulti.getRowCount();
  1353. }
  1354. HolonObject obtTemp = null;
  1355. String eleTempName = "";
  1356. if (rowsTotal != 0 && rowsTotal > yTemp) {
  1357. if (obj == null) {
  1358. String temp = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
  1359. int idTemp = Integer.parseInt(temp.split(", ")[1]);
  1360. obtTemp = (HolonObject) controller.searchByID(idTemp);
  1361. eleTempName = tableModelHolonElementMulti.getValueAt(yTemp, 1).toString();
  1362. ArrayList<HolonElement> eleTemp = new ArrayList<HolonElement>();
  1363. if (eleToDelete.containsKey(idTemp) && toMultiHash == 2) {
  1364. eleTemp = eleToDelete.get(idTemp);
  1365. if (!eleTemp.contains(obtTemp.searchElement(eleTempName))) {
  1366. eleTemp.add(obtTemp.searchElement(eleTempName));
  1367. eleToDelete.replace(idTemp, eleTemp);
  1368. }
  1369. } else if (toMultiHash == 2) {
  1370. eleTemp.add(obtTemp.searchElement(eleTempName));
  1371. eleToDelete.put(idTemp, eleTemp);
  1372. } else if (toMultiHash == 1) {
  1373. eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  1374. eleTemp.add(obtTemp.searchElement(eleTempName));
  1375. eleToDelete.put(idTemp, eleTemp);
  1376. }
  1377. } else {
  1378. eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  1379. obtTemp = obj;
  1380. eleTempName = tableModelHolonElementSingle.getValueAt(yTemp, 0).toString();
  1381. }
  1382. model.setSelectedHolonElement(obtTemp.searchElement(eleTempName));
  1383. return obtTemp.searchElement(eleTempName);
  1384. } else {
  1385. eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  1386. model.setSelectedHolonElement(null);
  1387. return null;
  1388. }
  1389. }
  1390. public HolonObject getHolonObj(int yValue) {
  1391. final int yTemp = (int) Math.floor(yValue / 16);
  1392. HolonObject obtTemp = null;
  1393. String temp = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
  1394. int idTemp = Integer.parseInt(temp.split(", ")[1]);
  1395. obtTemp = (HolonObject) controller.searchByID(idTemp);
  1396. return obtTemp;
  1397. }
  1398. /**
  1399. * Update the HolonElement Table, that means erase all rows and add the new
  1400. * rows with new data
  1401. */
  1402. public void refreshTableHolonElement() {
  1403. // Update of the Information about the HolonElements - only for
  1404. // HolonObjects
  1405. if (model.getSelectedCpsObjects().size() > 1) {
  1406. deleteRows(tableModelHolonElementMulti);
  1407. fillElementTable(model.getSelectedCpsObjects());
  1408. tableModelHolonElementMulti.fireTableDataChanged();
  1409. } else if (model.getSelectedCpsObjects().size() == 1) {
  1410. deleteRows(tableModelHolonElementSingle);
  1411. fillElementTable(model.getSelectedCpsObjects());
  1412. tableModelHolonElementSingle.fireTableDataChanged();
  1413. }
  1414. }
  1415. /**
  1416. * Erase all information of the HolonElement Model
  1417. */
  1418. public void deleteRows(PropertyTable t) {
  1419. if (t.getRowCount() > 0) {
  1420. for (int i = t.getRowCount() - 1; i > -1; i--) {
  1421. t.removeRow(i);
  1422. }
  1423. }
  1424. }
  1425. /**
  1426. * Add the Information of the given ArrayList in the HolonElement Model as
  1427. * Name,Energy and Amount
  1428. *
  1429. * @param elements
  1430. * ArrayList to be displayed
  1431. */
  1432. public void fillElementTable(ArrayList<CpsObject> objects) {
  1433. if (objects.size() > 1) {
  1434. for (CpsObject o : objects) {
  1435. if (o instanceof HolonObject) {
  1436. for (HolonElement he : ((HolonObject) o).getElements()) {
  1437. Object[] temp = { o.getName() + ", " + o.getID(), he.getEleName(), he.getEnergy(),
  1438. he.getAmount(), he.getActive() };
  1439. tableModelHolonElementMulti.addRow(temp);
  1440. }
  1441. }
  1442. }
  1443. } else if (objects.size() == 1) {
  1444. CpsObject o = objects.get(0);
  1445. if (o instanceof HolonObject) {
  1446. for (HolonElement he : ((HolonObject) o).getElements()) {
  1447. Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
  1448. tableModelHolonElementSingle.addRow(temp);
  1449. }
  1450. }
  1451. }
  1452. }
  1453. /**
  1454. * Update the information concerning properties of the selected CpsObject
  1455. */
  1456. public void refreshTableProperties() {
  1457. if (model.getSelectedCpsObjects().size() == 1) {
  1458. CpsObject tempCps = getActualCps();
  1459. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  1460. tableModelProperties.removeRow(2);
  1461. Object[] tempEnergy = { "Total Energy", ((HolonObject) tempCps).getCurrentEnergy() };
  1462. tableModelProperties.insertRow(2, tempEnergy);
  1463. }
  1464. }
  1465. }
  1466. private static void addPopup(Component component, final JPopupMenu popup) {
  1467. component.addMouseListener(new MouseAdapter() {
  1468. public void mousePressed(MouseEvent e) {
  1469. if (e.isPopupTrigger()) {
  1470. showMenu(e);
  1471. }
  1472. }
  1473. public void mouseReleased(MouseEvent e) {
  1474. if (e.isPopupTrigger()) {
  1475. showMenu(e);
  1476. }
  1477. }
  1478. private void showMenu(MouseEvent e) {
  1479. popup.show(e.getComponent(), e.getX(), e.getY());
  1480. }
  1481. });
  1482. }
  1483. }