GUI.java 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. package holeg.ui.view.main;
  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.Image;
  8. import java.awt.Point;
  9. import java.awt.Rectangle;
  10. import java.awt.Toolkit;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ComponentAdapter;
  14. import java.awt.event.ComponentEvent;
  15. import java.awt.event.KeyAdapter;
  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.io.File;
  21. import java.net.URI;
  22. import java.util.Collection;
  23. import java.util.logging.Logger;
  24. import java.util.prefs.Preferences;
  25. import javax.swing.AbstractAction;
  26. import javax.swing.ActionMap;
  27. import javax.swing.BoxLayout;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.InputMap;
  30. import javax.swing.JButton;
  31. import javax.swing.JCheckBoxMenuItem;
  32. import javax.swing.JComponent;
  33. import javax.swing.JFileChooser;
  34. import javax.swing.JFrame;
  35. import javax.swing.JLabel;
  36. import javax.swing.JMenu;
  37. import javax.swing.JMenuBar;
  38. import javax.swing.JMenuItem;
  39. import javax.swing.JOptionPane;
  40. import javax.swing.JPanel;
  41. import javax.swing.JPopupMenu;
  42. import javax.swing.JScrollPane;
  43. import javax.swing.JSplitPane;
  44. import javax.swing.JTabbedPane;
  45. import javax.swing.JTextField;
  46. import javax.swing.JToolBar;
  47. import javax.swing.JTree;
  48. import javax.swing.KeyStroke;
  49. import javax.swing.SwingUtilities;
  50. import javax.swing.filechooser.FileNameExtensionFilter;
  51. import javax.swing.tree.DefaultMutableTreeNode;
  52. import javax.swing.tree.DefaultTreeModel;
  53. import javax.swing.tree.TreeCellRenderer;
  54. import holeg.interfaces.LocalMode;
  55. import holeg.model.AbstractCanvasObject;
  56. import holeg.model.GroupNode;
  57. import holeg.model.HolonObject;
  58. import holeg.model.HolonSwitch;
  59. import holeg.preferences.ColorPreference;
  60. import holeg.preferences.ImagePreference;
  61. import holeg.ui.controller.Control;
  62. import holeg.ui.model.GuiSettings;
  63. import holeg.ui.model.IdCounter;
  64. import holeg.model.Model;
  65. import holeg.model.Model.FairnessModel;
  66. import holeg.ui.view.canvas.Canvas;
  67. import holeg.ui.view.component.ButtonTabComponent;
  68. import holeg.ui.view.dialog.AboutUsPopUp;
  69. import holeg.ui.view.dialog.AddObjectPopUp;
  70. import holeg.ui.view.dialog.CanvasResizePopUp;
  71. import holeg.ui.view.dialog.CreateNewDialog;
  72. import holeg.ui.view.dialog.CreateNewDialog.Option;
  73. import holeg.ui.view.dialog.EditEdgesPopUp;
  74. import holeg.ui.view.information.HolonInformationPanel;
  75. import holeg.ui.view.inspector.Inspector;
  76. import holeg.ui.view.inspector.UnitGraph;
  77. import holeg.ui.view.window.AddOnWindow;
  78. import holeg.ui.view.window.FlexWindow;
  79. import holeg.ui.view.window.Outliner;
  80. import holeg.ui.view.image.Import;
  81. /**
  82. * Graphical User Interface.
  83. *
  84. * @author Gruppe14
  85. */
  86. public class GUI {
  87. private static final Logger log = Logger.getLogger(Model.class.getName());
  88. /**
  89. * Menu on the Top containing File, Edit View Help etc
  90. */
  91. private final JMenuBar menuBar = new JMenuBar();
  92. private final JMenu mnNewMenu = new JMenu("File");
  93. private final JMenu mnNewMenuEdit = new JMenu("Edit");
  94. private final JMenu mnNewMenuOptions = new JMenu("Options");
  95. private final JMenu mnNewMenuView = new JMenu("View");
  96. private final JMenu menuWindow = new JMenu("Window");
  97. /** Help Menu containing helpful Informations and the AboutUs Popup */
  98. private final JMenu mnHelp = new JMenu("Help");
  99. /**
  100. * Help -> Introduction A small Introduction of the Application, SmartGrids and
  101. * Holons
  102. */
  103. private final JMenuItem mntmIntroduction = new JMenuItem("Introduction");
  104. /**
  105. * Help -> UserManual
  106. */
  107. private final JMenuItem mntmUserManual = new JMenuItem("User Manual");
  108. /** Help -> Algorithm Help Menu */
  109. private final JMenuItem mntmAlgorithmHelp = new JMenuItem("Algorithm Introduction");
  110. /** Help -> CodeDocumentation */
  111. private final JMenuItem mntmCodeDoc = new JMenuItem("Code Documentation");
  112. /** Help -> AboutUs */
  113. private final JMenuItem mntmAboutUs = new JMenuItem("About Us");
  114. /** checked if supplyBars should be shown */
  115. private final JCheckBoxMenuItem showSupplyBarsCheckBox = new JCheckBoxMenuItem("Show supply bars.");
  116. /** menu for the different fairness Models */
  117. private final JMenu mnFairnessModel = new JMenu("Fairness Model");
  118. /** press to supply minimum demand first */
  119. private final JMenuItem mntmFairMinFirst = new JMenuItem("Minimum demand first");
  120. /** press to give everyone the same energy */
  121. private final JMenuItem mntmFairAlleEqual = new JMenuItem("Equal supply for everyone");
  122. private final JMenuItem mntmOpen = new JMenuItem("Open");
  123. private final JMenuItem mntmNew = new JMenuItem("New");
  124. private final JMenuItem mntmSave = new JMenuItem("Save");
  125. private final JMenuItem mntmCanvasSize = new JMenuItem("Set View Size");
  126. private final JSplitPane splitPane = new JSplitPane();
  127. private final JSplitPane splitPane1 = new JSplitPane();
  128. // the tabbed canvas containing the different sub-net tabs of the grid (Main
  129. // Grid + Nodes of Nodes)
  130. private final JPanel myPanel = new JPanel(new BorderLayout());
  131. private final JTabbedPane tabbedPaneInnerOriginal = new JTabbedPane(JTabbedPane.TOP);
  132. // the main canvas where we can see the grid currently displayed
  133. private final JScrollPane canvasSP = new JScrollPane();
  134. private final JScrollPane scrollPane1 = new JScrollPane();
  135. // private final JScrollPane holonSP = new JScrollPane();
  136. // the original tabbed Pane (containing tabs for view, statistics, holon,
  137. // flexibility)
  138. private final JTabbedPane tabbedPaneOriginal = new JTabbedPane(JTabbedPane.TOP);
  139. private final JPopupMenu popmenuEdit = new JPopupMenu();
  140. private final JMenuItem editItem = new JMenuItem("Edit Object");
  141. private final JLabel elementGraph = new JLabel("None ");
  142. private final JTree categoryTree = new JTree();
  143. /******************************************
  144. ************* Right Container*************
  145. ******************************************
  146. * Right Container: here comes the information about the HolonObject, such as
  147. * HolonElements Information, Properties and Consumption/Production graph.
  148. **/
  149. private final Inspector inspector;
  150. private final HolonInformationPanel informationPanel;
  151. private final JSplitPane splitHolonElPro = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  152. // Prechoosed local Periods
  153. private final JScrollPane scrollProperties = new JScrollPane();
  154. // In this section is the graph for the selected HolonElement of the clicked
  155. private final Model model;
  156. private final Control control;
  157. // In this section are all the Holonelements that correspond to the clicked
  158. // HolonObject with consumption/production, name and amount.
  159. private final JPanel panel = new JPanel();
  160. private final JPanel panelHolonEl = new JPanel();
  161. // Buttons
  162. private final JButton btnAdd = new JButton();
  163. private final JPopupMenu btnAddPopUp = new JPopupMenu("Newacac");
  164. private final JMenuItem mItemNew = new JMenuItem("New..");
  165. private final JMenuItem mItemCategory = new JMenuItem("Category");
  166. private final JMenuItem mItemObject = new JMenuItem("Object");
  167. private final JMenuItem mItemSwitch = new JMenuItem("Switch");
  168. private final JButton btnDel = new JButton();
  169. private final JToolBar toolBar = new JToolBar();
  170. private final JToolBar toolBarHolonEl = new JToolBar();
  171. // Languages
  172. private Canvas canvas;
  173. private final UnitGraph unitGraph;
  174. /** Textfield to show the period of an element */
  175. private final JTextField unitGraphLocalPeriod = new JTextField(6);
  176. private final JMenuItem mntmUndo = new JMenuItem("Undo");
  177. private final JMenuItem mntmRedo = new JMenuItem("Redo");
  178. private final JMenuItem mntmEditEdges = new JMenuItem("Edge Properties");
  179. private final JMenuItem mntmAlignAll = new JMenuItem("Align All");
  180. private final JMenuItem mntmResetCategory = new JMenuItem("Reset Categories");
  181. // TODO(Tom2021-12-1) make GUI a JFRAME and remove holegJFrame
  182. private JFrame holegJFrame;
  183. // tabbedPaneOriginal or tabbedPaneSplit
  184. private JTabbedPane tabTemp;
  185. private String catOfObjToBeEdited;
  186. private Canvas unc;
  187. private JPanel contentPane;
  188. // Pop up Windows
  189. private AddObjectPopUp addObjectPopUP;
  190. private AboutUsPopUp aboutUsPopUp;
  191. // variables
  192. private boolean dragging = false;
  193. private String actualObjectClicked;
  194. private Image img = null;
  195. private AbstractCanvasObject tempCps = null;
  196. // Time Stuff
  197. private TimePanel timePanel;
  198. public TimePanel getTimePanel() {
  199. return timePanel;
  200. }
  201. private AbstractCanvasObject temp = null;
  202. private String warningText = "Warning";
  203. private String saveBeforeNew = "Do you want to save your current data?";
  204. private String eraseCategory = "Do you really want to delete the Category ";
  205. private String selectObjBeforeErase = "Please select a Category or an Object in the left library in order to delete something.";
  206. private JMenuItem removeItem = new JMenuItem("Remove");
  207. // Save / Load
  208. private final JFileChooser safeLoadFileChooser = initSaveLoadFileChooser();
  209. /**
  210. * Create the application.
  211. *
  212. * @param control the Controller
  213. */
  214. GUI(Control control) {
  215. this.control = control;
  216. this.informationPanel = new HolonInformationPanel(control);
  217. this.model = control.getModel();
  218. inspector = new Inspector(control);
  219. control.calculateStateAndVisualForCurrentTimeStep();
  220. this.unitGraph = new UnitGraph(control);
  221. this.canvas = new Canvas(control, model.getCanvas());
  222. initialize();
  223. updateCategories(GuiSettings.getCategories());
  224. control.OnCategoryChanged.addListener(() -> this.updateCategoryUI(GuiSettings.getCategories()));
  225. this.unc = this.canvas;
  226. }
  227. /**
  228. * Initialize the contents of the frame.
  229. */
  230. private void initialize() {
  231. holegJFrame = new JFrame();
  232. holegJFrame.setTitle("HOLEG Simulator");
  233. holegJFrame.setBounds(new Rectangle(1200, 800));
  234. //Center
  235. holegJFrame.setLocationRelativeTo(null);
  236. holegJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  237. holegJFrame.addWindowListener(new java.awt.event.WindowAdapter() {
  238. @Override
  239. public void windowClosing(java.awt.event.WindowEvent windowEvent) {
  240. if (JOptionPane.showConfirmDialog(holegJFrame, "Are you sure you want to exit?", "HOLEG",
  241. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
  242. //TODO(Tom2022-01-27):
  243. System.exit(0);
  244. }
  245. }
  246. });
  247. contentPane = (JPanel) holegJFrame.getContentPane();
  248. int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
  249. InputMap inputMap = contentPane.getInputMap(condition);
  250. ActionMap actionMap = contentPane.getActionMap();
  251. String cntrlZDown = "controlZ";
  252. inputMap.put(KeyStroke.getKeyStroke("control Z"), cntrlZDown);
  253. actionMap.put(cntrlZDown, new AbstractAction() {
  254. @Override
  255. public void actionPerformed(ActionEvent e) {
  256. //TODO(Tom2022-01-27): CtrlZ
  257. }
  258. });
  259. String cntrlYDown = "controlY";
  260. inputMap.put(KeyStroke.getKeyStroke("control Y"), cntrlYDown);
  261. actionMap.put(cntrlYDown, new AbstractAction() {
  262. @Override
  263. public void actionPerformed(ActionEvent e) {
  264. //TODO Ctrl Y
  265. }
  266. });
  267. String cntrlADown = "controlA";
  268. inputMap.put(KeyStroke.getKeyStroke("control A"), cntrlADown);
  269. AbstractAction controlA = new AbstractAction() {
  270. @Override
  271. public void actionPerformed(ActionEvent e) {
  272. GuiSettings.getSelectedObjects().clear();
  273. //TODO(Tom2022-01-27): Ctrl A
  274. }
  275. };
  276. actionMap.put(cntrlADown, controlA);
  277. String delDown = "delete";
  278. inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false), delDown);
  279. actionMap.put(delDown, new AbstractAction() {
  280. private static final long serialVersionUID = 1L;
  281. @Override
  282. public void actionPerformed(ActionEvent e) {
  283. chooseTabTemp();
  284. //TODO(Tom2022-01-27): delete
  285. GuiSettings.getSelectedObjects().clear();
  286. }
  287. });
  288. String cntrlCDown = "controlC";
  289. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  290. AbstractAction controlC = new AbstractAction() {
  291. private static final long serialVersionUID = 1L;
  292. @Override
  293. public void actionPerformed(ActionEvent e) {
  294. chooseTabTemp();
  295. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  296. if (!GuiSettings.getSelectedObjects().isEmpty()) {
  297. if (scrollPane.getViewport().getComponent(0)instanceof Canvas groupNodeCanvas)
  298. control.copy(groupNodeCanvas.getGroupNode());
  299. else
  300. control.copy(null);
  301. if (!GuiSettings.getClipboardObjects().isEmpty()) {
  302. //TODO(Tom2022-01-14): old code changes itemPaste
  303. //OLD: canvas.itemPaste.setEnabled(true);
  304. }
  305. }
  306. }
  307. };
  308. actionMap.put(cntrlCDown, controlC);
  309. String cntrlVDown = "controlV";
  310. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  311. AbstractAction controlV = new AbstractAction() {
  312. private static final long serialVersionUID = 1L;
  313. @Override
  314. public void actionPerformed(ActionEvent e) {
  315. //TODO(Tom2022-01-27): Paste
  316. }
  317. };
  318. actionMap.put(cntrlVDown, controlV);
  319. String cntrlXDown = "controlX";
  320. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  321. AbstractAction controlX = new AbstractAction() {
  322. private static final long serialVersionUID = 1L;
  323. @Override
  324. public void actionPerformed(ActionEvent e) {
  325. chooseTabTemp();
  326. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  327. if (!GuiSettings.getSelectedObjects().isEmpty()) {
  328. if (scrollPane.getViewport().getComponent(0)instanceof Canvas groupNodeCanvas) {
  329. control.cut(groupNodeCanvas.getGroupNode());
  330. control.calculateStateAndVisualForCurrentTimeStep();
  331. scrollPane.getViewport().getComponent(0).repaint();
  332. } else {
  333. control.cut(null);
  334. control.calculateStateAndVisualForCurrentTimeStep();
  335. log.info("canvas.repaint3");
  336. canvas.repaint();
  337. }
  338. if (!GuiSettings.getClipboardObjects().isEmpty()) {
  339. //TODO(Tom2022-01-14): old code changes itemPaste
  340. //OLD: canvas.itemPaste.setEnabled(true);
  341. }
  342. }
  343. }
  344. };
  345. actionMap.put(cntrlXDown, controlX);
  346. holegJFrame.setJMenuBar(menuBar);
  347. holegJFrame.setIconImage(Import.loadImage(ImagePreference.Logo, 30, 30));
  348. menuBar.add(mnNewMenu);
  349. mnNewMenu.add(mntmNew);
  350. mnNewMenu.add(mntmOpen);
  351. mnNewMenu.add(mntmSave);
  352. menuBar.add(mnNewMenuEdit);
  353. mnNewMenuEdit.add(mntmUndo);
  354. mnNewMenuEdit.add(mntmRedo);
  355. mnNewMenuEdit.add(mntmEditEdges);
  356. mntmEditEdges.addActionListener(actionEvent -> {
  357. EditEdgesPopUp edgePopUp = new EditEdgesPopUp(holegJFrame);
  358. edgePopUp.setCanvas(canvas);
  359. edgePopUp.setController(control);
  360. edgePopUp.setVisible(true);
  361. });
  362. // Edit -> Align All
  363. mnNewMenuEdit.add(mntmAlignAll);
  364. mntmAlignAll.addActionListener(actionEvent -> {
  365. // getScrollPaneFromTabbedPane().getViewport().getComponent(0) is always the
  366. // active canvas
  367. tryToAlignObjects();
  368. });
  369. menuBar.add(mnNewMenuOptions);
  370. mnNewMenuOptions.add(mntmResetCategory);
  371. mntmResetCategory.addActionListener(actionEvent -> {
  372. control.resetCategories();
  373. categoryTree.revalidate();
  374. categoryTree.repaint();
  375. });
  376. /**
  377. * Add Fairness Model Option to the option Menu
  378. */
  379. mnNewMenuOptions.add(mnFairnessModel);
  380. mnFairnessModel.add(mntmFairMinFirst);
  381. mntmFairMinFirst.setForeground(Color.BLUE);
  382. mntmFairMinFirst
  383. .setToolTipText("HolonObjects with the smallest mininum Demand will be partially supplied first.\n"
  384. + "After that as many HolonObjects as possible will get fully supplied.");
  385. mntmFairMinFirst.addActionListener(arg0 -> {
  386. control.getModel().setFairnessModel(FairnessModel.MininumDemandFirst);
  387. mntmFairMinFirst.setForeground(Color.BLUE);
  388. mntmFairAlleEqual.setForeground(mnFairnessModel.getForeground());
  389. control.calculateStateAndVisualForCurrentTimeStep();
  390. // Update UpperNodes
  391. Component canvasOrUpperNodeCanvas = getScrollPaneFromTabbedPane().getViewport().getComponent(0);
  392. if (canvasOrUpperNodeCanvas != null && canvasOrUpperNodeCanvas instanceof Canvas groupNodeCanvas) {
  393. groupNodeCanvas.repaint();
  394. }
  395. });
  396. mnFairnessModel.add(mntmFairAlleEqual);
  397. mntmFairAlleEqual.setToolTipText("HolonObjects will all get the same amount of energy.");
  398. mntmFairAlleEqual.addActionListener(arg0 -> {
  399. control.getModel().setFairnessModel(FairnessModel.AllEqual);
  400. mntmFairAlleEqual.setForeground(Color.BLUE);
  401. mntmFairMinFirst.setForeground(mnFairnessModel.getForeground());
  402. control.calculateStateAndVisualForCurrentTimeStep();
  403. // Update UpperNodes
  404. Component canvasOrUpperNodeCanvas = getScrollPaneFromTabbedPane().getViewport().getComponent(0);
  405. if (canvasOrUpperNodeCanvas != null && canvasOrUpperNodeCanvas instanceof Canvas groupNodeCanvas) {
  406. groupNodeCanvas.repaint();
  407. }
  408. });
  409. menuBar.add(mnNewMenuView);
  410. mnNewMenuView.add(mntmCanvasSize);
  411. mntmCanvasSize.addActionListener(actionEvent -> {
  412. CanvasResizePopUp popUp = new CanvasResizePopUp(model, control, canvas, this.tabbedPaneInnerOriginal,
  413. holegJFrame);
  414. popUp.setVisible(true);
  415. });
  416. tabbedPaneInnerOriginal.addChangeListener(change -> {
  417. control.clearSelection();
  418. });
  419. mnNewMenuView.add(mntmCanvasSize);
  420. /*
  421. * Adds Checkbox to turn supply bars on/off
  422. */
  423. mnNewMenuView.add(showSupplyBarsCheckBox);
  424. showSupplyBarsCheckBox.setSelected(true);
  425. showSupplyBarsCheckBox.addActionListener(arg0 -> {
  426. GuiSettings.showSupplyBars = showSupplyBarsCheckBox.isSelected();
  427. log.info("canvas.repaint4");
  428. canvas.repaint();
  429. // Update UpperNodes
  430. JScrollPane spane = getScrollPaneFromTabbedPane();
  431. if (spane != null) {
  432. Component canvasOrUpperNodeCanvas = spane.getViewport().getComponent(0);
  433. if (canvasOrUpperNodeCanvas != null
  434. && canvasOrUpperNodeCanvas instanceof Canvas groupNodeCanvas) {
  435. groupNodeCanvas.repaint();
  436. }
  437. }
  438. });
  439. initWindowMenu();
  440. /**
  441. * add Help Menu and its items
  442. */
  443. menuBar.add(mnHelp);
  444. mnHelp.add(mntmIntroduction);
  445. mnHelp.add(mntmUserManual);
  446. mnHelp.add(mntmAlgorithmHelp);
  447. mnHelp.add(mntmCodeDoc);
  448. mnHelp.add(mntmAboutUs);
  449. canvas.setBackground(Color.WHITE);
  450. canvas.setPreferredSize(new Dimension(GuiSettings.canvasSize.getX(), GuiSettings.canvasSize.getY()));
  451. /********************
  452. * RIGHT CONTAINER (INFORMATION)
  453. **********************/
  454. panelHolonEl.setLayout(new BoxLayout(panelHolonEl, BoxLayout.X_AXIS));
  455. toolBarHolonEl.setFloatable(false);
  456. panelHolonEl.add(toolBarHolonEl);
  457. /***********************
  458. * HolonElement Graph Actions
  459. **********************/
  460. /*
  461. * Update Local Period of an Element Graph
  462. */
  463. unitGraphLocalPeriod.addKeyListener(new KeyAdapter() {
  464. @Override
  465. public void keyReleased(KeyEvent e) {
  466. try {
  467. int localLength = Integer.parseInt(unitGraphLocalPeriod.getText());
  468. unitGraphLocalPeriod.setBackground(Color.WHITE);
  469. /**
  470. * set local graph Period
  471. */
  472. if (e.getKeyCode() == KeyEvent.VK_ENTER){
  473. LocalMode.Period period = new LocalMode.Period(localLength);
  474. period.setInterval(localLength);
  475. unitGraph.setPeriod(period);
  476. }
  477. } catch (NumberFormatException ex) {
  478. unitGraphLocalPeriod.setBackground(ColorPreference.GUI.PALE_RED);
  479. }
  480. }
  481. });
  482. /*****************************
  483. * RIGHT CONTAINER DONE
  484. *****************************/
  485. holegJFrame.getContentPane().setLayout(new BorderLayout(0, 0));
  486. /****************
  487. * Tree Stuff
  488. ****************/
  489. // Override Key Actions
  490. inputMap = categoryTree.getInputMap();
  491. inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
  492. inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
  493. inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
  494. inputMap.put(KeyStroke.getKeyStroke("control A"), cntrlADown);
  495. TreeCellRenderer customRenderer = new TreeCellRenderer() {
  496. @Override
  497. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
  498. boolean leaf, int row, boolean hasFocus) {
  499. JLabel label = new JLabel();
  500. Image imgR;
  501. if (leaf) {
  502. for (Category cat : GuiSettings.getCategories()) {
  503. for (AbstractCanvasObject cps : cat.getObjects()) {
  504. if (value.toString().equals(cps.getName())) {
  505. imgR = Import.loadImage(cps.getImagePath(), 50, 50);
  506. if (imgR != null) {
  507. label.setIcon(new ImageIcon(imgR));
  508. }
  509. label.setText(cps.getName());
  510. }
  511. }
  512. }
  513. }
  514. tree.setRowHeight(50);
  515. if (hasFocus) {
  516. label.setForeground(ColorPreference.Category.Focus);
  517. label.setOpaque(true);
  518. }
  519. if (label.getText().isEmpty()) {
  520. label.setText(value.toString());
  521. if (!value.toString().equals("Categories")) {
  522. label.setIcon(new ImageIcon(Import.loadImage(ImagePreference.Category.Folder)));
  523. }
  524. }
  525. return label;
  526. }
  527. };
  528. categoryTree.setCellRenderer(customRenderer);
  529. categoryTree.addMouseMotionListener(new MouseMotionAdapter() {
  530. public void mouseDragged(MouseEvent e) {
  531. checkForDragAndDrop(e);
  532. }
  533. /**
  534. * checks if an object of the current Panel could be replaced by the dragged
  535. * object
  536. *
  537. * @param e
  538. */
  539. private void checkForDragAndDrop(MouseEvent e) {
  540. try {
  541. /**
  542. * if no object gets dragged -> finished
  543. */
  544. if (!dragging)
  545. return;
  546. /**
  547. * select the current Panel
  548. */
  549. chooseTabTemp();
  550. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  551. if (scrollPane == null)
  552. return;
  553. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  554. /**
  555. * check for replacements on the canvas
  556. */
  557. if (canvasOrUpperNodeCanvas instanceof Canvas groupNodeCanvas) {
  558. if (unc.getMousePosition() == null)
  559. return;
  560. int x = (int) unc.getMousePosition().getX() + 16;
  561. int y = (int) unc.getMousePosition().getY() + 16;
  562. /**
  563. * check for replacement
  564. */
  565. groupNodeCanvas.checkForReplacement(x, y);
  566. /**
  567. * repaint
  568. */
  569. unc.invalidate();
  570. unc.repaint();
  571. } else {
  572. if (canvas.getMousePosition() == null)
  573. return;
  574. int x = (int) canvas.getMousePosition().getX() + 16;
  575. int y = (int) canvas.getMousePosition().getY() + 16;
  576. /**
  577. * check for replacement
  578. */
  579. canvas.checkForReplacement(x, y);
  580. /**
  581. * repaint
  582. */
  583. log.info("canvas.repaint5");
  584. canvas.repaint();
  585. }
  586. contentPane.updateUI();
  587. } catch (Exception eex) {
  588. eex.printStackTrace();
  589. }
  590. }
  591. });
  592. categoryTree.addMouseListener(new MouseAdapter() {
  593. public void mouseReleased(MouseEvent e) {
  594. try {
  595. if (dragging) {
  596. chooseTabTemp();
  597. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  598. Component canvasOrUpperNodeCanvas = scrollPane.getViewport().getComponent(0);
  599. if (canvasOrUpperNodeCanvas instanceof Canvas groupNodeCanvas) {
  600. int x = (int) groupNodeCanvas.getMousePosition().getX() + 16;
  601. int y = (int) groupNodeCanvas.getMousePosition().getY() + 16;
  602. AbstractCanvasObject h = null;
  603. if (tempCps instanceof HolonObject hO) {
  604. h = new HolonObject(hO);
  605. }
  606. if (tempCps instanceof HolonSwitch sw) {
  607. h = new HolonSwitch(sw);
  608. }
  609. h.setPosition(x, y);
  610. control.addObjectCanvas(model.getCanvas(), h);
  611. /**
  612. * object would be replaced
  613. */
  614. groupNodeCanvas.invalidate();
  615. control.calculateStateAndVisualForCurrentTimeStep();
  616. groupNodeCanvas.repaint();
  617. } else {
  618. int x = (int) canvas.getMousePosition().getX() + 16;
  619. int y = (int) canvas.getMousePosition().getY() + 16;
  620. AbstractCanvasObject h = null;
  621. if (tempCps instanceof HolonObject hO) {
  622. h = new HolonObject(hO);
  623. }
  624. if (tempCps instanceof HolonSwitch sw) {
  625. h = new HolonSwitch(sw);
  626. }
  627. h.setPosition(x, y);
  628. /**
  629. * close UpperNodeTabs of replaced UpperNode
  630. */
  631. //TODO(Tom2022-01-27):
  632. control.addObjectCanvas(model.getCanvas(), h);
  633. /**
  634. * no object should get replaced
  635. */
  636. log.info("canvas.repaint6");
  637. canvas.repaint();
  638. }
  639. control.calculateStateAndVisualForCurrentTimeStep();
  640. contentPane.updateUI();
  641. dragging = false;
  642. }
  643. } catch (Exception eex) {
  644. }
  645. holegJFrame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  646. }
  647. });
  648. popmenuEdit.add(editItem);
  649. popmenuEdit.add(removeItem);
  650. editItem.setEnabled(false);
  651. editItem.addActionListener(actionEvent -> {
  652. });
  653. categoryTree.addMouseListener(new MouseAdapter() {
  654. public void mousePressed(MouseEvent e) {
  655. try {
  656. actualObjectClicked = categoryTree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent()
  657. .toString();
  658. // if an Object was selected, the porperties are shown in
  659. // the table
  660. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) categoryTree
  661. .getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
  662. if (SwingUtilities.isRightMouseButton(e)) {
  663. for (Category cat : GuiSettings.getCategories()) {
  664. for (AbstractCanvasObject cps : cat.getObjects()) {
  665. if (actualObjectClicked.equals(cps.getName())
  666. && !(cps instanceof HolonSwitch)) {
  667. editItem.setEnabled(true);
  668. popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
  669. catOfObjToBeEdited = selectedNode.getParent().toString();
  670. tempCps = cps;
  671. }
  672. }
  673. }
  674. } else {
  675. for (Category cat : GuiSettings.getCategories()) {
  676. for (AbstractCanvasObject cps : cat.getObjects()) {
  677. if (actualObjectClicked.equals(cps.getName())) {
  678. File checkPath = new File(cps.getImagePath());
  679. if (checkPath.exists()) {
  680. img = new ImageIcon(cps.getImagePath()).getImage().getScaledInstance(32, 32,
  681. java.awt.Image.SCALE_SMOOTH);
  682. } else {
  683. img = Import.loadImage(cps.getImagePath(), 32, 32);
  684. }
  685. tempCps = cps;
  686. dragging = true;
  687. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
  688. "Image");
  689. holegJFrame.setCursor(cursor);
  690. }
  691. }
  692. }
  693. }
  694. } catch (Exception eex) {
  695. }
  696. }
  697. });
  698. editItem.addActionListener(actionEvent -> {
  699. // Remove the selected Object object
  700. // AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame
  701. // parentFrame)
  702. System.out.println("Edit");
  703. addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited, holegJFrame);
  704. addObjectPopUP.setCategory(catOfObjToBeEdited);
  705. addObjectPopUP.setController(control);
  706. addObjectPopUP.setVisible(true);
  707. });
  708. removeItem.addActionListener(actionEvent -> {
  709. // Remove the selected Object object
  710. log.info("catOfObjToBeEdited:" + catOfObjToBeEdited + ", tempCps:" + tempCps);
  711. control.findCategoryWithName(catOfObjToBeEdited).ifPresent(cat -> {
  712. cat.removeObjectsWithName(tempCps.getName());
  713. });
  714. });
  715. scrollPane1.setViewportView(categoryTree);
  716. scrollPane1.setColumnHeaderView(panel);
  717. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  718. toolBar.setAlignmentX(Component.LEFT_ALIGNMENT);
  719. toolBar.setFloatable(false);
  720. panel.add(toolBar);
  721. btnAddPopUp.add(mItemNew);
  722. mItemNew.addActionListener(actionEvent -> {
  723. new CreateNewDialog(control, holegJFrame);
  724. });
  725. btnAddPopUp.addSeparator();
  726. btnAddPopUp.add(mItemCategory);
  727. mItemCategory.addActionListener(actionEvent -> {
  728. new CreateNewDialog(control, Option.Category, holegJFrame);
  729. });
  730. btnAddPopUp.add(mItemObject);
  731. mItemObject.addActionListener(actionEvent -> {
  732. new CreateNewDialog(control, Option.Object, holegJFrame);
  733. });
  734. btnAddPopUp.add(mItemSwitch);
  735. mItemSwitch.addActionListener(actionEvent -> {
  736. new CreateNewDialog(control, Option.Switch, holegJFrame);
  737. });
  738. btnAdd.addActionListener(actionEvent -> btnAddPopUp.show(btnAdd, -1, +20));
  739. btnAdd.setIcon(new ImageIcon(Import.loadImage("images/buttons/plus.png", 16, 16)));
  740. btnAdd.setToolTipText("<html><b>New</b><br>Add a new Category or Item to the library.</html>");
  741. toolBar.add(btnAdd);
  742. /**
  743. * Help Menu Action Listeners
  744. */
  745. /**
  746. * URL of the telecommunication git Wiki
  747. */
  748. String tkWikiWebpage = "https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons/wiki/";
  749. /** open Introduction Web Page */
  750. mntmIntroduction.addMouseListener(new MouseAdapter() {
  751. @Override
  752. public void mousePressed(MouseEvent e) {
  753. openWebpage(tkWikiWebpage + "Introduction+V2.1");
  754. }
  755. });
  756. /** open UserManual WebPage */
  757. mntmUserManual.addMouseListener(new MouseAdapter() {
  758. @Override
  759. public void mousePressed(MouseEvent e) {
  760. openWebpage(tkWikiWebpage + "User+Manual+V2.1");
  761. }
  762. });
  763. /** open Algorithm Help Web Page */
  764. mntmAlgorithmHelp.addMouseListener(new MouseAdapter() {
  765. @Override
  766. public void mousePressed(MouseEvent e) {
  767. openWebpage(tkWikiWebpage + "Algorithms+V2.1");
  768. }
  769. });
  770. /** open Code Documentation Web Page */
  771. mntmCodeDoc.addMouseListener(new MouseAdapter() {
  772. @Override
  773. public void mousePressed(MouseEvent e) {
  774. openWebpage(tkWikiWebpage + "Code+documentation+V2.1");
  775. }
  776. });
  777. /**
  778. * Pop up - About Us with some important information about the developers,
  779. * source and programming stuff
  780. */
  781. mntmAboutUs.addMouseListener(new MouseAdapter() {
  782. @Override
  783. public void mousePressed(MouseEvent e) {
  784. aboutUsPopUp = new AboutUsPopUp(holegJFrame);
  785. aboutUsPopUp.setVisible(true);
  786. }
  787. });
  788. // Del Button
  789. btnDel.addActionListener(actionEvent -> {
  790. Object nodeInfo = categoryTree.getLastSelectedPathComponent();
  791. if (nodeInfo != null) {
  792. DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
  793. String nodeName = selectedNode.getUserObject().toString();
  794. int depthOfNode = selectedNode.getLevel();
  795. try {
  796. switch (depthOfNode) {
  797. case 1:
  798. int dialogResult = JOptionPane.showConfirmDialog(holegJFrame, eraseCategory + nodeName + "?",
  799. warningText, JOptionPane.YES_NO_OPTION);
  800. if (dialogResult == JOptionPane.YES_OPTION) {
  801. GuiSettings.getCategories().stream().filter(cat -> cat.getName() == nodeName).findAny()
  802. .ifPresent(cat -> {
  803. control.deleteCategory(cat);
  804. });
  805. }
  806. break;
  807. case 2:
  808. DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
  809. control.findCategoryWithName(parent.getUserObject().toString()).ifPresent(cat -> {
  810. cat.removeObjectsWithName(nodeName);
  811. });
  812. break;
  813. default:
  814. JOptionPane.showMessageDialog(holegJFrame, selectObjBeforeErase);
  815. }
  816. } catch (Exception e2) {
  817. }
  818. } else {
  819. JOptionPane.showMessageDialog(holegJFrame, selectObjBeforeErase);
  820. }
  821. categoryTree.repaint();
  822. });
  823. btnDel.setIcon(new ImageIcon(Import.loadImage("images/buttons/minus.png", 16, 16)));
  824. btnDel.setToolTipText("<html><b>Delete</b><br>Removes a Category or a Category Item.</html>");
  825. toolBar.add(btnDel);
  826. holegJFrame.getContentPane().add(splitPane);
  827. mntmNew.addActionListener(actionEvent -> {
  828. if (model.getCanvas().getObjectsInThisLayer().findAny().isPresent()) {
  829. int newWarning = JOptionPane.YES_NO_OPTION;
  830. int dialogForNewWarning = JOptionPane.showConfirmDialog(holegJFrame, saveBeforeNew, warningText,
  831. newWarning);
  832. if (dialogForNewWarning == JOptionPane.YES_OPTION) {
  833. mntmSave.doClick();
  834. }
  835. }
  836. // Remove all but main tab
  837. while (tabbedPaneInnerOriginal.getTabCount() > 1) {
  838. tabbedPaneInnerOriginal.remove(1);
  839. }
  840. control.clearSelection();
  841. model.clear();
  842. control.OnSelectionChanged.broadcast();
  843. GuiSettings.getSelectedEdges().clear();
  844. control.getModel().setCurrentIteration(0);
  845. elementGraph.setText("None");
  846. log.info("canvas.repaint7");
  847. canvas.repaint();
  848. IdCounter.reset();
  849. IdCounter.reset();
  850. control.calculateStateAndVisualForCurrentTimeStep();
  851. });
  852. mntmOpen.addActionListener(clicked -> {
  853. if(safeLoadFileChooser.showOpenDialog(this.holegJFrame) == JFileChooser.APPROVE_OPTION){
  854. control.loadFile(safeLoadFileChooser.getSelectedFile());
  855. //TODO(Tom2022-01-27): make better
  856. this.canvas.setGroupNode(control.getModel().getCanvas());
  857. this.canvas.repaint();
  858. }
  859. });
  860. mntmSave.addActionListener(clicked -> {
  861. log.info("Save Button Pressed");
  862. if(safeLoadFileChooser.showSaveDialog(this.holegJFrame) == JFileChooser.APPROVE_OPTION){
  863. String path = safeLoadFileChooser.getSelectedFile().getPath();
  864. if(!path.endsWith(".json")){
  865. path += ".json";
  866. }
  867. control.saveFile(new File(path));
  868. }
  869. });
  870. mntmUndo.addActionListener(new ActionListener() {
  871. @Override
  872. public void actionPerformed(ActionEvent evt) {
  873. menuUndoActionPerformed();
  874. }
  875. private void menuUndoActionPerformed() {
  876. // try {
  877. // control.loadAutoSave(control.getUndoSave());
  878. //
  879. // closeInvalidUpperNodeTabs();
  880. //
  881. // control.calculateStateAndVisualForCurrentTimeStep();
  882. // log.info("canvas.repaint9");
  883. // canvas.repaint();
  884. // } catch (IOException e) {
  885. // e.printStackTrace();
  886. // }
  887. }
  888. });
  889. mntmRedo.addActionListener(new ActionListener() {
  890. @Override
  891. public void actionPerformed(ActionEvent evt) {
  892. menuRedoActionPerformed();
  893. }
  894. private void menuRedoActionPerformed() {
  895. // try {
  896. // control.loadAutoSave(control.getRedoSave());
  897. //
  898. // closeInvalidUpperNodeTabs();
  899. //
  900. // control.calculateStateAndVisualForCurrentTimeStep();
  901. // log.info("canvas.repaint10");
  902. // canvas.repaint();
  903. // } catch (IOException e) {
  904. // e.printStackTrace();
  905. // }
  906. }
  907. });
  908. timePanel = new TimePanel(control);
  909. timePanel.setBorder(null);
  910. timePanel.getTimeSlider().addChangeListener(changeEvent -> {
  911. // TimeSliderChanged event
  912. control.calculateStateAndVisualForTimeStep(timePanel.getTimeSlider().getValue());
  913. unitGraph.repaint();
  914. contentPane.updateUI();
  915. });
  916. splitPane1.setMinimumSize(new Dimension(0, 25));
  917. splitPane.setRightComponent(splitPane1);
  918. splitPane.setDividerLocation(200);
  919. splitPane1.setDividerLocation(500);
  920. splitPane.setLeftComponent(scrollPane1);
  921. tabbedPaneOriginal.addTab("View", tabbedPaneInnerOriginal);
  922. myPanel.add(canvasSP, BorderLayout.CENTER);
  923. tabbedPaneInnerOriginal.addTab("Main Grid", myPanel);
  924. splitPane1.setLeftComponent(tabbedPaneOriginal);
  925. splitPane1.setRightComponent(splitHolonElPro);
  926. splitPane1.setResizeWeight(0.9);
  927. splitHolonElPro.setDividerLocation(700);
  928. // containing the graph and the elements-list
  929. splitHolonElPro.setTopComponent(inspector);
  930. // containing the object's properties
  931. splitHolonElPro.setBottomComponent(scrollProperties);
  932. canvasSP.setViewportView(canvas);
  933. // Set up of the Properties section
  934. scrollProperties.setViewportView(this.informationPanel);
  935. scrollProperties.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  936. scrollProperties.getVerticalScrollBar().setUnitIncrement(16);
  937. tabbedPaneOriginal.setBorder(null);
  938. scrollProperties.setBorder(null);
  939. splitPane.setBorder(null);
  940. splitPane1.setBorder(null);
  941. splitHolonElPro.setBorder(null);
  942. panelHolonEl.setBorder(null);
  943. canvasSP.setBorder(null);
  944. holegJFrame.getContentPane().add(timePanel, BorderLayout.SOUTH);
  945. // try {
  946. // control.loadAutoSave(System.getProperty("user.home") + "/.config/HolonGUI/Category/Category.json");
  947. // } catch (IOException e1) {
  948. // }
  949. canvasSP.addComponentListener(new ComponentAdapter() {
  950. @Override
  951. public void componentResized(ComponentEvent e) {
  952. GuiSettings.canvasSize.setX(Math.max(GuiSettings.canvasSize.getX(), canvasSP.getViewport().getWidth()));
  953. GuiSettings.canvasSize
  954. .setY(Math.max(GuiSettings.canvasSize.getY(), canvasSP.getViewport().getHeight()));
  955. log.info("canvas.repaint11");
  956. canvas.repaint();
  957. }
  958. });
  959. }
  960. private void tryToAlignObjects() {
  961. //TODO(Tom2022-01-14): recreateTryToAlignObjects
  962. }
  963. private JFileChooser initSaveLoadFileChooser(){
  964. JFileChooser safeLoadFileChooser = new JFileChooser(Preferences.userRoot().absolutePath());
  965. safeLoadFileChooser.setFileFilter(new FileNameExtensionFilter("Holeg json files", "json"));
  966. safeLoadFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  967. safeLoadFileChooser.setAcceptAllFileFilterUsed(false);
  968. return safeLoadFileChooser;
  969. }
  970. private void initWindowMenu() {
  971. menuBar.add(menuWindow);
  972. // Algo
  973. JMenuItem openMenu = new JMenuItem("Open Algorithm Panel", new ImageIcon(Import
  974. .loadImage(ImagePreference.Button.Menu.Algo).getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
  975. openMenu.addActionListener(actionEvent -> {
  976. new AddOnWindow(holegJFrame, control);
  977. });
  978. openMenu.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
  979. menuWindow.add(openMenu);
  980. // Outliner
  981. JMenuItem openOutliner = new JMenuItem("Open Outliner", new ImageIcon(Import
  982. .loadImage(ImagePreference.Button.Menu.Outliner).getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
  983. openOutliner.addActionListener(actionEvent -> {
  984. new Outliner(holegJFrame, model, control);
  985. });
  986. openOutliner
  987. .setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
  988. menuWindow.add(openOutliner);
  989. // FlexWindow
  990. JMenuItem openFlexMenuItem = new JMenuItem("Open Flexibility Panel", new ImageIcon(Import
  991. .loadImage(ImagePreference.Button.Menu.Algo).getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
  992. openFlexMenuItem.addActionListener(actionEvent -> {
  993. new FlexWindow(holegJFrame, control);
  994. });
  995. openFlexMenuItem
  996. .setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
  997. menuWindow.add(openFlexMenuItem);
  998. }
  999. /**
  1000. * reloads the Categories from Model.
  1001. *
  1002. * @param categories the current categories
  1003. */
  1004. private void updateCategories(Collection<Category> categories) {
  1005. DefaultTreeModel treemodel = new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
  1006. private static final long serialVersionUID = 1L;
  1007. {
  1008. DefaultMutableTreeNode node1;
  1009. for (Category c : categories) {
  1010. node1 = new DefaultMutableTreeNode(c.getName());
  1011. for (AbstractCanvasObject obj : c.getObjects()) {
  1012. node1.add(new DefaultMutableTreeNode(obj.getName()));
  1013. }
  1014. add(node1);
  1015. }
  1016. }
  1017. });
  1018. categoryTree.setModel(treemodel);
  1019. }
  1020. /**
  1021. * When changes are made to the Categories.
  1022. *
  1023. * @param categories the Categories
  1024. */
  1025. public void updateCategoryUI(Collection<Category> categories) {
  1026. DefaultTreeModel model = (DefaultTreeModel) categoryTree.getModel();
  1027. updateCategories(categories);
  1028. model.reload();
  1029. }
  1030. void setVisible(boolean value) {
  1031. holegJFrame.setVisible(value);
  1032. }
  1033. /*
  1034. * Open a new Tab with an UpperNodeCanvas
  1035. */
  1036. public void openNewUpperNodeTab(GroupNode node) {
  1037. chooseTabTemp();
  1038. JScrollPane scrollPane = getScrollPaneFromTabbedPane();
  1039. if (scrollPane.getViewport().getComponent(0)instanceof Canvas canvasPanel) {
  1040. unc = new Canvas(control, node);
  1041. }
  1042. // check if tab already open for clicked NodeOfNode
  1043. boolean dupl = false;
  1044. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1045. JScrollPane paneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1046. if (paneOriginal != null && ((Canvas) paneOriginal.getViewport().getComponent(0)).getGroupNode()
  1047. .getId() == node.getId()) {
  1048. dupl = true;
  1049. // set selected component to view
  1050. tabbedPaneOriginal.setSelectedComponent(tabbedPaneInnerOriginal);
  1051. // set selected tab in view to found upper-node-canvas
  1052. tabbedPaneInnerOriginal.setSelectedComponent(tabbedPaneInnerOriginal.getComponentAt(i));
  1053. }
  1054. // if we found a duplicate, break
  1055. if (dupl) {
  1056. break;
  1057. }
  1058. }
  1059. if (!dupl) {
  1060. JScrollPane sp = new JScrollPane(unc);
  1061. sp.setBorder(null);
  1062. // Selected tabbed Pane = tabbedPaneOriginal or tabbedPaneSplit
  1063. if (tabTemp == tabbedPaneOriginal) {
  1064. this.tabbedPaneInnerOriginal.add(node.getName(), sp);
  1065. this.tabbedPaneInnerOriginal.setSelectedComponent(sp);
  1066. this.tabbedPaneInnerOriginal.setTabComponentAt(this.tabbedPaneInnerOriginal.getTabCount() - 1,
  1067. new ButtonTabComponent(this.tabbedPaneInnerOriginal));
  1068. }
  1069. }
  1070. }
  1071. /**
  1072. * Removes UpperNodeTab if UpperNode was deleted
  1073. *
  1074. * @param cps the CPS object that is currently selected
  1075. */
  1076. private void removeUpperNodeTab(AbstractCanvasObject cps) {
  1077. if (cps instanceof GroupNode) {
  1078. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1079. JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1080. if (scrollPaneOriginal == null) {
  1081. } else if (((Canvas) scrollPaneOriginal.getViewport().getComponent(0)).getGroupNode()
  1082. .getId() == cps.getId()) {
  1083. ((ButtonTabComponent) tabbedPaneInnerOriginal.getTabComponentAt(i)).removeTabs();
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. }
  1089. /**
  1090. * chooses whether to set the tabTemp to tabbedPaneOriginal or tabbedPaneSplit
  1091. */
  1092. private void chooseTabTemp() {
  1093. // TODO(Tom2021-12-1) Remove tabTabbed
  1094. tabTemp = tabbedPaneOriginal;
  1095. }
  1096. private JScrollPane getScrollPaneFromTabbedPane() {
  1097. return getScrollPaneFromTabbedPane(-1);
  1098. }
  1099. private JScrollPane getScrollPaneFromTabbedPane(int index) {
  1100. Component upperLevelSelectedComponent;
  1101. if (tabTemp == null) {
  1102. return null;
  1103. }
  1104. if (index == -1) {
  1105. upperLevelSelectedComponent = tabTemp.getSelectedComponent();
  1106. } else {
  1107. upperLevelSelectedComponent = tabTemp.getComponentAt(index);
  1108. }
  1109. if (upperLevelSelectedComponent instanceof JTabbedPane) {
  1110. Component nextLevel = ((JTabbedPane) upperLevelSelectedComponent).getSelectedComponent();
  1111. if (nextLevel instanceof JPanel panel)
  1112. return (JScrollPane) panel.getComponent(0);
  1113. else
  1114. return (JScrollPane) nextLevel;
  1115. } else if (upperLevelSelectedComponent instanceof JScrollPane scrollPane) {
  1116. return scrollPane;
  1117. } else {
  1118. return null;
  1119. }
  1120. }
  1121. private void openWebpage(String URL) {
  1122. try {
  1123. java.awt.Desktop.getDesktop().browse(new URI(URL));
  1124. } catch (Exception e) {
  1125. e.printStackTrace();
  1126. }
  1127. }
  1128. /**
  1129. * closes all UpperNodeTabs, that don't have a valid UpperNode (e.g. if it was
  1130. * ungrouped/deleted/replaced and so on)
  1131. */
  1132. private void closeInvalidUpperNodeTabs() {
  1133. /**
  1134. * close bugged Tabs
  1135. */
  1136. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1137. JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1138. if (((Canvas) scrollPaneOriginal.getViewport().getComponent(0)).getGroupNode() == null) {
  1139. ((ButtonTabComponent) tabbedPaneInnerOriginal.getTabComponentAt(i)).removeTabs();
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. public void repaintCanvas() {
  1145. log.info("repaintCanvas");
  1146. tabbedPaneInnerOriginal.revalidate();
  1147. tabbedPaneInnerOriginal.repaint();
  1148. for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
  1149. JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal.getComponentAt(i);
  1150. scrollPaneOriginal.revalidate();
  1151. }
  1152. }
  1153. }