GUI.java 102 KB

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