GUI.java 105 KB

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