GUI.java 69 KB

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