GUI.java 83 KB

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