GUI.java 101 KB

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