GUI.java 102 KB

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