GUI.java 105 KB

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