GUI.java 103 KB

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