GUI.java 104 KB

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