GUI.java 74 KB

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