GUI.java 66 KB

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