GUI.java 83 KB

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