Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. package lab7;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11.  
  12. public class Controller implements ActionListener
  13. {
  14. private SQLHandler handler;
  15. private static String database = "person";
  16. private static String[] fields = {"id", "nume", "varsta"};
  17. private Boolean editAdd;
  18. private Integer currentIndex;
  19. private Integer currentIndexID;
  20. private Integer currentIndexName;
  21. private Integer currentIndexAge;
  22. private JTextField current;
  23. private JTextField idField;
  24. private JTextField nameField;
  25. private JTextField ageField;
  26. private JButton next;
  27. private JButton previous;
  28. private JButton first;
  29. private JButton last;
  30. private JButton add;
  31. private JButton remove;
  32. private JButton save;
  33. private JButton edit;
  34. private JButton search;
  35. private JButton cancel;
  36.  
  37. public Controller(JTextField current, JTextField idField, JTextField nameField, JTextField ageField, JButton next, JButton previous, JButton first, JButton last, JButton add, JButton remove, JButton save, JButton edit, JButton search, JButton cancel) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
  38. {
  39. this.handler = new SQLHandler(Controller.database, fields);
  40. this.currentIndex = 1;
  41. this.currentIndexID = 0;
  42. this.currentIndexName = 1;
  43. this.currentIndexAge = 2;
  44. this.current = current;
  45. this.idField = idField;
  46. this.nameField = nameField;
  47. this.ageField = ageField;
  48. this.next = next;
  49. this.previous = previous;
  50. this.first = first;
  51. this.last = last;
  52. this.add = add;
  53. this.remove = remove;
  54. this.save = save;
  55. this.edit = edit;
  56. this.search = search;
  57. this.cancel = cancel;
  58.  
  59. }
  60.  
  61. public void initialise()
  62. {
  63. ArrayList<String> result = handler.selectAll();
  64.  
  65. this.currentIndex = 1;
  66. this.currentIndexID = 0;
  67. this.currentIndexName = 1;
  68. this.currentIndexAge = 2;
  69.  
  70. this.idField.setEditable(false);
  71. this.nameField.setEditable(false);
  72. this.ageField.setEditable(false);
  73.  
  74. this.next.setEnabled(true);
  75. this.last.setEnabled(true);
  76.  
  77. this.save.setEnabled(false);
  78. this.cancel.setEnabled(false);
  79.  
  80. this.previous.setEnabled(false);
  81. this.first.setEnabled(false);
  82.  
  83. this.current.setText(this.currentIndex + "/" + (result.size()/Controller.fields.length));
  84.  
  85. this.idField.setText(result.get(this.currentIndexID));
  86. this.nameField.setText(result.get(this.currentIndexName));
  87. this.ageField.setText(result.get(this.currentIndexAge));
  88. }
  89.  
  90. private void firstAction()
  91. {
  92. this.initialise();
  93.  
  94. if(!this.next.isEnabled())
  95. {
  96. this.next.setEnabled(true);
  97. this.last.setEnabled(true);
  98. }
  99.  
  100. }
  101.  
  102. private void nextAction()
  103. {
  104. ArrayList<String> result = handler.selectAll();
  105.  
  106. if(this.currentIndex < (result.size()/Controller.fields.length))
  107. {
  108. if(!this.previous.isEnabled())
  109. {
  110. this.previous.setEnabled(true);
  111. this.first.setEnabled(true);
  112. }
  113.  
  114. this.currentIndex ++;
  115. this.currentIndexID += Controller.fields.length;
  116. this.currentIndexName += Controller.fields.length;
  117. this.currentIndexAge += Controller.fields.length;
  118.  
  119. this.idField.setText(result.get(this.currentIndexID));
  120. this.nameField.setText(result.get(this.currentIndexName));
  121. this.ageField.setText(result.get(this.currentIndexAge));
  122.  
  123. this.current.setText(this.currentIndex + "/" + (result.size()/Controller.fields.length));
  124. }
  125.  
  126. if(this.currentIndex == (result.size()/Controller.fields.length))
  127. {
  128. this.next.setEnabled(false);
  129. this.last.setEnabled(false);
  130. }
  131. }
  132.  
  133. private void previousAction()
  134. {
  135. ArrayList<String> result = handler.selectAll();
  136.  
  137. if(this.currentIndex > 1)
  138. {
  139. if(!this.next.isEnabled())
  140. {
  141. this.next.setEnabled(true);
  142. this.last.setEnabled(true);
  143. }
  144.  
  145. this.currentIndex --;
  146. this.currentIndexID -= Controller.fields.length;
  147. this.currentIndexName -= Controller.fields.length;
  148. this.currentIndexAge -= Controller.fields.length;
  149.  
  150. this.idField.setText(result.get(this.currentIndexID));
  151. this.nameField.setText(result.get(this.currentIndexName));
  152. this.ageField.setText(result.get(this.currentIndexAge));
  153.  
  154. this.current.setText(this.currentIndex + "/" + (result.size()/Controller.fields.length));
  155. }
  156.  
  157. if(this.currentIndex == 1)
  158. {
  159. this.previous.setEnabled(false);
  160. this.first.setEnabled(false);
  161. }
  162. }
  163.  
  164. private void lastAction()
  165. {
  166. ArrayList<String> result = handler.selectAll();
  167.  
  168. this.currentIndex = (result.size()/Controller.fields.length);
  169. this.currentIndexID = result.size() - Controller.fields.length;
  170. this.currentIndexName = result.size()- (Controller.fields.length - 1);
  171. this.currentIndexAge = result.size() - (Controller.fields.length - 2);
  172.  
  173. this.current.setText(this.currentIndex + "/" + (result.size()/Controller.fields.length));
  174.  
  175. this.idField.setText(result.get(this.currentIndexID));
  176. this.nameField.setText(result.get(this.currentIndexName));
  177. this.ageField.setText(result.get(this.currentIndexAge));
  178.  
  179. this.next.setEnabled(false);
  180. this.last.setEnabled(false);
  181.  
  182. if(!this.previous.isEnabled())
  183. {
  184. this.previous.setEnabled(true);
  185. this.first.setEnabled(true);
  186. }
  187. }
  188.  
  189. private void searchAction()
  190. {
  191. String name = JOptionPane.showInputDialog("Numele pe care il cautati");
  192.  
  193. ArrayList<String> result = handler.searchName(name);
  194.  
  195. if(!result.isEmpty())
  196. {
  197. this.initialise();
  198. while(!nameField.getText().equals(result.get(1)))
  199. nextAction();
  200. }
  201. else
  202. JOptionPane.showMessageDialog(null, "Nu a fost gasit/a " + name + "!");
  203.  
  204. }
  205.  
  206. private void removeAction()
  207. {
  208. int dialogResult = JOptionPane.showConfirmDialog (null, "Sigur vrei sa stergi datele acestea?","Warning",JOptionPane.YES_NO_OPTION);
  209.  
  210. if(dialogResult == JOptionPane.YES_OPTION)
  211. {
  212. handler.delete(Integer.parseInt(idField.getText()));
  213. }
  214.  
  215. this.initialise();
  216. }
  217.  
  218. private void editAddAction(String action)
  219. {
  220. this.first.setEnabled(false);
  221. this.previous.setEnabled(false);
  222. this.next.setEnabled(false);
  223. this.last.setEnabled(false);
  224. this.add.setEnabled(false);
  225. this.edit.setEnabled(false);
  226. this.remove.setEnabled(false);
  227. this.search.setEnabled(false);
  228. this.save.setEnabled(true);
  229. this.cancel.setEnabled(true);
  230.  
  231. this.nameField.setEditable(true);
  232. this.ageField.setEditable(true);
  233.  
  234. switch(action)
  235. {
  236. case "add":
  237. this.idField.setEditable(true);
  238. this.idField.setText("");
  239. this.nameField.setText("");
  240. this.ageField.setText("");
  241. this.current.setText("Noua inregistrare");
  242. this.editAdd = false;
  243. break;
  244. case "edit":
  245. this.current.setText("Modul editare");
  246. this.editAdd = true;
  247. break;
  248. }
  249. }
  250.  
  251. private void saveAction()
  252. {
  253. if(!this.editAdd)
  254. {
  255. this.handler.insert(new String[] {idField.getText(), nameField.getText(), ageField.getText()});
  256. }
  257. else
  258. {
  259. this.handler.update(new String[] {idField.getText(), nameField.getText(), ageField.getText()});
  260. }
  261.  
  262. this.first.setEnabled(true);
  263. this.previous.setEnabled(true);
  264. this.next.setEnabled(true);
  265. this.last.setEnabled(true);
  266. this.add.setEnabled(true);
  267. this.edit.setEnabled(true);
  268. this.remove.setEnabled(true);
  269. this.search.setEnabled(true);
  270. this.nameField.setEditable(false);
  271. this.ageField.setEditable(false);
  272. this.idField.setEditable(false);
  273.  
  274. this.initialise();
  275. }
  276.  
  277. private void cancelAction()
  278. {
  279. this.first.setEnabled(true);
  280. this.previous.setEnabled(true);
  281. this.next.setEnabled(true);
  282. this.last.setEnabled(true);
  283. this.add.setEnabled(true);
  284. this.edit.setEnabled(true);
  285. this.remove.setEnabled(true);
  286. this.search.setEnabled(true);
  287. this.nameField.setEditable(false);
  288. this.ageField.setEditable(false);
  289. this.idField.setEditable(false);
  290.  
  291. this.initialise();
  292. }
  293.  
  294. @Override
  295. public void actionPerformed(ActionEvent e)
  296. {
  297. JButton trigger = (JButton)e.getSource();
  298.  
  299. switch(trigger.getName())
  300. {
  301. case "first":
  302. this.firstAction();
  303. break;
  304. case "next":
  305. this.nextAction();
  306. break;
  307. case "previous":
  308. this.previousAction();
  309. break;
  310. case "last":
  311. lastAction();
  312. break;
  313. case "search":
  314. searchAction();
  315. break;
  316. case "remove":
  317. removeAction();
  318. break;
  319. case "edit":
  320. editAddAction("edit");
  321. break;
  322. case "add":
  323. editAddAction("add");
  324. break;
  325. case "save":
  326. saveAction();
  327. break;
  328. case "cancel":
  329. cancelAction();
  330. break;
  331. default:
  332. break;
  333. }
  334.  
  335. }
  336.  
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement