MainMenu.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package panels;
  2. import java.awt.BorderLayout;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.text.NumberFormat;
  7. import java.util.ArrayList;
  8. import javax.swing.JFileChooser;
  9. import javax.swing.JFormattedTextField;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.filechooser.FileNameExtensionFilter;
  14. import javax.swing.text.NumberFormatter;
  15. import menubar.Menubar;
  16. import windows.Remote;
  17. import customComponents.ComWriter;
  18. import customComponents.Filehandling;
  19. public class MainMenu extends JPanel{
  20. Menubar menubar;
  21. RouteSplitpane routeSplitpane;
  22. Iconbar iconbar;
  23. ComWriter bluetooth;
  24. Remote remote;
  25. Filehandling file;
  26. String currentRouteFile;
  27. int maxx, maxy;
  28. ArrayList<ArrayList<Integer>> currentRouteCoordinates;
  29. ArrayList<Character> currentRouteSteps;
  30. public MainMenu(){
  31. menubar = new Menubar();
  32. routeSplitpane = new RouteSplitpane();
  33. iconbar = new Iconbar();
  34. file = new Filehandling();
  35. //bluetooth = new ComWriter();
  36. remote = new Remote(new AfstandbedieningPanel(bluetooth));
  37. remote.dispose();
  38. setLayout(new BorderLayout(0,0));
  39. add(menubar, BorderLayout.NORTH);
  40. add(routeSplitpane);
  41. add(iconbar, BorderLayout.SOUTH);
  42. add(iconbar, BorderLayout.SOUTH);
  43. currentRouteCoordinates = new ArrayList<ArrayList<Integer>>();
  44. currentRouteSteps = new ArrayList<Character>();
  45. menubarMouseListeners();
  46. currentRouteFile = "";
  47. iconbar.currentroute.setText("Geen bestand");
  48. menubar.menu_file.opslaan.setEnabled(false);
  49. menubar.menu_file.opslaanals.setEnabled(false);
  50. }
  51. public String getCurrentRouteFile() {
  52. return currentRouteFile;
  53. }
  54. //methode om een route in te laden of om een nieuwe route aan te maken.
  55. public void setCurrentRouteFile(String route, ArrayList<ArrayList<Integer>> tempcorarray ){
  56. int c = JOptionPane.showConfirmDialog(null, "Weet u zeker dat u de huidige route wilt afsluiten?", "Alert: " + "Weet u het zeker?", JOptionPane.YES_NO_OPTION);
  57. if (c == JOptionPane.YES_OPTION) {
  58. if(tempcorarray != null){
  59. maxx = tempcorarray.get(0).get(0);
  60. maxy = tempcorarray.get(0).get(1);
  61. routeSplitpane.rightpanel.setGridsize(maxx, maxy);
  62. iconbar.currentGridSize.setText( maxx + " x " + maxy);
  63. tempcorarray.remove(0);
  64. currentRouteCoordinates = tempcorarray;
  65. currentRouteFile = route;
  66. iconbar.currentroute.setText(currentRouteFile);
  67. menubar.menu_file.opslaan.setEnabled(true);
  68. menubar.menu_file.opslaanals.setEnabled(true);
  69. }else{
  70. int[] tempsize = sizePopup();
  71. if(tempsize != null){
  72. routeSplitpane.rightpanel.setGridsize(tempsize[0], tempsize[1]);
  73. iconbar.currentGridSize.setText(tempsize[0] + " x " + tempsize[1]);
  74. currentRouteCoordinates.clear();
  75. currentRouteFile = "new";
  76. iconbar.currentroute.setText("Nieuw bestand");
  77. menubar.menu_file.opslaan.setEnabled(false);
  78. menubar.menu_file.opslaanals.setEnabled(true);
  79. }
  80. }
  81. }
  82. }
  83. //popup om de grootte van het veld in te voeren.
  84. public int[] sizePopup(){
  85. NumberFormatter formatter = new NumberFormatter(NumberFormat.getInstance());
  86. formatter.setValueClass(Integer.class);
  87. formatter.setMinimum(0);
  88. formatter.setMaximum(40);
  89. formatter.setCommitsOnValidEdit(true);
  90. JFormattedTextField field1 = new JFormattedTextField(formatter);
  91. JFormattedTextField field2 = new JFormattedTextField(formatter);
  92. JPanel panel = new JPanel(new GridLayout(0, 1));
  93. panel.add(new JLabel("Hoogte:"));
  94. panel.add(field1);
  95. panel.add(new JLabel("Breedte:"));
  96. panel.add(field2);
  97. int result = JOptionPane.showConfirmDialog(null, panel, "Veld grootte", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  98. if (result == JOptionPane.OK_OPTION) {
  99. if(field1.getValue() == null || field2.getValue() == null){
  100. JOptionPane.showMessageDialog(null, "Ongeldige grootte");
  101. return null;
  102. }else{
  103. return new int[]{(int) field1.getValue(),(int) field2.getValue()} ;
  104. }
  105. } else {
  106. return null;
  107. }
  108. }
  109. public void menubarMouseListeners(){
  110. //maakt de mouse listners voor de menubar
  111. menubar.menu_file.openen.addActionListener(new ActionListener() {
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. JFileChooser c = new JFileChooser();
  115. c.setFileFilter(new FileNameExtensionFilter("Route bestanden", "rt"));
  116. int rVal = c.showOpenDialog(MainMenu.this);
  117. if (rVal == JFileChooser.APPROVE_OPTION) {
  118. setCurrentRouteFile(c.getSelectedFile().getAbsolutePath(), file.readRouteFile(c.getSelectedFile().getAbsolutePath()));
  119. }
  120. }
  121. });
  122. menubar.menu_file.opslaan.addActionListener(new ActionListener() {
  123. @Override
  124. public void actionPerformed(ActionEvent arg0) {
  125. file.writeRouteFile(maxx, maxy, currentRouteCoordinates, currentRouteFile);
  126. }
  127. });
  128. menubar.menu_file.opslaanals.addActionListener(new ActionListener() {
  129. @Override
  130. public void actionPerformed(ActionEvent arg0) {
  131. JFileChooser c = new JFileChooser();
  132. c.setFileFilter(new FileNameExtensionFilter("Route bestanden", "rt"));
  133. int rVal = c.showOpenDialog(MainMenu.this);
  134. if (rVal == JFileChooser.APPROVE_OPTION) {
  135. file.writeRouteFile(maxx, maxy, currentRouteCoordinates, c.getSelectedFile().getAbsolutePath());
  136. }
  137. }
  138. });
  139. menubar.menu_file.nieuw.addActionListener(new ActionListener() {
  140. @Override
  141. public void actionPerformed(ActionEvent e) {
  142. setCurrentRouteFile("Nieuwe route", null);
  143. }
  144. });
  145. iconbar.irremote.addActionListener(new ActionListener() {
  146. @Override
  147. public void actionPerformed(ActionEvent e) {
  148. createRemote();
  149. }
  150. });
  151. }
  152. public void createRemote(){
  153. if(remote.isShowing()){
  154. remote.dispose();
  155. }else{
  156. remote.show(true);
  157. }
  158. }
  159. }