GUI.java 66 KB

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