GUI.java 70 KB

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