GUI.java 103 KB

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