MainMenu.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.io.IOException;
  7. import java.text.NumberFormat;
  8. import java.util.ArrayList;
  9. import javax.swing.JFileChooser;
  10. import javax.swing.JFormattedTextField;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14. import javax.swing.filechooser.FileNameExtensionFilter;
  15. import javax.swing.text.NumberFormatter;
  16. import menubar.Menubar;
  17. import windows.Remote;
  18. import customComponents.ComWriter;
  19. import customComponents.Filedata;
  20. import customComponents.Filehandling;
  21. public class MainMenu extends JPanel{
  22. Menubar menubar;
  23. RouteSplitpane routeSplitpane;
  24. Iconbar iconbar;
  25. ComWriter bluetooth;
  26. Remote remote;
  27. Filehandling file;
  28. String currentRouteFile;
  29. boolean startup;
  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. setLayout(new BorderLayout(0,0));
  38. add(menubar, BorderLayout.NORTH);
  39. add(routeSplitpane);
  40. add(iconbar, BorderLayout.SOUTH);
  41. menubarMouseListeners();
  42. currentRouteFile = "";
  43. startup = true;
  44. iconbar.currentroute.setText("Geen bestand");
  45. menubar.menu_file.opslaan.setEnabled(false);
  46. menubar.menu_file.opslaanals.setEnabled(false);
  47. Thread bt = new Thread(){
  48. public void run(){
  49. char c;
  50. while(true){
  51. try {
  52. c = (char) bluetooth.getInput().read();
  53. System.out.println(c);
  54. if(c == 'b'){
  55. iconbar.setMessage("Botsing gedetecteerd!", 3000);
  56. routeSplitpane.gat();
  57. }else if(c == 'g'){
  58. iconbar.setMessage("Gat gedetecteerd!", 3000);
  59. routeSplitpane.gat();
  60. }else if(c == 'k'){
  61. routeSplitpane.kruispunt();
  62. };
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. };
  69. bt.start();
  70. }
  71. public String getCurrentRouteFile() {
  72. return currentRouteFile;
  73. }
  74. //methode om een route in te laden of om een nieuwe route aan te maken.
  75. public void setCurrentRouteFile(String route, Filedata filedata ){
  76. int c;
  77. if(startup){
  78. c = 0;
  79. startup = false;
  80. }else{
  81. c = JOptionPane.showConfirmDialog(null, "Weet u zeker dat u de huidige route wilt afsluiten?", "Alert: " + "Weet u het zeker?", JOptionPane.YES_NO_OPTION);
  82. }
  83. if (c == JOptionPane.YES_OPTION) {
  84. if(filedata != null){
  85. // route openen via bestand
  86. int maxx = filedata.getColumns();
  87. int maxy = filedata.getRows();
  88. iconbar.currentGridSize.setText( maxx + " x " + maxy);
  89. routeSplitpane.setFieldSize(maxx, maxy, filedata.getCordinates(), filedata.getSteps());
  90. currentRouteFile = route;
  91. iconbar.currentroute.setText(currentRouteFile);
  92. menubar.menu_file.opslaan.setEnabled(true);
  93. menubar.menu_file.opslaanals.setEnabled(true);
  94. }else{
  95. // Geen route ingeladen, nieuwe route openen
  96. int[] tempsize = sizePopup();
  97. if(tempsize != null){
  98. routeSplitpane.setFieldSize(tempsize[0], tempsize[1], new ArrayList<ArrayList<Integer>>(), new ArrayList<Character>());
  99. iconbar.currentGridSize.setText(tempsize[0] + " x " + tempsize[1]);
  100. currentRouteFile = "new";
  101. iconbar.currentroute.setText("Nieuw bestand");
  102. menubar.menu_file.opslaan.setEnabled(false);
  103. menubar.menu_file.opslaanals.setEnabled(true);
  104. }
  105. }
  106. }
  107. }
  108. //popup om de grootte van het veld in te voeren.
  109. public int[] sizePopup(){
  110. NumberFormatter formatter = new NumberFormatter(NumberFormat.getInstance());
  111. formatter.setValueClass(Integer.class);
  112. formatter.setMinimum(0);
  113. formatter.setMaximum(40);
  114. formatter.setCommitsOnValidEdit(true);
  115. JFormattedTextField field1 = new JFormattedTextField(formatter);
  116. JFormattedTextField field2 = new JFormattedTextField(formatter);
  117. JPanel panel = new JPanel(new GridLayout(0, 1));
  118. panel.add(new JLabel("Hoogte:"));
  119. panel.add(field1);
  120. panel.add(new JLabel("Breedte:"));
  121. panel.add(field2);
  122. int result = JOptionPane.showConfirmDialog(null, panel, "Veld grootte", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  123. if (result == JOptionPane.OK_OPTION) {
  124. if(field1.getValue() == null || field2.getValue() == null){
  125. JOptionPane.showMessageDialog(null, "Ongeldige grootte");
  126. return null;
  127. }else{
  128. return new int[]{(int) field1.getValue(),(int) field2.getValue()} ;
  129. }
  130. } else {
  131. return null;
  132. }
  133. }
  134. public void menubarMouseListeners(){
  135. //maakt de mouse listners voor de menubar
  136. menubar.menu_file.openen.addActionListener(new ActionListener() {
  137. @Override
  138. public void actionPerformed(ActionEvent e) {
  139. JFileChooser c = new JFileChooser();
  140. c.setFileFilter(new FileNameExtensionFilter("Route bestanden", "rt"));
  141. int rVal = c.showOpenDialog(MainMenu.this);
  142. if (rVal == JFileChooser.APPROVE_OPTION) {
  143. setCurrentRouteFile(c.getSelectedFile().getAbsolutePath(), file.readRouteFile(c.getSelectedFile().getAbsolutePath()));
  144. }
  145. }
  146. });
  147. menubar.menu_file.opslaan.addActionListener(new ActionListener() {
  148. @Override
  149. public void actionPerformed(ActionEvent arg0) {
  150. file.writeRouteFile(routeSplitpane.getColumns(), routeSplitpane.getRows(), routeSplitpane.leftpanel.coordinaten, routeSplitpane.leftpanel.steps, currentRouteFile);
  151. }
  152. });
  153. menubar.menu_file.opslaanals.addActionListener(new ActionListener() {
  154. @Override
  155. public void actionPerformed(ActionEvent arg0) {
  156. JFileChooser c = new JFileChooser();
  157. c.setFileFilter(new FileNameExtensionFilter("Route bestanden", "rt"));
  158. int rVal = c.showOpenDialog(MainMenu.this);
  159. if (rVal == JFileChooser.APPROVE_OPTION) {
  160. file.writeRouteFile(routeSplitpane.getColumns(), routeSplitpane.getRows(), routeSplitpane.leftpanel.coordinaten, routeSplitpane.leftpanel.steps, c.getSelectedFile().getAbsolutePath());
  161. setCurrentRouteFile(c.getSelectedFile().getAbsolutePath(), file.readRouteFile(c.getSelectedFile().getAbsolutePath()));
  162. }
  163. }
  164. });
  165. menubar.menu_file.nieuw.addActionListener(new ActionListener() {
  166. @Override
  167. public void actionPerformed(ActionEvent e) {
  168. setCurrentRouteFile("Nieuwe route", null);
  169. }
  170. });
  171. menubar.menu_edit.ongedaan.addActionListener(new ActionListener() {
  172. @Override
  173. public void actionPerformed(ActionEvent arg0) {
  174. routeSplitpane.leftpanel.stepBackwards();
  175. }
  176. });
  177. iconbar.irremote.addActionListener(new ActionListener() {
  178. @Override
  179. public void actionPerformed(ActionEvent e) {
  180. createRemote();
  181. }
  182. });
  183. routeSplitpane.pauze.addActionListener(new ActionListener() {
  184. @Override
  185. public void actionPerformed(ActionEvent arg0) {
  186. bluetooth.writeString("p");
  187. }
  188. });
  189. routeSplitpane.hervatten.addActionListener(new ActionListener() {
  190. @Override
  191. public void actionPerformed(ActionEvent arg0) {
  192. bluetooth.writeString("h");
  193. }
  194. });
  195. routeSplitpane.upload.addActionListener(new ActionListener() {
  196. @Override
  197. public void actionPerformed(ActionEvent arg0) {
  198. routeSplitpane.rightpanel.clear();
  199. routeSplitpane.setCurrentstep(0);
  200. bluetooth.sendRoute(routeSplitpane.leftpanel.steps);
  201. int length = routeSplitpane.leftpanel.coordinaten.size()-1;
  202. routeSplitpane.rightpanel.setFinish(routeSplitpane.leftpanel.coordinaten.get(length).get(1), routeSplitpane.leftpanel.coordinaten.get(length).get(0));
  203. routeSplitpane.rightpanel.setBoebot(routeSplitpane.leftpanel.coordinaten.get(0).get(1), routeSplitpane.leftpanel.coordinaten.get(0).get(0), 0);
  204. }
  205. });
  206. routeSplitpane.automatisch.addActionListener(new ActionListener() {
  207. @Override
  208. public void actionPerformed(ActionEvent arg0) {
  209. routeSplitpane.automatischBerekenen();
  210. }
  211. });
  212. }
  213. public void createRemote(){
  214. if(remote.isShowing()){
  215. remote.dispose();
  216. }else{
  217. remote.show(true);
  218. }
  219. }
  220. }