GUI.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.beans.PropertyChangeEvent;
  15. import java.beans.PropertyChangeListener;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import javax.swing.BoxLayout;
  20. import javax.swing.DefaultComboBoxModel;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JButton;
  23. import javax.swing.JCheckBoxMenuItem;
  24. import javax.swing.JComboBox;
  25. import javax.swing.JEditorPane;
  26. import javax.swing.JFileChooser;
  27. import javax.swing.JFrame;
  28. import javax.swing.JLabel;
  29. import javax.swing.JMenu;
  30. import javax.swing.JMenuBar;
  31. import javax.swing.JMenuItem;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.JPanel;
  34. import javax.swing.JPopupMenu;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JSlider;
  37. import javax.swing.JSplitPane;
  38. import javax.swing.JTabbedPane;
  39. import javax.swing.JTable;
  40. import javax.swing.JTextField;
  41. import javax.swing.JToolBar;
  42. import javax.swing.JTree;
  43. import javax.swing.SwingUtilities;
  44. import javax.swing.border.LineBorder;
  45. import javax.swing.event.ChangeEvent;
  46. import javax.swing.event.ChangeListener;
  47. import javax.swing.filechooser.FileNameExtensionFilter;
  48. import javax.swing.table.DefaultTableModel;
  49. import javax.swing.tree.DefaultMutableTreeNode;
  50. import javax.swing.tree.DefaultTreeModel;
  51. import javax.swing.tree.TreeCellRenderer;
  52. import Interfaces.CategoryListener;
  53. import classes.Category;
  54. import classes.CpsEdge;
  55. import classes.CpsObject;
  56. import classes.HolonElement;
  57. import classes.HolonObject;
  58. import classes.HolonSwitch;
  59. import classes.HolonTransformer;
  60. import ui.controller.Control;
  61. import ui.model.Model;;
  62. public class GUI<E> implements CategoryListener {
  63. private JFrame frmCyberPhysical;
  64. private final JMenuBar menuBar = new JMenuBar();
  65. private final JMenu mnNewMenu = new JMenu("File");
  66. private final JMenu mnNewMenu_1 = new JMenu("Edit");
  67. private final JMenu mnNewMenu_2 = new JMenu("Options");
  68. private final JMenu mnNewMenu_3 = new JMenu("View");
  69. private final JMenu mnHelp = new JMenu("Help");
  70. private final JMenuItem mntmOpen = new JMenuItem("Open");
  71. private final JMenuItem mntmNew = new JMenuItem("New");
  72. private final JMenuItem mntmSave = new JMenuItem("Save");
  73. private final JMenuItem aboutUs = new JMenuItem("About Us");
  74. private final JMenuItem canvasSize = new JMenuItem("View Size");
  75. private final JSplitPane splitPane = new JSplitPane();
  76. private final JSplitPane splitPane_1 = new JSplitPane();
  77. private final JScrollPane scrollPane_1 = new JScrollPane();
  78. private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  79. private final JScrollPane scrollPane_2 = new JScrollPane();
  80. private JPopupMenu popmenuEdit = new JPopupMenu();
  81. private JMenuItem editItem = new JMenuItem("Edit Object");
  82. private final JLabel maxGraph = new JLabel("100%");
  83. private final JLabel medGraph = new JLabel("50%");
  84. private final JLabel minGraph = new JLabel("0%");
  85. private final JLabel elementGraph = new JLabel("None ");
  86. private final ArrayList<HolonElement> selectedElements = new ArrayList<HolonElement>();
  87. private String holonEleNamesDisplayed = "";
  88. private final JTree tree = new JTree();
  89. private final JEditorPane dtrpnHereWillBe = new JEditorPane();
  90. /******************************************
  91. ************* Right Container*************
  92. ******************************************/
  93. // Right Container: here comes the information about the HolonObject, such
  94. // as HolonElements Information, Properties and Consumption/Production graph
  95. private final JSplitPane split_HolonEl_Pro = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  96. private final JSplitPane split_Graph_HolonEl = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  97. // In this section are all the Holonelements that correspond to the clicked
  98. // HolonObject with consumption/production, name and amount.
  99. private JTable tableHolonElement = new JTable();
  100. private PropertyTable tableModelHolonElement = new PropertyTable();
  101. private final JPanel scrollElements = new JPanel();
  102. private JScrollPane tableHolonElementScrollPane = new JScrollPane();
  103. // In this section are all the properties that correspond to the clicked
  104. // HolonObject, such as connections, name, Type, etc.
  105. private JTable tableProperties = new JTable();
  106. private JPanel graphLabel = new JPanel();
  107. private DefaulTable tableModelProperties;
  108. private final JScrollPane scrollProperties = new JScrollPane();
  109. // In this section is the graph for the selected HolonElement of the clicked
  110. // HolonObject
  111. private JTable tableGraph = new JTable();
  112. private DefaultTableModel tableModelGraph = new DefaultTableModel();
  113. private final JScrollPane scrollGraph = new JScrollPane();
  114. private Model model;
  115. private final Control controller;
  116. // Pop up Windows
  117. private AddObjectPopUp addObjectPopUP;
  118. private AboutUsPopUp aboutUsPopUp;
  119. private AddElementPopUp addElementPopUp;
  120. private final JPanel panel = new JPanel();
  121. private final JPanel panel_HolonEl = new JPanel();
  122. private final JComboBox comboBox = new JComboBox();
  123. // private final JComboBox comboBoxGraph = new JComboBox();
  124. // Buttons
  125. private final JButton btnAdd = new JButton("+");
  126. private final JButton btnDel = new JButton("-");
  127. private final JButton btnAddHolEL = new JButton("+");
  128. private final JButton btnDelHolEL = new JButton("-");
  129. private final JButton resetGraphBtn = new JButton("Reset");
  130. private final JToolBar toolBar = new JToolBar();
  131. private final JToolBar toolBarHolonEl = new JToolBar();
  132. private final JToolBar toolBarGraph = new JToolBar();
  133. // variables
  134. private boolean dragging = false;
  135. private String actualObjectClicked;
  136. private Image img = null;
  137. private CpsObject tempCps = null;
  138. private HolonElement tempElement = null;
  139. private int yValueElements = 0;
  140. private MyCanvas canvas;
  141. private UnitGraph unitGraph; // for testing, remove later
  142. private final JSplitPane splitPane_3 = new JSplitPane();
  143. private final JSlider sizeSlider = new JSlider();
  144. private final JLabel lblImageSize = new JLabel("Image Size");
  145. // Time Stuff
  146. private TimePanel timePanel;
  147. private final JMenu mnAlgorithm = new JMenu("Algorithm");
  148. private final JCheckBoxMenuItem chckbxmntmUseAlgorithm = new JCheckBoxMenuItem("Use Algorithm");
  149. private final JSplitPane splitPane_2 = new JSplitPane();
  150. private final JLabel lblSelect = new JLabel("Select");
  151. private final JComboBox comboBoxAlgo = new JComboBox();
  152. private int yTHIS;
  153. private int xTHIS;
  154. private int yBTHIS;
  155. private int xBTHIS;
  156. private CpsObject temp = null;
  157. /**
  158. * Create the application.
  159. */
  160. public GUI(Control control) {
  161. this.controller = control;
  162. this.model = control.getModel();
  163. this.canvas = new MyCanvas(model, control);
  164. this.unitGraph = new UnitGraph(model, control);
  165. control.initListener(this);
  166. initialize();
  167. updateCategories(model.getCategories());
  168. }
  169. /**
  170. * Initialize the contents of the frame.
  171. */
  172. @SuppressWarnings({ "serial", "unchecked" })
  173. private void initialize() {
  174. frmCyberPhysical = new JFrame();
  175. frmCyberPhysical.setTitle("Cyber Physical Systems Model");
  176. frmCyberPhysical.setBounds(100, 100, 1000, 800);
  177. frmCyberPhysical.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  178. frmCyberPhysical.setJMenuBar(menuBar);
  179. frmCyberPhysical.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  180. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  181. menuBar.add(mnNewMenu);
  182. mnNewMenu.add(mntmNew);
  183. mnNewMenu.add(mntmOpen);
  184. mnNewMenu.add(mntmSave);
  185. menuBar.add(mnNewMenu_1);
  186. menuBar.add(mnNewMenu_2);
  187. menuBar.add(mnNewMenu_3);
  188. mnNewMenu_3.add(canvasSize);
  189. canvasSize.addActionListener(new ActionListener() {
  190. @Override
  191. public void actionPerformed(ActionEvent e) {
  192. JPanel myPanel = new JPanel();
  193. JTextField field1 = new JTextField("" + canvas.getWidth() + "");
  194. JTextField field2 = new JTextField("" + canvas.getHeight() + "");
  195. myPanel.add(field1);
  196. myPanel.add(field2);
  197. JOptionPane.showMessageDialog(null, myPanel);
  198. canvas.setPreferredSize(
  199. new Dimension(Integer.parseInt(field1.getText()), Integer.parseInt(field2.getText())));
  200. canvas.repaint();
  201. }
  202. });
  203. mnNewMenu_3.add(splitPane_3);
  204. sizeSlider.setMinimum(15);
  205. sizeSlider.setMaximum(115);
  206. sizeSlider.addChangeListener(new ChangeListener() {
  207. @Override
  208. public void stateChanged(ChangeEvent e) {
  209. controller.setScale(sizeSlider.getValue());
  210. tree.setRowHeight(model.getScale());
  211. canvas.objectSelectionHighlighting();
  212. canvas.repaint();
  213. }
  214. });
  215. splitPane_3.setRightComponent(sizeSlider);
  216. splitPane_3.setLeftComponent(lblImageSize);
  217. menuBar.add(mnHelp);
  218. mnHelp.add(aboutUs);
  219. menuBar.add(mnAlgorithm);
  220. mnAlgorithm.add(chckbxmntmUseAlgorithm);
  221. mnAlgorithm.add(splitPane_2);
  222. splitPane_2.setLeftComponent(lblSelect);
  223. splitPane_2.setRightComponent(comboBoxAlgo);
  224. canvas.setBackground(Color.WHITE);
  225. canvas.setPreferredSize(new Dimension(1000, 1000));
  226. JScrollPane canvasSP = new JScrollPane(canvas);
  227. canvasSP.setBorder(null);
  228. tabbedPane.addTab("Modeling", new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/home.png"))
  229. .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)), canvasSP, "Model a CPS");
  230. tabbedPane.addTab("Simulation",
  231. new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  232. .getScaledInstance(30, 30, Image.SCALE_SMOOTH)),
  233. scrollPane_2, "Simulate the CPS");
  234. dtrpnHereWillBe.setText("Here will be the Simulation");
  235. scrollPane_2.setViewportView(dtrpnHereWillBe);
  236. /********************
  237. * RIGHT CONTAINER (INFORMATION)
  238. **********************/
  239. // Set up of the HolonElements section
  240. Object[] columnNames = { "Device", "Energy", "Quantity", "Activated" };
  241. tableModelHolonElement.setColumnIdentifiers(columnNames);
  242. tableHolonElement.setBorder(null);
  243. tableHolonElement.setModel(tableModelHolonElement);
  244. tableHolonElement.setFillsViewportHeight(true);
  245. tableHolonElement.setCellSelectionEnabled(true);
  246. tableHolonElement.setColumnSelectionAllowed(true);
  247. tableHolonElementScrollPane.setViewportView(tableHolonElement);
  248. scrollElements.setLayout(new BorderLayout(0, 0));
  249. scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
  250. scrollElements.add(tableHolonElementScrollPane);
  251. panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
  252. toolBarHolonEl.add(btnAddHolEL);
  253. toolBarHolonEl.add(btnDelHolEL);
  254. toolBarHolonEl.setFloatable(false);
  255. panel_HolonEl.add(toolBarHolonEl);
  256. // Set up of the Properties section
  257. Object[] colNames = { "Field", "Information" };
  258. tableModelProperties = new DefaulTable(100, colNames.length);
  259. tableModelProperties.setColumnIdentifiers(colNames);
  260. tableProperties.setModel(tableModelProperties);
  261. tableProperties.setFillsViewportHeight(true);
  262. tableProperties.setCellSelectionEnabled(true);
  263. tableProperties.setColumnSelectionAllowed(true);
  264. scrollProperties.setViewportView(tableProperties);
  265. // Set up of the Graph section
  266. Object[] tempText = { "Here comes the graph for each clicked HolonElement" };
  267. tableModelGraph.setColumnIdentifiers(tempText);
  268. tableGraph.setModel(tableModelGraph);
  269. tableGraph.setFillsViewportHeight(true);
  270. tableGraph.setCellSelectionEnabled(true);
  271. tableGraph.setColumnSelectionAllowed(true);
  272. scrollGraph.setViewportView(unitGraph);
  273. graphLabel.setLayout(new BorderLayout(0, 10));
  274. graphLabel.add(maxGraph, BorderLayout.NORTH);
  275. graphLabel.add(medGraph, BorderLayout.CENTER);
  276. graphLabel.add(minGraph, BorderLayout.SOUTH);
  277. toolBarGraph.add(elementGraph);
  278. toolBarGraph.add(resetGraphBtn);
  279. toolBarGraph.setFloatable(false);
  280. scrollGraph.setRowHeaderView(graphLabel);
  281. scrollGraph.setColumnHeaderView(toolBarGraph);
  282. /***********************
  283. * HolonElement Table Actions
  284. **********************/
  285. /*
  286. * Add HolonElement to given HolonObject
  287. */
  288. btnAddHolEL.addActionListener(new ActionListener() {
  289. public void actionPerformed(ActionEvent arg0) {
  290. CpsObject tempCpsObject = getActualCps();
  291. if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
  292. && tempCpsObject.getID() != 0) {
  293. addElementPopUp = new AddElementPopUp();
  294. addElementPopUp.setActualCps(getActualCps());
  295. addElementPopUp.setVisible(true);
  296. HolonElement ele = addElementPopUp.getElement();
  297. controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
  298. ele.getEnergy());
  299. refreshTableHolonElement();
  300. refreshTableProperties();
  301. }
  302. }
  303. });
  304. /*
  305. * Delete the choosen HolonElement of the selected HolonObject
  306. */
  307. btnDelHolEL.addActionListener(new ActionListener() {
  308. public void actionPerformed(ActionEvent arg0) {
  309. if (getActualCps().getClass() == HolonObject.class) {
  310. HolonObject obj = (HolonObject) getActualCps();
  311. tempElement = getActualHolonElement(obj, yValueElements);
  312. if (tempElement != null && obj.getClass() == HolonObject.class && obj.getID() != 0) {
  313. controller.deleteElementCanvas(obj.getID(), tempElement.getEleName());
  314. refreshTableHolonElement();
  315. refreshTableProperties();
  316. }
  317. }
  318. }
  319. });
  320. /*
  321. * Communication between HolonElement Table and displayed Graph
  322. */
  323. tableHolonElement.addMouseListener(new MouseAdapter() {
  324. public void mousePressed(MouseEvent e) {
  325. HolonObject obj = (HolonObject) getActualCps();
  326. yValueElements = e.getY();
  327. HolonElement ele = getActualHolonElement(obj, yValueElements);
  328. if (e.isControlDown() && ele != null) {
  329. if (!selectedElements.contains(ele)) {
  330. selectedElements.add(ele);
  331. if (!holonEleNamesDisplayed.equals("None ")) {
  332. holonEleNamesDisplayed = holonEleNamesDisplayed + "; " + ele.getEleName() + " ";
  333. } else {
  334. holonEleNamesDisplayed = ele.getEleName() + " ";
  335. }
  336. unitGraph.repaintWithNewElement(selectedElements);
  337. }
  338. } else if (ele != null) {
  339. selectedElements.clear();
  340. selectedElements.add(ele);
  341. holonEleNamesDisplayed = ele.getEleName() + " ";
  342. unitGraph.repaintWithNewElement(selectedElements);
  343. } else {
  344. elementGraph.setText("None ");
  345. unitGraph.empty();
  346. }
  347. // if any HolonElement is double-clicked --> Edit instance of
  348. // selected HolonElement
  349. if (e.getClickCount() == 2) {
  350. yTHIS = e.getY();
  351. xTHIS = e.getX();
  352. }
  353. if (e.getClickCount() == 1 && ele == null) {
  354. selectedElements.clear();
  355. holonEleNamesDisplayed = "None ";
  356. }
  357. for (int i = 0; i < selectedElements.size(); i++) {
  358. if (i == 0) {
  359. System.out.println("Selected Items: " + selectedElements.get(i).getEleName());
  360. } else {
  361. System.out.println(selectedElements.get(i).getEleName());
  362. }
  363. }
  364. elementGraph.setText(holonEleNamesDisplayed);
  365. yBTHIS = e.getY();
  366. xBTHIS = e.getX();
  367. }
  368. });
  369. /*
  370. * If the HolonElement Table enters to editing instance, than is the
  371. * propertyChangeListener triggered
  372. */
  373. tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
  374. @Override
  375. public void propertyChange(PropertyChangeEvent evt) {
  376. try {
  377. int yMouse = yTHIS;
  378. int yBMouse = yBTHIS;
  379. int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
  380. int selectedValueY = (int) Math.floor(yMouse / 16);
  381. int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
  382. int selectedValueBY = (int) Math.floor(yBMouse / 16);
  383. if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
  384. if (selectedValueBX == 3) {
  385. HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse);
  386. String newBStuff = tableModelHolonElement.getValueAt(selectedValueBY, selectedValueBX)
  387. .toString();
  388. Boolean bTemp = Boolean.parseBoolean(newBStuff);
  389. eleBTemp.setActive(bTemp);
  390. } else {
  391. HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
  392. String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX)
  393. .toString();
  394. if (selectedValueX == 0) {
  395. eleTemp.setEleName(newStuff);
  396. } else if (selectedValueX == 1) {
  397. Float ftemp = Float.parseFloat(newStuff);
  398. eleTemp.setEnergy(ftemp);
  399. } else if (selectedValueX == 2) {
  400. Integer iTemp = Integer.parseInt(newStuff);
  401. eleTemp.setAmount(iTemp);
  402. }
  403. }
  404. refreshTableProperties();
  405. tableModelHolonElement.fireTableDataChanged();
  406. }
  407. } catch (Exception e) {
  408. }
  409. }
  410. });
  411. /***********************
  412. * HolonElement Properties Actions
  413. **********************/
  414. /*
  415. * If any value at the Properties Table enters to editing instance, than
  416. * is PropertyChangeListener triggered (For HolonObject, the only value
  417. * to be edited is the name and for CpsEdge the Max.flow)
  418. */
  419. tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
  420. @Override
  421. public void propertyChange(PropertyChangeEvent evt) {
  422. try {
  423. Object temp;
  424. if (getActualCps() != null) {
  425. if (getActualCps() instanceof HolonObject) {
  426. temp = tableModelProperties.getValueAt(0, 1);
  427. getActualCps().setName(temp.toString());
  428. } else if (getActualCps() instanceof HolonTransformer) {
  429. // get Info of the Properties for Transformer
  430. }
  431. } else {
  432. temp = tableModelProperties.getValueAt(2, 1);
  433. Float ftemp = Float.parseFloat(temp.toString());
  434. model.getSelectedEdge().setCapacity(ftemp);
  435. }
  436. canvas.repaint();
  437. } catch (Exception e) {
  438. }
  439. }
  440. });
  441. /***********************
  442. * HolonElement Graph Actions
  443. **********************/
  444. /*
  445. * Reset the graph of the selected HolonElement
  446. */
  447. resetGraphBtn.setBorder(new LineBorder(Color.BLACK));
  448. resetGraphBtn.addActionListener(new ActionListener() {
  449. public void actionPerformed(ActionEvent arg0) {
  450. unitGraph.reset();
  451. }
  452. });
  453. /*****************************
  454. * RIGHT CONTAINER DONE
  455. *****************************/
  456. frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
  457. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  458. @Override
  459. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  460. boolean leaf, int row, boolean hasFocus) {
  461. JLabel label = new JLabel();
  462. Image imgR = null;
  463. if (leaf) {
  464. for (Category cat : model.getCategories()) {
  465. for (CpsObject cps : cat.getObjects()) {
  466. if (value.toString().compareTo(cps.getObjName()) == 0) {
  467. File checkPath = new File(cps.getImage());
  468. if (checkPath.exists()) {
  469. imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  470. controller.getScale(), controller.getScale(), java.awt.Image.SCALE_SMOOTH);
  471. } else {
  472. imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  473. .getScaledInstance(controller.getScale(), controller.getScale(),
  474. java.awt.Image.SCALE_SMOOTH);
  475. }
  476. if (imgR != null) {
  477. label.setIcon(new ImageIcon(imgR));
  478. }
  479. label.setText(cps.getName());
  480. }
  481. }
  482. }
  483. }
  484. tree.setRowHeight(model.getScale());
  485. if (hasFocus) {
  486. label.setForeground(new Color(0, 0, 255));
  487. label.setOpaque(true);
  488. }
  489. if (label.getText().length() == 0) {
  490. label.setText(value.toString());
  491. if (value.toString().compareTo("Categories") != 0) {
  492. label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
  493. }
  494. }
  495. return label;
  496. }
  497. };
  498. tree.setCellRenderer(customRenderer);
  499. tree.addMouseListener(new MouseAdapter() {
  500. public void mouseReleased(MouseEvent e) {
  501. try {
  502. if (dragging) {
  503. int x = (int) canvas.getMousePosition().getX();
  504. int y = (int) canvas.getMousePosition().getY();
  505. CpsObject h = null;
  506. if (tempCps.getClass() == HolonObject.class) {
  507. h = new HolonObject(tempCps);
  508. }
  509. if (tempCps.getClass() == HolonSwitch.class) {
  510. h = new HolonSwitch(tempCps);
  511. }
  512. if (tempCps.getClass() == HolonTransformer.class) {
  513. h = new HolonTransformer(tempCps);
  514. }
  515. h.setPosition(x, y);
  516. controller.addObjectCanvas(h);
  517. canvas.repaint();
  518. dragging = false;
  519. }
  520. } catch (Exception e2) {
  521. }
  522. frmCyberPhysical.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  523. }
  524. });
  525. popmenuEdit.add(editItem);
  526. editItem.setEnabled(false);
  527. editItem.addActionListener(new ActionListener() {
  528. @Override
  529. public void actionPerformed(ActionEvent e) {
  530. }
  531. });
  532. tree.addMouseListener(new MouseAdapter() {
  533. public void mousePressed(MouseEvent e) {
  534. try {
  535. actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
  536. // if an Object was selected, the porperties are shown in
  537. // the table
  538. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
  539. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  540. if (selectedNode.getLevel() == 2) {
  541. CpsObject selected = controller.searchCategoryObject(selectedNode.getParent().toString(),
  542. selectedNode.toString());
  543. deleteRows();
  544. // if (selected instanceof HolonObject && selected !=
  545. // null) {
  546. // selected = (HolonObject) selected;
  547. // fillElementTable(((HolonObject)
  548. // selected).getElements());
  549. // }
  550. }
  551. if (SwingUtilities.isRightMouseButton(e)) {
  552. editItem.setEnabled(true);
  553. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  554. } else {
  555. for (Category cat : model.getCategories()) {
  556. for (CpsObject cps : cat.getObjects()) {
  557. if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
  558. File checkPath = new File(cps.getImage());
  559. if (checkPath.exists()) {
  560. img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
  561. controller.getScale(), controller.getScale(),
  562. java.awt.Image.SCALE_SMOOTH);
  563. } else {
  564. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
  565. .getScaledInstance(controller.getScale(), controller.getScale(),
  566. java.awt.Image.SCALE_SMOOTH);
  567. }
  568. tempCps = cps;
  569. dragging = true;
  570. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  571. "Image");
  572. frmCyberPhysical.setCursor(cursor);
  573. }
  574. }
  575. }
  576. }
  577. } catch (Exception e2) {
  578. }
  579. }
  580. });
  581. scrollPane_1.setViewportView(tree);
  582. scrollPane_1.setColumnHeaderView(panel);
  583. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  584. toolBar.setFloatable(false);
  585. panel.add(toolBar);
  586. toolBar.add(comboBox);
  587. comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Transformer", "Switch" }));
  588. // Add Button
  589. btnAdd.addActionListener(new ActionListener() {
  590. public void actionPerformed(ActionEvent arg0) {
  591. Object nodeInfo = tree.getLastSelectedPathComponent();
  592. String selectedOption = comboBox.getSelectedItem().toString();
  593. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  594. switch (selectedOption) {
  595. case "Category":
  596. String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
  597. if (catName.length() != 0) {
  598. controller.addCategory(catName);
  599. }
  600. break;
  601. case "Object":
  602. if (selectedNode == null) {
  603. JOptionPane.showMessageDialog(new JFrame(),
  604. "Please select a Category first before adding " + selectedOption + ".");
  605. }
  606. if (selectedNode.getLevel() == 1) {
  607. addObjectPopUP = new AddObjectPopUp();
  608. addObjectPopUP.setVisible(true);
  609. addObjectPopUP.setController(controller);
  610. addObjectPopUP.setCategory(selectedNode.toString());
  611. }
  612. break;
  613. default:
  614. addObjectAction(selectedOption, selectedNode);
  615. break;
  616. }
  617. tree.repaint();
  618. }
  619. });
  620. /**
  621. * Pop up - About Us with some important information about the
  622. * developers, source and programming stuff
  623. */
  624. aboutUs.addMouseListener(new MouseAdapter() {
  625. @Override
  626. public void mousePressed(MouseEvent e) {
  627. aboutUsPopUp = new AboutUsPopUp();
  628. aboutUsPopUp.setVisible(true);
  629. }
  630. });
  631. /**
  632. * Update of every interaction between the user and the canvas (only on
  633. * the canvas). Basically the update of all the information concerning
  634. * the clicked HolonObject
  635. */
  636. canvas.addMouseListener(new MouseAdapter() {
  637. @Override
  638. public void mousePressed(MouseEvent e) {
  639. if (temp == null || temp.getID() != model.getSelectedObjectID()) {
  640. unitGraph.empty();
  641. elementGraph.setText("None ");
  642. }
  643. temp = getActualCps();
  644. // Update of the Information about the HolonElements - only for
  645. // if (temp instanceof HolonObject) {
  646. // refreshTableHolonElement();
  647. // }
  648. // Update of the Information about the Properties - only for
  649. // CpsObjects
  650. // Erase old data
  651. if (tableModelProperties.getRowCount() > 0) {
  652. for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
  653. tableModelProperties.removeRow(i);
  654. }
  655. }
  656. // Write new data
  657. if (temp != null) {
  658. Object[] tempName = { "Name", temp.getName() };
  659. tableModelProperties.addRow(tempName);
  660. Object[] tempId = { "ID", temp.getID() };
  661. tableModelProperties.addRow(tempId);
  662. if (temp instanceof HolonObject) {
  663. refreshTableHolonElement();
  664. Object[] tempEnergy = { "Total Energy", ((HolonObject) temp).getCurrentEnergy() };
  665. tableModelProperties.addRow(tempEnergy);
  666. } else if (temp instanceof HolonTransformer) {
  667. deleteRows();
  668. Object[] tempRatioPerc = { "Ratio Type", true };
  669. tableModelProperties.addRow(tempRatioPerc);
  670. } else if (temp instanceof HolonSwitch) {
  671. deleteRows();
  672. Object[] tempActive = { "Active", ((HolonSwitch) temp).getActiveAt()[model.getCurIteration()] };
  673. tableModelProperties.addRow(tempActive);
  674. unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
  675. elementGraph.setText(temp.getName());
  676. } else {
  677. deleteRows();
  678. }
  679. tableModelProperties.setCellEditable(0, 1, true);
  680. tableModelProperties.setCellEditable(2, 1, false);
  681. ArrayList<CpsEdge> temp_array = temp.getConnections();
  682. if (!temp_array.isEmpty()) {
  683. boolean first = true;
  684. for (CpsEdge temp2 : temp_array) {
  685. if (first) {
  686. first = false;
  687. if (temp.getID() != temp2.getA().getID()) {
  688. Object[] tempConnection = { temp.getName() + " is connected to",
  689. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  690. tableModelProperties.addRow(tempConnection);
  691. } else {
  692. Object[] tempConnection = { temp.getName() + " is connected to",
  693. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  694. tableModelProperties.addRow(tempConnection);
  695. }
  696. } else {
  697. if (temp.getID() != temp2.getA().getID()) {
  698. Object[] tempConnection = { "",
  699. temp2.getA().getName() + " with ID: " + temp2.getA().getID() };
  700. tableModelProperties.addRow(tempConnection);
  701. } else {
  702. Object[] tempConnection = { "",
  703. temp2.getB().getName() + " with ID: " + temp2.getB().getID() };
  704. tableModelProperties.addRow(tempConnection);
  705. }
  706. }
  707. }
  708. }
  709. } else if (model.getSelectedEdge() != null) {
  710. Object[] tempName = { "Name", "Edge: " + model.getSelectedEdge().getA().getName() + " to "
  711. + model.getSelectedEdge().getB().getName() };
  712. tableModelProperties.addRow(tempName);
  713. Object[] tempFlow = { "Current flow", model.getSelectedEdge().getFlow() };
  714. tableModelProperties.addRow(tempFlow);
  715. Object[] tempCapacity = { "Max. Capacity", model.getSelectedEdge().getCapacity() };
  716. tableModelProperties.addRow(tempCapacity);
  717. tableModelProperties.setCellEditable(0, 1, false);
  718. tableModelProperties.setCellEditable(2, 1, true);
  719. } else if (getActualCps() == null) {
  720. deleteRows();
  721. }
  722. }
  723. });
  724. toolBar.add(btnAdd);
  725. // Del Button
  726. btnDel.addActionListener(new ActionListener() {
  727. public void actionPerformed(ActionEvent arg0) {
  728. Object nodeInfo = tree.getLastSelectedPathComponent();
  729. if (nodeInfo != null) {
  730. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  731. String nodeName = selectedNode.getUserObject().toString();
  732. int depthOfNode = selectedNode.getLevel();
  733. switch (depthOfNode) {
  734. case 1:
  735. int dialogResult = JOptionPane.showConfirmDialog(null,
  736. "Do you realy want to delete the Category " + nodeName + "?", "Warning",
  737. JOptionPane.YES_NO_OPTION);
  738. if (dialogResult == JOptionPane.YES_OPTION) {
  739. controller.deleteCategory(nodeName);
  740. }
  741. break;
  742. case 2:
  743. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  744. controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
  745. break;
  746. default:
  747. JOptionPane.showMessageDialog(new JFrame(),
  748. "Please select a Category or an Object in order to delete something.");
  749. }
  750. } else {
  751. JOptionPane.showMessageDialog(new JFrame(),
  752. "Please select a Category or an Object in order to delete something.");
  753. }
  754. tree.repaint();
  755. }
  756. });
  757. toolBar.add(btnDel);
  758. frmCyberPhysical.getContentPane().add(splitPane);
  759. mntmNew.addActionListener(new ActionListener() {
  760. @Override
  761. public void actionPerformed(ActionEvent arg0) {
  762. model.getEdgesOnCanvas().removeAll(model.getEdgesOnCanvas());
  763. model.getObjectsOnCanvas().removeAll(model.getObjectsOnCanvas());
  764. controller.setSelectedObjectID(0);
  765. controller.setSelecteEdge(null);
  766. controller.setCurIteration(0);
  767. unitGraph.empty();
  768. elementGraph.setText("None ");
  769. canvas.tempCps = null;
  770. canvas.objectSelectionHighlighting();
  771. canvas.repaint();
  772. }
  773. });
  774. mntmOpen.addActionListener(new java.awt.event.ActionListener() {
  775. @Override
  776. public void actionPerformed(java.awt.event.ActionEvent evt) {
  777. menuFileExitActionPerformed(evt);
  778. }
  779. private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
  780. JFileChooser fileChooser = new JFileChooser();
  781. JFrame test = new JFrame();
  782. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  783. fileChooser.setFileFilter(jsonFilter);
  784. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  785. File file = fileChooser.getSelectedFile();
  786. try {
  787. controller.loadFile(file.getAbsolutePath());
  788. canvas.repaint();
  789. tree.repaint();
  790. } catch (IOException e) {
  791. // TODO Auto-generated catch block
  792. e.printStackTrace();
  793. }
  794. }
  795. }
  796. });
  797. mntmSave.addActionListener(new java.awt.event.ActionListener() {
  798. @Override
  799. public void actionPerformed(java.awt.event.ActionEvent evt) {
  800. menuSaveActionPerformed(evt);
  801. }
  802. private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
  803. JFileChooser fileChooser = new JFileChooser();
  804. JFrame test = new JFrame();
  805. FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
  806. fileChooser.setFileFilter(jsonFilter);
  807. if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
  808. String file = fileChooser.getSelectedFile().getPath();
  809. if (!file.endsWith(".json"))
  810. file += ".json";
  811. try {
  812. controller.saveFile(new File(file).getAbsolutePath());
  813. } catch (IOException e) {
  814. // TODO Auto-generated catch block
  815. e.printStackTrace();
  816. }
  817. }
  818. }
  819. });
  820. timePanel = new TimePanel(model, controller);
  821. timePanel.setBorder(null);
  822. ((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
  823. @Override
  824. public void stateChanged(ChangeEvent e) {
  825. unitGraph.repaint();
  826. }
  827. });
  828. splitPane.setRightComponent(splitPane_1);
  829. splitPane.setDividerLocation(200);
  830. splitPane_1.setDividerLocation(500);
  831. splitPane.setLeftComponent(scrollPane_1);
  832. splitPane_1.setLeftComponent(tabbedPane);
  833. splitPane_1.setRightComponent(split_HolonEl_Pro);
  834. split_HolonEl_Pro.setDividerLocation(600);
  835. split_HolonEl_Pro.setTopComponent(split_Graph_HolonEl);
  836. split_HolonEl_Pro.setBottomComponent(scrollProperties);
  837. split_Graph_HolonEl.setDividerLocation(150);
  838. split_Graph_HolonEl.setTopComponent(scrollGraph);
  839. split_Graph_HolonEl.setBottomComponent(scrollElements);
  840. tabbedPane.setBorder(null);
  841. scrollProperties.setBorder(null);
  842. scrollGraph.setBorder(null);
  843. scrollElements.setBorder(null);
  844. splitPane.setBorder(null);
  845. splitPane_1.setBorder(null);
  846. split_HolonEl_Pro.setBorder(null);
  847. split_Graph_HolonEl.setBorder(null);
  848. scrollPane_2.setBorder(null);
  849. panel_HolonEl.setBorder(null);
  850. tableHolonElementScrollPane.setBorder(null);
  851. frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
  852. }
  853. /*
  854. * adds a specific object type to selected Category also handles input
  855. * windows and illegal inputs
  856. */
  857. public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
  858. if (selectedNode == null) {
  859. JOptionPane.showMessageDialog(new JFrame(),
  860. "Please select a Category first before adding " + objType + ".");
  861. }
  862. // if selected node is a directory for Categories
  863. else {
  864. if (selectedNode.getLevel() == 1) {
  865. String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
  866. Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
  867. if (objname.length() != 0) {
  868. switch (objType) {
  869. case "Switch":
  870. controller.addSwitch(cat, objname);
  871. break;
  872. case "Transformer":
  873. controller.addTransformer(cat, objname);
  874. break;
  875. }
  876. }
  877. } else {
  878. JOptionPane.showMessageDialog(new JFrame(),
  879. "Objects can not be added to Objects. Please select a Category.");
  880. }
  881. }
  882. }
  883. /**
  884. * reloads the Categories from Model
  885. */
  886. public void updateCategories(final ArrayList<Category> categories) {
  887. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  888. {
  889. DefaultMutableTreeNode node_1;
  890. for (Category c : categories) {
  891. node_1 = new DefaultMutableTreeNode(c.getName());
  892. // kann eventuell umgeändert werden
  893. for (CpsObject obj : c.getObjects()) {
  894. node_1.add(new DefaultMutableTreeNode(obj.getObjName()));
  895. }
  896. add(node_1);
  897. }
  898. }
  899. }));
  900. }
  901. /**
  902. *
  903. */
  904. public void onChange(ArrayList<Category> categories) {
  905. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  906. updateCategories(categories);
  907. model.reload();
  908. }
  909. /**
  910. *
  911. * @return
  912. */
  913. public JFrame getFrmCyberPhysical() {
  914. return frmCyberPhysical;
  915. }
  916. /**
  917. * Getter for selected CpsObject
  918. *
  919. * @return selected CpsObject
  920. */
  921. public CpsObject getActualCps() {
  922. int tempID = model.getSelectedObjectID();
  923. CpsObject tempCps = controller.searchByID(tempID);
  924. return tempCps;
  925. }
  926. /**
  927. * Search for actual selected HolonElement
  928. *
  929. * @param obj
  930. * selected HolonObject
  931. * @param yValue
  932. * Y-Coord in the HolonElementsTable
  933. * @return the selected HolonElement
  934. */
  935. public HolonElement getActualHolonElement(HolonObject obj, int yValue) {
  936. final int yTemp = (int) Math.floor(yValue / 16);
  937. int rowsTotal = tableModelHolonElement.getRowCount();
  938. if (rowsTotal != 0 && rowsTotal > yTemp) {
  939. model.setSelectedHolonElement(obj.getElements().get(yTemp));
  940. return obj.getElements().get(yTemp);
  941. } else {
  942. model.setSelectedHolonElement(null);
  943. return null;
  944. }
  945. }
  946. /**
  947. * Update the HolonElement Table, that means erase all rows and add the new
  948. * rows with new data
  949. */
  950. public void refreshTableHolonElement() {
  951. // Update of the Information about the HolonElements - only for
  952. // HolonObjects
  953. deleteRows();
  954. if (getActualCps() != null) {
  955. fillElementTable(((HolonObject) getActualCps()).getElements());
  956. }
  957. tableModelHolonElement.fireTableDataChanged();
  958. }
  959. /**
  960. * Erase all information of the HolonElement Model
  961. */
  962. public void deleteRows() {
  963. if (tableModelHolonElement.getRowCount() > 0) {
  964. for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
  965. tableModelHolonElement.removeRow(i);
  966. }
  967. }
  968. }
  969. /**
  970. * Add the Information of the given ArrayList in the HolonElement Model as
  971. * Name,Energy and Amount
  972. *
  973. * @param elements
  974. * ArrayList to be displayed
  975. */
  976. public void fillElementTable(ArrayList<HolonElement> elements) {
  977. for (HolonElement he : elements) {
  978. Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
  979. tableModelHolonElement.addRow(temp);
  980. }
  981. }
  982. /**
  983. * Update the information concerning properties of the selected CpsObject
  984. */
  985. public void refreshTableProperties() {
  986. CpsObject tempCps = getActualCps();
  987. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  988. tableModelProperties.removeRow(2);
  989. Object[] tempEnergy = { "Total Energy", ((HolonObject) tempCps).getCurrentEnergy() };
  990. tableModelProperties.insertRow(2, tempEnergy);
  991. } else if (tempCps instanceof HolonTransformer) {
  992. // Refresh Transformer Info
  993. }
  994. }
  995. private static void addPopup(Component component, final JPopupMenu popup) {
  996. component.addMouseListener(new MouseAdapter() {
  997. public void mousePressed(MouseEvent e) {
  998. if (e.isPopupTrigger()) {
  999. showMenu(e);
  1000. }
  1001. }
  1002. public void mouseReleased(MouseEvent e) {
  1003. if (e.isPopupTrigger()) {
  1004. showMenu(e);
  1005. }
  1006. }
  1007. private void showMenu(MouseEvent e) {
  1008. popup.show(e.getComponent(), e.getX(), e.getY());
  1009. }
  1010. });
  1011. }
  1012. }