GUI.java 102 KB

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