GUI.java 71 KB

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