GUI.java 103 KB

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