GUI.java 47 KB

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