GUI.java 102 KB

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