GUI.java 83 KB

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