GUI.java 68 KB

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