GUI.java 68 KB

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