GUI.java 83 KB

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