GUI.java 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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. }
  743. }
  744. refreshTableProperties();
  745. tableModelHolonElementSingle.fireTableDataChanged();
  746. controller.calculateStateForTimeStep(model.getCurIteration());
  747. unitGraph.repaint();
  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. }