GUI.java 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  1. package ui.view;
  2. import classes.*;
  3. import com.google.gson.JsonNull;
  4. import com.google.gson.JsonObject;
  5. import com.google.gson.JsonParseException;
  6. import interfaces.CategoryListener;
  7. import org.apache.commons.compress.archivers.ArchiveException;
  8. import ui.controller.Control;
  9. import ui.controller.UpdateController;
  10. import ui.model.Model;
  11. import javax.swing.*;
  12. import javax.swing.border.LineBorder;
  13. import javax.swing.filechooser.FileNameExtensionFilter;
  14. import javax.swing.table.DefaultTableModel;
  15. import javax.swing.table.TableCellEditor;
  16. import javax.swing.table.TableCellRenderer;
  17. import javax.swing.tree.DefaultMutableTreeNode;
  18. import javax.swing.tree.DefaultTreeModel;
  19. import javax.swing.tree.TreeCellRenderer;
  20. import java.awt.*;
  21. import java.awt.datatransfer.UnsupportedFlavorException;
  22. import java.awt.event.*;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Timer;
  28. import java.util.TimerTask;
  29. import java.util.stream.Collectors;
  30. /**
  31. * Graphical User Interface.
  32. *
  33. * @author Gruppe14
  34. */
  35. public class GUI implements CategoryListener {
  36. private final AlgorithmMenu algorithmMenu;
  37. private final JMenuBar menuBar = new JMenuBar();
  38. private final JMenu mnNewMenu = new JMenu("File");
  39. private final JMenu mnNewMenuEdit = new JMenu("Edit");
  40. private final JMenu mnNewMenuOptions = new JMenu("Options");
  41. private final JMenu mnNewMenuView = new JMenu("View");
  42. private final JMenu mnHelp = new JMenu("Help");
  43. private final JCheckBox consoleLogCheckBox = new JCheckBox("Show program console log");
  44. private final JCheckBox useFlexibleDevicesCheckBox = new JCheckBox("Automatically use flexible devices");
  45. private final JMenuItem mntmOpen = new JMenuItem("Open");
  46. private final JMenuItem mntmNew = new JMenuItem("New");
  47. private final JMenuItem mntmSave = new JMenuItem("Save");
  48. private final JMenuItem mntmAboutUs = new JMenuItem("About Us");
  49. private final JMenuItem mntmCanvasSize = new JMenuItem("View Size");
  50. private final JMenuItem mntmBackground = new JMenuItem("Background Image");
  51. private final JMenuItem mntmSplitView = new JMenuItem("Split View");
  52. private final JSplitPane splitPane = new JSplitPane();
  53. private final JSplitPane splitPane1 = new JSplitPane();
  54. private final JSplitPane splitPaneCanvasConsole = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  55. // the tabbed canvas containing the different sub-net tabs of the grid (Main Grid + Nodes of Nodes)
  56. private final JTabbedPane tabbedPaneInnerOriginal = new JTabbedPane(JTabbedPane.TOP);
  57. // the main canvas where we can see the grid currently displayed
  58. private final JScrollPane canvasSP = new JScrollPane();
  59. private final JScrollPane scrollPane1 = new JScrollPane();
  60. // private final JScrollPane holonSP = new JScrollPane();
  61. // the original tabbed Pane (containing tabs for view, statistics, holon, flexibility)
  62. private final JTabbedPane tabbedPaneOriginal = new JTabbedPane(JTabbedPane.TOP);
  63. // the same tabbed Pane that appears once the view is split
  64. private final JTabbedPane tabbedPaneSplit = new JTabbedPane(JTabbedPane.TOP);
  65. private final JPopupMenu popmenuEdit = new JPopupMenu();
  66. private final JMenuItem editItem = new JMenuItem("Edit Object");
  67. private final StatisticPanel statSplitPane;
  68. private final JScrollPane statScrollPane;
  69. private final JLabel maxGraph = new JLabel("100%");
  70. private final JLabel medGraph = new JLabel("50%");
  71. private final JLabel minGraph = new JLabel("0%");
  72. private final JLabel elementGraph = new JLabel("None ");
  73. private final ArrayList<HolonElement> selectedElements = new ArrayList<>();
  74. private final JTree tree = new JTree();
  75. /******************************************
  76. ************* Right Container*************
  77. ******************************************
  78. * Right Container: here comes the information about the HolonObject, such
  79. * as HolonElements Information, Properties and Consumption/Production
  80. * graph.
  81. **/
  82. private final JSplitPane splitHolonElPro = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  83. private final JSplitPane splitGraphHolonEl = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  84. // Model for single or multi selection
  85. private final JPanel scrollElements = new JPanel();
  86. private final JScrollPane tableHolonElementScrollPane = new JScrollPane();
  87. // In this section are all the properties that correspond to the clicked
  88. // HolonObject, such as connections, name, Type, etc.
  89. // Table for Properties --> Cell with (0,1) is editable by CpsObjt and
  90. // Cell(3,1) is editable by Edges
  91. private final JTable tableProperties = new JTable() {
  92. private static final long serialVersionUID = 1L;
  93. @Override
  94. public TableCellRenderer getCellRenderer(int row, int column) {
  95. if (getValueAt(row, column) instanceof Boolean) {
  96. return super.getDefaultRenderer(Boolean.class);
  97. } else {
  98. return super.getCellRenderer(row, column);
  99. }
  100. }
  101. @Override
  102. public TableCellEditor getCellEditor(int row, int column) {
  103. if (getValueAt(row, column) instanceof Boolean) {
  104. return super.getDefaultEditor(Boolean.class);
  105. } else {
  106. return super.getCellEditor(row, column);
  107. }
  108. }
  109. };
  110. private final JPanel graphLabel = new JPanel();
  111. private final JScrollPane scrollProperties = new JScrollPane();
  112. // In this section is the graph for the selected HolonElement of the clicked
  113. // HolonObject
  114. private final JTable tableGraph = new JTable();
  115. private final DefaultTableModel tableModelGraph = new DefaultTableModel();
  116. private final JScrollPane scrollGraph = new JScrollPane();
  117. private final Model model;
  118. private final Control controller;
  119. // In this section are all the Holonelements that correspond to the clicked
  120. // HolonObject with consumption/production, name and amount.
  121. private final JPanel panel = new JPanel();
  122. private final JPanel panelHolonEl = new JPanel();
  123. private final JComboBox comboBox = new JComboBox();
  124. // Buttons
  125. private final JButton btnAdd = new JButton("+");
  126. private final JButton btnDel = new JButton("-");
  127. private final JButton btnAddHolEL = new JButton("+");
  128. private final JButton btnDelHolEL = new JButton("-");
  129. private final JButton resetGraphBtn = new JButton("Reset");
  130. private final JToolBar toolBar = new JToolBar();
  131. private final JToolBar toolBarHolonEl = new JToolBar();
  132. private final JToolBar toolBarGraph = new JToolBar();
  133. // Languages
  134. private final JMenuItem englishBtn = new JMenuItem("EN");
  135. private final JMenuItem spanishBtn = new JMenuItem("ES");
  136. private final JMenuItem germanBtn = new JMenuItem("DE");
  137. private final JMenuItem czechBtn = new JMenuItem("CZ");
  138. private final JMenuItem chineseBtn = new JMenuItem("ZH");
  139. // private final JComboBox comboBoxGraph = new JComboBox();
  140. private final Console console = new Console();
  141. private final MyCanvas canvas;
  142. private final HolonCanvas holonCanvas;
  143. private final UnitGraph unitGraph;
  144. private final JSplitPane splitPane3 = new JSplitPane();
  145. private final JSlider sizeSlider = new JSlider();
  146. private final JLabel lblImageSize = new JLabel(Languages.getLanguage()[94]);
  147. private final JMenuItem mntmUndo = new JMenuItem("Undo");
  148. private final JMenuItem mntmRedo = new JMenuItem("Redo");
  149. private final JMenuItem mntmEditEdges = new JMenuItem("Edge Properties");
  150. private final JMenuItem mntmFindReplace = new JMenuItem("Find/ Replace");
  151. private final JMenuItem mntmEditShowedInformation = new JMenuItem("Edit showed Information");
  152. private final JMenuItem mntmResetCategory = new JMenuItem("Reset Categories");
  153. private final JMenu mnLanguage = new JMenu("Language");
  154. private final String[] columnNamesMulti = {"Object", "Nr.", "Device", "Energy", "Flexible Energy Available",
  155. "Quantity", "Activated", "Flexible"};
  156. private final String[] columnNamesSingle = {"Nr.", "Device", "Energy", "Flexible Energy Available", "Quantity",
  157. "Activated", "Flexible"};
  158. private final ArrayList<PropertyTable> tables = new ArrayList<>();
  159. private final String[] comboBoxCat = {"Category", "Object", "Switch"};
  160. private final UpdateController updCon;
  161. private final JSplitPane splitPane_1 = new JSplitPane();
  162. private final JSlider holonBodySizeSlider = new JSlider();
  163. private final JLabel lblHolonBodySize = new JLabel("HolonBody SIze");
  164. private final JLabel lblSelectedElement = new JLabel("Selected Element: ");
  165. // for doubleclick
  166. private boolean click = false;
  167. private JFrame frmCyberPhysical;
  168. private JTabbedPane tabTemp; // tabbedPaneOriginal or tabbedPaneSplit
  169. private JSplitPane tempSplit;
  170. private boolean initSplit = true;
  171. private String catOfObjToBeEdited;
  172. private UpperNodeCanvas unc;
  173. private JPanel contentPane;
  174. private String holonEleNamesDisplayed = "None ";
  175. // Pop up Windows
  176. private AddObjectPopUp addObjectPopUP;
  177. private AboutUsPopUp aboutUsPopUp;
  178. private AddElementPopUp addElementPopUp;
  179. // variables
  180. private boolean dragging = false;
  181. private String actualObjectClicked;
  182. private Image img = null;
  183. private AbstractCpsObject tempCps = null;
  184. private int yValueElements = 0;
  185. // Time Stuff
  186. private TimePanel timePanel;
  187. // Coord for all Cells with text
  188. private int yThis;
  189. private int xThis;
  190. // Coord for all Cells with boolean values (checkbox)
  191. private int yBTis;
  192. private int xBThis;
  193. // Coord for the Edit-Modus in the PropertieTable
  194. private int yProThis;
  195. private int xProThis;
  196. private int yProThisOneClick;
  197. private int xProThisOneClick;
  198. private AbstractCpsObject temp = null;
  199. private String warningText = "Warning";
  200. private String saveBeforeNew = "Do you want to save your current data?";
  201. private String eraseCategory = "Do you really want to delete the Category ";
  202. private String selectObjBeforeErase = "Please select a Category or an Object in order to delete something.";
  203. /**
  204. * Create the application.
  205. *
  206. * @param control the Controller
  207. */
  208. GUI(Control control) {
  209. this.controller = control;
  210. this.model = control.getModel();
  211. statSplitPane = new StatisticPanel(controller);
  212. model.addGraphListener(statSplitPane);
  213. model.setStatPanel(statSplitPane);
  214. statScrollPane = new JScrollPane(statSplitPane);
  215. this.unitGraph = new UnitGraph(model, control);
  216. this.canvas = new MyCanvas(model, control, unitGraph);
  217. this.holonCanvas = new HolonCanvas(model, control);
  218. control.initListener(this);
  219. controller.setCanvas(canvas);
  220. model.setConsole(console);
  221. algorithmMenu = new AlgorithmMenu(model, control);
  222. initialize();
  223. updateCategories(model.getCategories());
  224. updCon = new UpdateController(model, controller);
  225. }
  226. // /**
  227. // * Adds a Popup.
  228. // *
  229. // * @param component Component
  230. // * @param popup PopupMenu
  231. // */
  232. // private static void addPopup(Component component, final JPopupMenu popup) {
  233. // component.addMouseListener(new MouseAdapter() {
  234. // public void mousePressed(MouseEvent e) {
  235. // if (e.isPopupTrigger()) {
  236. // showMenu(e);
  237. // }
  238. // }
  239. //
  240. // public void mouseReleased(MouseEvent e) {
  241. // if (e.isPopupTrigger()) {
  242. // showMenu(e);
  243. // }
  244. // }
  245. //
  246. // private void showMenu(MouseEvent e) {
  247. // popup.show(e.getComponent(), e.getX(), e.getY());
  248. // }
  249. //
  250. // });
  251. // }
  252. /**
  253. * Initialize the contents of the frame.
  254. */
  255. @SuppressWarnings({"serial", "unchecked"})
  256. private void initialize() {
  257. frmCyberPhysical = new JFrame();
  258. frmCyberPhysical.setTitle("Cyber Physical Systems Model");
  259. frmCyberPhysical.setBounds(100, 100, 1000, 800);
  260. frmCyberPhysical.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  261. frmCyberPhysical.setExtendedState(JFrame.MAXIMIZED_BOTH);
  262. frmCyberPhysical.addWindowListener(new java.awt.event.WindowAdapter() {
  263. @Override
  264. public void windowClosing(java.awt.event.WindowEvent windowEvent) {
  265. if (JOptionPane.showConfirmDialog(frmCyberPhysical, Languages.getLanguage()[88],
  266. "Cyber Physical Systems Model", JOptionPane.YES_NO_OPTION,
  267. JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
  268. controller
  269. .deleteDirectory(new File(System.getProperty("user.home") + "/.config/HolonGUI/Autosave"));
  270. System.exit(0);
  271. }
  272. }
  273. });
  274. contentPane = (JPanel) frmCyberPhysical.getContentPane();
  275. int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
  276. InputMap inputMap = contentPane.getInputMap(condition);
  277. ActionMap actionMap = contentPane.getActionMap();
  278. String cntrlZDown = "controlZ";
  279. inputMap.put(KeyStroke.getKeyStroke("control Z"), cntrlZDown);
  280. actionMap.put(cntrlZDown, new AbstractAction() {
  281. private static final long serialVersionUID = 1L;
  282. @Override
  283. public void actionPerformed(ActionEvent e) {
  284. try {
  285. controller.loadAutoSave(controller.getUndoSave());
  286. canvas.repaint();
  287. unitGraph.update(model.getObjectsOnCanvas());
  288. updateUpperNodes();
  289. } catch (IOException eex) {
  290. eex.printStackTrace();
  291. }
  292. }
  293. });
  294. String cntrlYDown = "controlY";
  295. inputMap.put(KeyStroke.getKeyStroke("control Y"), cntrlYDown);
  296. actionMap.put(cntrlYDown, new AbstractAction() {
  297. private static final long serialVersionUID = 1L;
  298. @Override
  299. public void actionPerformed(ActionEvent e) {
  300. try {
  301. controller.loadAutoSave(controller.getRedoSave());
  302. canvas.repaint();
  303. unitGraph.update(model.getObjectsOnCanvas());
  304. updateUpperNodes();
  305. } catch (IOException ex) {
  306. ex.printStackTrace();
  307. }
  308. }
  309. });
  310. String cntrlADown = "controlA";
  311. inputMap.put(KeyStroke.getKeyStroke("control A"), cntrlADown);
  312. AbstractAction controlA = new AbstractAction() {
  313. private static final long serialVersionUID = 1L;
  314. @Override
  315. public void actionPerformed(ActionEvent e) {
  316. chooseTabTemp();
  317. model.getSelectedCpsObjects().clear();
  318. // Uppernode Canvas?
  319. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  320. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  321. if (canvasOrUpperNodeCanvas instanceof UpperNodeCanvas) {
  322. UpperNodeCanvas uNC = (UpperNodeCanvas) canvasOrUpperNodeCanvas;
  323. for (AbstractCpsObject cps : uNC.upperNode.getNodes()) {
  324. controller.addSelectedObject(cps);
  325. }
  326. uNC.repaint();
  327. // or Canvas?
  328. } else if (canvasOrUpperNodeCanvas instanceof MyCanvas) {
  329. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  330. controller.addSelectedObject(cps);
  331. }
  332. canvas.repaint();
  333. }
  334. controller.getObjectsInDepth();
  335. }
  336. };
  337. actionMap.put(cntrlADown, controlA);
  338. String delDown = "delete";
  339. inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false), delDown);
  340. actionMap.put(delDown, new AbstractAction() {
  341. private static final long serialVersionUID = 1L;
  342. @Override
  343. public void actionPerformed(ActionEvent e) {
  344. chooseTabTemp();
  345. // Uppernode Canvas?
  346. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  347. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  348. // check whether a producer was deleted (this triggers a complete re-evaluation of the net)
  349. boolean wasProducerDeleted = false;
  350. if (canvasOrUpperNodeCanvas instanceof UpperNodeCanvas) {
  351. UpperNodeCanvas uNC = (UpperNodeCanvas) canvasOrUpperNodeCanvas;
  352. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  353. if (cps instanceof HolonObject
  354. && ((HolonObject) cps).getState() == HolonObject.PRODUCER) {
  355. wasProducerDeleted = true;
  356. }
  357. controller.removeTrackingObj(cps);
  358. if (uNC.upperNode.getNodes().contains(cps)) {
  359. controller.delObjUpperNode(cps, uNC.upperNode);
  360. unc.setToolTip(false);
  361. // remove UpperNodeTab if UpperNode deleted
  362. removeUpperNodeTab(cps);
  363. }
  364. }
  365. uNC.repaint();
  366. // or Canvas?
  367. } else if (canvasOrUpperNodeCanvas instanceof MyCanvas) {
  368. boolean save = false;
  369. for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {
  370. AbstractCpsObject cps = model.getSelectedCpsObjects().get(j);
  371. if (cps instanceof HolonObject
  372. && ((HolonObject) cps).getState() == HolonObject.PRODUCER) {
  373. wasProducerDeleted = true;
  374. }
  375. controller.removeTrackingObj(cps);
  376. if (j < model.getSelectedCpsObjects().size() - 1)
  377. save = true;
  378. controller.delCanvasObject(cps, save);
  379. canvas.setToolTip(false);
  380. // remove UpperNodeTab if UpperNode deleted
  381. removeUpperNodeTab(cps);
  382. }
  383. canvas.repaint();
  384. }
  385. // recalculate net if a producer was deleted
  386. if (wasProducerDeleted) {
  387. controller.resetSimulation();
  388. controller.calculateStateForCurrentTimeStep();
  389. }
  390. model.getSelectedCpsObjects().clear();
  391. hideScrollGraph();
  392. }
  393. });
  394. String cntrlFDown = "controlF";
  395. inputMap.put(KeyStroke.getKeyStroke("control F"), cntrlFDown);
  396. actionMap.put(cntrlFDown, new AbstractAction() {
  397. private static final long serialVersionUID = 1L;
  398. @Override
  399. public void actionPerformed(ActionEvent e) {
  400. SearchPopUp dialog = new SearchPopUp(controller, canvas);
  401. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  402. dialog.setVisible(true);
  403. }
  404. });
  405. String cntrlCDown = "controlC";
  406. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  407. AbstractAction controlC = new AbstractAction() {
  408. private static final long serialVersionUID = 1L;
  409. @Override
  410. public void actionPerformed(ActionEvent e) {
  411. chooseTabTemp();
  412. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  413. if (!model.getSelectedCpsObjects().isEmpty()) {
  414. if (scrollPane.getViewport().getComponent(0) instanceof UpperNodeCanvas)
  415. controller.copy(((UpperNodeCanvas) scrollPane.getViewport().getComponent(0)).upperNode);
  416. else
  417. controller.copy(null);
  418. if (!model.getClipboradObjects().isEmpty()) {
  419. canvas.itemPaste.setEnabled(true);
  420. }
  421. }
  422. }
  423. };
  424. actionMap.put(cntrlCDown, controlC);
  425. String cntrlVDown = "controlV";
  426. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  427. AbstractAction controlV = new AbstractAction() {
  428. private static final long serialVersionUID = 1L;
  429. @Override
  430. public void actionPerformed(ActionEvent e) {
  431. try {
  432. tabTemp = null;
  433. if (tabbedPaneOriginal.getMousePosition() != null) {
  434. tabTemp = tabbedPaneOriginal;
  435. } else {
  436. if (!initSplit) {
  437. tabTemp = tabbedPaneSplit;
  438. }
  439. }
  440. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  441. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  442. if (tabTemp != null && canvasOrUpperNodeCanvas instanceof UpperNodeCanvas) {
  443. controller.paste(
  444. ((UpperNodeCanvas) canvasOrUpperNodeCanvas).upperNode,
  445. canvasOrUpperNodeCanvas.getMousePosition());
  446. unitGraph.update(model.getSelectedCpsObjects());
  447. scrollPane.getViewport().getComponent(0).repaint();
  448. } else {
  449. controller.paste(null, canvas.getMousePosition());
  450. unitGraph.update(model.getSelectedCpsObjects());
  451. canvas.repaint();
  452. }
  453. } catch (HeadlessException | JsonParseException | UnsupportedFlavorException | IOException e1) {
  454. JLabel message = new JLabel("The Clipboard information cannot be pasted into Application.");
  455. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  456. }
  457. }
  458. };
  459. actionMap.put(cntrlVDown, controlV);
  460. String cntrlXDown = "controlX";
  461. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  462. AbstractAction controlX = new AbstractAction() {
  463. private static final long serialVersionUID = 1L;
  464. @Override
  465. public void actionPerformed(ActionEvent e) {
  466. chooseTabTemp();
  467. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  468. if (!model.getSelectedCpsObjects().isEmpty()) {
  469. if (scrollPane.getViewport().getComponent(0) instanceof UpperNodeCanvas) {
  470. controller.cut(((UpperNodeCanvas) scrollPane.getViewport().getComponent(0)).upperNode);
  471. } else {
  472. controller.cut(null);
  473. }
  474. if (!model.getClipboradObjects().isEmpty()) {
  475. canvas.itemPaste.setEnabled(true);
  476. }
  477. canvas.repaint();
  478. }
  479. }
  480. };
  481. actionMap.put(cntrlXDown, controlX);
  482. frmCyberPhysical.setJMenuBar(menuBar);
  483. frmCyberPhysical.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  484. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  485. menuBar.add(mnNewMenu);
  486. mnNewMenu.add(mntmNew);
  487. mnNewMenu.add(mntmOpen);
  488. mnNewMenu.add(mntmSave);
  489. menuBar.add(mnNewMenuEdit);
  490. mnNewMenuEdit.add(mntmUndo);
  491. mnNewMenuEdit.add(mntmRedo);
  492. mnLanguage.add(englishBtn);
  493. englishBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagEN.png").getImage().getScaledInstance(20,
  494. 20, java.awt.Image.SCALE_SMOOTH)));
  495. mnLanguage.add(spanishBtn);
  496. spanishBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagES.png").getImage().getScaledInstance(20,
  497. 20, java.awt.Image.SCALE_SMOOTH)));
  498. mnLanguage.add(germanBtn);
  499. germanBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagDE.png").getImage().getScaledInstance(20,
  500. 20, java.awt.Image.SCALE_SMOOTH)));
  501. // mnLanguage.add(czechBtn);
  502. // czechBtn.setIcon(new ImageIcon(new
  503. // ImageIcon("res/Button_Images/FlagCZ.png").getImage().getScaledInstance(20,
  504. // 20, java.awt.Image.SCALE_SMOOTH)));
  505. mnLanguage.add(chineseBtn);
  506. chineseBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagZH.png").getImage().getScaledInstance(20,
  507. 20, java.awt.Image.SCALE_SMOOTH)));
  508. mntmFindReplace.addActionListener(actionEvent -> {
  509. try {
  510. SearchPopUp dialog = new SearchPopUp(controller, canvas);
  511. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  512. dialog.setVisible(true);
  513. controller.getObjectsInDepth();
  514. } catch (Exception ex) {
  515. ex.printStackTrace();
  516. }
  517. });
  518. mnNewMenuEdit.add(mntmFindReplace);
  519. mntmEditShowedInformation.addActionListener(actionEvent -> {
  520. try {
  521. DisplayedInformationPopUp dialog = new DisplayedInformationPopUp(canvas, contentPane, controller);
  522. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  523. dialog.setVisible(true);
  524. } catch (Exception ex) {
  525. ex.printStackTrace();
  526. }
  527. });
  528. mnNewMenuEdit.add(mntmEditShowedInformation);
  529. mnNewMenuEdit.add(mntmEditEdges);
  530. mntmEditEdges.addActionListener(actionEvent -> {
  531. EditEdgesPopUp edgePopUp = new EditEdgesPopUp();
  532. edgePopUp.setCanvas(canvas);
  533. edgePopUp.setController(controller);
  534. edgePopUp.setVisible(true);
  535. });
  536. menuBar.add(mnNewMenuOptions);
  537. mnNewMenuOptions.add(mntmResetCategory);
  538. mntmResetCategory.addActionListener(actionEvent -> {
  539. ArrayList<Category> cat = model.getCategories();
  540. try {
  541. while (!cat.isEmpty()) {
  542. controller.deleteCategory(cat.get(0).getName());
  543. }
  544. controller.resetCategorys();
  545. } catch (Exception e2) {
  546. }
  547. tree.repaint();
  548. });
  549. mnNewMenuOptions.add(consoleLogCheckBox);
  550. consoleLogCheckBox.setSelected(true);
  551. consoleLogCheckBox.addActionListener(arg0 -> controller.setShowConsoleLog(consoleLogCheckBox.isSelected()));
  552. mnNewMenuOptions.add(useFlexibleDevicesCheckBox);
  553. useFlexibleDevicesCheckBox.setSelected(true);
  554. useFlexibleDevicesCheckBox.addActionListener(
  555. actionEvent -> model.setUseFlexibleDevices(useFlexibleDevicesCheckBox.isSelected()));
  556. menuBar.add(mnNewMenuView);
  557. mnNewMenuView.add(mntmCanvasSize);
  558. mntmCanvasSize.addActionListener(actionEvent -> {
  559. CanvasResizePopUp popUp = new CanvasResizePopUp(model, controller, canvas, tabbedPaneOriginal, tabbedPaneSplit);
  560. popUp.setVisible(true);
  561. });
  562. mnNewMenuView.add(splitPane3);
  563. sizeSlider.setMinimum(15);
  564. sizeSlider.setMaximum(115);
  565. sizeSlider.addChangeListener(changeEvent -> {
  566. controller.setScale(sizeSlider.getValue());
  567. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  568. if (scrollPane.getViewport()
  569. .getComponent(0) instanceof UpperNodeCanvas) {
  570. scrollPane.getViewport().getComponent(0).repaint();
  571. } else {
  572. canvas.repaint();
  573. }
  574. if (!initSplit && scrollPane.getViewport().getComponent(0) instanceof UpperNodeCanvas) {
  575. scrollPane.getViewport().getComponent(0).repaint();
  576. } else {
  577. canvas.repaint();
  578. }
  579. });
  580. mnNewMenuView.add(mntmCanvasSize);
  581. mnNewMenuView.add(mntmSplitView);
  582. // Split View
  583. mntmSplitView.addActionListener(actionEvent -> {
  584. if (splitPaneCanvasConsole.getLeftComponent() instanceof JSplitPane) {
  585. initSplit = true;
  586. Component tempC = tabbedPaneSplit.getSelectedComponent();
  587. tabbedPaneOriginal.setComponentAt(tabbedPaneSplit.getSelectedIndex(), tempC);
  588. tabbedPaneSplit.removeAll();
  589. splitPaneCanvasConsole.setLeftComponent(tabbedPaneOriginal);
  590. } else {
  591. for (int i = 0; i < tabbedPaneOriginal.getTabCount(); i++) {
  592. // add outer tabs
  593. // (inner tabs are added by tabbedPaneSplit changelistener)
  594. tabbedPaneSplit.addTab(tabbedPaneOriginal.getTitleAt(i), null);
  595. }
  596. if (tabbedPaneOriginal.getSelectedComponent() == statScrollPane) {
  597. tabbedPaneOriginal.setComponentAt(0, null);
  598. tabbedPaneSplit.setComponentAt(0, canvasSP);
  599. tabbedPaneSplit.setSelectedIndex(0);
  600. } else {
  601. tabbedPaneOriginal.setComponentAt(1, null);
  602. tabbedPaneSplit.setComponentAt(1, statScrollPane);
  603. tabbedPaneSplit.setSelectedIndex(1);
  604. }
  605. tempSplit = new JSplitPane();
  606. tempSplit.setBorder(null);
  607. tempSplit.setRightComponent(tabbedPaneSplit);
  608. tempSplit.setLeftComponent(tabbedPaneOriginal);
  609. tempSplit.setDividerLocation(tabbedPaneOriginal.getWidth() / 2);
  610. tempSplit.setResizeWeight(0.9);
  611. splitPaneCanvasConsole.setLeftComponent(tempSplit);
  612. initSplit = false;
  613. }
  614. contentPane.updateUI();
  615. });
  616. mnNewMenuView.add(mntmBackground);
  617. mntmBackground.addActionListener(actionEvent -> {
  618. tabTemp = tabbedPaneOriginal;
  619. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  620. if (scrollPane.getViewport().getComponent(0) instanceof MyCanvas) {
  621. BackgroundPopUp backgroundDialog = new BackgroundPopUp(model, controller, canvas, null);
  622. backgroundDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  623. backgroundDialog.setVisible(true);
  624. } else if (scrollPane.getViewport().getComponent(0) instanceof UpperNodeCanvas) {
  625. UpperNodeCanvas uNodeCanvas = (UpperNodeCanvas) (scrollPane.getViewport().getComponent(0));
  626. BackgroundPopUp backgroundDialog = new BackgroundPopUp(model, controller, null,
  627. uNodeCanvas.upperNode);
  628. backgroundDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  629. backgroundDialog.setVisible(true);
  630. uNodeCanvas.repaint();
  631. }
  632. });
  633. splitPane3.setRightComponent(sizeSlider);
  634. splitPane3.setLeftComponent(lblImageSize);
  635. mnNewMenuView.add(splitPane_1);
  636. holonBodySizeSlider.setValue(100);
  637. holonBodySizeSlider.setMinimum(1);
  638. holonBodySizeSlider.addChangeListener(changeEvent -> {
  639. controller.setHolonBodyScale(holonBodySizeSlider.getValue());
  640. tree.setRowHeight(50);
  641. canvas.repaint();
  642. });
  643. splitPane_1.setRightComponent(holonBodySizeSlider);
  644. splitPane_1.setLeftComponent(lblHolonBodySize);
  645. menuBar.add(algorithmMenu);
  646. menuBar.add(mnHelp);
  647. mnHelp.add(mntmAboutUs);
  648. tabbedPaneOriginal.addChangeListener(changeEvent -> {
  649. if (tabbedPaneOriginal.getSelectedComponent() == null) {
  650. Component tempC = tabbedPaneSplit.getSelectedComponent();
  651. tabbedPaneSplit.setComponentAt(tabbedPaneOriginal.getSelectedIndex(), null);
  652. tabbedPaneOriginal.setComponentAt(tabbedPaneOriginal.getSelectedIndex(), tempC);
  653. tempC = tabbedPaneOriginal.getComponentAt((tabbedPaneOriginal.getSelectedIndex() + 1) % (tabbedPaneOriginal.getTabCount()));
  654. tabbedPaneOriginal.setComponentAt((tabbedPaneOriginal.getSelectedIndex() + 1) % (tabbedPaneOriginal.getTabCount()), null);
  655. tabbedPaneSplit.setComponentAt((tabbedPaneOriginal.getSelectedIndex() + 1) % (tabbedPaneOriginal.getTabCount()), tempC);
  656. tabbedPaneSplit.setSelectedIndex((tabbedPaneOriginal.getSelectedIndex() + 1) % (tabbedPaneOriginal.getTabCount()));
  657. contentPane.updateUI();
  658. }
  659. });
  660. tabbedPaneSplit.addChangeListener(changeEvent -> {
  661. if (tabbedPaneSplit.getSelectedComponent() == null && !initSplit) {
  662. Component tempC = tabbedPaneOriginal.getComponentAt(tabbedPaneSplit.getSelectedIndex());
  663. tabbedPaneOriginal.setComponentAt(tabbedPaneSplit.getSelectedIndex(), null);
  664. tabbedPaneSplit.setComponentAt(tabbedPaneSplit.getSelectedIndex(), tempC);
  665. for (int i = 0; i < tabbedPaneOriginal.getTabCount(); i++) {
  666. if (tabbedPaneSplit.getComponentAt(i) != null && tabbedPaneSplit.getComponentAt(i) != tempC) {
  667. tempC = tabbedPaneSplit.getComponentAt(i);
  668. tabbedPaneSplit.setComponentAt(i, null);
  669. tabbedPaneOriginal.setComponentAt(i, tempC);
  670. break;
  671. }
  672. }
  673. if (tabbedPaneOriginal.getSelectedIndex() == tabbedPaneSplit.getSelectedIndex()) {
  674. tabbedPaneOriginal.setSelectedIndex((tabbedPaneSplit.getSelectedIndex() + 1) % tabbedPaneOriginal.getTabCount());
  675. }
  676. contentPane.updateUI();
  677. }
  678. });
  679. mnNewMenuOptions.add(mnLanguage);
  680. canvas.setBackground(Color.WHITE);
  681. canvas.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
  682. holonCanvas.setBackground(Color.WHITE);
  683. holonCanvas.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
  684. /********************
  685. * RIGHT CONTAINER (INFORMATION)
  686. **********************/
  687. // Set up of the HolonElements section
  688. // Two different Models: Multi for multi-selection mode and Single for
  689. // single-selection mode (CPS-Object)
  690. model.getMultiTable().setColumnIdentifiers(columnNamesMulti);
  691. model.getSingleTable().setColumnIdentifiers(columnNamesSingle);
  692. model.getTableHolonElement().setBorder(null);
  693. model.getTableHolonElement().setFillsViewportHeight(true);
  694. model.getTableHolonElement().setCellSelectionEnabled(true);
  695. model.getTableHolonElement().setColumnSelectionAllowed(true);
  696. tableHolonElementScrollPane.setViewportView(model.getTableHolonElement());
  697. scrollElements.setLayout(new BorderLayout(0, 0));
  698. scrollElements.add(panelHolonEl, BorderLayout.NORTH);
  699. scrollElements.add(tableHolonElementScrollPane);
  700. panelHolonEl.setLayout(new BoxLayout(panelHolonEl, BoxLayout.X_AXIS));
  701. toolBarHolonEl.add(btnAddHolEL);
  702. toolBarHolonEl.add(btnDelHolEL);
  703. toolBarHolonEl.setFloatable(false);
  704. panelHolonEl.add(toolBarHolonEl);
  705. // Set up of the Properties section
  706. tableProperties.setModel(model.getPropertyTable());
  707. tableProperties.setFillsViewportHeight(true);
  708. tableProperties.setCellSelectionEnabled(true);
  709. tableProperties.setColumnSelectionAllowed(true);
  710. scrollProperties.setViewportView(tableProperties);
  711. // Set up of the Graph section
  712. tableGraph.setModel(tableModelGraph);
  713. tableGraph.setFillsViewportHeight(true);
  714. tableGraph.setCellSelectionEnabled(true);
  715. tableGraph.setColumnSelectionAllowed(true);
  716. scrollGraph.setViewportView(unitGraph);
  717. graphLabel.setLayout(new BorderLayout(0, 10));
  718. graphLabel.add(maxGraph, BorderLayout.NORTH);
  719. graphLabel.add(medGraph, BorderLayout.CENTER);
  720. graphLabel.add(minGraph, BorderLayout.SOUTH);
  721. toolBarGraph.add(lblSelectedElement);
  722. toolBarGraph.add(elementGraph);
  723. Component horizontalStrut = Box.createHorizontalStrut(toolBarGraph.getWidth() - resetGraphBtn.getWidth());
  724. toolBarGraph.add(horizontalStrut);
  725. resetGraphBtn.setHorizontalAlignment(SwingConstants.RIGHT);
  726. toolBarGraph.add(resetGraphBtn);
  727. toolBarGraph.setFloatable(false);
  728. scrollGraph.setRowHeaderView(graphLabel);
  729. scrollGraph.setColumnHeaderView(toolBarGraph);
  730. tables.add(model.getSingleTable());
  731. tables.add(model.getMultiTable());
  732. /***********************
  733. * HolonElement Table Actions
  734. **********************/
  735. /*
  736. * Add HolonElement to given HolonObject
  737. */
  738. btnAddHolEL.addActionListener(actionEvent -> {
  739. if (model.getSelectedCpsObjects().size() == 1) {
  740. AbstractCpsObject tempCpsObject = updCon.getActualCps();
  741. if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
  742. && tempCpsObject.getId() != 0) {
  743. addElementPopUp = new AddElementPopUp();
  744. addElementPopUp.setActualCps(updCon.getActualCps());
  745. addElementPopUp.setVisible(true);
  746. HolonElement ele = addElementPopUp.getElement();
  747. if (ele != null) {
  748. controller.addElementCanvasObject(tempCpsObject.getId(), ele.getEleName(), ele.getAmount(),
  749. ele.getEnergyPerElement(), ele.getId());
  750. }
  751. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  752. updCon.refreshTableProperties(model.getPropertyTable());
  753. controller.calculateStateForTimeStep(model.getCurIteration());
  754. contentPane.updateUI();
  755. }
  756. }
  757. });
  758. /*
  759. * Delete the chosen HolonElement of the selected HolonObject,
  760. * Multi-Selection for CpsObjects as well as for HolonElements possible
  761. */
  762. btnDelHolEL.addActionListener(actionEvent -> {
  763. // For Single Selection of CpsObject
  764. if (model.getSelectedCpsObjects().size() == 1) {
  765. if (updCon.getActualCps().getClass() == HolonObject.class) {
  766. HolonObject obj = (HolonObject) updCon.getActualCps();
  767. for (HolonElement e : selectedElements) {
  768. controller.deleteElementCanvas(obj.getId(), e.getId());
  769. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  770. updCon.refreshTableProperties(model.getPropertyTable());
  771. controller.calculateStateForTimeStep(model.getCurIteration());
  772. contentPane.updateUI();
  773. // Names displayed in graph are not updated
  774. }
  775. model.getEleToDelete().clear();
  776. selectedElements.clear();
  777. }
  778. // For MultiSelection of CpsObject
  779. } else if (model.getSelectedCpsObjects().size() > 1) {
  780. for (Integer i : model.getEleToDelete().keySet()) {
  781. for (HolonElement e : model.getEleToDelete().get(i)) {
  782. controller.deleteElementCanvas(i, e.getId());
  783. }
  784. }
  785. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  786. updCon.refreshTableProperties(model.getPropertyTable());
  787. model.getEleToDelete().clear();
  788. selectedElements.clear();
  789. }
  790. updCon.refreshTableProperties(model.getPropertyTable());
  791. elementGraph.setText(Languages.getLanguage()[25]);
  792. holonEleNamesDisplayed = Languages.getLanguage()[25];
  793. });
  794. /*
  795. * Communication between HolonElement Table and displayed Graph and
  796. * Properties, as well as selection of different HolonElements
  797. */
  798. model.getTableHolonElement().addMouseListener(new MouseAdapter() {
  799. public void mousePressed(MouseEvent e) {
  800. HolonObject obj = null;
  801. if (model.getSelectedCpsObjects().size() == 1
  802. && model.getSelectedCpsObjects().get(0) instanceof HolonObject) {
  803. obj = (HolonObject) updCon.getActualCps();
  804. }
  805. yValueElements = e.getY();
  806. HolonElement ele;
  807. // Search for current clicked HolonElement
  808. if (model.getSelectedCpsObjects().size() == 1) {
  809. ele = updCon.getActualHolonElement(obj, yValueElements, 0, tables);
  810. } else {
  811. ele = updCon.getActualHolonElement(null, yValueElements, 0, tables);
  812. }
  813. // Multi-Selection of HolonElements through control button +
  814. // mouse click
  815. if (ele != null) {
  816. // make energy-consumption graph visible
  817. showScrollGraph();
  818. if (e.isControlDown()) {
  819. if (!selectedElements.contains(ele)) {
  820. selectedElements.add(ele);
  821. if (!holonEleNamesDisplayed.equals(Languages.getLanguage()[25])) {
  822. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  823. } else {
  824. holonEleNamesDisplayed = ele.getEleName() + " ";
  825. }
  826. unitGraph.repaintWithNewElement(selectedElements);
  827. }
  828. updCon.getActualHolonElement(null, yValueElements, 2, tables);
  829. }
  830. // if no control-button pressed but a HolonElement chosen
  831. else {
  832. selectedElements.clear();
  833. selectedElements.add(ele);
  834. updCon.getActualHolonElement(null, yValueElements, 1, tables);
  835. holonEleNamesDisplayed = ele.getEleName() + " ";
  836. unitGraph.repaintWithNewElement(selectedElements);
  837. }
  838. } else {
  839. elementGraph.setText(Languages.getLanguage()[25]);
  840. hideScrollGraph();
  841. }
  842. // if any HolonElement is double-clicked --> Edit-Mode started
  843. // for selected HolonElement
  844. if (e.getClickCount() == 2) {
  845. yThis = e.getY();
  846. xThis = e.getX();
  847. }
  848. // for single click and empty slot
  849. if (e.getClickCount() == 1 && ele == null) {
  850. selectedElements.clear();
  851. holonEleNamesDisplayed = Languages.getLanguage()[25];
  852. }
  853. elementGraph.setText(holonEleNamesDisplayed);
  854. yBTis = e.getY();
  855. xBThis = e.getX();
  856. }
  857. });
  858. /*
  859. * Triggered every time a change is made
  860. */
  861. model.getTableHolonElement().addPropertyChangeListener(propertyChangeEvent -> {
  862. try {
  863. int yMouse = yThis;
  864. int yBMouse = yBTis;
  865. int selectedValueY = (int) Math.floor(yMouse / 16);
  866. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  867. // ------------------ multi-selection mode ------------------
  868. if (model.getSelectedCpsObjects().size() > 1) {
  869. int selectedValueX = (int) Math.floor(xThis / (model.getTableHolonElement().getWidth() / 8));
  870. int selectedValueBX = (int) Math.floor(xBThis / (model.getTableHolonElement().getWidth() / 8));
  871. if (updCon.getHolonObj(yMouse, model.getMultiTable()) != null) {
  872. // For active column (boolean with a checkbox)
  873. if (selectedValueBX == 6) {
  874. HolonElement eleBTemp = updCon.getActualHolonElement(null, yBMouse, 0, tables);
  875. String newBStuff = model.getMultiTable().getValueAt(selectedValueBY, selectedValueBX)
  876. .toString();
  877. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  878. eleBTemp.setActive(bTemp);
  879. }
  880. // For activeFlex column (boolean with a checkbox)
  881. else if (selectedValueBX == 7) {
  882. HolonElement eleBTemp = updCon.getActualHolonElement(null, yBMouse, 0, tables);
  883. String newBStuff = model.getMultiTable().getValueAt(selectedValueBY, selectedValueBX)
  884. .toString();
  885. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  886. eleBTemp.setFlexible(bTemp);
  887. } else {
  888. // Update of HolonElement
  889. HolonElement eleTemp = updCon.getActualHolonElement(null, yMouse, 0, tables);
  890. String newStuff = model.getMultiTable().getValueAt(selectedValueY, selectedValueX)
  891. .toString();
  892. // Name update
  893. if (selectedValueX == 2) {
  894. eleTemp.setEleName(newStuff);
  895. }
  896. // Energy Update
  897. else if (selectedValueX == 3) {
  898. Float ftemp = Float.parseFloat(newStuff);
  899. eleTemp.setEnergyPerElement(ftemp);
  900. }
  901. // Flexibility
  902. else if (selectedValueX == 4) {
  903. Float ftemp = Float.parseFloat(newStuff);
  904. eleTemp.setFlexibleEnergyAvailable(ftemp);
  905. }
  906. // Amount of Elements update
  907. else if (selectedValueX == 5) {
  908. Integer iTemp = Integer.parseInt(newStuff);
  909. eleTemp.setAmount(iTemp);
  910. }
  911. }
  912. }
  913. } // ------------------ single-selection mode ------------------
  914. else if (model.getSelectedCpsObjects().size() == 1) {
  915. int selectedValueX = (int) Math.floor(xThis / (model.getTableHolonElement().getWidth() / 7));
  916. int selectedValueBX = (int) Math.floor(xBThis / (model.getTableHolonElement().getWidth() / 7));
  917. if (updCon.getActualCps() != null && updCon.getActualCps().getClass() == HolonObject.class) {
  918. HolonElement eleTemp;
  919. // For active column (boolean with a checkbox)
  920. if (selectedValueBX == 5) {
  921. eleTemp = updCon
  922. .getActualHolonElement((HolonObject) updCon.getActualCps(), yBMouse, 0, tables);
  923. String newBStuff = model.getSingleTable().getValueAt(selectedValueBY, selectedValueBX)
  924. .toString();
  925. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  926. eleTemp.setActive(bTemp);
  927. } // For activeFlex column (boolean with a checkbox)
  928. else if (selectedValueBX == 6) {
  929. eleTemp = updCon
  930. .getActualHolonElement((HolonObject) updCon.getActualCps(), yBMouse, 0, tables);
  931. String newBStuff = model.getSingleTable().getValueAt(selectedValueBY, selectedValueBX)
  932. .toString();
  933. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  934. eleTemp.setFlexible(bTemp);
  935. } else {
  936. // Update of HolonElement
  937. eleTemp = updCon.getActualHolonElement((HolonObject) updCon.getActualCps(),
  938. yMouse, 0, tables);
  939. String newStuff = model.getSingleTable().getValueAt(selectedValueY, selectedValueX)
  940. .toString();
  941. // Name update
  942. if (selectedValueX == 1) {
  943. eleTemp.setEleName(newStuff);
  944. } // Energy Update
  945. else if (selectedValueX == 2) {
  946. Float ftemp = Float.parseFloat(newStuff);
  947. eleTemp.setEnergyPerElement(ftemp);
  948. } // Flexibility
  949. else if (selectedValueX == 3) {
  950. Float ftemp = Float.parseFloat(newStuff);
  951. eleTemp.setFlexibleEnergyAvailable(ftemp);
  952. if (eleTemp.isFlexible()) {
  953. eleTemp.setEnergyPerElement(0);
  954. // TODO: recalculate energy
  955. }
  956. }
  957. // Amount of Elements update
  958. else if (selectedValueX == 4) {
  959. Integer iTemp = Integer.parseInt(newStuff);
  960. eleTemp.setAmount(iTemp);
  961. }
  962. }
  963. // set all cells anew, if anything was changed
  964. updateElementTableAfterChange(eleTemp, selectedValueBY);
  965. }
  966. }
  967. updCon.refreshTableProperties(model.getPropertyTable());
  968. model.getSingleTable().fireTableDataChanged();
  969. controller.calculateStateForTimeStep(model.getCurIteration());
  970. contentPane.updateUI();
  971. unitGraph.repaint();
  972. } catch (Exception e) {
  973. }
  974. });
  975. /***********************
  976. * HolonElement Properties Actions
  977. **********************/
  978. /*
  979. * Update of the mouse coord for Edit-Mode
  980. */
  981. tableProperties.addMouseListener(new MouseAdapter() {
  982. public void mousePressed(MouseEvent e) {
  983. if (e.getClickCount() == 2) {
  984. yProThis = e.getY();
  985. xProThis = e.getX();
  986. }
  987. yProThisOneClick = e.getY();
  988. xProThisOneClick = e.getX();
  989. }
  990. });
  991. /*
  992. * Update any change in the Property table
  993. */
  994. tableProperties.addPropertyChangeListener(propertyChangeEvent -> {
  995. try {
  996. Object temp;
  997. Object btemp;
  998. int selValueY = (int) Math.floor(yProThis / 16);
  999. int selValueX = (int) Math.floor(xProThis / (tableProperties.getWidth() / 2));
  1000. int selValueYBool = (int) Math.floor(yProThisOneClick / 16);
  1001. int selValueXBool = (int) Math.floor(xProThisOneClick / (tableProperties.getWidth() / 2));
  1002. if (updCon.getActualCps() != null) {
  1003. temp = model.getPropertyTable().getValueAt(selValueY, selValueX);
  1004. btemp = model.getPropertyTable().getValueAt(selValueYBool, selValueXBool);
  1005. // Case: Switch selected
  1006. if (updCon.getActualCps() instanceof HolonSwitch) {
  1007. if (selValueY == 0 && selValueX != 0) {
  1008. updCon.getActualCps().setName(temp.toString());
  1009. }
  1010. if (selValueYBool == 2) {
  1011. Boolean bbTemp = Boolean.parseBoolean(btemp.toString());
  1012. ((HolonSwitch) updCon.getActualCps()).setManualMode(bbTemp);
  1013. if (bbTemp) {
  1014. model.getPropertyTable().setCellEditable(3, 1, true);
  1015. } else {
  1016. model.getPropertyTable().setCellEditable(3, 1, false);
  1017. }
  1018. } else if (selValueYBool == 3) {
  1019. if (((HolonSwitch) updCon.getActualCps()).getManualMode()) {
  1020. model.getPropertyTable().setCellEditable(3, 1, true);
  1021. Boolean bTemp = Boolean.parseBoolean(btemp.toString());
  1022. ((HolonSwitch) updCon.getActualCps()).setManualState(bTemp);
  1023. }
  1024. }
  1025. } // Case: UpperNode selected
  1026. else if (updCon.getActualCps() instanceof CpsUpperNode && selValueX != 0) {
  1027. // Name edition through double click
  1028. updCon.getActualCps().setName(temp.toString());
  1029. // Update of name on the tab in the tab section
  1030. for (int index = 4; index < tabbedPaneOriginal.getTabCount(); index++) {
  1031. tabTemp = tabbedPaneOriginal;
  1032. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1033. UpperNodeCanvas unc = ((UpperNodeCanvas) scrollPane.getViewport().getComponent(0));
  1034. if (unc.upperNode.getId() == updCon.getActualCps().getId()) {
  1035. tabbedPaneOriginal.setTitleAt(index, unc.upperNode.getName());
  1036. tabbedPaneOriginal.updateUI();
  1037. if (!initSplit) {
  1038. tabbedPaneSplit.setTitleAt(index, unc.upperNode.getName());
  1039. tabbedPaneSplit.updateUI();
  1040. }
  1041. }
  1042. }
  1043. } // Case: HolonObject is selected
  1044. else if (updCon.getActualCps() instanceof HolonObject && selValueX != 0) {
  1045. // Name edition through double click
  1046. updCon.getActualCps().setName(temp.toString());
  1047. }
  1048. } // Case: Edge is selected
  1049. else {
  1050. temp = model.getPropertyTable().getValueAt(selValueY, selValueX);
  1051. btemp = model.getPropertyTable().getValueAt(selValueYBool, selValueXBool);
  1052. // Edit modus for capacity by double clicking
  1053. if (selValueY == 2) {
  1054. Float ftemp;
  1055. if (Float.parseFloat(temp.toString()) >= 0.0) {
  1056. ftemp = Float.parseFloat(temp.toString());
  1057. } else {
  1058. ftemp = model.getSelectedEdge().getCapacity();
  1059. }
  1060. model.getSelectedEdge().setCapacity(ftemp);
  1061. }
  1062. // Status edition through a check box
  1063. if (selValueYBool == 3) {
  1064. Boolean bbTemp = Boolean.parseBoolean(btemp.toString());
  1065. model.getSelectedEdge().setWorkingState(bbTemp);
  1066. }
  1067. }
  1068. canvas.repaint();
  1069. } catch (Exception e) {
  1070. }
  1071. });
  1072. /***********************
  1073. * HolonElement Graph Actions
  1074. **********************/
  1075. /*
  1076. * Reset the graph of the selected HolonElement
  1077. */
  1078. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  1079. resetGraphBtn.addActionListener(actionEvent -> unitGraph.reset());
  1080. /*****************************
  1081. * RIGHT CONTAINER DONE
  1082. *****************************/
  1083. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  1084. /****************
  1085. * Tree Stuff
  1086. ****************/
  1087. // Override Key Actions
  1088. inputMap = tree.getInputMap();
  1089. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  1090. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  1091. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  1092. inputMap.put(KeyStroke.getKeyStroke("control A"), cntrlADown);
  1093. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  1094. @Override
  1095. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  1096. boolean leaf, int row, boolean hasFocus) {
  1097. JLabel label = new JLabel();
  1098. Image imgR;
  1099. if (leaf) {
  1100. for (Category cat : model.getCategories()) {
  1101. for (AbstractCpsObject cps : cat.getObjects()) {
  1102. if (value.toString().compareTo(cps.getObjName()) == 0) {
  1103. File checkPath = new File(cps.getImage());
  1104. if (checkPath.exists()) {
  1105. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
  1106. java.awt.Image.SCALE_SMOOTH);
  1107. } else {
  1108. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  1109. .getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  1110. }
  1111. if (imgR != null) {
  1112. label.setIcon(new ImageIcon(imgR));
  1113. }
  1114. label.setText(cps.getName());
  1115. }
  1116. }
  1117. }
  1118. }
  1119. tree.setRowHeight(50);
  1120. if (hasFocus) {
  1121. label.setForeground(new Color(0, 0, 255));
  1122. label.setOpaque(true);
  1123. }
  1124. if (label.getText().length() == 0) {
  1125. label.setText(value.toString());
  1126. if (value.toString().compareTo("Categories") != 0) {
  1127. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  1128. }
  1129. }
  1130. return label;
  1131. }
  1132. };
  1133. tree.setCellRenderer(customRenderer);
  1134. tree.addMouseListener(new MouseAdapter() {
  1135. public void mouseReleased(MouseEvent e) {
  1136. try {
  1137. if (dragging) {
  1138. chooseTabTemp();
  1139. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1140. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  1141. if (canvasOrUpperNodeCanvas instanceof UpperNodeCanvas) {
  1142. UpperNodeCanvas unc = (UpperNodeCanvas) canvasOrUpperNodeCanvas;
  1143. int x = (int) unc.getMousePosition().getX() + 16;
  1144. int y = (int) unc.getMousePosition().getY() + 16;
  1145. if (x > unc.upperNode.getLeftBorder()) {
  1146. AbstractCpsObject h = null;
  1147. if (tempCps instanceof HolonObject) {
  1148. h = new HolonObject(tempCps);
  1149. }
  1150. if (tempCps instanceof HolonSwitch) {
  1151. h = new HolonSwitch(tempCps);
  1152. }
  1153. if (tempCps instanceof HolonTransformer) {
  1154. h = new HolonTransformer(tempCps);
  1155. }
  1156. h.setPosition(x, y);
  1157. controller.addObjUpperNode(h, unc.upperNode);
  1158. unc.invalidate();
  1159. unc.repaint();
  1160. unc.setXY((int) canvas.getMousePosition().getX(),
  1161. (int) canvas.getMousePosition().getY());
  1162. }
  1163. } else {
  1164. int x = (int) canvas.getMousePosition().getX() + 16;
  1165. int y = (int) canvas.getMousePosition().getY() + 16;
  1166. AbstractCpsObject h = null;
  1167. if (tempCps instanceof HolonObject) {
  1168. h = new HolonObject(tempCps);
  1169. }
  1170. if (tempCps instanceof HolonSwitch) {
  1171. h = new HolonSwitch(tempCps);
  1172. }
  1173. if (tempCps instanceof HolonTransformer) {
  1174. h = new HolonTransformer(tempCps);
  1175. }
  1176. h.setPosition(x, y);
  1177. controller.addObjectCanvas(h);
  1178. canvas.invalidate();
  1179. canvas.setXY((int) canvas.getMousePosition().getX(),
  1180. (int) canvas.getMousePosition().getY());
  1181. canvas.repaint();
  1182. }
  1183. controller.calculateStateForCurrentTimeStep();
  1184. contentPane.updateUI();
  1185. dragging = false;
  1186. }
  1187. } catch (Exception eex) {
  1188. }
  1189. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  1190. }
  1191. });
  1192. popmenuEdit.add(editItem);
  1193. editItem.setEnabled(false);
  1194. editItem.addActionListener(actionEvent -> {
  1195. });
  1196. tree.addMouseListener(new MouseAdapter() {
  1197. public void mousePressed(MouseEvent e) {
  1198. try {
  1199. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  1200. // if an Object was selected, the porperties are shown in
  1201. // the table
  1202. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  1203. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  1204. if (selectedNode.getLevel() == 2) {
  1205. controller.searchCategoryObject(selectedNode.getParent().toString(), selectedNode.toString());
  1206. updCon.deleteRows(model.getSingleTable());
  1207. updCon.deleteRows(model.getMultiTable());
  1208. // if (selected instanceof HolonObject && selected !=
  1209. // null) {
  1210. // selected = (HolonObject) selected;
  1211. // fillElementTable(((HolonObject)
  1212. // selected).getElements());
  1213. // }
  1214. }
  1215. if (SwingUtilities.isRightMouseButton(e)) {
  1216. for (Category cat : model.getCategories()) {
  1217. for (AbstractCpsObject cps : cat.getObjects()) {
  1218. if (actualObjectClicked.compareTo(cps.getObjName()) == 0
  1219. && !(cps instanceof HolonSwitch)) {
  1220. editItem.setEnabled(true);
  1221. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  1222. catOfObjToBeEdited = selectedNode.getParent().toString();
  1223. tempCps = cps;
  1224. }
  1225. }
  1226. }
  1227. } else {
  1228. for (Category cat : model.getCategories()) {
  1229. for (AbstractCpsObject cps : cat.getObjects()) {
  1230. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  1231. File checkPath = new File(cps.getImage());
  1232. if (checkPath.exists()) {
  1233. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(32, 32,
  1234. java.awt.Image.SCALE_SMOOTH);
  1235. } else {
  1236. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  1237. .getScaledInstance(32, 32, java.awt.Image.SCALE_SMOOTH);
  1238. }
  1239. tempCps = cps;
  1240. dragging = true;
  1241. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  1242. "Image");
  1243. frmCyberPhysical.setCursor(cursor);
  1244. }
  1245. }
  1246. }
  1247. }
  1248. } catch (Exception eex) {
  1249. }
  1250. }
  1251. });
  1252. editItem.addActionListener(actionEvent -> {
  1253. // Remove the selected Object object
  1254. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
  1255. addObjectPopUP.setCategory(catOfObjToBeEdited);
  1256. addObjectPopUP.setController(controller);
  1257. addObjectPopUP.setVisible(true);
  1258. });
  1259. scrollPane1.setViewportView(tree);
  1260. scrollPane1.setColumnHeaderView(panel);
  1261. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  1262. toolBar.setAlignmentX(Component.LEFT_ALIGNMENT);
  1263. toolBar.setFloatable(false);
  1264. panel.add(toolBar);
  1265. comboBox.setMaximumSize(new Dimension(100, 20));
  1266. toolBar.add(comboBox);
  1267. comboBox.setModel(new DefaultComboBoxModel(comboBoxCat));
  1268. // Add Buttonnew DefaultComboBoxModel(comboBoxCat)
  1269. btnAdd.addActionListener(actionEvent -> {
  1270. Object nodeInfo = tree.getLastSelectedPathComponent();
  1271. String selectedOption = comboBox.getSelectedItem().toString();
  1272. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1273. try {
  1274. switch (selectedOption) {
  1275. case "Category":
  1276. String catName = JOptionPane.showInputDialog(Languages.getLanguage()[56]);
  1277. if (catName.length() != 0) {
  1278. controller.addCategory(catName);
  1279. }
  1280. break;
  1281. case "Object":
  1282. if (selectedNode == null) {
  1283. JOptionPane.showMessageDialog(new JFrame(),
  1284. Languages.getLanguage()[57] + selectedOption + ".");
  1285. }
  1286. if (selectedNode != null && selectedNode.getLevel() == 1) {
  1287. AbstractCpsObject tmp = new HolonObject("");
  1288. addObjectPopUP = new AddObjectPopUp(false, tmp, null);
  1289. addObjectPopUP.setVisible(true);
  1290. addObjectPopUP.setController(controller);
  1291. addObjectPopUP.setCategory(selectedNode.toString());
  1292. }
  1293. break;
  1294. default:
  1295. addObjectAction(selectedOption, selectedNode);
  1296. break;
  1297. }
  1298. } catch (Exception e) {
  1299. }
  1300. tree.repaint();
  1301. });
  1302. /**
  1303. * Pop up - About Us with some important information about the
  1304. * developers, source and programming stuff
  1305. */
  1306. mntmAboutUs.addMouseListener(new MouseAdapter() {
  1307. @Override
  1308. public void mousePressed(MouseEvent e) {
  1309. aboutUsPopUp = new AboutUsPopUp();
  1310. aboutUsPopUp.setVisible(true);
  1311. }
  1312. });
  1313. /**
  1314. * Update of every interaction between the user and the canvas (only on
  1315. * the canvas). Basically the update of all the information concerning
  1316. * the clicked HolonObject. For multi-selection, the propertyTable would
  1317. * be disabled
  1318. */
  1319. canvas.addMouseListener(new MouseAdapter() {
  1320. @Override
  1321. public void mousePressed(MouseEvent e) {
  1322. }
  1323. @Override
  1324. public void mouseReleased(MouseEvent e) {
  1325. holonEleNamesDisplayed = Languages.getLanguage()[25];
  1326. // If any empty space is clicked
  1327. if (temp == null || temp.getId() != model.getSelectedObjectID()) {
  1328. hideScrollGraph();
  1329. elementGraph.setText(Languages.getLanguage()[25]);
  1330. }
  1331. temp = updCon.getActualCps();
  1332. // Erase old data in the PropertyTable
  1333. if (model.getPropertyTable().getRowCount() > 0) {
  1334. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  1335. model.getPropertyTable().removeRow(i);
  1336. }
  1337. }
  1338. if (e.isControlDown() && temp != null) {
  1339. if (model.getSelectedCpsObjects().contains(temp)) {
  1340. controller.deleteSelectedObject(temp);
  1341. } else {
  1342. controller.addSelectedObject(temp);
  1343. }
  1344. }
  1345. if (temp instanceof HolonSwitch) {
  1346. showScrollGraph();
  1347. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  1348. unitGraph.fillArrayofBooleans();
  1349. }
  1350. // Write new data in the PropertyTable
  1351. updCon.paintProperties(temp);
  1352. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  1353. // New Tab with NodeOfNode
  1354. if (doubleClick() && MouseEvent.BUTTON3 != e.getButton() && temp instanceof CpsUpperNode) {
  1355. openNewUpperNodeTab();
  1356. try {
  1357. controller.autoSave();
  1358. } catch (IOException e1) {
  1359. e1.printStackTrace();
  1360. }
  1361. }
  1362. if (model.getSelectedCpsObjects().size() > 1) {
  1363. model.getTableHolonElement().setModel(model.getMultiTable());
  1364. } else if (model.getSelectedCpsObjects().size() == 1) {
  1365. model.getTableHolonElement().setModel(model.getSingleTable());
  1366. }
  1367. }
  1368. });
  1369. holonCanvas.addMouseListener(new MouseAdapter() {
  1370. @Override
  1371. public void mousePressed(MouseEvent e) {
  1372. hideScrollGraph();
  1373. elementGraph.setText(Languages.getLanguage()[25]);
  1374. if (model.getPropertyTable().getRowCount() > 0) {
  1375. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  1376. model.getPropertyTable().removeRow(i);
  1377. }
  1378. }
  1379. updCon.deleteRows(model.getMultiTable());
  1380. updCon.deleteRows(model.getSingleTable());
  1381. updCon.paintHolonBody(model.getSelectedHolonBody());
  1382. }
  1383. });
  1384. toolBar.add(btnAdd);
  1385. // Del Button
  1386. btnDel.addActionListener(actionEvent -> {
  1387. Object nodeInfo = tree.getLastSelectedPathComponent();
  1388. if (nodeInfo != null) {
  1389. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1390. String nodeName = selectedNode.getUserObject().toString();
  1391. int depthOfNode = selectedNode.getLevel();
  1392. try {
  1393. switch (depthOfNode) {
  1394. case 1:
  1395. int dialogResult = JOptionPane.showConfirmDialog(null, eraseCategory + nodeName + "?",
  1396. warningText, JOptionPane.YES_NO_OPTION);
  1397. if (dialogResult == JOptionPane.YES_OPTION) {
  1398. controller.deleteCategory(nodeName);
  1399. }
  1400. break;
  1401. case 2:
  1402. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  1403. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  1404. break;
  1405. default:
  1406. JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
  1407. }
  1408. } catch (Exception e2) {
  1409. }
  1410. } else {
  1411. JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
  1412. }
  1413. tree.repaint();
  1414. });
  1415. toolBar.add(btnDel);
  1416. frmCyberPhysical.getContentPane().add(splitPane);
  1417. mntmNew.addActionListener(actionEvent -> {
  1418. if (model.getObjectsOnCanvas().size() != 0) {
  1419. int newWarning = JOptionPane.YES_NO_OPTION;
  1420. int dialogForNewWarning = JOptionPane.showConfirmDialog(null, saveBeforeNew, warningText,
  1421. newWarning);
  1422. if (dialogForNewWarning == JOptionPane.YES_OPTION) {
  1423. mntmSave.doClick();
  1424. }
  1425. }
  1426. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  1427. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  1428. controller.setSelectedObjectID(0);
  1429. controller.setSelecteEdge(null);
  1430. controller.setCurIteration(0);
  1431. hideScrollGraph();
  1432. elementGraph.setText(Languages.getLanguage()[25]);
  1433. canvas.tempCps = null;
  1434. canvas.repaint();
  1435. IdCounter.resetCounter();
  1436. IdCounterElem.resetCounter();
  1437. });
  1438. mntmOpen.addActionListener(new ActionListener() {
  1439. @Override
  1440. public void actionPerformed(ActionEvent evt) {
  1441. menuFileExitActionPerformed();
  1442. }
  1443. private void menuFileExitActionPerformed() {
  1444. JFileChooser fileChooser = new JFileChooser();
  1445. JFrame test = new JFrame();
  1446. FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
  1447. fileChooser.setFileFilter(holonFilter);
  1448. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  1449. File file = fileChooser.getSelectedFile();
  1450. try {
  1451. controller.loadFile(file.getAbsolutePath());
  1452. canvas.repaint();
  1453. unitGraph.update(model.getObjectsOnCanvas());
  1454. tree.repaint();
  1455. for (JsonObject json : model.getStatisticData()) {
  1456. readStatistics(json);
  1457. }
  1458. } catch (IOException | ArchiveException e) {
  1459. e.printStackTrace();
  1460. JLabel message = new JLabel("The savefile is corrupt and cannot be opened.");
  1461. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  1462. }
  1463. }
  1464. }
  1465. });
  1466. mntmSave.addActionListener(new ActionListener() {
  1467. @Override
  1468. public void actionPerformed(ActionEvent evt) {
  1469. menuSaveActionPerformed();
  1470. }
  1471. private void menuSaveActionPerformed() {
  1472. JFileChooser fileChooser = new JFileChooser();
  1473. JFrame test = new JFrame();
  1474. FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
  1475. fileChooser.setFileFilter(holonFilter);
  1476. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  1477. String file = fileChooser.getSelectedFile().getPath();
  1478. if (fileChooser.getFileFilter().equals(holonFilter)) {
  1479. if (!file.contains("."))
  1480. file += ".holon";
  1481. }
  1482. if (!file.endsWith(".holon")) {
  1483. String suffix = file.substring(file.lastIndexOf("."), file.length());
  1484. String[] options = new String[]{"keep .holon", "use " + suffix};
  1485. JLabel message = new JLabel(
  1486. "Are you sure to use the extension \"" + suffix + "\" instead of \".holon\"?");
  1487. int response = JOptionPane.showOptionDialog(null, message, "", JOptionPane.DEFAULT_OPTION,
  1488. JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
  1489. if (response == 0)
  1490. file = file.replace(suffix, ".holon");
  1491. }
  1492. try {
  1493. controller.saveFile(new File(file).getAbsolutePath());
  1494. } catch (IOException | ArchiveException e) {
  1495. e.printStackTrace();
  1496. }
  1497. }
  1498. }
  1499. });
  1500. englishBtn.addActionListener(actionEvent -> {
  1501. Languages.setLanguage(0);
  1502. refreshLanguages();
  1503. updCon.refreshTableProperties(model.getPropertyTable());
  1504. });
  1505. spanishBtn.addActionListener(actionEvent -> {
  1506. Languages.setLanguage(1);
  1507. refreshLanguages();
  1508. updCon.refreshTableProperties(model.getPropertyTable());
  1509. });
  1510. germanBtn.addActionListener(actionEvent -> {
  1511. Languages.setLanguage(2);
  1512. refreshLanguages();
  1513. updCon.refreshTableProperties(model.getPropertyTable());
  1514. });
  1515. czechBtn.addActionListener(actionEvent -> {
  1516. Languages.setLanguage(3);
  1517. refreshLanguages();
  1518. updCon.refreshTableProperties(model.getPropertyTable());
  1519. });
  1520. chineseBtn.addActionListener(actionEvent -> {
  1521. Languages.setLanguage(4);
  1522. refreshLanguages();
  1523. updCon.refreshTableProperties(model.getPropertyTable());
  1524. });
  1525. mntmUndo.addActionListener(new ActionListener() {
  1526. @Override
  1527. public void actionPerformed(ActionEvent evt) {
  1528. menuUndoActionPerformed();
  1529. }
  1530. private void menuUndoActionPerformed() {
  1531. try {
  1532. controller.loadAutoSave(controller.getUndoSave());
  1533. canvas.repaint();
  1534. repaintGraphAfterUndoRedo();
  1535. hideScrollGraph();
  1536. } catch (IOException e) {
  1537. e.printStackTrace();
  1538. }
  1539. }
  1540. });
  1541. mntmRedo.addActionListener(new ActionListener() {
  1542. @Override
  1543. public void actionPerformed(ActionEvent evt) {
  1544. menuRedoActionPerformed();
  1545. }
  1546. private void menuRedoActionPerformed() {
  1547. try {
  1548. controller.loadAutoSave(controller.getRedoSave());
  1549. canvas.repaint();
  1550. repaintGraphAfterUndoRedo();
  1551. hideScrollGraph();
  1552. } catch (IOException e) {
  1553. e.printStackTrace();
  1554. }
  1555. }
  1556. });
  1557. timePanel = new TimePanel(model, controller);
  1558. timePanel.setBorder(null);
  1559. ((JSlider) (timePanel.getComponent(1))).addChangeListener(changeEvent -> {
  1560. int i = model.getCurIteration();
  1561. controller.calculateStateForTimeStep(i);
  1562. unitGraph.repaint();
  1563. if (model.getIsSimRunning()) {
  1564. controller.runAlgorithm(model, controller);
  1565. statSplitPane.repaintGraphs();
  1566. }
  1567. contentPane.updateUI();
  1568. });
  1569. splitPane1.setMinimumSize(new Dimension(0, 25));
  1570. splitPane.setRightComponent(splitPane1);
  1571. splitPane.setDividerLocation(200);
  1572. splitPane1.setDividerLocation(500);
  1573. splitPaneCanvasConsole.setDividerLocation(550);
  1574. splitPaneCanvasConsole.setResizeWeight(0.9);
  1575. splitPane.setLeftComponent(scrollPane1);
  1576. splitPaneCanvasConsole.setLeftComponent(tabbedPaneOriginal);
  1577. tabbedPaneOriginal.addTab("View", tabbedPaneInnerOriginal);
  1578. tabbedPaneInnerOriginal.addTab("Main Grid", canvasSP);
  1579. // tabbedPaneSplit.addTab("View", tabbedPaneInnerSplit);
  1580. // tabbedPaneInnerSplit.addTab("Main Grid", null);
  1581. tabbedPaneOriginal.addTab("Statistics", statScrollPane);
  1582. tabbedPaneOriginal.addTab("Holon", holonCanvas);
  1583. FlexiblePane flexPane = new FlexiblePane(controller);
  1584. controller.setFlexiblePane(flexPane);
  1585. controller.getModel().getObjectListeners().add(flexPane);
  1586. tabbedPaneOriginal.addTab("Flexibility", flexPane);
  1587. splitPaneCanvasConsole.setRightComponent(console);
  1588. splitPane1.setLeftComponent(splitPaneCanvasConsole);
  1589. splitPane1.setRightComponent(splitHolonElPro);
  1590. splitPane1.setResizeWeight(0.9);
  1591. splitHolonElPro.setDividerLocation(400);
  1592. splitHolonElPro.setTopComponent(splitGraphHolonEl);
  1593. splitHolonElPro.setBottomComponent(scrollProperties);
  1594. splitGraphHolonEl.setDividerLocation(150);
  1595. splitGraphHolonEl.setTopComponent(scrollGraph);
  1596. splitGraphHolonEl.setBottomComponent(scrollElements);
  1597. canvasSP.setViewportView(canvas);
  1598. algorithmMenu.setBackground(new Color(240, 240, 240));
  1599. tabbedPaneOriginal.setBorder(null);
  1600. scrollProperties.setBorder(null);
  1601. scrollGraph.setBorder(null);
  1602. scrollElements.setBorder(null);
  1603. splitPane.setBorder(null);
  1604. splitPane1.setBorder(null);
  1605. splitPaneCanvasConsole.setBorder(null);
  1606. splitHolonElPro.setBorder(null);
  1607. splitGraphHolonEl.setBorder(null);
  1608. panelHolonEl.setBorder(null);
  1609. canvasSP.setBorder(null);
  1610. tableHolonElementScrollPane.setBorder(null);
  1611. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  1612. MouseAdapter focusCanvasAdapter = new FocusCanvasMouseAdapter();
  1613. model.getTableHolonElement().addMouseListener(focusCanvasAdapter);
  1614. tableProperties.addMouseListener(focusCanvasAdapter);
  1615. console.getConsoleText().addMouseListener(focusCanvasAdapter);
  1616. try {
  1617. controller.loadAutoSave(System.getProperty("user.home") + "/.config/HolonGUI/Category/Category.json");
  1618. } catch (IOException e1) {
  1619. }
  1620. String autoPath = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  1621. File dest = new File(autoPath);
  1622. if (dest.listFiles().length > 1) {
  1623. int dialogButton = JOptionPane.YES_NO_OPTION;
  1624. int dialogResult = JOptionPane.showConfirmDialog(null, "Old autosave file was found, should it be loaded?",
  1625. warningText, dialogButton);
  1626. if (dialogResult == JOptionPane.YES_OPTION) {
  1627. if (dest.exists()) {
  1628. model.setAutoSaveNr(dest.listFiles().length - 1);
  1629. mntmRedo.doClick();
  1630. } else {
  1631. JOptionPane.showMessageDialog(frmCyberPhysical, "Autosave could not be loaded.");
  1632. setUpAutoSave(dest);
  1633. }
  1634. } else {
  1635. setUpAutoSave(dest);
  1636. }
  1637. }
  1638. canvasSP.addComponentListener(new ComponentAdapter() {
  1639. @Override
  1640. public void componentResized(ComponentEvent e) {
  1641. controller.setCanvasX(Math.max(model.getCanvasX(), canvasSP.getViewport().getWidth()));
  1642. controller.setCanvasY(Math.max(model.getCanvasY(), canvasSP.getViewport().getHeight()));
  1643. canvas.repaint();
  1644. }
  1645. });
  1646. }
  1647. private void showScrollGraph() {
  1648. scrollGraph.setVisible(true);
  1649. splitGraphHolonEl.setTopComponent(scrollGraph);
  1650. splitGraphHolonEl.setDividerLocation(150);
  1651. }
  1652. private void hideScrollGraph() {
  1653. scrollGraph.setVisible(false);
  1654. unitGraph.empty();
  1655. splitGraphHolonEl.remove(scrollGraph);
  1656. splitGraphHolonEl.setDividerLocation(0);
  1657. }
  1658. /**
  1659. * Sets up autosave if no old one is loaded at the beginning
  1660. *
  1661. * @param dest path to save-folder
  1662. */
  1663. private void setUpAutoSave(File dest) {
  1664. dest.mkdirs();
  1665. try {
  1666. controller.autoSave();
  1667. } catch (IOException e1) {
  1668. e1.printStackTrace();
  1669. }
  1670. }
  1671. /**
  1672. * adds a specific object type to selected Category also handles input
  1673. * windows and illegal inputs.
  1674. *
  1675. * @param objType type of the Object
  1676. * @param selectedNode The selected Node
  1677. */
  1678. private void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  1679. if (selectedNode == null) {
  1680. JOptionPane.showMessageDialog(new JFrame(),
  1681. "Please select a Category first before adding " + objType + ".");
  1682. }
  1683. // if selected node is a directory for Categories
  1684. else {
  1685. if (selectedNode.getLevel() == 1) {
  1686. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  1687. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  1688. if (objname.length() != 0) {
  1689. try {
  1690. switch (objType) {
  1691. case "Switch":
  1692. controller.addSwitch(cat, objname);
  1693. break;
  1694. }
  1695. } catch (Exception e) {
  1696. }
  1697. }
  1698. } else {
  1699. JOptionPane.showMessageDialog(new JFrame(),
  1700. "Objects can not be added to Objects. Please select a Category.");
  1701. }
  1702. }
  1703. }
  1704. /**
  1705. * reloads the Categories from Model.
  1706. *
  1707. * @param categories the current categories
  1708. */
  1709. private void updateCategories(final ArrayList<Category> categories) {
  1710. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  1711. private static final long serialVersionUID = 1L;
  1712. {
  1713. DefaultMutableTreeNode node1;
  1714. for (Category c : categories) {
  1715. node1 = new DefaultMutableTreeNode(c.getName());
  1716. // kann eventuell umgeändert werden
  1717. for (AbstractCpsObject obj : c.getObjects()) {
  1718. node1.add(new DefaultMutableTreeNode(obj.getObjName()));
  1719. }
  1720. add(node1);
  1721. }
  1722. }
  1723. }));
  1724. }
  1725. /**
  1726. * When changes are made to the Categories.
  1727. *
  1728. * @param categories the Categories
  1729. */
  1730. public void onChange(ArrayList<Category> categories) {
  1731. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  1732. updateCategories(categories);
  1733. model.reload();
  1734. }
  1735. /**
  1736. * Get the Frame.
  1737. *
  1738. * @return the Frame
  1739. */
  1740. JFrame getFrmCyberPhysical() {
  1741. return frmCyberPhysical;
  1742. }
  1743. private void refreshLanguages() {
  1744. String[] tempArray = Languages.getLanguage();
  1745. // ToolBar
  1746. mnNewMenu.setText(tempArray[0]);
  1747. mntmNew.setText(tempArray[1]);
  1748. mntmOpen.setText(tempArray[2]);
  1749. mntmSave.setText(tempArray[3]);
  1750. mnNewMenuEdit.setText(tempArray[4]);
  1751. mntmUndo.setText(tempArray[5]);
  1752. mntmRedo.setText(tempArray[6]);
  1753. mntmFindReplace.setText(tempArray[7]);
  1754. mntmEditShowedInformation.setText(tempArray[8]);
  1755. mnNewMenuOptions.setText(tempArray[9]);
  1756. mntmResetCategory.setText(tempArray[10]);
  1757. mnNewMenuView.setText(tempArray[11]);
  1758. mntmCanvasSize.setText(tempArray[12]);
  1759. mnHelp.setText(tempArray[13]);
  1760. mntmAboutUs.setText(tempArray[14]);
  1761. mntmEditEdges.setText(tempArray[15]);
  1762. mnLanguage.setText(tempArray[16]);
  1763. canvas.updateLanguages();
  1764. // Tables
  1765. System.arraycopy(tempArray, 17, columnNamesMulti, 0, columnNamesMulti.length);
  1766. model.getMultiTable().setColumnIdentifiers(columnNamesMulti);
  1767. System.arraycopy(tempArray, 18, columnNamesSingle, 0, columnNamesSingle.length);
  1768. model.getSingleTable().setColumnIdentifiers(columnNamesSingle);
  1769. model.colNames[0] = tempArray[23];
  1770. model.colNames[1] = tempArray[24];
  1771. model.getPropertyTable().setColumnIdentifiers(model.colNames);
  1772. // Graph
  1773. elementGraph.setText(tempArray[25]);
  1774. holonEleNamesDisplayed = tempArray[25];
  1775. resetGraphBtn.setText(tempArray[26]);
  1776. // Warning PopUps
  1777. warningText = tempArray[27];
  1778. saveBeforeNew = tempArray[28];
  1779. eraseCategory = tempArray[29];
  1780. selectObjBeforeErase = tempArray[30];
  1781. // SimMenu
  1782. algorithmMenu.algoFolderButton.setText(Languages.getLanguage()[85]);
  1783. // TimePanel
  1784. timePanel.playBtn.setToolTipText(Languages.getLanguage()[89]);
  1785. timePanel.timeResetBtn.setToolTipText(Languages.getLanguage()[90]);
  1786. timePanel.timeForwardBtn.setToolTipText(Languages.getLanguage()[91]);
  1787. timePanel.timeBackwardBtn.setToolTipText(Languages.getLanguage()[92]);
  1788. timePanel.timeSlider.setToolTipText(Languages.getLanguage()[93]);
  1789. // Image Size Slider
  1790. lblImageSize.setText(Languages.getLanguage()[94]);
  1791. }
  1792. /**
  1793. * Checks if a double click was made.
  1794. *
  1795. * @return true if doublecklick, false if not
  1796. */
  1797. private boolean doubleClick() {
  1798. if (click) {
  1799. click = false;
  1800. return true;
  1801. } else {
  1802. click = true;
  1803. Timer t = new Timer("doubleclickTimer", false);
  1804. t.schedule(new TimerTask() {
  1805. @Override
  1806. public void run() {
  1807. click = false;
  1808. }
  1809. }, 350);
  1810. }
  1811. return false;
  1812. }
  1813. /*
  1814. * Open a new Tab with an UpperNodeCanvas
  1815. */
  1816. private void openNewUpperNodeTab() {
  1817. chooseTabTemp();
  1818. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1819. if (scrollPane.getViewport().getComponent(0) instanceof MyCanvas) {
  1820. unc = new UpperNodeCanvas(model, controller, unitGraph, (CpsUpperNode) temp, "");
  1821. } else {
  1822. unc = new UpperNodeCanvas(model, controller, unitGraph, (CpsUpperNode) temp,
  1823. ((UpperNodeCanvas) scrollPane.getViewport().getComponent(0)).path + " -> ");
  1824. }
  1825. unc.setShowedInformation(canvas.getShowedInformation());
  1826. // check if tab already open for clicked NodeOfNode
  1827. boolean dupl = false;
  1828. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1829. JScrollPane paneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1830. if (paneOriginal != null &&
  1831. ((UpperNodeCanvas) paneOriginal.getViewport().getComponent(0)).upperNode.getId() == temp.getId()) {
  1832. dupl = true;
  1833. // set selected component to view
  1834. tabbedPaneOriginal.setSelectedComponent(tabbedPaneInnerOriginal);
  1835. // set selected tab in view to found upper-node-canvas
  1836. tabbedPaneInnerOriginal.setSelectedComponent(tabbedPaneInnerOriginal.getComponentAt(i));
  1837. }
  1838. if (tabbedPaneSplit.getTabCount() > 0) {
  1839. JTabbedPane tabbedPaneInnerSplit = (JTabbedPane) tabbedPaneSplit.getComponentAt(0);
  1840. if (tabbedPaneInnerSplit != null) {
  1841. JScrollPane paneSplit = (JScrollPane) tabbedPaneInnerSplit.getComponentAt(i);
  1842. if (!initSplit && paneSplit != null
  1843. && ((UpperNodeCanvas) paneSplit.getViewport().getComponent(0)).upperNode.getId() == temp.getId()) {
  1844. dupl = true;
  1845. // set selected component to view
  1846. tabbedPaneSplit.setSelectedComponent(tabbedPaneInnerSplit);
  1847. // set selected tab in view to found upper-node-canvas
  1848. tabbedPaneInnerSplit.setSelectedComponent(tabbedPaneInnerSplit.getComponentAt(i));
  1849. }
  1850. }
  1851. }
  1852. // if we found a duplicate, break
  1853. if (dupl) {
  1854. break;
  1855. }
  1856. }
  1857. if (!dupl) {
  1858. unc.setBorder(null);
  1859. unc.setBackground(Color.WHITE);
  1860. unc.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
  1861. unc.addMouseListener(new MouseAdapter() {
  1862. @Override
  1863. public void mousePressed(MouseEvent e) {
  1864. hideScrollGraph();
  1865. holonEleNamesDisplayed = Languages.getLanguage()[25];
  1866. elementGraph.setText(holonEleNamesDisplayed);
  1867. chooseTabTemp();
  1868. JScrollPane scrollPane1 = getScrollPaneFromTabbedPane();
  1869. temp = ((UpperNodeCanvas) scrollPane1.getViewport().getComponent(0)).tempCps;
  1870. if (doubleClick() && MouseEvent.BUTTON3 != e.getButton() && temp instanceof CpsUpperNode) {
  1871. openNewUpperNodeTab();
  1872. }
  1873. if (temp instanceof HolonSwitch) {
  1874. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  1875. unitGraph.fillArrayofBooleans();
  1876. }
  1877. }
  1878. });
  1879. JScrollPane sp = new JScrollPane(unc);
  1880. sp.setBorder(null);
  1881. // Selected tabbed Pane = tabbedPaneOriginal or tabbedPaneSplit
  1882. if (tabTemp == tabbedPaneOriginal) {
  1883. this.tabbedPaneInnerOriginal.add(temp.getName(), sp);
  1884. this.tabbedPaneInnerOriginal.setSelectedComponent(sp);
  1885. this.tabbedPaneInnerOriginal.setTabComponentAt(this.tabbedPaneInnerOriginal.getTabCount() - 1,
  1886. new ButtonTabComponent(this.tabbedPaneInnerOriginal, tabbedPaneSplit));
  1887. } else {
  1888. JTabbedPane tabbedPaneInnerSplit = (JTabbedPane) tabbedPaneSplit.getComponentAt(0);
  1889. tabbedPaneInnerSplit.add(temp.getName(), sp);
  1890. Component tempC = tabbedPaneInnerSplit.getSelectedComponent();
  1891. tabbedPaneInnerSplit.setComponentAt(tabbedPaneInnerSplit.getSelectedIndex(), null);
  1892. tabbedPaneOriginal.setComponentAt(tabbedPaneInnerSplit.getSelectedIndex(), tempC);
  1893. tabbedPaneInnerSplit.setSelectedComponent(sp);
  1894. tabbedPaneInnerOriginal.add(temp.getName(), null);
  1895. tabbedPaneInnerOriginal.setTabComponentAt(tabbedPaneInnerOriginal.getTabCount() - 1,
  1896. new ButtonTabComponent(tabbedPaneInnerOriginal, tabbedPaneInnerSplit));
  1897. }
  1898. temp = null;
  1899. }
  1900. }
  1901. private void updateUpperNodes() {
  1902. for (Component c : tabbedPaneInnerOriginal.getComponents()) {
  1903. if (!(c instanceof JScrollPane)) {
  1904. continue;
  1905. }
  1906. Component pane = ((JScrollPane) c).getViewport().getComponent(0);
  1907. if (pane instanceof UpperNodeCanvas) {
  1908. temp = model.getHashcodeMap().get(((UpperNodeCanvas) pane).code);
  1909. ((UpperNodeCanvas) pane).upperNode = (CpsUpperNode) temp;
  1910. }
  1911. }
  1912. }
  1913. /**
  1914. * Removes UpperNodeTab if UpperNode was deleted
  1915. *
  1916. * @param cps the CPS object that is currently selected
  1917. */
  1918. private void removeUpperNodeTab(AbstractCpsObject cps) {
  1919. if (cps instanceof CpsUpperNode) {
  1920. boolean splitView = false;
  1921. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1922. JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1923. if (scrollPaneOriginal == null) {
  1924. splitView = true;
  1925. } else if (((UpperNodeCanvas) scrollPaneOriginal.getViewport().getComponent(0)).upperNode.getId()
  1926. == cps.getId()) {
  1927. ((ButtonTabComponent) tabbedPaneInnerOriginal.getTabComponentAt(i)).removeTabs();
  1928. break;
  1929. }
  1930. }
  1931. // If SplitView is on and the view on
  1932. // tabbedPaneSplit is the deleted upperNode
  1933. if (splitView && tabbedPaneSplit.getComponentCount() > 0) {
  1934. JTabbedPane tabbedPaneInnerSplit = (JTabbedPane) tabbedPaneSplit.getComponentAt(0);
  1935. if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInnerSplit.getSelectedComponent())
  1936. .getViewport().getComponent(0)).upperNode.getId() == cps.getId()) {
  1937. ((ButtonTabComponent) tabbedPaneInnerOriginal
  1938. .getTabComponentAt(tabbedPaneInnerSplit.getSelectedIndex())).removeTabs();
  1939. }
  1940. }
  1941. }
  1942. }
  1943. private void readStatistics(JsonObject json) {
  1944. List<String> keys = json.entrySet().stream().map(i -> i.getKey())
  1945. .collect(Collectors.toCollection(ArrayList::new));
  1946. // Saved Values
  1947. JsonObject obj;
  1948. AbstractCpsObject cps;
  1949. int prop;
  1950. Color color;
  1951. StatisticGraphPanel stat = new StatisticGraphPanel(model, controller, json.get("KEY").getAsString(),
  1952. model.getGraphTable());
  1953. for (String k : keys) {
  1954. if (!k.equals("KEY")) {
  1955. obj = json.get(k).getAsJsonObject();
  1956. cps = (obj.get("ID") == JsonNull.INSTANCE ? null : controller.searchTracked(obj.get("ID").getAsInt()));
  1957. prop = obj.get("PROPERTY").getAsInt();
  1958. color = model.getGson().fromJson(obj.get("COLOR"), Color.class);
  1959. stat.addObject(new TrackedDataSet(cps, prop, color));
  1960. }
  1961. model.getGraphTable().put(json.get("KEY").getAsString(), stat);
  1962. model.getStatPanel().getGraphPanel().add(stat);
  1963. }
  1964. }
  1965. /**
  1966. * chooses whether to set the tabTemp to tabbedPaneOriginal or tabbedPaneSplit
  1967. */
  1968. private void chooseTabTemp() {
  1969. // is the uppernode on tabbedPaneOriginal or tabbedPaneSplit
  1970. if (tabbedPaneOriginal.getMousePosition() != null) {
  1971. tabTemp = tabbedPaneOriginal;
  1972. } else {
  1973. tabTemp = tabbedPaneSplit;
  1974. }
  1975. }
  1976. private JScrollPane getScrollPaneFromTabbedPane(JTabbedPane tabbedPane, int index) {
  1977. tabTemp = tabbedPane;
  1978. return getScrollPaneFromTabbedPane(index);
  1979. }
  1980. // private JScrollPane getScrollPaneFromTabbedPane(JTabbedPane tabbedPane) {
  1981. // return getScrollPaneFromTabbedPane(tabbedPane, -1);
  1982. // }
  1983. private JScrollPane getScrollPaneFromTabbedPane() {
  1984. return getScrollPaneFromTabbedPane(-1);
  1985. }
  1986. private JScrollPane getScrollPaneFromTabbedPane(int index) {
  1987. Component upperLevelSelectedComponent;
  1988. if (index == -1) {
  1989. upperLevelSelectedComponent = tabTemp.getSelectedComponent();
  1990. } else {
  1991. upperLevelSelectedComponent = tabTemp.getComponentAt(index);
  1992. }
  1993. if (upperLevelSelectedComponent instanceof JTabbedPane) {
  1994. return (JScrollPane) ((JTabbedPane) upperLevelSelectedComponent).getSelectedComponent();
  1995. } else if (upperLevelSelectedComponent instanceof JScrollPane) {
  1996. return (JScrollPane) upperLevelSelectedComponent;
  1997. } else {
  1998. return null;
  1999. }
  2000. }
  2001. private void repaintGraphAfterUndoRedo() {
  2002. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  2003. unitGraph.repaintGraph(cps);
  2004. }
  2005. }
  2006. /**
  2007. * if flexibility was turned on, then active needs to be turned off, the energy currently produced/consumed
  2008. *
  2009. * @param eleBTemp element that needs to be updated
  2010. * @param selectedValueBY the y value in the table
  2011. */
  2012. private void updateElementTableAfterChange(HolonElement eleBTemp, int selectedValueBY) {
  2013. model.getSingleTable().setValueAt(eleBTemp.isFlexible(), selectedValueBY, 6);
  2014. model.getSingleTable().setValueAt(eleBTemp.isActive(), selectedValueBY, 5);
  2015. model.getSingleTable().setValueAt(eleBTemp.getAmount(), selectedValueBY, 4);
  2016. model.getSingleTable().setValueAt(eleBTemp.getFlexibleEnergyAvailablePerElement(), selectedValueBY, 3);
  2017. model.getSingleTable().setValueAt(eleBTemp.getEnergyPerElement(), selectedValueBY, 2);
  2018. }
  2019. /**
  2020. * Custom Mouse adapter makes contentPane gain focus once mouse leaves this component
  2021. * so copy/paste/cut and "select all" works
  2022. */
  2023. private class FocusCanvasMouseAdapter extends MouseAdapter {
  2024. @Override
  2025. public void mouseExited(MouseEvent e) {
  2026. contentPane.requestFocus();
  2027. }
  2028. }
  2029. }