GUI.java 83 KB

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