GUI.java 83 KB

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