GUI.java 61 KB

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