GUI.java 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  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. controller.calculateStateForTimeStep(model.getCurIteration());
  752. triggerUpdateController(null);
  753. contentPane.updateUI();
  754. }
  755. }
  756. });
  757. /*
  758. * Delete the chosen HolonElement of the selected HolonObject,
  759. * Multi-Selection for CpsObjects as well as for HolonElements possible
  760. */
  761. btnDelHolEL.addActionListener(actionEvent -> {
  762. // For Single Selection of CpsObject
  763. if (model.getSelectedCpsObjects().size() == 1) {
  764. if (updCon.getActualCps().getClass() == HolonObject.class) {
  765. HolonObject obj = (HolonObject) updCon.getActualCps();
  766. for (HolonElement e : selectedElements) {
  767. controller.deleteElementCanvas(obj.getId(), e.getId());
  768. controller.calculateStateForTimeStep(model.getCurIteration());
  769. triggerUpdateController(null);
  770. contentPane.updateUI();
  771. // Names displayed in graph are not updated
  772. }
  773. model.getEleToDelete().clear();
  774. selectedElements.clear();
  775. }
  776. // For MultiSelection of CpsObject
  777. } else if (model.getSelectedCpsObjects().size() > 1) {
  778. for (Integer i : model.getEleToDelete().keySet()) {
  779. for (HolonElement e : model.getEleToDelete().get(i)) {
  780. controller.deleteElementCanvas(i, e.getId());
  781. }
  782. }
  783. triggerUpdateController(null);
  784. model.getEleToDelete().clear();
  785. selectedElements.clear();
  786. }
  787. triggerUpdateController(null);
  788. elementGraph.setText(Languages.getLanguage()[25]);
  789. holonEleNamesDisplayed = Languages.getLanguage()[25];
  790. });
  791. /*
  792. * Communication between HolonElement Table and displayed Graph and
  793. * Properties, as well as selection of different HolonElements
  794. */
  795. model.getTableHolonElement().addMouseListener(new MouseAdapter() {
  796. public void mousePressed(MouseEvent e) {
  797. HolonObject obj = null;
  798. if (model.getSelectedCpsObjects().size() == 1
  799. && model.getSelectedCpsObjects().get(0) instanceof HolonObject) {
  800. obj = (HolonObject) updCon.getActualCps();
  801. }
  802. yValueElements = e.getY();
  803. HolonElement ele;
  804. // Search for current clicked HolonElement
  805. if (model.getSelectedCpsObjects().size() == 1) {
  806. ele = updCon.getActualHolonElement(obj, yValueElements, 0, tables);
  807. } else {
  808. ele = updCon.getActualHolonElement(null, yValueElements, 0, tables);
  809. }
  810. // Multi-Selection of HolonElements through control button +
  811. // mouse click
  812. if (ele != null) {
  813. // make energy-consumption graph visible
  814. showScrollGraph();
  815. if (e.isControlDown()) {
  816. if (!selectedElements.contains(ele)) {
  817. selectedElements.add(ele);
  818. if (!holonEleNamesDisplayed.equals(Languages.getLanguage()[25])) {
  819. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  820. } else {
  821. holonEleNamesDisplayed = ele.getEleName() + " ";
  822. }
  823. unitGraph.repaintWithNewElement(selectedElements);
  824. }
  825. updCon.getActualHolonElement(null, yValueElements, 2, tables);
  826. }
  827. // if no control-button pressed but a HolonElement chosen
  828. else {
  829. selectedElements.clear();
  830. selectedElements.add(ele);
  831. updCon.getActualHolonElement(null, yValueElements, 1, tables);
  832. holonEleNamesDisplayed = ele.getEleName() + " ";
  833. unitGraph.repaintWithNewElement(selectedElements);
  834. }
  835. } else {
  836. elementGraph.setText(Languages.getLanguage()[25]);
  837. hideScrollGraph();
  838. }
  839. // if any HolonElement is double-clicked --> Edit-Mode started
  840. // for selected HolonElement
  841. if (e.getClickCount() == 2) {
  842. yThis = e.getY();
  843. xThis = e.getX();
  844. }
  845. // for single click and empty slot
  846. if (e.getClickCount() == 1 && ele == null) {
  847. selectedElements.clear();
  848. holonEleNamesDisplayed = Languages.getLanguage()[25];
  849. }
  850. elementGraph.setText(holonEleNamesDisplayed);
  851. yBTis = e.getY();
  852. xBThis = e.getX();
  853. }
  854. });
  855. /*
  856. * Triggered every time a change is made
  857. */
  858. model.getTableHolonElement().addPropertyChangeListener(propertyChangeEvent -> {
  859. try {
  860. int yMouse = yThis;
  861. int yBMouse = yBTis;
  862. int selectedValueY = (int) Math.floor(yMouse / 16);
  863. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  864. // ------------------ multi-selection mode ------------------
  865. if (model.getSelectedCpsObjects().size() > 1) {
  866. int selectedValueX = (int) Math.floor(xThis / (model.getTableHolonElement().getWidth() / 8));
  867. int selectedValueBX = (int) Math.floor(xBThis / (model.getTableHolonElement().getWidth() / 8));
  868. if (updCon.getHolonObj(yMouse, model.getMultiTable()) != null) {
  869. // For active column (boolean with a checkbox)
  870. if (selectedValueBX == 6) {
  871. HolonElement eleBTemp = updCon.getActualHolonElement(null, yBMouse, 0, tables);
  872. String newBStuff = model.getMultiTable().getValueAt(selectedValueBY, selectedValueBX)
  873. .toString();
  874. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  875. eleBTemp.setActive(bTemp);
  876. }
  877. // For activeFlex column (boolean with a checkbox)
  878. else if (selectedValueBX == 7) {
  879. HolonElement eleBTemp = updCon.getActualHolonElement(null, yBMouse, 0, tables);
  880. String newBStuff = model.getMultiTable().getValueAt(selectedValueBY, selectedValueBX)
  881. .toString();
  882. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  883. eleBTemp.setFlexible(bTemp);
  884. } else {
  885. // Update of HolonElement
  886. HolonElement eleTemp = updCon.getActualHolonElement(null, yMouse, 0, tables);
  887. String newStuff = model.getMultiTable().getValueAt(selectedValueY, selectedValueX)
  888. .toString();
  889. // Name update
  890. if (selectedValueX == 2) {
  891. eleTemp.setEleName(newStuff);
  892. }
  893. // Energy Update
  894. else if (selectedValueX == 3) {
  895. Float ftemp = Float.parseFloat(newStuff);
  896. eleTemp.setEnergyPerElement(ftemp);
  897. }
  898. // Flexibility
  899. else if (selectedValueX == 4) {
  900. Float ftemp = Float.parseFloat(newStuff);
  901. eleTemp.setFlexibleEnergyAvailable(ftemp);
  902. }
  903. // Amount of Elements update
  904. else if (selectedValueX == 5) {
  905. Integer iTemp = Integer.parseInt(newStuff);
  906. eleTemp.setAmount(iTemp);
  907. }
  908. }
  909. }
  910. } // ------------------ single-selection mode ------------------
  911. else if (model.getSelectedCpsObjects().size() == 1) {
  912. int selectedValueX = (int) Math.floor(xThis / (model.getTableHolonElement().getWidth() / 7));
  913. int selectedValueBX = (int) Math.floor(xBThis / (model.getTableHolonElement().getWidth() / 7));
  914. if (updCon.getActualCps() != null && updCon.getActualCps().getClass() == HolonObject.class) {
  915. HolonElement eleTemp;
  916. // For active column (boolean with a checkbox)
  917. if (selectedValueBX == 5) {
  918. eleTemp = updCon
  919. .getActualHolonElement((HolonObject) updCon.getActualCps(), yBMouse, 0, tables);
  920. String newBStuff = model.getSingleTable().getValueAt(selectedValueBY, selectedValueBX)
  921. .toString();
  922. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  923. eleTemp.setActive(bTemp);
  924. } // For activeFlex column (boolean with a checkbox)
  925. else if (selectedValueBX == 6) {
  926. eleTemp = updCon
  927. .getActualHolonElement((HolonObject) updCon.getActualCps(), yBMouse, 0, tables);
  928. String newBStuff = model.getSingleTable().getValueAt(selectedValueBY, selectedValueBX)
  929. .toString();
  930. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  931. eleTemp.setFlexible(bTemp);
  932. } else {
  933. // Update of HolonElement
  934. eleTemp = updCon.getActualHolonElement((HolonObject) updCon.getActualCps(),
  935. yMouse, 0, tables);
  936. String newStuff = model.getSingleTable().getValueAt(selectedValueY, selectedValueX)
  937. .toString();
  938. // Name update
  939. if (selectedValueX == 1) {
  940. eleTemp.setEleName(newStuff);
  941. } // Energy Update
  942. else if (selectedValueX == 2) {
  943. Float ftemp = Float.parseFloat(newStuff);
  944. eleTemp.setEnergyPerElement(ftemp);
  945. } // Flexibility
  946. else if (selectedValueX == 3) {
  947. Float ftemp = Float.parseFloat(newStuff);
  948. eleTemp.setFlexibleEnergyAvailable(ftemp);
  949. // if this is a flexible device and the flexibly available energy was changed,
  950. // set used energy to 0, so that it can be computed anew
  951. if (eleTemp.isFlexible()) {
  952. eleTemp.setEnergyPerElement(0);
  953. }
  954. }
  955. // Amount of Elements update
  956. else if (selectedValueX == 4) {
  957. Integer iTemp = Integer.parseInt(newStuff);
  958. eleTemp.setAmount(iTemp);
  959. }
  960. }
  961. // set all cells anew, if anything was changed
  962. updateElementTableAfterChange(eleTemp, selectedValueBY);
  963. }
  964. }
  965. controller.calculateStateForTimeStep(model.getCurIteration());
  966. model.getSingleTable().fireTableDataChanged();
  967. triggerUpdateController(null);
  968. contentPane.updateUI();
  969. unitGraph.repaint();
  970. triggerUpdateController(null);
  971. } catch (Exception e) {
  972. }
  973. });
  974. /***********************
  975. * HolonElement Properties Actions
  976. **********************/
  977. /*
  978. * Update of the mouse coord for Edit-Mode
  979. */
  980. tableProperties.addMouseListener(new MouseAdapter() {
  981. public void mousePressed(MouseEvent e) {
  982. if (e.getClickCount() == 2) {
  983. yProThis = e.getY();
  984. xProThis = e.getX();
  985. }
  986. yProThisOneClick = e.getY();
  987. xProThisOneClick = e.getX();
  988. }
  989. });
  990. /*
  991. * Update any change in the Property table
  992. */
  993. tableProperties.addPropertyChangeListener(propertyChangeEvent -> {
  994. try {
  995. Object temp;
  996. Object btemp;
  997. int selValueY = (int) Math.floor(yProThis / 16);
  998. int selValueX = (int) Math.floor(xProThis / (tableProperties.getWidth() / 2));
  999. int selValueYBool = (int) Math.floor(yProThisOneClick / 16);
  1000. int selValueXBool = (int) Math.floor(xProThisOneClick / (tableProperties.getWidth() / 2));
  1001. if (updCon.getActualCps() != null) {
  1002. temp = model.getPropertyTable().getValueAt(selValueY, selValueX);
  1003. btemp = model.getPropertyTable().getValueAt(selValueYBool, selValueXBool);
  1004. // Case: Switch selected
  1005. if (updCon.getActualCps() instanceof HolonSwitch) {
  1006. if (selValueY == 0 && selValueX != 0) {
  1007. updCon.getActualCps().setName(temp.toString());
  1008. }
  1009. if (selValueYBool == 2) {
  1010. Boolean bbTemp = Boolean.parseBoolean(btemp.toString());
  1011. ((HolonSwitch) updCon.getActualCps()).setManualMode(bbTemp);
  1012. if (bbTemp) {
  1013. model.getPropertyTable().setCellEditable(3, 1, true);
  1014. } else {
  1015. model.getPropertyTable().setCellEditable(3, 1, false);
  1016. }
  1017. } else if (selValueYBool == 3) {
  1018. if (((HolonSwitch) updCon.getActualCps()).getManualMode()) {
  1019. model.getPropertyTable().setCellEditable(3, 1, true);
  1020. Boolean bTemp = Boolean.parseBoolean(btemp.toString());
  1021. ((HolonSwitch) updCon.getActualCps()).setManualState(bTemp);
  1022. }
  1023. }
  1024. } // Case: UpperNode selected
  1025. else if (updCon.getActualCps() instanceof CpsUpperNode && selValueX != 0) {
  1026. // Name edition through double click
  1027. updCon.getActualCps().setName(temp.toString());
  1028. // Update of name on the tab in the tab section
  1029. for (int index = 4; index < tabbedPaneOriginal.getTabCount(); index++) {
  1030. tabTemp = tabbedPaneOriginal;
  1031. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1032. UpperNodeCanvas unc = ((UpperNodeCanvas) scrollPane.getViewport().getComponent(0));
  1033. if (unc.upperNode.getId() == updCon.getActualCps().getId()) {
  1034. tabbedPaneOriginal.setTitleAt(index, unc.upperNode.getName());
  1035. tabbedPaneOriginal.updateUI();
  1036. if (!initSplit) {
  1037. tabbedPaneSplit.setTitleAt(index, unc.upperNode.getName());
  1038. tabbedPaneSplit.updateUI();
  1039. }
  1040. }
  1041. }
  1042. } // Case: HolonObject is selected
  1043. else if (updCon.getActualCps() instanceof HolonObject && selValueX != 0) {
  1044. // Name edition through double click
  1045. updCon.getActualCps().setName(temp.toString());
  1046. }
  1047. } // Case: Edge is selected
  1048. else {
  1049. temp = model.getPropertyTable().getValueAt(selValueY, selValueX);
  1050. btemp = model.getPropertyTable().getValueAt(selValueYBool, selValueXBool);
  1051. // Edit modus for capacity by double clicking
  1052. if (selValueY == 2) {
  1053. Float ftemp;
  1054. if (Float.parseFloat(temp.toString()) >= 0.0) {
  1055. ftemp = Float.parseFloat(temp.toString());
  1056. } else {
  1057. ftemp = model.getSelectedEdge().getCapacity();
  1058. }
  1059. model.getSelectedEdge().setCapacity(ftemp);
  1060. }
  1061. // Status edition through a check box
  1062. if (selValueYBool == 3) {
  1063. Boolean bbTemp = Boolean.parseBoolean(btemp.toString());
  1064. model.getSelectedEdge().setWorkingState(bbTemp);
  1065. }
  1066. }
  1067. canvas.repaint();
  1068. } catch (Exception e) {
  1069. }
  1070. });
  1071. /***********************
  1072. * HolonElement Graph Actions
  1073. **********************/
  1074. /*
  1075. * Reset the graph of the selected HolonElement
  1076. */
  1077. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  1078. resetGraphBtn.addActionListener(actionEvent -> unitGraph.reset());
  1079. /*****************************
  1080. * RIGHT CONTAINER DONE
  1081. *****************************/
  1082. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  1083. /****************
  1084. * Tree Stuff
  1085. ****************/
  1086. // Override Key Actions
  1087. inputMap = tree.getInputMap();
  1088. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  1089. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  1090. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  1091. inputMap.put(KeyStroke.getKeyStroke("control A"), cntrlADown);
  1092. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  1093. @Override
  1094. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  1095. boolean leaf, int row, boolean hasFocus) {
  1096. JLabel label = new JLabel();
  1097. Image imgR;
  1098. if (leaf) {
  1099. for (Category cat : model.getCategories()) {
  1100. for (AbstractCpsObject cps : cat.getObjects()) {
  1101. if (value.toString().compareTo(cps.getObjName()) == 0) {
  1102. File checkPath = new File(cps.getImage());
  1103. if (checkPath.exists()) {
  1104. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
  1105. java.awt.Image.SCALE_SMOOTH);
  1106. } else {
  1107. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  1108. .getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  1109. }
  1110. if (imgR != null) {
  1111. label.setIcon(new ImageIcon(imgR));
  1112. }
  1113. label.setText(cps.getName());
  1114. }
  1115. }
  1116. }
  1117. }
  1118. tree.setRowHeight(50);
  1119. if (hasFocus) {
  1120. label.setForeground(new Color(0, 0, 255));
  1121. label.setOpaque(true);
  1122. }
  1123. if (label.getText().length() == 0) {
  1124. label.setText(value.toString());
  1125. if (value.toString().compareTo("Categories") != 0) {
  1126. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  1127. }
  1128. }
  1129. return label;
  1130. }
  1131. };
  1132. tree.setCellRenderer(customRenderer);
  1133. tree.addMouseListener(new MouseAdapter() {
  1134. public void mouseReleased(MouseEvent e) {
  1135. try {
  1136. if (dragging) {
  1137. chooseTabTemp();
  1138. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1139. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  1140. if (canvasOrUpperNodeCanvas instanceof UpperNodeCanvas) {
  1141. UpperNodeCanvas unc = (UpperNodeCanvas) canvasOrUpperNodeCanvas;
  1142. int x = (int) unc.getMousePosition().getX() + 16;
  1143. int y = (int) unc.getMousePosition().getY() + 16;
  1144. if (x > unc.upperNode.getLeftBorder()) {
  1145. AbstractCpsObject h = null;
  1146. if (tempCps instanceof HolonObject) {
  1147. h = new HolonObject(tempCps);
  1148. }
  1149. if (tempCps instanceof HolonSwitch) {
  1150. h = new HolonSwitch(tempCps);
  1151. }
  1152. if (tempCps instanceof HolonTransformer) {
  1153. h = new HolonTransformer(tempCps);
  1154. }
  1155. h.setPosition(x, y);
  1156. controller.addObjUpperNode(h, unc.upperNode);
  1157. unc.invalidate();
  1158. unc.repaint();
  1159. unc.setXY((int) canvas.getMousePosition().getX(),
  1160. (int) canvas.getMousePosition().getY());
  1161. }
  1162. } else {
  1163. int x = (int) canvas.getMousePosition().getX() + 16;
  1164. int y = (int) canvas.getMousePosition().getY() + 16;
  1165. AbstractCpsObject h = null;
  1166. if (tempCps instanceof HolonObject) {
  1167. h = new HolonObject(tempCps);
  1168. }
  1169. if (tempCps instanceof HolonSwitch) {
  1170. h = new HolonSwitch(tempCps);
  1171. }
  1172. if (tempCps instanceof HolonTransformer) {
  1173. h = new HolonTransformer(tempCps);
  1174. }
  1175. h.setPosition(x, y);
  1176. controller.addObjectCanvas(h);
  1177. canvas.invalidate();
  1178. canvas.setXY((int) canvas.getMousePosition().getX(),
  1179. (int) canvas.getMousePosition().getY());
  1180. canvas.repaint();
  1181. }
  1182. controller.calculateStateForCurrentTimeStep();
  1183. contentPane.updateUI();
  1184. dragging = false;
  1185. }
  1186. } catch (Exception eex) {
  1187. }
  1188. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  1189. }
  1190. });
  1191. popmenuEdit.add(editItem);
  1192. editItem.setEnabled(false);
  1193. editItem.addActionListener(actionEvent -> {
  1194. });
  1195. tree.addMouseListener(new MouseAdapter() {
  1196. public void mousePressed(MouseEvent e) {
  1197. try {
  1198. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  1199. // if an Object was selected, the porperties are shown in
  1200. // the table
  1201. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  1202. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  1203. if (selectedNode.getLevel() == 2) {
  1204. controller.searchCategoryObject(selectedNode.getParent().toString(), selectedNode.toString());
  1205. updCon.deleteRows(model.getSingleTable());
  1206. updCon.deleteRows(model.getMultiTable());
  1207. // if (selected instanceof HolonObject && selected !=
  1208. // null) {
  1209. // selected = (HolonObject) selected;
  1210. // fillElementTable(((HolonObject)
  1211. // selected).getElements());
  1212. // }
  1213. }
  1214. if (SwingUtilities.isRightMouseButton(e)) {
  1215. for (Category cat : model.getCategories()) {
  1216. for (AbstractCpsObject cps : cat.getObjects()) {
  1217. if (actualObjectClicked.compareTo(cps.getObjName()) == 0
  1218. && !(cps instanceof HolonSwitch)) {
  1219. editItem.setEnabled(true);
  1220. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  1221. catOfObjToBeEdited = selectedNode.getParent().toString();
  1222. tempCps = cps;
  1223. }
  1224. }
  1225. }
  1226. } else {
  1227. for (Category cat : model.getCategories()) {
  1228. for (AbstractCpsObject cps : cat.getObjects()) {
  1229. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  1230. File checkPath = new File(cps.getImage());
  1231. if (checkPath.exists()) {
  1232. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(32, 32,
  1233. java.awt.Image.SCALE_SMOOTH);
  1234. } else {
  1235. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  1236. .getScaledInstance(32, 32, java.awt.Image.SCALE_SMOOTH);
  1237. }
  1238. tempCps = cps;
  1239. dragging = true;
  1240. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  1241. "Image");
  1242. frmCyberPhysical.setCursor(cursor);
  1243. }
  1244. }
  1245. }
  1246. }
  1247. } catch (Exception eex) {
  1248. }
  1249. }
  1250. });
  1251. editItem.addActionListener(actionEvent -> {
  1252. // Remove the selected Object object
  1253. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
  1254. addObjectPopUP.setCategory(catOfObjToBeEdited);
  1255. addObjectPopUP.setController(controller);
  1256. addObjectPopUP.setVisible(true);
  1257. });
  1258. scrollPane1.setViewportView(tree);
  1259. scrollPane1.setColumnHeaderView(panel);
  1260. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  1261. toolBar.setAlignmentX(Component.LEFT_ALIGNMENT);
  1262. toolBar.setFloatable(false);
  1263. panel.add(toolBar);
  1264. comboBox.setMaximumSize(new Dimension(100, 20));
  1265. toolBar.add(comboBox);
  1266. comboBox.setModel(new DefaultComboBoxModel(comboBoxCat));
  1267. // Add Buttonnew DefaultComboBoxModel(comboBoxCat)
  1268. btnAdd.addActionListener(actionEvent -> {
  1269. Object nodeInfo = tree.getLastSelectedPathComponent();
  1270. String selectedOption = comboBox.getSelectedItem().toString();
  1271. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1272. try {
  1273. switch (selectedOption) {
  1274. case "Category":
  1275. String catName = JOptionPane.showInputDialog(Languages.getLanguage()[56]);
  1276. if (catName.length() != 0) {
  1277. controller.addCategory(catName);
  1278. }
  1279. break;
  1280. case "Object":
  1281. if (selectedNode == null) {
  1282. JOptionPane.showMessageDialog(new JFrame(),
  1283. Languages.getLanguage()[57] + selectedOption + ".");
  1284. }
  1285. if (selectedNode != null && selectedNode.getLevel() == 1) {
  1286. AbstractCpsObject tmp = new HolonObject("");
  1287. addObjectPopUP = new AddObjectPopUp(false, tmp, null);
  1288. addObjectPopUP.setVisible(true);
  1289. addObjectPopUP.setController(controller);
  1290. addObjectPopUP.setCategory(selectedNode.toString());
  1291. }
  1292. break;
  1293. default:
  1294. addObjectAction(selectedOption, selectedNode);
  1295. break;
  1296. }
  1297. } catch (Exception e) {
  1298. }
  1299. tree.repaint();
  1300. });
  1301. /**
  1302. * Pop up - About Us with some important information about the
  1303. * developers, source and programming stuff
  1304. */
  1305. mntmAboutUs.addMouseListener(new MouseAdapter() {
  1306. @Override
  1307. public void mousePressed(MouseEvent e) {
  1308. aboutUsPopUp = new AboutUsPopUp();
  1309. aboutUsPopUp.setVisible(true);
  1310. }
  1311. });
  1312. /**
  1313. * Update of every interaction between the user and the canvas (only on
  1314. * the canvas). Basically the update of all the information concerning
  1315. * the clicked HolonObject. For multi-selection, the propertyTable would
  1316. * be disabled
  1317. */
  1318. canvas.addMouseListener(new MouseAdapter() {
  1319. @Override
  1320. public void mousePressed(MouseEvent e) {
  1321. }
  1322. @Override
  1323. public void mouseReleased(MouseEvent e) {
  1324. holonEleNamesDisplayed = Languages.getLanguage()[25];
  1325. // If any empty space is clicked
  1326. if (temp == null || temp.getId() != model.getSelectedObjectID()) {
  1327. hideScrollGraph();
  1328. elementGraph.setText(Languages.getLanguage()[25]);
  1329. }
  1330. temp = updCon.getActualCps();
  1331. // Erase old data in the PropertyTable
  1332. if (model.getPropertyTable().getRowCount() > 0) {
  1333. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  1334. model.getPropertyTable().removeRow(i);
  1335. }
  1336. }
  1337. if (e.isControlDown() && temp != null) {
  1338. if (model.getSelectedCpsObjects().contains(temp)) {
  1339. controller.deleteSelectedObject(temp);
  1340. } else {
  1341. controller.addSelectedObject(temp);
  1342. }
  1343. }
  1344. if (temp instanceof HolonSwitch) {
  1345. showScrollGraph();
  1346. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  1347. unitGraph.fillArrayofBooleans();
  1348. }
  1349. // Write new data in the PropertyTable
  1350. triggerUpdateController(temp);
  1351. // New Tab with NodeOfNode
  1352. if (doubleClick() && MouseEvent.BUTTON3 != e.getButton() && temp instanceof CpsUpperNode) {
  1353. openNewUpperNodeTab();
  1354. try {
  1355. controller.autoSave();
  1356. } catch (IOException e1) {
  1357. e1.printStackTrace();
  1358. }
  1359. }
  1360. if (model.getSelectedCpsObjects().size() > 1) {
  1361. model.getTableHolonElement().setModel(model.getMultiTable());
  1362. } else if (model.getSelectedCpsObjects().size() == 1) {
  1363. model.getTableHolonElement().setModel(model.getSingleTable());
  1364. }
  1365. }
  1366. });
  1367. holonCanvas.addMouseListener(new MouseAdapter() {
  1368. @Override
  1369. public void mousePressed(MouseEvent e) {
  1370. hideScrollGraph();
  1371. elementGraph.setText(Languages.getLanguage()[25]);
  1372. if (model.getPropertyTable().getRowCount() > 0) {
  1373. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  1374. model.getPropertyTable().removeRow(i);
  1375. }
  1376. }
  1377. updCon.deleteRows(model.getMultiTable());
  1378. updCon.deleteRows(model.getSingleTable());
  1379. updCon.paintHolonBody(model.getSelectedHolonBody());
  1380. }
  1381. });
  1382. toolBar.add(btnAdd);
  1383. // Del Button
  1384. btnDel.addActionListener(actionEvent -> {
  1385. Object nodeInfo = tree.getLastSelectedPathComponent();
  1386. if (nodeInfo != null) {
  1387. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1388. String nodeName = selectedNode.getUserObject().toString();
  1389. int depthOfNode = selectedNode.getLevel();
  1390. try {
  1391. switch (depthOfNode) {
  1392. case 1:
  1393. int dialogResult = JOptionPane.showConfirmDialog(null, eraseCategory + nodeName + "?",
  1394. warningText, JOptionPane.YES_NO_OPTION);
  1395. if (dialogResult == JOptionPane.YES_OPTION) {
  1396. controller.deleteCategory(nodeName);
  1397. }
  1398. break;
  1399. case 2:
  1400. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  1401. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  1402. break;
  1403. default:
  1404. JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
  1405. }
  1406. } catch (Exception e2) {
  1407. }
  1408. } else {
  1409. JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
  1410. }
  1411. tree.repaint();
  1412. });
  1413. toolBar.add(btnDel);
  1414. frmCyberPhysical.getContentPane().add(splitPane);
  1415. mntmNew.addActionListener(actionEvent -> {
  1416. if (model.getObjectsOnCanvas().size() != 0) {
  1417. int newWarning = JOptionPane.YES_NO_OPTION;
  1418. int dialogForNewWarning = JOptionPane.showConfirmDialog(null, saveBeforeNew, warningText,
  1419. newWarning);
  1420. if (dialogForNewWarning == JOptionPane.YES_OPTION) {
  1421. mntmSave.doClick();
  1422. }
  1423. }
  1424. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  1425. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  1426. controller.setSelectedObjectID(0);
  1427. controller.setSelecteEdge(null);
  1428. controller.setCurIteration(0);
  1429. hideScrollGraph();
  1430. elementGraph.setText(Languages.getLanguage()[25]);
  1431. canvas.tempCps = null;
  1432. canvas.repaint();
  1433. IdCounter.resetCounter();
  1434. IdCounterElem.resetCounter();
  1435. });
  1436. mntmOpen.addActionListener(new ActionListener() {
  1437. @Override
  1438. public void actionPerformed(ActionEvent evt) {
  1439. menuFileExitActionPerformed();
  1440. }
  1441. private void menuFileExitActionPerformed() {
  1442. JFileChooser fileChooser = new JFileChooser();
  1443. JFrame test = new JFrame();
  1444. FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
  1445. fileChooser.setFileFilter(holonFilter);
  1446. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  1447. File file = fileChooser.getSelectedFile();
  1448. try {
  1449. controller.loadFile(file.getAbsolutePath());
  1450. canvas.repaint();
  1451. unitGraph.update(model.getObjectsOnCanvas());
  1452. tree.repaint();
  1453. for (JsonObject json : model.getStatisticData()) {
  1454. readStatistics(json);
  1455. }
  1456. } catch (IOException | ArchiveException e) {
  1457. e.printStackTrace();
  1458. JLabel message = new JLabel("The savefile is corrupt and cannot be opened.");
  1459. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  1460. }
  1461. }
  1462. }
  1463. });
  1464. mntmSave.addActionListener(new ActionListener() {
  1465. @Override
  1466. public void actionPerformed(ActionEvent evt) {
  1467. menuSaveActionPerformed();
  1468. }
  1469. private void menuSaveActionPerformed() {
  1470. JFileChooser fileChooser = new JFileChooser();
  1471. JFrame test = new JFrame();
  1472. FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
  1473. fileChooser.setFileFilter(holonFilter);
  1474. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  1475. String file = fileChooser.getSelectedFile().getPath();
  1476. if (fileChooser.getFileFilter().equals(holonFilter)) {
  1477. if (!file.contains("."))
  1478. file += ".holon";
  1479. }
  1480. if (!file.endsWith(".holon")) {
  1481. String suffix = file.substring(file.lastIndexOf("."), file.length());
  1482. String[] options = new String[]{"keep .holon", "use " + suffix};
  1483. JLabel message = new JLabel(
  1484. "Are you sure to use the extension \"" + suffix + "\" instead of \".holon\"?");
  1485. int response = JOptionPane.showOptionDialog(null, message, "", JOptionPane.DEFAULT_OPTION,
  1486. JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
  1487. if (response == 0)
  1488. file = file.replace(suffix, ".holon");
  1489. }
  1490. try {
  1491. controller.saveFile(new File(file).getAbsolutePath());
  1492. } catch (IOException | ArchiveException e) {
  1493. e.printStackTrace();
  1494. }
  1495. }
  1496. }
  1497. });
  1498. englishBtn.addActionListener(actionEvent -> {
  1499. Languages.setLanguage(0);
  1500. refreshLanguages();
  1501. updCon.refreshTableProperties(model.getPropertyTable());
  1502. });
  1503. spanishBtn.addActionListener(actionEvent -> {
  1504. Languages.setLanguage(1);
  1505. refreshLanguages();
  1506. updCon.refreshTableProperties(model.getPropertyTable());
  1507. });
  1508. germanBtn.addActionListener(actionEvent -> {
  1509. Languages.setLanguage(2);
  1510. refreshLanguages();
  1511. updCon.refreshTableProperties(model.getPropertyTable());
  1512. });
  1513. czechBtn.addActionListener(actionEvent -> {
  1514. Languages.setLanguage(3);
  1515. refreshLanguages();
  1516. updCon.refreshTableProperties(model.getPropertyTable());
  1517. });
  1518. chineseBtn.addActionListener(actionEvent -> {
  1519. Languages.setLanguage(4);
  1520. refreshLanguages();
  1521. updCon.refreshTableProperties(model.getPropertyTable());
  1522. });
  1523. mntmUndo.addActionListener(new ActionListener() {
  1524. @Override
  1525. public void actionPerformed(ActionEvent evt) {
  1526. menuUndoActionPerformed();
  1527. }
  1528. private void menuUndoActionPerformed() {
  1529. try {
  1530. controller.loadAutoSave(controller.getUndoSave());
  1531. canvas.repaint();
  1532. repaintGraphAfterUndoRedo();
  1533. hideScrollGraph();
  1534. } catch (IOException e) {
  1535. e.printStackTrace();
  1536. }
  1537. }
  1538. });
  1539. mntmRedo.addActionListener(new ActionListener() {
  1540. @Override
  1541. public void actionPerformed(ActionEvent evt) {
  1542. menuRedoActionPerformed();
  1543. }
  1544. private void menuRedoActionPerformed() {
  1545. try {
  1546. controller.loadAutoSave(controller.getRedoSave());
  1547. canvas.repaint();
  1548. repaintGraphAfterUndoRedo();
  1549. hideScrollGraph();
  1550. } catch (IOException e) {
  1551. e.printStackTrace();
  1552. }
  1553. }
  1554. });
  1555. timePanel = new TimePanel(model, controller);
  1556. timePanel.setBorder(null);
  1557. ((JSlider) (timePanel.getComponent(1))).addChangeListener(changeEvent -> {
  1558. int i = model.getCurIteration();
  1559. controller.calculateStateForTimeStep(i);
  1560. unitGraph.repaint();
  1561. if (model.getIsSimRunning()) {
  1562. controller.runAlgorithm(model, controller);
  1563. statSplitPane.repaintGraphs();
  1564. }
  1565. contentPane.updateUI();
  1566. });
  1567. splitPane1.setMinimumSize(new Dimension(0, 25));
  1568. splitPane.setRightComponent(splitPane1);
  1569. splitPane.setDividerLocation(200);
  1570. splitPane1.setDividerLocation(500);
  1571. splitPaneCanvasConsole.setDividerLocation(550);
  1572. splitPaneCanvasConsole.setResizeWeight(0.9);
  1573. splitPane.setLeftComponent(scrollPane1);
  1574. splitPaneCanvasConsole.setLeftComponent(tabbedPaneOriginal);
  1575. tabbedPaneOriginal.addTab("View", tabbedPaneInnerOriginal);
  1576. tabbedPaneInnerOriginal.addTab("Main Grid", canvasSP);
  1577. // tabbedPaneSplit.addTab("View", tabbedPaneInnerSplit);
  1578. // tabbedPaneInnerSplit.addTab("Main Grid", null);
  1579. tabbedPaneOriginal.addTab("Statistics", statScrollPane);
  1580. tabbedPaneOriginal.addTab("Holon", holonCanvas);
  1581. FlexiblePane flexPane = new FlexiblePane(controller);
  1582. controller.setFlexiblePane(flexPane);
  1583. controller.getModel().getObjectListeners().add(flexPane);
  1584. tabbedPaneOriginal.addTab("Flexibility", flexPane);
  1585. splitPaneCanvasConsole.setRightComponent(console);
  1586. splitPane1.setLeftComponent(splitPaneCanvasConsole);
  1587. splitPane1.setRightComponent(splitHolonElPro);
  1588. splitPane1.setResizeWeight(0.9);
  1589. splitHolonElPro.setDividerLocation(400);
  1590. splitHolonElPro.setTopComponent(splitGraphHolonEl);
  1591. splitHolonElPro.setBottomComponent(scrollProperties);
  1592. splitGraphHolonEl.setDividerLocation(150);
  1593. splitGraphHolonEl.setTopComponent(scrollGraph);
  1594. splitGraphHolonEl.setBottomComponent(scrollElements);
  1595. canvasSP.setViewportView(canvas);
  1596. algorithmMenu.setBackground(new Color(240, 240, 240));
  1597. tabbedPaneOriginal.setBorder(null);
  1598. scrollProperties.setBorder(null);
  1599. scrollGraph.setBorder(null);
  1600. scrollElements.setBorder(null);
  1601. splitPane.setBorder(null);
  1602. splitPane1.setBorder(null);
  1603. splitPaneCanvasConsole.setBorder(null);
  1604. splitHolonElPro.setBorder(null);
  1605. splitGraphHolonEl.setBorder(null);
  1606. panelHolonEl.setBorder(null);
  1607. canvasSP.setBorder(null);
  1608. tableHolonElementScrollPane.setBorder(null);
  1609. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  1610. MouseAdapter focusCanvasAdapter = new FocusCanvasMouseAdapter();
  1611. model.getTableHolonElement().addMouseListener(focusCanvasAdapter);
  1612. tableProperties.addMouseListener(focusCanvasAdapter);
  1613. console.getConsoleText().addMouseListener(focusCanvasAdapter);
  1614. try {
  1615. controller.loadAutoSave(System.getProperty("user.home") + "/.config/HolonGUI/Category/Category.json");
  1616. } catch (IOException e1) {
  1617. }
  1618. String autoPath = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  1619. File dest = new File(autoPath);
  1620. if (dest.listFiles().length > 1) {
  1621. int dialogButton = JOptionPane.YES_NO_OPTION;
  1622. int dialogResult = JOptionPane.showConfirmDialog(null, "Old autosave file was found, should it be loaded?",
  1623. warningText, dialogButton);
  1624. if (dialogResult == JOptionPane.YES_OPTION) {
  1625. if (dest.exists()) {
  1626. model.setAutoSaveNr(dest.listFiles().length - 1);
  1627. mntmRedo.doClick();
  1628. } else {
  1629. JOptionPane.showMessageDialog(frmCyberPhysical, "Autosave could not be loaded.");
  1630. setUpAutoSave(dest);
  1631. }
  1632. } else {
  1633. setUpAutoSave(dest);
  1634. }
  1635. }
  1636. canvasSP.addComponentListener(new ComponentAdapter() {
  1637. @Override
  1638. public void componentResized(ComponentEvent e) {
  1639. controller.setCanvasX(Math.max(model.getCanvasX(), canvasSP.getViewport().getWidth()));
  1640. controller.setCanvasY(Math.max(model.getCanvasY(), canvasSP.getViewport().getHeight()));
  1641. canvas.repaint();
  1642. }
  1643. });
  1644. }
  1645. private void showScrollGraph() {
  1646. scrollGraph.setVisible(true);
  1647. splitGraphHolonEl.setTopComponent(scrollGraph);
  1648. splitGraphHolonEl.setDividerLocation(150);
  1649. }
  1650. private void hideScrollGraph() {
  1651. scrollGraph.setVisible(false);
  1652. unitGraph.empty();
  1653. splitGraphHolonEl.remove(scrollGraph);
  1654. splitGraphHolonEl.setDividerLocation(0);
  1655. }
  1656. /**
  1657. * Sets up autosave if no old one is loaded at the beginning
  1658. *
  1659. * @param dest path to save-folder
  1660. */
  1661. private void setUpAutoSave(File dest) {
  1662. dest.mkdirs();
  1663. try {
  1664. controller.autoSave();
  1665. } catch (IOException e1) {
  1666. e1.printStackTrace();
  1667. }
  1668. }
  1669. /**
  1670. * adds a specific object type to selected Category also handles input
  1671. * windows and illegal inputs.
  1672. *
  1673. * @param objType type of the Object
  1674. * @param selectedNode The selected Node
  1675. */
  1676. private void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  1677. if (selectedNode == null) {
  1678. JOptionPane.showMessageDialog(new JFrame(),
  1679. "Please select a Category first before adding " + objType + ".");
  1680. }
  1681. // if selected node is a directory for Categories
  1682. else {
  1683. if (selectedNode.getLevel() == 1) {
  1684. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  1685. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  1686. if (objname.length() != 0) {
  1687. try {
  1688. switch (objType) {
  1689. case "Switch":
  1690. controller.addSwitch(cat, objname);
  1691. break;
  1692. }
  1693. } catch (Exception e) {
  1694. }
  1695. }
  1696. } else {
  1697. JOptionPane.showMessageDialog(new JFrame(),
  1698. "Objects can not be added to Objects. Please select a Category.");
  1699. }
  1700. }
  1701. }
  1702. /**
  1703. * reloads the Categories from Model.
  1704. *
  1705. * @param categories the current categories
  1706. */
  1707. private void updateCategories(final ArrayList<Category> categories) {
  1708. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  1709. private static final long serialVersionUID = 1L;
  1710. {
  1711. DefaultMutableTreeNode node1;
  1712. for (Category c : categories) {
  1713. node1 = new DefaultMutableTreeNode(c.getName());
  1714. // kann eventuell umgeändert werden
  1715. for (AbstractCpsObject obj : c.getObjects()) {
  1716. node1.add(new DefaultMutableTreeNode(obj.getObjName()));
  1717. }
  1718. add(node1);
  1719. }
  1720. }
  1721. }));
  1722. }
  1723. /**
  1724. * When changes are made to the Categories.
  1725. *
  1726. * @param categories the Categories
  1727. */
  1728. public void onChange(ArrayList<Category> categories) {
  1729. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  1730. updateCategories(categories);
  1731. model.reload();
  1732. }
  1733. /**
  1734. * Get the Frame.
  1735. *
  1736. * @return the Frame
  1737. */
  1738. JFrame getFrmCyberPhysical() {
  1739. return frmCyberPhysical;
  1740. }
  1741. private void refreshLanguages() {
  1742. String[] tempArray = Languages.getLanguage();
  1743. // ToolBar
  1744. mnNewMenu.setText(tempArray[0]);
  1745. mntmNew.setText(tempArray[1]);
  1746. mntmOpen.setText(tempArray[2]);
  1747. mntmSave.setText(tempArray[3]);
  1748. mnNewMenuEdit.setText(tempArray[4]);
  1749. mntmUndo.setText(tempArray[5]);
  1750. mntmRedo.setText(tempArray[6]);
  1751. mntmFindReplace.setText(tempArray[7]);
  1752. mntmEditShowedInformation.setText(tempArray[8]);
  1753. mnNewMenuOptions.setText(tempArray[9]);
  1754. mntmResetCategory.setText(tempArray[10]);
  1755. mnNewMenuView.setText(tempArray[11]);
  1756. mntmCanvasSize.setText(tempArray[12]);
  1757. mnHelp.setText(tempArray[13]);
  1758. mntmAboutUs.setText(tempArray[14]);
  1759. mntmEditEdges.setText(tempArray[15]);
  1760. mnLanguage.setText(tempArray[16]);
  1761. canvas.updateLanguages();
  1762. // Tables
  1763. System.arraycopy(tempArray, 17, columnNamesMulti, 0, columnNamesMulti.length);
  1764. model.getMultiTable().setColumnIdentifiers(columnNamesMulti);
  1765. System.arraycopy(tempArray, 18, columnNamesSingle, 0, columnNamesSingle.length);
  1766. model.getSingleTable().setColumnIdentifiers(columnNamesSingle);
  1767. model.colNames[0] = tempArray[23];
  1768. model.colNames[1] = tempArray[24];
  1769. model.getPropertyTable().setColumnIdentifiers(model.colNames);
  1770. // Graph
  1771. elementGraph.setText(tempArray[25]);
  1772. holonEleNamesDisplayed = tempArray[25];
  1773. resetGraphBtn.setText(tempArray[26]);
  1774. // Warning PopUps
  1775. warningText = tempArray[27];
  1776. saveBeforeNew = tempArray[28];
  1777. eraseCategory = tempArray[29];
  1778. selectObjBeforeErase = tempArray[30];
  1779. // SimMenu
  1780. algorithmMenu.algoFolderButton.setText(Languages.getLanguage()[85]);
  1781. // TimePanel
  1782. timePanel.playBtn.setToolTipText(Languages.getLanguage()[89]);
  1783. timePanel.timeResetBtn.setToolTipText(Languages.getLanguage()[90]);
  1784. timePanel.timeForwardBtn.setToolTipText(Languages.getLanguage()[91]);
  1785. timePanel.timeBackwardBtn.setToolTipText(Languages.getLanguage()[92]);
  1786. timePanel.timeSlider.setToolTipText(Languages.getLanguage()[93]);
  1787. // Image Size Slider
  1788. lblImageSize.setText(Languages.getLanguage()[94]);
  1789. }
  1790. /**
  1791. * Checks if a double click was made.
  1792. *
  1793. * @return true if doublecklick, false if not
  1794. */
  1795. private boolean doubleClick() {
  1796. if (click) {
  1797. click = false;
  1798. return true;
  1799. } else {
  1800. click = true;
  1801. Timer t = new Timer("doubleclickTimer", false);
  1802. t.schedule(new TimerTask() {
  1803. @Override
  1804. public void run() {
  1805. click = false;
  1806. }
  1807. }, 350);
  1808. }
  1809. return false;
  1810. }
  1811. /*
  1812. * Open a new Tab with an UpperNodeCanvas
  1813. */
  1814. private void openNewUpperNodeTab() {
  1815. chooseTabTemp();
  1816. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1817. if (scrollPane.getViewport().getComponent(0) instanceof MyCanvas) {
  1818. unc = new UpperNodeCanvas(model, controller, unitGraph, (CpsUpperNode) temp, "");
  1819. } else {
  1820. unc = new UpperNodeCanvas(model, controller, unitGraph, (CpsUpperNode) temp,
  1821. ((UpperNodeCanvas) scrollPane.getViewport().getComponent(0)).path + " -> ");
  1822. }
  1823. unc.setShowedInformation(canvas.getShowedInformation());
  1824. // check if tab already open for clicked NodeOfNode
  1825. boolean dupl = false;
  1826. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1827. JScrollPane paneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1828. if (paneOriginal != null &&
  1829. ((UpperNodeCanvas) paneOriginal.getViewport().getComponent(0)).upperNode.getId() == temp.getId()) {
  1830. dupl = true;
  1831. // set selected component to view
  1832. tabbedPaneOriginal.setSelectedComponent(tabbedPaneInnerOriginal);
  1833. // set selected tab in view to found upper-node-canvas
  1834. tabbedPaneInnerOriginal.setSelectedComponent(tabbedPaneInnerOriginal.getComponentAt(i));
  1835. }
  1836. if (tabbedPaneSplit.getTabCount() > 0) {
  1837. JTabbedPane tabbedPaneInnerSplit = (JTabbedPane) tabbedPaneSplit.getComponentAt(0);
  1838. if (tabbedPaneInnerSplit != null) {
  1839. JScrollPane paneSplit = (JScrollPane) tabbedPaneInnerSplit.getComponentAt(i);
  1840. if (!initSplit && paneSplit != null
  1841. && ((UpperNodeCanvas) paneSplit.getViewport().getComponent(0)).upperNode.getId() == temp.getId()) {
  1842. dupl = true;
  1843. // set selected component to view
  1844. tabbedPaneSplit.setSelectedComponent(tabbedPaneInnerSplit);
  1845. // set selected tab in view to found upper-node-canvas
  1846. tabbedPaneInnerSplit.setSelectedComponent(tabbedPaneInnerSplit.getComponentAt(i));
  1847. }
  1848. }
  1849. }
  1850. // if we found a duplicate, break
  1851. if (dupl) {
  1852. break;
  1853. }
  1854. }
  1855. if (!dupl) {
  1856. unc.setBorder(null);
  1857. unc.setBackground(Color.WHITE);
  1858. unc.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
  1859. unc.addMouseListener(new MouseAdapter() {
  1860. @Override
  1861. public void mousePressed(MouseEvent e) {
  1862. hideScrollGraph();
  1863. holonEleNamesDisplayed = Languages.getLanguage()[25];
  1864. elementGraph.setText(holonEleNamesDisplayed);
  1865. chooseTabTemp();
  1866. JScrollPane scrollPane1 = getScrollPaneFromTabbedPane();
  1867. temp = ((UpperNodeCanvas) scrollPane1.getViewport().getComponent(0)).tempCps;
  1868. if (doubleClick() && MouseEvent.BUTTON3 != e.getButton() && temp instanceof CpsUpperNode) {
  1869. openNewUpperNodeTab();
  1870. }
  1871. if (temp instanceof HolonSwitch) {
  1872. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  1873. unitGraph.fillArrayofBooleans();
  1874. }
  1875. }
  1876. });
  1877. JScrollPane sp = new JScrollPane(unc);
  1878. sp.setBorder(null);
  1879. // Selected tabbed Pane = tabbedPaneOriginal or tabbedPaneSplit
  1880. if (tabTemp == tabbedPaneOriginal) {
  1881. this.tabbedPaneInnerOriginal.add(temp.getName(), sp);
  1882. this.tabbedPaneInnerOriginal.setSelectedComponent(sp);
  1883. this.tabbedPaneInnerOriginal.setTabComponentAt(this.tabbedPaneInnerOriginal.getTabCount() - 1,
  1884. new ButtonTabComponent(this.tabbedPaneInnerOriginal, tabbedPaneSplit));
  1885. } else {
  1886. JTabbedPane tabbedPaneInnerSplit = (JTabbedPane) tabbedPaneSplit.getComponentAt(0);
  1887. tabbedPaneInnerSplit.add(temp.getName(), sp);
  1888. Component tempC = tabbedPaneInnerSplit.getSelectedComponent();
  1889. tabbedPaneInnerSplit.setComponentAt(tabbedPaneInnerSplit.getSelectedIndex(), null);
  1890. tabbedPaneOriginal.setComponentAt(tabbedPaneInnerSplit.getSelectedIndex(), tempC);
  1891. tabbedPaneInnerSplit.setSelectedComponent(sp);
  1892. tabbedPaneInnerOriginal.add(temp.getName(), null);
  1893. tabbedPaneInnerOriginal.setTabComponentAt(tabbedPaneInnerOriginal.getTabCount() - 1,
  1894. new ButtonTabComponent(tabbedPaneInnerOriginal, tabbedPaneInnerSplit));
  1895. }
  1896. temp = null;
  1897. }
  1898. }
  1899. private void updateUpperNodes() {
  1900. for (Component c : tabbedPaneInnerOriginal.getComponents()) {
  1901. if (!(c instanceof JScrollPane)) {
  1902. continue;
  1903. }
  1904. Component pane = ((JScrollPane) c).getViewport().getComponent(0);
  1905. if (pane instanceof UpperNodeCanvas) {
  1906. temp = model.getHashcodeMap().get(((UpperNodeCanvas) pane).code);
  1907. ((UpperNodeCanvas) pane).upperNode = (CpsUpperNode) temp;
  1908. }
  1909. }
  1910. }
  1911. /**
  1912. * Removes UpperNodeTab if UpperNode was deleted
  1913. *
  1914. * @param cps the CPS object that is currently selected
  1915. */
  1916. private void removeUpperNodeTab(AbstractCpsObject cps) {
  1917. if (cps instanceof CpsUpperNode) {
  1918. boolean splitView = false;
  1919. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1920. JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1921. if (scrollPaneOriginal == null) {
  1922. splitView = true;
  1923. } else if (((UpperNodeCanvas) scrollPaneOriginal.getViewport().getComponent(0)).upperNode.getId()
  1924. == cps.getId()) {
  1925. ((ButtonTabComponent) tabbedPaneInnerOriginal.getTabComponentAt(i)).removeTabs();
  1926. break;
  1927. }
  1928. }
  1929. // If SplitView is on and the view on
  1930. // tabbedPaneSplit is the deleted upperNode
  1931. if (splitView && tabbedPaneSplit.getComponentCount() > 0) {
  1932. JTabbedPane tabbedPaneInnerSplit = (JTabbedPane) tabbedPaneSplit.getComponentAt(0);
  1933. if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInnerSplit.getSelectedComponent())
  1934. .getViewport().getComponent(0)).upperNode.getId() == cps.getId()) {
  1935. ((ButtonTabComponent) tabbedPaneInnerOriginal
  1936. .getTabComponentAt(tabbedPaneInnerSplit.getSelectedIndex())).removeTabs();
  1937. }
  1938. }
  1939. }
  1940. }
  1941. private void readStatistics(JsonObject json) {
  1942. List<String> keys = json.entrySet().stream().map(i -> i.getKey())
  1943. .collect(Collectors.toCollection(ArrayList::new));
  1944. // Saved Values
  1945. JsonObject obj;
  1946. AbstractCpsObject cps;
  1947. int prop;
  1948. Color color;
  1949. StatisticGraphPanel stat = new StatisticGraphPanel(model, controller, json.get("KEY").getAsString(),
  1950. model.getGraphTable());
  1951. for (String k : keys) {
  1952. if (!k.equals("KEY")) {
  1953. obj = json.get(k).getAsJsonObject();
  1954. cps = (obj.get("ID") == JsonNull.INSTANCE ? null : controller.searchTracked(obj.get("ID").getAsInt()));
  1955. prop = obj.get("PROPERTY").getAsInt();
  1956. color = model.getGson().fromJson(obj.get("COLOR"), Color.class);
  1957. stat.addObject(new TrackedDataSet(cps, prop, color));
  1958. }
  1959. model.getGraphTable().put(json.get("KEY").getAsString(), stat);
  1960. model.getStatPanel().getGraphPanel().add(stat);
  1961. }
  1962. }
  1963. /**
  1964. * chooses whether to set the tabTemp to tabbedPaneOriginal or tabbedPaneSplit
  1965. */
  1966. private void chooseTabTemp() {
  1967. // is the uppernode on tabbedPaneOriginal or tabbedPaneSplit
  1968. if (tabbedPaneOriginal.getMousePosition() != null) {
  1969. tabTemp = tabbedPaneOriginal;
  1970. } else {
  1971. tabTemp = tabbedPaneSplit;
  1972. }
  1973. }
  1974. private JScrollPane getScrollPaneFromTabbedPane(JTabbedPane tabbedPane, int index) {
  1975. tabTemp = tabbedPane;
  1976. return getScrollPaneFromTabbedPane(index);
  1977. }
  1978. // private JScrollPane getScrollPaneFromTabbedPane(JTabbedPane tabbedPane) {
  1979. // return getScrollPaneFromTabbedPane(tabbedPane, -1);
  1980. // }
  1981. private JScrollPane getScrollPaneFromTabbedPane() {
  1982. return getScrollPaneFromTabbedPane(-1);
  1983. }
  1984. private JScrollPane getScrollPaneFromTabbedPane(int index) {
  1985. Component upperLevelSelectedComponent;
  1986. if (index == -1) {
  1987. upperLevelSelectedComponent = tabTemp.getSelectedComponent();
  1988. } else {
  1989. upperLevelSelectedComponent = tabTemp.getComponentAt(index);
  1990. }
  1991. if (upperLevelSelectedComponent instanceof JTabbedPane) {
  1992. return (JScrollPane) ((JTabbedPane) upperLevelSelectedComponent).getSelectedComponent();
  1993. } else if (upperLevelSelectedComponent instanceof JScrollPane) {
  1994. return (JScrollPane) upperLevelSelectedComponent;
  1995. } else {
  1996. return null;
  1997. }
  1998. }
  1999. private void repaintGraphAfterUndoRedo() {
  2000. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  2001. unitGraph.repaintGraph(cps);
  2002. }
  2003. }
  2004. /**
  2005. * if flexibility was turned on, then active needs to be turned off, the energy currently produced/consumed
  2006. *
  2007. * @param eleBTemp element that needs to be updated
  2008. * @param selectedValueBY the y value in the table
  2009. */
  2010. private void updateElementTableAfterChange(HolonElement eleBTemp, int selectedValueBY) {
  2011. model.getSingleTable().setValueAt(eleBTemp.isFlexible(), selectedValueBY, 6);
  2012. model.getSingleTable().setValueAt(eleBTemp.isActive(), selectedValueBY, 5);
  2013. model.getSingleTable().setValueAt(eleBTemp.getAmount(), selectedValueBY, 4);
  2014. model.getSingleTable().setValueAt(eleBTemp.getFlexibleEnergyAvailablePerElement(), selectedValueBY, 3);
  2015. model.getSingleTable().setValueAt(eleBTemp.getEnergyPerElement(), selectedValueBY, 2);
  2016. }
  2017. private void triggerUpdateController(AbstractCpsObject temp) {
  2018. if (temp != null) {
  2019. updCon.paintProperties(temp);
  2020. }
  2021. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  2022. updCon.refreshTableProperties(model.getPropertyTable());
  2023. }
  2024. /**
  2025. * Custom Mouse adapter makes contentPane gain focus once mouse leaves this component
  2026. * so copy/paste/cut and "select all" works
  2027. */
  2028. private class FocusCanvasMouseAdapter extends MouseAdapter {
  2029. @Override
  2030. public void mouseExited(MouseEvent e) {
  2031. contentPane.requestFocus();
  2032. }
  2033. }
  2034. }