GUI.java 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Cursor;
  6. import java.awt.Dimension;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import java.beans.PropertyChangeEvent;
  16. import java.beans.PropertyChangeListener;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import javax.swing.AbstractAction;
  22. import javax.swing.ActionMap;
  23. import javax.swing.BoxLayout;
  24. import javax.swing.DefaultComboBoxModel;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.InputMap;
  27. import javax.swing.JButton;
  28. import javax.swing.JComboBox;
  29. import javax.swing.JComponent;
  30. import javax.swing.JDialog;
  31. import javax.swing.JFileChooser;
  32. import javax.swing.JFrame;
  33. import javax.swing.JLabel;
  34. import javax.swing.JMenu;
  35. import javax.swing.JMenuBar;
  36. import javax.swing.JMenuItem;
  37. import javax.swing.JOptionPane;
  38. import javax.swing.JPanel;
  39. import javax.swing.JPopupMenu;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JSlider;
  42. import javax.swing.JSplitPane;
  43. import javax.swing.JTabbedPane;
  44. import javax.swing.JTable;
  45. import javax.swing.JTextField;
  46. import javax.swing.JToolBar;
  47. import javax.swing.JTree;
  48. import javax.swing.KeyStroke;
  49. import javax.swing.SwingUtilities;
  50. import javax.swing.border.LineBorder;
  51. import javax.swing.event.ChangeEvent;
  52. import javax.swing.event.ChangeListener;
  53. import javax.swing.filechooser.FileNameExtensionFilter;
  54. import javax.swing.table.DefaultTableModel;
  55. import javax.swing.table.TableCellEditor;
  56. import javax.swing.table.TableCellRenderer;
  57. import javax.swing.tree.DefaultMutableTreeNode;
  58. import javax.swing.tree.DefaultTreeModel;
  59. import javax.swing.tree.TreeCellRenderer;
  60. import classes.AbstractCpsObject;
  61. import classes.Category;
  62. import classes.CpsEdge;
  63. import classes.HolonElement;
  64. import classes.HolonObject;
  65. import classes.HolonSwitch;
  66. import classes.HolonTransformer;
  67. import classes.IdCounter;
  68. import classes.IdCounterElem;
  69. import interfaces.CategoryListener;
  70. import ui.controller.Control;
  71. import ui.model.Model;
  72. /**
  73. * Graphical User Interface.
  74. *
  75. * @author Gruppe14
  76. *
  77. * @param <E>
  78. * Generic
  79. */
  80. public class GUI<E> implements CategoryListener {
  81. private JFrame frmCyberPhysical;
  82. private final SimulationMenu simMenu;
  83. private final JMenuBar menuBar = new JMenuBar();
  84. private final JMenu mnNewMenu = new JMenu("File");
  85. private final JMenu mnNewMenuEdit = new JMenu("Edit");
  86. private final JMenu mnNewMenuOptions = new JMenu("Options");
  87. private final JMenu mnNewMenuView = new JMenu("View");
  88. private final JMenu mnHelp = new JMenu("Help");
  89. private final JMenuItem mntmOpen = new JMenuItem("Open");
  90. private final JMenuItem mntmNew = new JMenuItem("New");
  91. private final JMenuItem mntmSave = new JMenuItem("Save");
  92. private final JMenuItem aboutUs = new JMenuItem("About Us");
  93. private final JMenuItem canvasSize = new JMenuItem("View Size");
  94. private final JSplitPane splitPane = new JSplitPane();
  95. private final JSplitPane splitPane1 = new JSplitPane();
  96. private final JSplitPane splitPaneCanvasConsole = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  97. private final JScrollPane canvasSP = new JScrollPane();
  98. private final JScrollPane scrollPane1 = new JScrollPane();
  99. private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  100. private final JPanel panelTapped_SimMenu = new JPanel();
  101. private JPopupMenu popmenuEdit = new JPopupMenu();
  102. private JMenuItem editItem = new JMenuItem("Edit Object");
  103. private String catOfObjToBeEdited;
  104. private final JScrollPane statTab = new JScrollPane();
  105. private StatisticPane statPane = new StatisticPane();
  106. private JScrollPane statScrollPane = new JScrollPane(statPane);
  107. private final JLabel maxGraph = new JLabel("100%");
  108. private final JLabel medGraph = new JLabel("50%");
  109. private final JLabel minGraph = new JLabel("0%");
  110. private final JLabel elementGraph = new JLabel("None ");
  111. private final ArrayList<HolonElement> selectedElements = new ArrayList<HolonElement>();
  112. private String holonEleNamesDisplayed = "None ";
  113. private HashMap<Integer, ArrayList<HolonElement>> eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  114. private final JTree tree = new JTree();
  115. /******************************************
  116. ************* Right Container*************
  117. ******************************************
  118. * Right Container: here comes the information about the HolonObject, such
  119. * as HolonElements Information, Properties and Consumption/Production
  120. * graph.
  121. **/
  122. private final JSplitPane splitHolonElPro = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  123. private final JSplitPane splitGraphHolonEl = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  124. // In this section are all the Holonelements that correspond to the clicked
  125. // HolonObject with consumption/production, name and amount.
  126. // Table for HolonElements --> all cells are editable
  127. private JTable tableHolonElement = new JTable();
  128. // Model for single or multi selection
  129. private PropertyTable tableModelHolonElementMulti = new PropertyTable();
  130. private PropertyTable tableModelHolonElementSingle = new PropertyTable();
  131. private final JPanel scrollElements = new JPanel();
  132. private JScrollPane tableHolonElementScrollPane = new JScrollPane();
  133. // In this section are all the properties that correspond to the clicked
  134. // HolonObject, such as connections, name, Type, etc.
  135. // Table for Properties --> Cell with (0,1) is editable by CpsObjt and
  136. // Cell(3,1) is editable by Edges
  137. private JTable tableProperties = new JTable() {
  138. private static final long serialVersionUID = 1L;
  139. @Override
  140. public TableCellRenderer getCellRenderer(int row, int column) {
  141. if (getValueAt(row, column) instanceof Boolean) {
  142. return super.getDefaultRenderer(Boolean.class);
  143. } else {
  144. return super.getCellRenderer(row, column);
  145. }
  146. }
  147. @Override
  148. public TableCellEditor getCellEditor(int row, int column) {
  149. if (getValueAt(row, column) instanceof Boolean) {
  150. return super.getDefaultEditor(Boolean.class);
  151. } else {
  152. return super.getCellEditor(row, column);
  153. }
  154. }
  155. };
  156. private JPanel graphLabel = new JPanel();
  157. private DefaulTable tableModelProperties;
  158. private final JScrollPane scrollProperties = new JScrollPane();
  159. // In this section is the graph for the selected HolonElement of the clicked
  160. // HolonObject
  161. private JTable tableGraph = new JTable();
  162. private DefaultTableModel tableModelGraph = new DefaultTableModel();
  163. private final JScrollPane scrollGraph = new JScrollPane();
  164. private Model model;
  165. private final Control controller;
  166. // Pop up Windows
  167. private AddObjectPopUp addObjectPopUP;
  168. private AboutUsPopUp aboutUsPopUp;
  169. private AddElementPopUp addElementPopUp;
  170. private final JPanel panel = new JPanel();
  171. private final JPanel panelHolonEl = new JPanel();
  172. private final JComboBox comboBox = new JComboBox();
  173. // private final JComboBox comboBoxGraph = new JComboBox();
  174. // Buttons
  175. private final JButton btnAdd = new JButton("+");
  176. private final JButton btnDel = new JButton("-");
  177. private final JButton btnAddHolEL = new JButton("+");
  178. private final JButton btnDelHolEL = new JButton("-");
  179. private final JButton resetGraphBtn = new JButton("Reset");
  180. private final JToolBar toolBar = new JToolBar();
  181. private final JToolBar toolBarHolonEl = new JToolBar();
  182. private final JToolBar toolBarGraph = new JToolBar();
  183. // private final Languages languages = new Languages();
  184. // Languages
  185. private final JMenuItem englishBtn = new JMenuItem("EN");
  186. private final JMenuItem spanishBtn = new JMenuItem("ES");
  187. private final JMenuItem germanBtn = new JMenuItem("DE");
  188. private final JMenuItem czechBtn = new JMenuItem("CZ");
  189. private final JMenuItem chineseBtn = new JMenuItem("ZH");
  190. // variables
  191. private boolean dragging = false;
  192. private String actualObjectClicked;
  193. private Image img = null;
  194. private AbstractCpsObject tempCps = null;
  195. private int yValueElements = 0;
  196. private Console console = new Console();
  197. private MyCanvas canvas;
  198. private UnitGraph unitGraph;
  199. private StatisticGraph statGraph;
  200. private final JSplitPane splitPane3 = new JSplitPane();
  201. private final JSlider sizeSlider = new JSlider();
  202. private final JLabel lblImageSize = new JLabel(Languages.getLanguage()[94]);
  203. // Time Stuff
  204. private TimePanel timePanel;
  205. // Coord for all Cells with text
  206. private int yThis;
  207. private int xThis;
  208. // Coord for all Cells with boolean values (checkbox)
  209. private int yBTis;
  210. private int xBThis;
  211. // Coord for the Edit-Modus in the PropertieTable
  212. private int yProThis;
  213. private int xProThis;
  214. private AbstractCpsObject temp = null;
  215. private final JMenuItem mntmUndo = new JMenuItem("Undo");
  216. private final JMenuItem mntmRedo = new JMenuItem("Redo");
  217. private final JMenuItem mntmEditEdges = new JMenuItem("Edit Edges");
  218. private final JMenuItem mntmFindReplace = new JMenuItem("Find/ Replace");
  219. private final JMenuItem mntmEditShowedInformation = new JMenuItem("Edit showed Information");
  220. private final JMenuItem mntmResetCategory = new JMenuItem("Reset Categories");
  221. private final JMenu mnLanguage = new JMenu("Language");
  222. private String[] columnNamesMulti = { "Object", "Nr.", "Device", "Energy", "Quantity", "Activated" };
  223. private String[] columnNamesSingle = { "Nr.", "Device", "Energy", "Quantity", "Activated" };
  224. private String[] colNames = { "Field", "Information" };
  225. private String[] comboBoxCat = { "Category", "Object", "Switch" };
  226. private String warningText = "Warning";
  227. private String saveBeforeNew = "Do you want to save your current data?";
  228. private String eraseCategory = "Do you really want to delete the Category ";
  229. private String selectObjBeforeErase = "Please select a Category or an Object in order to delete something.";
  230. /**
  231. * Create the application.
  232. *
  233. * @param control
  234. * the Controller
  235. */
  236. public GUI(Control control) {
  237. this.controller = control;
  238. this.model = control.getModel();
  239. this.canvas = new MyCanvas(model, control);
  240. this.unitGraph = new UnitGraph(model, control);
  241. this.statGraph = new StatisticGraph(model, controller);
  242. control.initListener(this);
  243. controller.setCanvas(canvas);
  244. model.setConsole(console);
  245. simMenu = new SimulationMenu(model, control);
  246. initialize();
  247. updateCategories(model.getCategories());
  248. }
  249. /**
  250. * Initialize the contents of the frame.
  251. */
  252. @SuppressWarnings({ "serial", "unchecked" })
  253. private void initialize() {
  254. frmCyberPhysical = new JFrame();
  255. frmCyberPhysical.setTitle("Cyber Physical Systems Model");
  256. frmCyberPhysical.setBounds(100, 100, 1000, 800);
  257. frmCyberPhysical.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  258. frmCyberPhysical.addWindowListener(new java.awt.event.WindowAdapter() {
  259. @Override
  260. public void windowClosing(java.awt.event.WindowEvent windowEvent) {
  261. if (JOptionPane.showConfirmDialog(frmCyberPhysical, Languages.getLanguage()[88],
  262. "Cyber Physical Systems Model", JOptionPane.YES_NO_OPTION,
  263. JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
  264. controller.deleteDirectory(new File(System.getProperty("user.home") + "/HolonGUI/Autosave"));
  265. System.exit(0);
  266. }
  267. }
  268. });
  269. JPanel contentPane = (JPanel) frmCyberPhysical.getContentPane();
  270. int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
  271. InputMap inputMap = contentPane.getInputMap(condition);
  272. ActionMap actionMap = contentPane.getActionMap();
  273. String cntrlZDown = "controlZ";
  274. inputMap.put(KeyStroke.getKeyStroke("control Z"), cntrlZDown);
  275. actionMap.put(cntrlZDown, new AbstractAction() {
  276. private static final long serialVersionUID = 1L;
  277. @Override
  278. public void actionPerformed(ActionEvent e) {
  279. try {
  280. controller.loadFile(controller.getUndoSave());
  281. canvas.repaint();
  282. ArrayList<HolonElement> tempList = new ArrayList<>();
  283. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  284. if (cps instanceof HolonObject) {
  285. for (HolonElement h : ((HolonObject) cps).getElements()) {
  286. tempList.add(h);
  287. unitGraph.repaintWithNewElement(tempList);
  288. unitGraph.fillArrayofValue();
  289. tempList.remove(0);
  290. }
  291. } else if (cps instanceof HolonSwitch) {
  292. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  293. unitGraph.fillArrayofBooleans();
  294. }
  295. }
  296. unitGraph.empty();
  297. } catch (IOException eex) {
  298. // TODO Auto-generated catch block
  299. eex.printStackTrace();
  300. }
  301. }
  302. });
  303. String cntrlYDown = "controlY";
  304. inputMap.put(KeyStroke.getKeyStroke("control Y"), cntrlYDown);
  305. actionMap.put(cntrlYDown, new AbstractAction() {
  306. private static final long serialVersionUID = 1L;
  307. @Override
  308. public void actionPerformed(ActionEvent e) {
  309. try {
  310. controller.loadFile(controller.getRedoSave());
  311. canvas.repaint();
  312. ArrayList<HolonElement> tempList = new ArrayList<>();
  313. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  314. if (cps instanceof HolonObject) {
  315. for (HolonElement h : ((HolonObject) cps).getElements()) {
  316. tempList.add(h);
  317. unitGraph.repaintWithNewElement(tempList);
  318. unitGraph.fillArrayofValue();
  319. tempList.remove(0);
  320. }
  321. } else if (cps instanceof HolonSwitch) {
  322. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  323. unitGraph.fillArrayofBooleans();
  324. }
  325. }
  326. unitGraph.empty();
  327. } catch (IOException ex) {
  328. // TODO Auto-generated catch block
  329. ex.printStackTrace();
  330. }
  331. }
  332. });
  333. String cntrlADown = "controlA";
  334. inputMap.put(KeyStroke.getKeyStroke("control A"), cntrlADown);
  335. actionMap.put(cntrlADown, new AbstractAction() {
  336. private static final long serialVersionUID = 1L;
  337. @Override
  338. public void actionPerformed(ActionEvent e) {
  339. model.getSelectedCpsObjects().clear();
  340. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  341. controller.addSelectedObject(cps);
  342. }
  343. canvas.repaint();
  344. }
  345. });
  346. String delDown = "delete";
  347. inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false), delDown);
  348. actionMap.put(delDown, new AbstractAction() {
  349. private static final long serialVersionUID = 1L;
  350. @Override
  351. public void actionPerformed(ActionEvent e) {
  352. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  353. controller.delCanvasObject(cps);
  354. }
  355. model.getSelectedCpsObjects().clear();
  356. canvas.repaint();
  357. unitGraph.empty();
  358. }
  359. });
  360. String cntrlFDown = "controlF";
  361. inputMap.put(KeyStroke.getKeyStroke("control F"), cntrlFDown);
  362. actionMap.put(cntrlFDown, new AbstractAction() {
  363. private static final long serialVersionUID = 1L;
  364. @Override
  365. public void actionPerformed(ActionEvent e) {
  366. SearchPopUp dialog = new SearchPopUp(controller, canvas);
  367. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  368. dialog.setVisible(true);
  369. }
  370. });
  371. String cntrlCDown = "controlC";
  372. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  373. actionMap.put(cntrlCDown, new AbstractAction() {
  374. private static final long serialVersionUID = 1L;
  375. @Override
  376. public void actionPerformed(ActionEvent e) {
  377. if (!model.getSelectedCpsObjects().isEmpty()) {
  378. controller.copyObjects();
  379. if (!model.getClipboradObjects().isEmpty()) {
  380. canvas.itemPaste.setEnabled(true);
  381. }
  382. }
  383. }
  384. });
  385. String cntrlVDown = "controlV";
  386. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  387. actionMap.put(cntrlVDown, new AbstractAction() {
  388. private static final long serialVersionUID = 1L;
  389. @Override
  390. public void actionPerformed(ActionEvent e) {
  391. controller.pasteObjects(canvas.getMousePosition());
  392. canvas.repaint();
  393. }
  394. });
  395. String cntrlXDown = "controlX";
  396. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  397. actionMap.put(cntrlXDown, new AbstractAction() {
  398. private static final long serialVersionUID = 1L;
  399. @Override
  400. public void actionPerformed(ActionEvent e) {
  401. if (!model.getSelectedCpsObjects().isEmpty()) {
  402. controller.cutObjects();
  403. if (!model.getClipboradObjects().isEmpty()) {
  404. canvas.itemPaste.setEnabled(true);
  405. }
  406. canvas.repaint();
  407. }
  408. }
  409. });
  410. frmCyberPhysical.setJMenuBar(menuBar);
  411. frmCyberPhysical.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  412. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  413. menuBar.add(mnNewMenu);
  414. mnNewMenu.add(mntmNew);
  415. mnNewMenu.add(mntmOpen);
  416. mnNewMenu.add(mntmSave);
  417. menuBar.add(mnNewMenuEdit);
  418. mnNewMenuEdit.add(mntmUndo);
  419. mnNewMenuEdit.add(mntmRedo);
  420. mnLanguage.add(englishBtn);
  421. englishBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagEN.png").getImage().getScaledInstance(20,
  422. 20, java.awt.Image.SCALE_SMOOTH)));
  423. mnLanguage.add(spanishBtn);
  424. spanishBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagES.png").getImage().getScaledInstance(20,
  425. 20, java.awt.Image.SCALE_SMOOTH)));
  426. mnLanguage.add(germanBtn);
  427. germanBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagDE.png").getImage().getScaledInstance(20,
  428. 20, java.awt.Image.SCALE_SMOOTH)));
  429. // mnLanguage.add(czechBtn);
  430. // czechBtn.setIcon(new ImageIcon(new
  431. // ImageIcon("res/Button_Images/FlagCZ.png").getImage().getScaledInstance(20,
  432. // 20, java.awt.Image.SCALE_SMOOTH)));
  433. mnLanguage.add(chineseBtn);
  434. chineseBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagZH.png").getImage().getScaledInstance(20,
  435. 20, java.awt.Image.SCALE_SMOOTH)));
  436. mntmFindReplace.addActionListener(new ActionListener() {
  437. public void actionPerformed(ActionEvent e) {
  438. try {
  439. SearchPopUp dialog = new SearchPopUp(controller, canvas);
  440. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  441. dialog.setVisible(true);
  442. } catch (Exception ex) {
  443. ex.printStackTrace();
  444. }
  445. }
  446. });
  447. mnNewMenuEdit.add(mntmFindReplace);
  448. mntmEditShowedInformation.addActionListener(new ActionListener() {
  449. public void actionPerformed(ActionEvent e) {
  450. try {
  451. ShowedInformationPopUp dialog = new ShowedInformationPopUp(canvas);
  452. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  453. dialog.setVisible(true);
  454. } catch (Exception ex) {
  455. ex.printStackTrace();
  456. }
  457. }
  458. });
  459. mnNewMenuEdit.add(mntmEditShowedInformation);
  460. menuBar.add(mnNewMenuOptions);
  461. mntmEditEdges.addActionListener(new ActionListener() {
  462. public void actionPerformed(ActionEvent e) {
  463. EditEdgesPopUp edgePopUp = new EditEdgesPopUp();
  464. edgePopUp.setCanvas(canvas);
  465. edgePopUp.setController(controller);
  466. edgePopUp.setVisible(true);
  467. }
  468. });
  469. mnNewMenuOptions.add(mntmEditEdges);
  470. mnNewMenuOptions.add(mntmResetCategory);
  471. mntmResetCategory.addActionListener(new ActionListener() {
  472. @Override
  473. public void actionPerformed(ActionEvent e) {
  474. ArrayList<Category> cat = model.getCategories();
  475. while (!cat.isEmpty()) {
  476. controller.deleteCategory(cat.get(0).getName());
  477. ;
  478. }
  479. controller.resetCategorys();
  480. tree.repaint();
  481. }
  482. });
  483. menuBar.add(mnNewMenuView);
  484. mnNewMenuView.add(canvasSize);
  485. canvasSize.addActionListener(new ActionListener() {
  486. @Override
  487. public void actionPerformed(ActionEvent e) {
  488. JPanel myPanel = new JPanel();
  489. JTextField field1 = new JTextField("" + canvas.getWidth() + "");
  490. JTextField field2 = new JTextField("" + canvas.getHeight() + "");
  491. myPanel.add(field1);
  492. myPanel.add(field2);
  493. JOptionPane.showMessageDialog(null, myPanel);
  494. controller.setCanvasX(Integer.parseInt(field1.getText()));
  495. controller.setCanvasY(Integer.parseInt(field2.getText()));
  496. canvas.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
  497. canvas.repaint();
  498. }
  499. });
  500. mnNewMenuView.add(splitPane3);
  501. sizeSlider.setMinimum(15);
  502. sizeSlider.setMaximum(115);
  503. sizeSlider.addChangeListener(new ChangeListener() {
  504. @Override
  505. public void stateChanged(ChangeEvent e) {
  506. controller.setScale(sizeSlider.getValue());
  507. tree.setRowHeight(50);
  508. canvas.repaint();
  509. }
  510. });
  511. splitPane3.setRightComponent(sizeSlider);
  512. splitPane3.setLeftComponent(lblImageSize);
  513. menuBar.add(mnHelp);
  514. mnHelp.add(aboutUs);
  515. mnNewMenuOptions.add(mnLanguage);
  516. canvas.setBackground(Color.WHITE);
  517. canvas.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
  518. /********************
  519. * RIGHT CONTAINER (INFORMATION)
  520. **********************/
  521. // Set up of the HolonElements section
  522. // Two different Models: Multi for multi-selection mode and Single for
  523. // single-selection mode (CPS-Object)
  524. tableModelHolonElementMulti.setColumnIdentifiers(columnNamesMulti);
  525. tableModelHolonElementSingle.setColumnIdentifiers(columnNamesSingle);
  526. tableHolonElement.setBorder(null);
  527. tableHolonElement.setFillsViewportHeight(true);
  528. tableHolonElement.setCellSelectionEnabled(true);
  529. tableHolonElement.setColumnSelectionAllowed(true);
  530. tableHolonElementScrollPane.setViewportView(tableHolonElement);
  531. scrollElements.setLayout(new BorderLayout(0, 0));
  532. scrollElements.add(panelHolonEl, BorderLayout.NORTH);
  533. scrollElements.add(tableHolonElementScrollPane);
  534. panelHolonEl.setLayout(new BoxLayout(panelHolonEl, BoxLayout.X_AXIS));
  535. toolBarHolonEl.add(btnAddHolEL);
  536. toolBarHolonEl.add(btnDelHolEL);
  537. toolBarHolonEl.setFloatable(false);
  538. panelHolonEl.add(toolBarHolonEl);
  539. // Set up of the Properties section
  540. tableModelProperties = new DefaulTable(1000, colNames.length);
  541. tableModelProperties.setColumnIdentifiers(colNames);
  542. tableProperties.setModel(tableModelProperties);
  543. tableProperties.setFillsViewportHeight(true);
  544. tableProperties.setCellSelectionEnabled(true);
  545. tableProperties.setColumnSelectionAllowed(true);
  546. scrollProperties.setViewportView(tableProperties);
  547. // Set up of the Graph section
  548. tableGraph.setModel(tableModelGraph);
  549. tableGraph.setFillsViewportHeight(true);
  550. tableGraph.setCellSelectionEnabled(true);
  551. tableGraph.setColumnSelectionAllowed(true);
  552. scrollGraph.setViewportView(unitGraph);
  553. graphLabel.setLayout(new BorderLayout(0, 10));
  554. graphLabel.add(maxGraph, BorderLayout.NORTH);
  555. graphLabel.add(medGraph, BorderLayout.CENTER);
  556. graphLabel.add(minGraph, BorderLayout.SOUTH);
  557. toolBarGraph.add(elementGraph);
  558. toolBarGraph.add(resetGraphBtn);
  559. toolBarGraph.setFloatable(false);
  560. scrollGraph.setRowHeaderView(graphLabel);
  561. scrollGraph.setColumnHeaderView(toolBarGraph);
  562. /***********************
  563. * HolonElement Table Actions
  564. **********************/
  565. /*
  566. * Add HolonElement to given HolonObject
  567. */
  568. btnAddHolEL.addActionListener(new ActionListener() {
  569. public void actionPerformed(ActionEvent arg0) {
  570. if (model.getSelectedCpsObjects().size() == 1) {
  571. AbstractCpsObject tempCpsObject = getActualCps();
  572. if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
  573. && tempCpsObject.getID() != 0) {
  574. addElementPopUp = new AddElementPopUp();
  575. addElementPopUp.setActualCps(getActualCps());
  576. addElementPopUp.setVisible(true);
  577. HolonElement ele = addElementPopUp.getElement();
  578. if (ele != null) {
  579. controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
  580. ele.getEnergy());
  581. }
  582. refreshTableHolonElement();
  583. refreshTableProperties();
  584. controller.calculateStateForTimeStep(model.getCurIteration());
  585. }
  586. }
  587. }
  588. });
  589. /*
  590. * Delete the chosen HolonElement of the selected HolonObject,
  591. * Multi-Selection for CpsObjects as well as for HolonElements possible
  592. */
  593. btnDelHolEL.addActionListener(new ActionListener() {
  594. public void actionPerformed(ActionEvent arg0) {
  595. // For Single Selection of CpsObject
  596. if (model.getSelectedCpsObjects().size() == 1) {
  597. if (getActualCps().getClass() == HolonObject.class) {
  598. HolonObject obj = (HolonObject) getActualCps();
  599. for (HolonElement e : selectedElements) {
  600. controller.deleteElementCanvas(obj.getID(), e.getId());
  601. refreshTableHolonElement();
  602. refreshTableProperties();
  603. controller.calculateStateForTimeStep(model.getCurIteration());
  604. // Names displayed in graph are not updated
  605. }
  606. eleToDelete.clear();
  607. selectedElements.clear();
  608. }
  609. // For MultiSelection of CpsObject
  610. } else if (model.getSelectedCpsObjects().size() > 1) {
  611. for (Integer i : eleToDelete.keySet()) {
  612. for (HolonElement e : eleToDelete.get(i)) {
  613. controller.deleteElementCanvas(i, e.getId());
  614. }
  615. }
  616. refreshTableHolonElement();
  617. refreshTableProperties();
  618. eleToDelete.clear();
  619. selectedElements.clear();
  620. }
  621. elementGraph.setText("None ");
  622. holonEleNamesDisplayed = "None ";
  623. }
  624. });
  625. /*
  626. * Communication between HolonElement Table and displayed Graph and
  627. * Properties, as well as selection of differet HolonElements
  628. */
  629. tableHolonElement.addMouseListener(new MouseAdapter() {
  630. public void mousePressed(MouseEvent e) {
  631. HolonObject obj = (HolonObject) getActualCps();
  632. yValueElements = e.getY();
  633. HolonElement ele = null;
  634. // Search for current clicked HolonElement
  635. if (model.getSelectedCpsObjects().size() == 1) {
  636. ele = getActualHolonElement(obj, yValueElements, 0);
  637. } else {
  638. ele = getActualHolonElement(null, yValueElements, 0);
  639. }
  640. // Multi-Selection of HolonElements through control button +
  641. // mouse click
  642. if (e.isControlDown() && ele != null) {
  643. if (!selectedElements.contains(ele)) {
  644. selectedElements.add(ele);
  645. if (!holonEleNamesDisplayed.equals("None ")) {
  646. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  647. } else {
  648. holonEleNamesDisplayed = ele.getEleName() + " ";
  649. }
  650. unitGraph.repaintWithNewElement(selectedElements);
  651. }
  652. getActualHolonElement(null, yValueElements, 2);
  653. } // if no control-button pressed but a HolonElement choose
  654. else if (ele != null) {
  655. selectedElements.clear();
  656. selectedElements.add(ele);
  657. getActualHolonElement(null, yValueElements, 1);
  658. holonEleNamesDisplayed = ele.getEleName() + " ";
  659. unitGraph.repaintWithNewElement(selectedElements);
  660. } // If any empty space is clicked
  661. else {
  662. elementGraph.setText(Languages.getLanguage()[25]);
  663. unitGraph.empty();
  664. }
  665. // if any HolonElement is double-clicked --> Edit-Mode started
  666. // for selected HolonElement
  667. if (e.getClickCount() == 2) {
  668. yThis = e.getY();
  669. xThis = e.getX();
  670. }
  671. // for single click and empty slot
  672. if (e.getClickCount() == 1 && ele == null) {
  673. selectedElements.clear();
  674. holonEleNamesDisplayed = Languages.getLanguage()[25];
  675. }
  676. elementGraph.setText(holonEleNamesDisplayed);
  677. yBTis = e.getY();
  678. xBThis = e.getX();
  679. }
  680. });
  681. /*
  682. * Triggered every time a change is made
  683. */
  684. tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
  685. @Override
  686. public void propertyChange(PropertyChangeEvent evt) {
  687. try {
  688. int yMouse = yThis;
  689. int yBMouse = yBTis;
  690. int selectedValueY = (int) Math.floor(yMouse / 16);
  691. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  692. // for multi-selection mode
  693. if (model.getSelectedCpsObjects().size() > 1) {
  694. int selectedValueX = (int) Math.floor(xThis / (tableHolonElement.getWidth() / 6));
  695. int selectedValueBX = (int) Math.floor(xBThis / (tableHolonElement.getWidth() / 6));
  696. if (getHolonObj(yMouse) != null) {
  697. // For last column (boolean with a checkbox)
  698. if (selectedValueBX == 5) {
  699. HolonElement eleBTemp = getActualHolonElement(null, yBMouse, 0);
  700. String newBStuff = tableModelHolonElementMulti
  701. .getValueAt(selectedValueBY, selectedValueBX).toString();
  702. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  703. eleBTemp.setActive(bTemp);
  704. } else {
  705. // Update of HolonElement
  706. HolonElement eleTemp = getActualHolonElement(null, yMouse, 0);
  707. String newStuff = tableModelHolonElementMulti.getValueAt(selectedValueY, selectedValueX)
  708. .toString();
  709. // Name update
  710. if (selectedValueX == 2) {
  711. eleTemp.setEleName(newStuff);
  712. }
  713. // Energy Update
  714. else if (selectedValueX == 3) {
  715. Float ftemp = Float.parseFloat(newStuff);
  716. eleTemp.setEnergy(ftemp);
  717. }
  718. // Amount of Elements update
  719. else if (selectedValueX == 4) {
  720. Integer iTemp = Integer.parseInt(newStuff);
  721. eleTemp.setAmount(iTemp);
  722. }
  723. }
  724. }
  725. } // For single-selection mode
  726. else if (model.getSelectedCpsObjects().size() == 1) {
  727. int selectedValueX = (int) Math.floor(xThis / (tableHolonElement.getWidth() / 5));
  728. int selectedValueBX = (int) Math.floor(xBThis / (tableHolonElement.getWidth() / 5));
  729. if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
  730. // For last column (boolean with a checkbox)
  731. if (selectedValueBX == 4) {
  732. HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse, 0);
  733. String newBStuff = tableModelHolonElementSingle
  734. .getValueAt(selectedValueBY, selectedValueBX).toString();
  735. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  736. eleBTemp.setActive(bTemp);
  737. } else {
  738. // Update of HolonElement
  739. HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse, 0);
  740. String newStuff = tableModelHolonElementSingle
  741. .getValueAt(selectedValueY, selectedValueX).toString();
  742. // Name update
  743. if (selectedValueX == 1) {
  744. eleTemp.setEleName(newStuff);
  745. } // Energy Update
  746. else if (selectedValueX == 2) {
  747. Float ftemp = Float.parseFloat(newStuff);
  748. eleTemp.setEnergy(ftemp);
  749. } // Amount of Elements update
  750. else if (selectedValueX == 3) {
  751. Integer iTemp = Integer.parseInt(newStuff);
  752. eleTemp.setAmount(iTemp);
  753. }
  754. }
  755. }
  756. }
  757. refreshTableProperties();
  758. tableModelHolonElementSingle.fireTableDataChanged();
  759. controller.calculateStateForTimeStep(model.getCurIteration());
  760. unitGraph.repaint();
  761. } catch (Exception e) {
  762. }
  763. }
  764. });
  765. /***********************
  766. * HolonElement Properties Actions
  767. **********************/
  768. /*
  769. * Update of the mouse coord for Edit-Mode
  770. */
  771. tableProperties.addMouseListener(new MouseAdapter() {
  772. public void mousePressed(MouseEvent e) {
  773. if (e.getClickCount() == 2) {
  774. yProThis = e.getY();
  775. xProThis = e.getX();
  776. }
  777. }
  778. });
  779. /*
  780. * Update any change in the Property table
  781. */
  782. tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
  783. @Override
  784. public void propertyChange(PropertyChangeEvent evt) {
  785. try {
  786. Object temp;
  787. Object btemp;
  788. Point mousePos = tableProperties.getMousePosition();
  789. int selValueY = (int) Math.floor(yProThis / 16);
  790. int selValueX = (int) Math.floor(xProThis / (tableProperties.getWidth() / 2));
  791. if (getActualCps() != null) {
  792. temp = tableModelProperties.getValueAt(selValueY, selValueX);
  793. if (getActualCps() instanceof HolonSwitch) {
  794. btemp = tableModelProperties.getValueAt(mousePos.y / tableProperties.getRowHeight(),
  795. mousePos.x / (tableProperties.getWidth() / 2));
  796. if (mousePos.y / tableProperties.getRowHeight() == 0) {
  797. getActualCps().setName(btemp.toString());
  798. } else if (mousePos.y / tableProperties.getRowHeight() == 2) {
  799. Boolean bbTemp = Boolean.parseBoolean(btemp.toString());
  800. ((HolonSwitch) getActualCps()).setManualMode(bbTemp);
  801. if (bbTemp) {
  802. tableModelProperties.setCellEditable(3, 1, true);
  803. } else {
  804. tableModelProperties.setCellEditable(3, 1, false);
  805. }
  806. } else if (mousePos.y / tableProperties.getRowHeight() == 3) {
  807. if (((HolonSwitch) getActualCps()).getManualMode()) {
  808. tableModelProperties.setCellEditable(3, 1, true);
  809. Boolean bTemp = Boolean.parseBoolean(temp.toString());
  810. ((HolonSwitch) getActualCps()).setManualState(bTemp);
  811. }
  812. }
  813. } else if (getActualCps() instanceof HolonObject) {
  814. getActualCps().setName(temp.toString());
  815. }
  816. } else {
  817. temp = tableModelProperties.getValueAt(selValueY, selValueX);
  818. btemp = tableModelProperties.getValueAt(mousePos.y / tableProperties.getRowHeight(),
  819. mousePos.x / (tableProperties.getWidth() / 2));
  820. if (selValueY == 2) {
  821. Float ftemp;
  822. if (Float.parseFloat(temp.toString()) >= 0.0) {
  823. ftemp = Float.parseFloat(temp.toString());
  824. } else {
  825. ftemp = model.getSelectedEdge().getCapacity();
  826. }
  827. model.getSelectedEdge().setCapacity(ftemp);
  828. } else if (mousePos.y / tableProperties.getRowHeight() == 3) {
  829. Boolean bbTemp = Boolean.parseBoolean(btemp.toString());
  830. model.getSelectedEdge().setState(bbTemp);
  831. }
  832. }
  833. canvas.repaint();
  834. } catch (Exception e) {
  835. }
  836. }
  837. });
  838. /***********************
  839. * HolonElement Graph Actions
  840. **********************/
  841. /*
  842. * Reset the graph of the selected HolonElement
  843. */
  844. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  845. resetGraphBtn.addActionListener(new ActionListener() {
  846. public void actionPerformed(ActionEvent arg0) {
  847. unitGraph.reset();
  848. }
  849. });
  850. /*****************************
  851. * RIGHT CONTAINER DONE
  852. *****************************/
  853. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  854. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  855. @Override
  856. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  857. boolean leaf, int row, boolean hasFocus) {
  858. JLabel label = new JLabel();
  859. Image imgR = null;
  860. if (leaf) {
  861. for (Category cat : model.getCategories()) {
  862. for (AbstractCpsObject cps : cat.getObjects()) {
  863. if (value.toString().compareTo(cps.getObjName()) == 0) {
  864. File checkPath = new File(cps.getImage());
  865. if (checkPath.exists()) {
  866. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
  867. java.awt.Image.SCALE_SMOOTH);
  868. } else {
  869. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  870. .getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  871. }
  872. if (imgR != null) {
  873. label.setIcon(new ImageIcon(imgR));
  874. }
  875. label.setText(cps.getName());
  876. }
  877. }
  878. }
  879. }
  880. tree.setRowHeight(50);
  881. if (hasFocus) {
  882. label.setForeground(new Color(0, 0, 255));
  883. label.setOpaque(true);
  884. }
  885. if (label.getText().length() == 0) {
  886. label.setText(value.toString());
  887. if (value.toString().compareTo("Categories") != 0) {
  888. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  889. }
  890. }
  891. return label;
  892. }
  893. };
  894. tree.setCellRenderer(customRenderer);
  895. tree.addMouseListener(new MouseAdapter() {
  896. public void mouseReleased(MouseEvent e) {
  897. try {
  898. if (dragging) {
  899. int x = (int) canvas.getMousePosition().getX();
  900. int y = (int) canvas.getMousePosition().getY();
  901. AbstractCpsObject h = null;
  902. if (tempCps.getClass() == HolonObject.class) {
  903. h = new HolonObject(tempCps);
  904. }
  905. if (tempCps.getClass() == HolonSwitch.class) {
  906. h = new HolonSwitch(tempCps);
  907. }
  908. if (tempCps.getClass() == HolonTransformer.class) {
  909. h = new HolonTransformer(tempCps);
  910. }
  911. h.setPosition(x, y);
  912. controller.addObjectCanvas(h);
  913. canvas.invalidate();
  914. canvas.repaint();
  915. dragging = false;
  916. }
  917. } catch (Exception eex) {
  918. }
  919. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  920. }
  921. });
  922. popmenuEdit.add(editItem);
  923. editItem.setEnabled(false);
  924. editItem.addActionListener(new ActionListener() {
  925. @Override
  926. public void actionPerformed(ActionEvent e) {
  927. }
  928. });
  929. tree.addMouseListener(new MouseAdapter() {
  930. public void mousePressed(MouseEvent e) {
  931. try {
  932. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  933. // if an Object was selected, the porperties are shown in
  934. // the table
  935. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  936. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  937. if (selectedNode.getLevel() == 2) {
  938. controller.searchCategoryObject(selectedNode.getParent().toString(), selectedNode.toString());
  939. deleteRows(tableModelHolonElementSingle);
  940. deleteRows(tableModelHolonElementMulti);
  941. // if (selected instanceof HolonObject && selected !=
  942. // null) {
  943. // selected = (HolonObject) selected;
  944. // fillElementTable(((HolonObject)
  945. // selected).getElements());
  946. // }
  947. }
  948. if (SwingUtilities.isRightMouseButton(e)) {
  949. for (Category cat : model.getCategories()) {
  950. for (AbstractCpsObject cps : cat.getObjects()) {
  951. if (actualObjectClicked.compareTo(cps.getObjName()) == 0
  952. && !(cps instanceof HolonSwitch)) {
  953. editItem.setEnabled(true);
  954. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  955. catOfObjToBeEdited = selectedNode.getParent().toString();
  956. tempCps = cps;
  957. }
  958. }
  959. }
  960. } else {
  961. for (Category cat : model.getCategories()) {
  962. for (AbstractCpsObject cps : cat.getObjects()) {
  963. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  964. File checkPath = new File(cps.getImage());
  965. if (checkPath.exists()) {
  966. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
  967. java.awt.Image.SCALE_SMOOTH);
  968. } else {
  969. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  970. .getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
  971. }
  972. tempCps = cps;
  973. dragging = true;
  974. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  975. "Image");
  976. frmCyberPhysical.setCursor(cursor);
  977. }
  978. }
  979. }
  980. }
  981. } catch (Exception eex) {
  982. }
  983. }
  984. });
  985. editItem.addActionListener(new ActionListener() {
  986. @Override
  987. public void actionPerformed(ActionEvent e) {
  988. // Remove the selected Object object
  989. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
  990. addObjectPopUP.setCategory(catOfObjToBeEdited);
  991. addObjectPopUP.setController(controller);
  992. addObjectPopUP.setVisible(true);
  993. }
  994. });
  995. scrollPane1.setViewportView(tree);
  996. scrollPane1.setColumnHeaderView(panel);
  997. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  998. toolBar.setFloatable(false);
  999. panel.add(toolBar);
  1000. toolBar.add(comboBox);
  1001. comboBox.setModel(new DefaultComboBoxModel(comboBoxCat));
  1002. // Add Buttonnew DefaultComboBoxModel(comboBoxCat)
  1003. btnAdd.addActionListener(new ActionListener() {
  1004. public void actionPerformed(ActionEvent arg0) {
  1005. Object nodeInfo = tree.getLastSelectedPathComponent();
  1006. String selectedOption = comboBox.getSelectedItem().toString();
  1007. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1008. try {
  1009. switch (selectedOption) {
  1010. case "Category":
  1011. String catName = JOptionPane.showInputDialog(Languages.getLanguage()[56]);
  1012. if (catName.length() != 0) {
  1013. controller.addCategory(catName);
  1014. }
  1015. break;
  1016. case "Object":
  1017. if (selectedNode == null) {
  1018. JOptionPane.showMessageDialog(new JFrame(),
  1019. Languages.getLanguage()[57] + selectedOption + ".");
  1020. }
  1021. if (selectedNode.getLevel() == 1) {
  1022. AbstractCpsObject tmp = new HolonObject("");
  1023. addObjectPopUP = new AddObjectPopUp(false, tmp, null);
  1024. addObjectPopUP.setVisible(true);
  1025. addObjectPopUP.setController(controller);
  1026. addObjectPopUP.setCategory(selectedNode.toString());
  1027. }
  1028. break;
  1029. default:
  1030. addObjectAction(selectedOption, selectedNode);
  1031. break;
  1032. }
  1033. } catch (Exception e) {
  1034. // TODO: handle exception
  1035. }
  1036. tree.repaint();
  1037. }
  1038. });
  1039. /**
  1040. * Pop up - About Us with some important information about the
  1041. * developers, source and programming stuff
  1042. */
  1043. aboutUs.addMouseListener(new MouseAdapter() {
  1044. @Override
  1045. public void mousePressed(MouseEvent e) {
  1046. aboutUsPopUp = new AboutUsPopUp();
  1047. aboutUsPopUp.setVisible(true);
  1048. }
  1049. });
  1050. /**
  1051. * Update of every interaction between the user and the canvas (only on
  1052. * the canvas). Basically the update of all the information concerning
  1053. * the clicked HolonObject. For multi-selection, the propertyTable would
  1054. * be disabled
  1055. */
  1056. canvas.addMouseListener(new MouseAdapter() {
  1057. @Override
  1058. public void mousePressed(MouseEvent e) {
  1059. selectedElements.clear();
  1060. holonEleNamesDisplayed = Languages.getLanguage()[25];
  1061. // If any empty space is clicked
  1062. if (temp == null || temp.getID() != model.getSelectedObjectID()) {
  1063. unitGraph.empty();
  1064. elementGraph.setText(Languages.getLanguage()[25]);
  1065. }
  1066. temp = getActualCps();
  1067. // Erase old data in the PropertyTable
  1068. if (tableModelProperties.getRowCount() > 0) {
  1069. for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
  1070. tableModelProperties.removeRow(i);
  1071. }
  1072. }
  1073. if (e.isControlDown() && temp != null) {
  1074. if (model.getSelectedCpsObjects().contains(temp)) {
  1075. controller.deleteSelectedObject(temp);
  1076. } else {
  1077. controller.addSelectedObject(temp);
  1078. }
  1079. }
  1080. // Write new data in the PropertyTable
  1081. if (temp != null) {
  1082. // Name of the CpsObject
  1083. Object[] tempName = { Languages.getLanguage()[72], temp.getName() };
  1084. tableModelProperties.addRow(tempName);
  1085. // Id of the CpsObject
  1086. Object[] tempId = { "ID", temp.getID() };
  1087. tableModelProperties.addRow(tempId);
  1088. // For HolonObjects the Total Energy (production or
  1089. // consumption) is calculated
  1090. if (temp instanceof HolonObject) {
  1091. refreshTableHolonElement();
  1092. Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) temp).getCurrentEnergy() };
  1093. tableModelProperties.addRow(tempEnergy);
  1094. tableModelProperties.setCellEditable(0, 1, true);
  1095. tableModelProperties.setCellEditable(2, 1, false);
  1096. tableModelProperties.setCellEditable(3, 1, false);
  1097. } // For HolonSwitches is showed the actual status (active
  1098. // or inactive)
  1099. else if (temp instanceof HolonSwitch) {
  1100. deleteRows(tableModelHolonElementSingle);
  1101. deleteRows(tableModelHolonElementMulti);
  1102. Object[] tempMode = { Languages.getLanguage()[74], ((HolonSwitch) temp).getManualMode() };
  1103. tableModelProperties.addRow(tempMode);
  1104. if (((HolonSwitch) temp).getManualMode()) {
  1105. Object[] tempActive = { Languages.getLanguage()[75],
  1106. ((HolonSwitch) temp).getActiveManual() };
  1107. tableModelProperties.addRow(tempActive);
  1108. tableModelProperties.setCellEditable(3, 1, true);
  1109. } else {
  1110. Object[] tempActive = { Languages.getLanguage()[75],
  1111. ((HolonSwitch) temp).getActiveAt()[model.getCurIteration()] };
  1112. tableModelProperties.addRow(tempActive);
  1113. tableModelProperties.setCellEditable(3, 1, false);
  1114. }
  1115. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  1116. elementGraph.setText(temp.getName());
  1117. tableModelProperties.setCellEditable(0, 1, true);
  1118. tableModelProperties.setCellEditable(2, 1, true);
  1119. } else {
  1120. deleteRows(tableModelHolonElementSingle);
  1121. deleteRows(tableModelHolonElementMulti);
  1122. }
  1123. // For Objects the only editable cell is the name
  1124. ArrayList<CpsEdge> tempArray = temp.getConnections();
  1125. // If the clicked object has connections
  1126. if (!tempArray.isEmpty()) {
  1127. boolean first = true;
  1128. for (CpsEdge temp2 : tempArray) {
  1129. if (first) {
  1130. first = false;
  1131. if (temp.getID() != temp2.getA().getID()) {
  1132. Object[] tempConnection = { temp.getName() + Languages.getLanguage()[76],
  1133. temp2.getA().getName() + Languages.getLanguage()[77]
  1134. + temp2.getA().getID() };
  1135. tableModelProperties.addRow(tempConnection);
  1136. } else {
  1137. Object[] tempConnection = { temp.getName() + Languages.getLanguage()[76],
  1138. temp2.getB().getName() + Languages.getLanguage()[77]
  1139. + temp2.getB().getID() };
  1140. tableModelProperties.addRow(tempConnection);
  1141. }
  1142. } else {
  1143. if (temp.getID() != temp2.getA().getID()) {
  1144. Object[] tempConnection = { "", temp2.getA().getName() + Languages.getLanguage()[77]
  1145. + temp2.getA().getID() };
  1146. tableModelProperties.addRow(tempConnection);
  1147. } else {
  1148. Object[] tempConnection = { "", temp2.getB().getName() + Languages.getLanguage()[77]
  1149. + temp2.getB().getID() };
  1150. tableModelProperties.addRow(tempConnection);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. } // If the clicked Object is an edge
  1156. else if (model.getSelectedEdge() != null) {
  1157. // Name displayed
  1158. Object[] tempName = { Languages.getLanguage()[72],
  1159. Languages.getLanguage()[78] + model.getSelectedEdge().getA().getName()
  1160. + Languages.getLanguage()[79] + model.getSelectedEdge().getB().getName() };
  1161. tableModelProperties.addRow(tempName);
  1162. // Current Flow displayed
  1163. Object[] tempFlow = { Languages.getLanguage()[80], model.getSelectedEdge().getFlow() };
  1164. tableModelProperties.addRow(tempFlow);
  1165. // Max Capacity displayed
  1166. Object[] tempCapacity = { Languages.getLanguage()[81], model.getSelectedEdge().getCapacity() };
  1167. tableModelProperties.addRow(tempCapacity);
  1168. // Status displayed
  1169. Object[] tempStatus = { Languages.getLanguage()[82], model.getSelectedEdge().getState() };
  1170. tableModelProperties.addRow(tempStatus);
  1171. // For edges, the only possible editable cell is the max
  1172. // flow
  1173. tableModelProperties.setCellEditable(0, 1, false);
  1174. tableModelProperties.setCellEditable(2, 1, true);
  1175. tableModelProperties.setCellEditable(3, 1, true);
  1176. } else if (getActualCps() == null) {
  1177. deleteRows(tableModelHolonElementSingle);
  1178. deleteRows(tableModelHolonElementMulti);
  1179. } // Update of the HolonElementTable (Single- or
  1180. // Multi-Selection)
  1181. if (model.getSelectedCpsObjects().size() > 1) {
  1182. tableHolonElement.setModel(tableModelHolonElementMulti);
  1183. } else if (model.getSelectedCpsObjects().size() == 1) {
  1184. tableHolonElement.setModel(tableModelHolonElementSingle);
  1185. }
  1186. }
  1187. @Override
  1188. public void mouseReleased(MouseEvent e) {
  1189. refreshTableHolonElement();
  1190. refreshTableProperties();
  1191. if (model.getSelectedCpsObjects().size() > 1) {
  1192. tableHolonElement.setModel(tableModelHolonElementMulti);
  1193. } else if (model.getSelectedCpsObjects().size() == 1) {
  1194. tableHolonElement.setModel(tableModelHolonElementSingle);
  1195. }
  1196. }
  1197. });
  1198. toolBar.add(btnAdd);
  1199. // Del Button
  1200. btnDel.addActionListener(new ActionListener() {
  1201. public void actionPerformed(ActionEvent arg0) {
  1202. Object nodeInfo = tree.getLastSelectedPathComponent();
  1203. if (nodeInfo != null) {
  1204. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  1205. String nodeName = selectedNode.getUserObject().toString();
  1206. int depthOfNode = selectedNode.getLevel();
  1207. switch (depthOfNode) {
  1208. case 1:
  1209. int dialogResult = JOptionPane.showConfirmDialog(null, eraseCategory + nodeName + "?",
  1210. warningText, JOptionPane.YES_NO_OPTION);
  1211. if (dialogResult == JOptionPane.YES_OPTION) {
  1212. controller.deleteCategory(nodeName);
  1213. }
  1214. break;
  1215. case 2:
  1216. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  1217. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  1218. break;
  1219. default:
  1220. JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
  1221. }
  1222. } else {
  1223. JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
  1224. }
  1225. tree.repaint();
  1226. }
  1227. });
  1228. toolBar.add(btnDel);
  1229. frmCyberPhysical.getContentPane().add(splitPane);
  1230. mntmNew.addActionListener(new ActionListener() {
  1231. @Override
  1232. public void actionPerformed(ActionEvent arg0) {
  1233. if (model.getObjectsOnCanvas().size() != 0) {
  1234. int newWarning = JOptionPane.YES_NO_OPTION;
  1235. int dialogForNewWarning = JOptionPane.showConfirmDialog(null, saveBeforeNew, warningText,
  1236. newWarning);
  1237. if (dialogForNewWarning == JOptionPane.YES_OPTION) {
  1238. mntmSave.doClick();
  1239. }
  1240. }
  1241. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  1242. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  1243. controller.setSelectedObjectID(0);
  1244. controller.setSelecteEdge(null);
  1245. controller.setCurIteration(0);
  1246. unitGraph.empty();
  1247. elementGraph.setText(Languages.getLanguage()[25]);
  1248. canvas.tempCps = null;
  1249. canvas.repaint();
  1250. IdCounter.resetCounter();
  1251. IdCounterElem.resetCounter();
  1252. }
  1253. });
  1254. mntmOpen.addActionListener(new java.awt.event.ActionListener() {
  1255. @Override
  1256. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1257. menuFileExitActionPerformed(evt);
  1258. }
  1259. private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
  1260. JFileChooser fileChooser = new JFileChooser();
  1261. JFrame test = new JFrame();
  1262. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  1263. fileChooser.setFileFilter(jsonFilter);
  1264. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  1265. File file = fileChooser.getSelectedFile();
  1266. try {
  1267. controller.loadFile(file.getAbsolutePath());
  1268. canvas.repaint();
  1269. ArrayList<HolonElement> tempList = new ArrayList<>();
  1270. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  1271. if (cps instanceof HolonObject) {
  1272. for (HolonElement h : ((HolonObject) cps).getElements()) {
  1273. tempList.add(h);
  1274. unitGraph.repaintWithNewElement(tempList);
  1275. unitGraph.fillArrayofValue();
  1276. tempList.remove(0);
  1277. }
  1278. } else if (cps instanceof HolonSwitch) {
  1279. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  1280. unitGraph.fillArrayofBooleans();
  1281. }
  1282. }
  1283. unitGraph.empty();
  1284. tree.repaint();
  1285. } catch (IOException e) {
  1286. // TODO Auto-generated catch block
  1287. e.printStackTrace();
  1288. }
  1289. }
  1290. }
  1291. });
  1292. mntmSave.addActionListener(new java.awt.event.ActionListener() {
  1293. @Override
  1294. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1295. menuSaveActionPerformed(evt);
  1296. }
  1297. private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
  1298. JFileChooser fileChooser = new JFileChooser();
  1299. JFrame test = new JFrame();
  1300. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  1301. fileChooser.setFileFilter(jsonFilter);
  1302. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  1303. String file = fileChooser.getSelectedFile().getPath();
  1304. if (!file.endsWith(".json"))
  1305. file += ".json";
  1306. try {
  1307. controller.saveFile(new File(file).getAbsolutePath());
  1308. } catch (IOException e) {
  1309. // TODO Auto-generated catch block
  1310. e.printStackTrace();
  1311. }
  1312. }
  1313. }
  1314. });
  1315. englishBtn.addActionListener(new ActionListener() {
  1316. @Override
  1317. public void actionPerformed(ActionEvent e) {
  1318. Languages.setLanguage(0);
  1319. refreshLanguages();
  1320. refreshTableProperties();
  1321. }
  1322. });
  1323. spanishBtn.addActionListener(new ActionListener() {
  1324. @Override
  1325. public void actionPerformed(ActionEvent e) {
  1326. Languages.setLanguage(1);
  1327. refreshLanguages();
  1328. refreshTableProperties();
  1329. }
  1330. });
  1331. germanBtn.addActionListener(new ActionListener() {
  1332. @Override
  1333. public void actionPerformed(ActionEvent e) {
  1334. Languages.setLanguage(2);
  1335. refreshLanguages();
  1336. refreshTableProperties();
  1337. }
  1338. });
  1339. czechBtn.addActionListener(new ActionListener() {
  1340. @Override
  1341. public void actionPerformed(ActionEvent e) {
  1342. Languages.setLanguage(3);
  1343. refreshLanguages();
  1344. refreshTableProperties();
  1345. }
  1346. });
  1347. chineseBtn.addActionListener(new ActionListener() {
  1348. @Override
  1349. public void actionPerformed(ActionEvent e) {
  1350. Languages.setLanguage(4);
  1351. refreshLanguages();
  1352. refreshTableProperties();
  1353. }
  1354. });
  1355. mntmUndo.addActionListener(new java.awt.event.ActionListener() {
  1356. @Override
  1357. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1358. menuUndoActionPerformed(evt);
  1359. }
  1360. private void menuUndoActionPerformed(java.awt.event.ActionEvent evt) {
  1361. try {
  1362. controller.loadFile(controller.getUndoSave());
  1363. canvas.repaint();
  1364. ArrayList<HolonElement> tempList = new ArrayList<>();
  1365. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  1366. if (cps instanceof HolonObject) {
  1367. for (HolonElement h : ((HolonObject) cps).getElements()) {
  1368. tempList.add(h);
  1369. unitGraph.repaintWithNewElement(tempList);
  1370. unitGraph.fillArrayofValue();
  1371. tempList.remove(0);
  1372. }
  1373. } else if (cps instanceof HolonSwitch) {
  1374. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  1375. unitGraph.fillArrayofBooleans();
  1376. }
  1377. }
  1378. unitGraph.empty();
  1379. } catch (IOException e) {
  1380. // TODO Auto-generated catch block
  1381. e.printStackTrace();
  1382. }
  1383. }
  1384. });
  1385. mntmRedo.addActionListener(new java.awt.event.ActionListener() {
  1386. @Override
  1387. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1388. menuRedoActionPerformed(evt);
  1389. }
  1390. private void menuRedoActionPerformed(java.awt.event.ActionEvent evt) {
  1391. try {
  1392. controller.loadFile(controller.getRedoSave());
  1393. canvas.repaint();
  1394. ArrayList<HolonElement> tempList = new ArrayList<>();
  1395. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  1396. if (cps instanceof HolonObject) {
  1397. for (HolonElement h : ((HolonObject) cps).getElements()) {
  1398. tempList.add(h);
  1399. unitGraph.repaintWithNewElement(tempList);
  1400. unitGraph.fillArrayofValue();
  1401. tempList.remove(0);
  1402. }
  1403. } else if (cps instanceof HolonSwitch) {
  1404. unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
  1405. unitGraph.fillArrayofBooleans();
  1406. }
  1407. }
  1408. unitGraph.empty();
  1409. } catch (IOException e) {
  1410. // TODO Auto-generated catch block
  1411. e.printStackTrace();
  1412. }
  1413. }
  1414. });
  1415. timePanel = new TimePanel(model, controller);
  1416. timePanel.setBorder(null);
  1417. ((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
  1418. @Override
  1419. public void stateChanged(ChangeEvent e) {
  1420. int i = model.getCurIteration();
  1421. controller.calculateStateForTimeStep(i);
  1422. unitGraph.repaint();
  1423. statGraph.repaint();
  1424. }
  1425. });
  1426. splitPane.setRightComponent(splitPane1);
  1427. splitPane.setDividerLocation(200);
  1428. splitPane1.setDividerLocation(500);
  1429. splitPaneCanvasConsole.setDividerLocation(500);
  1430. splitPane.setLeftComponent(scrollPane1);
  1431. splitPaneCanvasConsole.setLeftComponent(panelTapped_SimMenu);
  1432. tabbedPane.addTab("View", canvasSP);
  1433. tabbedPane.addTab("Statistics", new JPanel());
  1434. // splitPaneCanvasConsole.setLeftComponent(canvasSP);
  1435. splitPaneCanvasConsole.setRightComponent(console);
  1436. splitPane1.setLeftComponent(splitPaneCanvasConsole);
  1437. splitPane1.setRightComponent(splitHolonElPro);
  1438. splitPane1.setResizeWeight(0.9);
  1439. splitHolonElPro.setDividerLocation(400);
  1440. splitHolonElPro.setTopComponent(splitGraphHolonEl);
  1441. splitHolonElPro.setBottomComponent(scrollProperties);
  1442. splitGraphHolonEl.setDividerLocation(150);
  1443. splitGraphHolonEl.setTopComponent(scrollGraph);
  1444. splitGraphHolonEl.setBottomComponent(scrollElements);
  1445. canvasSP.setViewportView(canvas);
  1446. panelTapped_SimMenu.setLayout(new BorderLayout());
  1447. panelTapped_SimMenu.add(simMenu, BorderLayout.NORTH);
  1448. panelTapped_SimMenu.add(tabbedPane);
  1449. simMenu.setBackground(new Color(240, 240, 240));
  1450. panelTapped_SimMenu.setBorder(null);
  1451. tabbedPane.setBorder(null);
  1452. scrollProperties.setBorder(null);
  1453. scrollGraph.setBorder(null);
  1454. scrollElements.setBorder(null);
  1455. splitPane.setBorder(null);
  1456. splitPane1.setBorder(null);
  1457. splitPaneCanvasConsole.setBorder(null);
  1458. splitHolonElPro.setBorder(null);
  1459. splitGraphHolonEl.setBorder(null);
  1460. panelHolonEl.setBorder(null);
  1461. canvasSP.setBorder(null);
  1462. tableHolonElementScrollPane.setBorder(null);
  1463. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  1464. String autoPath = System.getProperty("user.home") + "/HolonGUI/Autosave/";
  1465. File dest = new File(autoPath);
  1466. if (dest.listFiles().length > 1) {
  1467. int dialogButton = JOptionPane.YES_NO_OPTION;
  1468. int dialogResult = JOptionPane.showConfirmDialog(null, "Old autosave file was found, should it be loaded?",
  1469. warningText, dialogButton);
  1470. if (dialogResult == JOptionPane.YES_OPTION) {
  1471. model.setAutoSaveNr(dest.listFiles().length - 1);
  1472. mntmRedo.doClick();
  1473. } else {
  1474. controller.deleteDirectory(dest);
  1475. dest.mkdirs();
  1476. try {
  1477. controller.autoSave();
  1478. } catch (IOException e1) {
  1479. // TODO Auto-generated catch block
  1480. e1.printStackTrace();
  1481. }
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * adds a specific object type to selected Category also handles input
  1487. * windows and illegal inputs.
  1488. *
  1489. * @param objType
  1490. * type of the Object
  1491. * @param selectedNode
  1492. * The selected Node
  1493. */
  1494. public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  1495. if (selectedNode == null) {
  1496. JOptionPane.showMessageDialog(new JFrame(),
  1497. "Please select a Category first before adding " + objType + ".");
  1498. }
  1499. // if selected node is a directory for Categories
  1500. else {
  1501. if (selectedNode.getLevel() == 1) {
  1502. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  1503. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  1504. if (objname.length() != 0) {
  1505. switch (objType) {
  1506. case "Switch":
  1507. controller.addSwitch(cat, objname);
  1508. break;
  1509. }
  1510. }
  1511. } else {
  1512. JOptionPane.showMessageDialog(new JFrame(),
  1513. "Objects can not be added to Objects. Please select a Category.");
  1514. }
  1515. }
  1516. }
  1517. /**
  1518. * reloads the Categories from Model.
  1519. *
  1520. * @param categories
  1521. * the current categories
  1522. */
  1523. public void updateCategories(final ArrayList<Category> categories) {
  1524. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  1525. private static final long serialVersionUID = 1L;
  1526. {
  1527. DefaultMutableTreeNode node1;
  1528. for (Category c : categories) {
  1529. node1 = new DefaultMutableTreeNode(c.getName());
  1530. // kann eventuell umgeändert werden
  1531. for (AbstractCpsObject obj : c.getObjects()) {
  1532. node1.add(new DefaultMutableTreeNode(obj.getObjName()));
  1533. }
  1534. add(node1);
  1535. }
  1536. }
  1537. }));
  1538. }
  1539. /**
  1540. * When changes are made to the Categories.
  1541. *
  1542. * @param categories
  1543. * the Categories
  1544. */
  1545. public void onChange(ArrayList<Category> categories) {
  1546. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  1547. updateCategories(categories);
  1548. model.reload();
  1549. }
  1550. /**
  1551. * Get the Frame.
  1552. *
  1553. * @return the Frame
  1554. */
  1555. public JFrame getFrmCyberPhysical() {
  1556. return frmCyberPhysical;
  1557. }
  1558. /**
  1559. * Getter for selected CpsObject.
  1560. *
  1561. * @return selected CpsObject
  1562. */
  1563. private AbstractCpsObject getActualCps() {
  1564. int tempID = model.getSelectedObjectID();
  1565. AbstractCpsObject tempCps = controller.searchByID(tempID);
  1566. return tempCps;
  1567. }
  1568. /**
  1569. * Search for actual selected HolonElement.
  1570. *
  1571. * @param obj
  1572. * selected HolonObject, if obj==null means multi-selection
  1573. * active
  1574. * @param yValue
  1575. * Y-Coord in the HolonElementsTable
  1576. * @param toMultiHash
  1577. * 0 means no MultiSelection, 1 means MultiSelection without
  1578. * Control, 2 means MultiSelection with Control
  1579. * @return the selected HolonElement
  1580. */
  1581. private HolonElement getActualHolonElement(HolonObject obj, int yValue, int toMultiHash) {
  1582. final int yTemp = (int) Math.floor(yValue / 16);
  1583. int rowsTotal = 0;
  1584. // Filter for search --> single and multi selection
  1585. if (obj == null) {
  1586. rowsTotal = tableModelHolonElementMulti.getRowCount();
  1587. } else {
  1588. rowsTotal = tableModelHolonElementSingle.getRowCount();
  1589. }
  1590. // search for the clicked HolonObject and HolonElement --> in the
  1591. // HolonElementTable
  1592. HolonObject obtTemp = null;
  1593. HolonElement toReturnEle = null;
  1594. int id = 0;
  1595. if (rowsTotal != 0 && rowsTotal > yTemp) {
  1596. // Multi-Selection search
  1597. if (obj == null) {
  1598. String tempStringObj = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
  1599. int idTempObj = Integer.parseInt(tempStringObj.split(", ")[1]);
  1600. obtTemp = (HolonObject) controller.searchByID(idTempObj);
  1601. id = Integer.parseInt(tableModelHolonElementMulti.getValueAt(yTemp, 1).toString());
  1602. ArrayList<HolonElement> eleTemp = new ArrayList<HolonElement>();
  1603. if (eleToDelete.containsKey(idTempObj) && toMultiHash == 2) {
  1604. eleTemp = eleToDelete.get(idTempObj);
  1605. if (!eleTemp.contains(obtTemp.searchElementById(id))) {
  1606. eleTemp.add(obtTemp.searchElementById(id));
  1607. eleToDelete.replace(idTempObj, eleTemp);
  1608. }
  1609. } else if (toMultiHash == 2) {
  1610. eleTemp.add(obtTemp.searchElementById(id));
  1611. eleToDelete.put(idTempObj, eleTemp);
  1612. } else if (toMultiHash == 1) {
  1613. eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  1614. eleTemp.add(obtTemp.searchElementById(id));
  1615. eleToDelete.put(idTempObj, eleTemp);
  1616. } else if (toMultiHash == 0) {
  1617. toReturnEle = obtTemp.searchElementById(id);
  1618. }
  1619. } // Single-Selection search
  1620. else {
  1621. eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  1622. id = Integer.parseInt(tableModelHolonElementSingle.getValueAt(yTemp, 0).toString());
  1623. toReturnEle = obj.searchElementById(id);
  1624. }
  1625. model.setSelectedHolonElement(toReturnEle);
  1626. return toReturnEle;
  1627. } // If no HolonObject selected
  1628. else {
  1629. eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
  1630. model.setSelectedHolonElement(null);
  1631. return null;
  1632. }
  1633. }
  1634. /**
  1635. * Search for clicked HolonObject through the mouse's y-Coord.
  1636. *
  1637. * @param yValue
  1638. * the Y Coordination
  1639. * @return clicked HolonObject
  1640. */
  1641. private HolonObject getHolonObj(int yValue) {
  1642. final int yTemp = (int) Math.floor(yValue / 16);
  1643. HolonObject obtTemp = null;
  1644. String temp = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
  1645. int idTemp = Integer.parseInt(temp.split(", ")[1]);
  1646. obtTemp = (HolonObject) controller.searchByID(idTemp);
  1647. return obtTemp;
  1648. }
  1649. /**
  1650. * Update the HolonElement Table, that means erase all rows and add the new
  1651. * rows with new data.
  1652. */
  1653. private void refreshTableHolonElement() {
  1654. // Update of the Information about the HolonElements - only for
  1655. // HolonObjects
  1656. if (model.getSelectedCpsObjects().size() > 1) {
  1657. deleteRows(tableModelHolonElementMulti);
  1658. fillElementTable(model.getSelectedCpsObjects());
  1659. tableModelHolonElementMulti.fireTableDataChanged();
  1660. } else if (model.getSelectedCpsObjects().size() == 1) {
  1661. deleteRows(tableModelHolonElementSingle);
  1662. fillElementTable(model.getSelectedCpsObjects());
  1663. tableModelHolonElementSingle.fireTableDataChanged();
  1664. }
  1665. }
  1666. /**
  1667. * Erase all information of the HolonElement Model.
  1668. *
  1669. * @param t
  1670. * the Table
  1671. */
  1672. private void deleteRows(PropertyTable t) {
  1673. if (t.getRowCount() > 0) {
  1674. for (int i = t.getRowCount() - 1; i > -1; i--) {
  1675. t.removeRow(i);
  1676. }
  1677. }
  1678. }
  1679. /**
  1680. * Add the Information of the given ArrayList in the HolonElement Model as
  1681. * Name,Energy and Amount.
  1682. *
  1683. * @param objects
  1684. * ArrayList to be displayed
  1685. */
  1686. private void fillElementTable(ArrayList<AbstractCpsObject> objects) {
  1687. if (objects.size() > 1) {
  1688. for (AbstractCpsObject o : objects) {
  1689. if (o instanceof HolonObject) {
  1690. for (HolonElement he : ((HolonObject) o).getElements()) {
  1691. Object[] temp = { o.getName() + ", " + o.getID(), he.getId(), he.getEleName(), he.getEnergy(),
  1692. he.getAmount(), he.getActive() };
  1693. tableModelHolonElementMulti.addRow(temp);
  1694. }
  1695. }
  1696. }
  1697. } else if (objects.size() == 1) {
  1698. AbstractCpsObject o = objects.get(0);
  1699. if (o instanceof HolonObject) {
  1700. for (HolonElement he : ((HolonObject) o).getElements()) {
  1701. Object[] temp = { he.getId(), he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
  1702. tableModelHolonElementSingle.addRow(temp);
  1703. }
  1704. }
  1705. }
  1706. }
  1707. /**
  1708. * Update the information concerning properties of the selected CpsObject.
  1709. */
  1710. private void refreshTableProperties() {
  1711. if (model.getSelectedCpsObjects().size() == 1) {
  1712. AbstractCpsObject tempCps = getActualCps();
  1713. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  1714. tableModelProperties.removeRow(2);
  1715. Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) tempCps).getCurrentEnergy() };
  1716. tableModelProperties.insertRow(2, tempEnergy);
  1717. }
  1718. }
  1719. }
  1720. /**
  1721. * Adds a Popup.
  1722. *
  1723. * @param component
  1724. * Component
  1725. * @param popup
  1726. * PopupMenu
  1727. */
  1728. private static void addPopup(Component component, final JPopupMenu popup) {
  1729. component.addMouseListener(new MouseAdapter() {
  1730. public void mousePressed(MouseEvent e) {
  1731. if (e.isPopupTrigger()) {
  1732. showMenu(e);
  1733. }
  1734. }
  1735. public void mouseReleased(MouseEvent e) {
  1736. if (e.isPopupTrigger()) {
  1737. showMenu(e);
  1738. }
  1739. }
  1740. private void showMenu(MouseEvent e) {
  1741. popup.show(e.getComponent(), e.getX(), e.getY());
  1742. }
  1743. });
  1744. }
  1745. private void refreshLanguages() {
  1746. String[] tempArray = Languages.getLanguage();
  1747. // ToolBar
  1748. mnNewMenu.setText(tempArray[0]);
  1749. mntmNew.setText(tempArray[1]);
  1750. mntmOpen.setText(tempArray[2]);
  1751. mntmSave.setText(tempArray[3]);
  1752. mnNewMenuEdit.setText(tempArray[4]);
  1753. mntmUndo.setText(tempArray[5]);
  1754. mntmRedo.setText(tempArray[6]);
  1755. mntmFindReplace.setText(tempArray[7]);
  1756. mntmEditShowedInformation.setText(tempArray[8]);
  1757. mnNewMenuOptions.setText(tempArray[9]);
  1758. mntmResetCategory.setText(tempArray[10]);
  1759. mnNewMenuView.setText(tempArray[11]);
  1760. canvasSize.setText(tempArray[12]);
  1761. mnHelp.setText(tempArray[13]);
  1762. aboutUs.setText(tempArray[14]);
  1763. mntmEditEdges.setText(tempArray[15]);
  1764. mnLanguage.setText(tempArray[16]);
  1765. // Tables
  1766. for (int i = 0; i < columnNamesMulti.length; i++) {
  1767. columnNamesMulti[i] = tempArray[i + 17];
  1768. }
  1769. tableModelHolonElementMulti.setColumnIdentifiers(columnNamesMulti);
  1770. for (int i = 0; i < columnNamesSingle.length; i++) {
  1771. columnNamesSingle[i] = tempArray[i + 18];
  1772. }
  1773. tableModelHolonElementSingle.setColumnIdentifiers(columnNamesSingle);
  1774. colNames[0] = tempArray[23];
  1775. colNames[1] = tempArray[24];
  1776. tableModelProperties.setColumnIdentifiers(colNames);
  1777. // Graph
  1778. elementGraph.setText(tempArray[25]);
  1779. holonEleNamesDisplayed = tempArray[25];
  1780. resetGraphBtn.setText(tempArray[26]);
  1781. // Warning PopUps
  1782. warningText = tempArray[27];
  1783. saveBeforeNew = tempArray[28];
  1784. eraseCategory = tempArray[29];
  1785. selectObjBeforeErase = tempArray[30];
  1786. // SimMenu
  1787. simMenu.simButton.setText(Languages.getLanguage()[83]);
  1788. simMenu.simSpeedLabel.setText(Languages.getLanguage()[84]);
  1789. simMenu.algoFolderButton.setText(Languages.getLanguage()[85]);
  1790. // TimePanel
  1791. timePanel.playBtn.setToolTipText(Languages.getLanguage()[89]);
  1792. timePanel.timeResetBtn.setToolTipText(Languages.getLanguage()[90]);
  1793. timePanel.timeForwardBtn.setToolTipText(Languages.getLanguage()[91]);
  1794. timePanel.timeBackwardBtn.setToolTipText(Languages.getLanguage()[92]);
  1795. timePanel.timeSlider.setToolTipText(Languages.getLanguage()[93]);
  1796. // Image Size Slider
  1797. lblImageSize.setText(Languages.getLanguage()[94]);
  1798. }
  1799. }