GUI.java 105 KB

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