GUI.java 70 KB

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