GUI.java 69 KB

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