GUI.java 101 KB

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