GUI.java 67 KB

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