GUI.java 102 KB

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