GUI.java 68 KB

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