GUI.java 68 KB

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