GUI.java 46 KB

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