GUI.java 83 KB

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