Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.27 KB | None | 0 0
  1. package application;
  2.  
  3. import javafx.application.Application;
  4.  
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.geometry.Pos;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Alert;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.ButtonType;
  12. import javafx.scene.control.CheckBox;
  13. import javafx.scene.control.Hyperlink;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.PasswordField;
  16. import javafx.scene.control.TextField;
  17. import javafx.scene.control.ToolBar;
  18. import javafx.scene.control.Alert.AlertType;
  19. import javafx.scene.image.Image;
  20. import javafx.scene.image.ImageView;
  21. import javafx.scene.input.KeyCode;
  22. import javafx.scene.input.KeyEvent;
  23. import javafx.scene.layout.AnchorPane;
  24. import javafx.scene.layout.BorderPane;
  25. import javafx.scene.layout.HBox;
  26. import javafx.scene.layout.VBox;
  27. import javafx.scene.shape.Line;
  28. import javafx.stage.Stage;
  29. import javafx.stage.WindowEvent;
  30.  
  31. import java.sql.SQLException;
  32. import java.util.Optional;
  33.  
  34. import dbsettings.db;
  35. import model.functions;
  36.  
  37.  
  38. public class Main extends Application{
  39.  
  40. static Stage actualstage = null;
  41. functions functions = new functions();
  42. db dbcon = new db();
  43. private String userlevel = "";
  44. private String loggeduser = "";
  45.  
  46. public static void main(String[] args) {
  47. launch(args);
  48. }
  49. public void start(Stage loginpage) throws Exception {
  50. functions.conection(dbcon); //Use database connection in functions class
  51. actualstage = loginpage;
  52. //Testing
  53. try {
  54. dbcon.getallusers();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. try {
  59. dbcon.getalluserlevels();
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. //Login setup
  64. VBox vbox = new VBox();
  65. Image img = new Image("logo.png");
  66. ImageView imageView = new ImageView(img);
  67. imageView.setFitHeight(100);
  68. imageView.setFitWidth(100);
  69. imageView.setTranslateX(95);
  70. imageView.setTranslateY(3);
  71. Label label = new Label("Ludzu, ielogojies, vai izveido jaunu profilu");
  72. label.setMaxWidth(Double.MAX_VALUE);
  73. AnchorPane.setLeftAnchor(label, 0.0);
  74. AnchorPane.setRightAnchor(label, 0.0);
  75. label.setAlignment(Pos.CENTER);
  76. label.setStyle("-fx-font-size: 14; -fx-font-weight: bold;");
  77. Label label2 = new Label("username:");
  78. label2.setStyle("-fx-font-weight: bold;");
  79. TextField username = new TextField();
  80. Label label3 = new Label("password:");
  81. label3.setStyle("-fx-font-weight: bold;");
  82. PasswordField password = new PasswordField();
  83. Button loginbutton = new Button("Go");
  84. loginbutton.setTranslateX(129);
  85. Button registerbutton = new Button("Registreties");
  86. registerbutton.setTranslateX(107);
  87. loginbutton.setOnAction(new EventHandler<ActionEvent>() {
  88.  
  89. @Override
  90. public void handle(ActionEvent arg0) {
  91. boolean status = false;
  92. String getTextfromusername = username.getText();
  93. String getTextfrompass = password.getText();
  94. String passwordcode = "";
  95. try {
  96. passwordcode = functions.encrypt(getTextfrompass);
  97. } catch (Exception e1) {
  98. e1.printStackTrace();
  99. }
  100. try {
  101. status = dbcon.login(getTextfromusername, passwordcode);
  102. } catch (SQLException e) {
  103. e.printStackTrace();
  104. }
  105. if(status) {
  106. System.out.println("Autentifikacija izdevusies. Parbauda userlevel...");
  107.  
  108. try {
  109. userlevel = dbcon.getuserlevel(getTextfromusername);
  110. loggeduser = getTextfromusername;
  111. } catch (SQLException e) {
  112. e.printStackTrace();
  113. }
  114. System.out.println("Ieguts userlevel:" + userlevel);
  115. if(userlevel.equals("banned")) {
  116. System.out.println("Autentifikacija neizdevas(lietotajs ir nobanots)");
  117. Alert alert = new Alert(Alert.AlertType.ERROR);
  118. alert.setTitle("Banned");
  119. alert.setHeaderText("Tavs profils ir banots par noteikumu neieverosanu.");
  120. alert.show();
  121. password.setText("");
  122. }
  123. else{
  124. System.out.println("Ielade foruma lapu");
  125. loginpage.close();
  126. mainview();
  127. }
  128. }
  129. else {
  130. System.out.println("Autentifikacija neizdevas");
  131. Alert alert = new Alert(Alert.AlertType.ERROR);
  132. alert.setTitle("Kluda");
  133. alert.setHeaderText("Nepareizs lietotajvards/parole");
  134. alert.show();
  135. password.setText("");
  136. }
  137. }
  138.  
  139. });
  140. registerbutton.setOnAction(new EventHandler<ActionEvent>() {
  141.  
  142. @Override
  143. public void handle(ActionEvent event) {
  144. registrationview();
  145.  
  146. }
  147. });
  148. password.setOnKeyPressed(new EventHandler<KeyEvent>() {
  149.  
  150. @Override
  151. public void handle(KeyEvent event) {
  152. if(event.getCode().equals(KeyCode.ENTER)) {
  153. loginbutton.fire();
  154. }
  155. }
  156. });
  157. vbox.getChildren().addAll(imageView, label, label2, username, label3, password, loginbutton, registerbutton);
  158. Scene scene = new Scene(vbox, 300, 255);
  159. scene.getStylesheets().add("loginstylesheet.css");
  160. loginpage.resizableProperty().setValue(Boolean.FALSE);
  161. loginpage.setScene(scene);
  162. loginpage.setTitle("Forums");
  163. loginpage.getIcons().add(new Image("logo.png"));
  164. loginpage.show();
  165.  
  166.  
  167.  
  168. }
  169.  
  170. private void registrationview() {
  171. Stage stage = new Stage();
  172. VBox box = new VBox();
  173. Image img = new Image("logo.png");
  174. ImageView imageView = new ImageView(img);
  175. imageView.setFitHeight(100);
  176. imageView.setFitWidth(100);
  177. imageView.setTranslateX(130);
  178. imageView.setTranslateY(3);
  179. Label reginf = new Label("Registracija");
  180. reginf.setStyle("-fx-font-size: 20; -fx-font-weight: bold;");
  181. reginf.setMaxWidth(Double.MAX_VALUE);
  182. AnchorPane.setLeftAnchor(reginf, 0.0);
  183. AnchorPane.setRightAnchor(reginf, 0.0);
  184. reginf.setAlignment(Pos.CENTER);
  185. Label usernametxt = new Label("Izvelies lietotajvardu");
  186. usernametxt.setStyle("-fx-font-weight: bold;");
  187. TextField username = new TextField();
  188. Label passtxt = new Label("Ievadi paroli");
  189. passtxt.setStyle("-fx-font-weight: bold;");
  190. PasswordField password = new PasswordField();
  191. Label passtxt2 = new Label("Ievadi velreiz paroli");
  192. passtxt2.setStyle("-fx-font-weight: bold;");
  193. PasswordField password2 = new PasswordField();
  194. Button register = new Button("Registreties");
  195. register.setTranslateX(128);
  196. Label userexists = new Label("");
  197. userexists.setStyle("-fx-text-fill: red;");
  198. Label passnotmatch = new Label("");
  199. passnotmatch.setStyle("-fx-text-fill: red;");
  200. Label usernametooshort = new Label("Lietotajvarda jabut vismaz 4 simboliem");
  201. usernametooshort.setStyle("-fx-text-fill: red;");
  202. Label passtooshort = new Label("Parole jabut vismaz 4 simboliem");
  203. passtooshort.setStyle("-fx-text-fill: red;");
  204. Label namecontainsspaces = new Label("");
  205. namecontainsspaces.setStyle("-fx-text-fill: red;");
  206. CheckBox rulescheckBox = new CheckBox("");
  207. rulescheckBox.setTranslateY(20);
  208. Hyperlink ruleslink = new Hyperlink("Piekrîtu foruma noteikumiem");
  209. ruleslink.setTranslateX(15);
  210.  
  211. username.setOnKeyReleased(new EventHandler<KeyEvent>() {
  212.  
  213. @Override
  214. public void handle(KeyEvent arg0) {
  215. boolean status = true;
  216. String getTextfromusername = username.getText();
  217. try {
  218. status = dbcon.checkIfUserExists(getTextfromusername);
  219. } catch (SQLException e) {
  220. e.printStackTrace();
  221. }
  222. if(status && getTextfromusername.length() > 1)
  223. userexists.setText("Lietotajvards ir aiznemts");
  224. else
  225. userexists.setText("");
  226. if(getTextfromusername.length() < 4)
  227. usernametooshort.setText("Lietotajvarda jabut vismaz 4 simboliem");
  228. else
  229. usernametooshort.setText("");
  230. int spacescount = 0;
  231. spacescount = functions.countspacesinusername(getTextfromusername);
  232. if (spacescount > 0)
  233. namecontainsspaces.setText("Lietotajvards nedrikst saturet atstarpes");
  234. else
  235. namecontainsspaces.setText("");
  236. }
  237. });
  238. password.setOnKeyReleased(new EventHandler<KeyEvent>() {
  239.  
  240. @Override
  241. public void handle(KeyEvent arg0) {
  242. String getTextfrompassword = password.getText();
  243. String getTextfrompassword2 = password2.getText();
  244. if(!getTextfrompassword.equals(getTextfrompassword2) && getTextfrompassword.length() > 0)
  245. passnotmatch.setText("Paroles nesakrit!");
  246. else
  247. passnotmatch.setText("");
  248. if(getTextfrompassword.length() < 4)
  249. passtooshort.setText("Parole jabut vismaz 4 simboliem");
  250. else
  251. passtooshort.setText("");
  252. }
  253. });
  254. password2.setOnKeyReleased(new EventHandler<KeyEvent>() {
  255.  
  256. @Override
  257. public void handle(KeyEvent arg0) {
  258. String getTextfrompassword = password.getText();
  259. String getTextfrompassword2 = password2.getText();
  260. if(!getTextfrompassword.equals(getTextfrompassword2) && getTextfrompassword.length() > 0)
  261. passnotmatch.setText("Paroles nesakrit!");
  262. else
  263. passnotmatch.setText("");
  264. }
  265. });
  266. register.setOnAction(new EventHandler<ActionEvent>() {
  267.  
  268. @Override
  269. public void handle(ActionEvent event) {
  270. boolean status = false;
  271. String getTextfromusername = username.getText();
  272. String getTextfrompass = password.getText();
  273. String getTextfrompass2 = password2.getText();
  274. String passwordcode = "";
  275. int spacescount = 0;
  276. spacescount = functions.countspacesinusername(getTextfromusername);
  277. if(rulescheckBox.isSelected()) {
  278. if(getTextfrompass.equals(getTextfrompass2)) {
  279. if(getTextfromusername.length() > 3 && getTextfrompass.length() > 3 && spacescount == 0) {
  280. try {
  281. passwordcode = functions.encrypt(getTextfrompass);
  282. } catch (Exception e1) {
  283. e1.printStackTrace();
  284. }
  285. try {
  286. status = dbcon.adduser(getTextfromusername, passwordcode);
  287. } catch (SQLException e) {
  288. e.printStackTrace();
  289. }
  290. if(status) {
  291. Alert regsuc = new Alert(Alert.AlertType.INFORMATION);
  292. System.out.println("Registracija veiksmiga(" + getTextfromusername + ")");
  293. regsuc.setTitle("Registracija veiksmiga");
  294. regsuc.setHeaderText("Lietotajs izveidots. Tagad variet ielogoties ar savu jauno profilu");
  295. regsuc.show();
  296. stage.close();
  297. }
  298. else {
  299. Alert regfail = new Alert(Alert.AlertType.ERROR);
  300. System.out.println("Registracija neveiksmiga(server error vai username aiznemts)");
  301. regfail.setTitle("Registracija neizdevusies");
  302. regfail.setHeaderText("Registracija neveiksmiga, lietotajvards nav pieejams");
  303. regfail.show();
  304. password.setText("");
  305. password2.setText("");
  306. }
  307. }
  308. else {
  309. Alert alert = new Alert(Alert.AlertType.ERROR);
  310. System.out.println("Registracija neveiksmiga(parak isa username/parole vai atstarpes parole)");
  311. alert.setTitle("Kluda");
  312. alert.setHeaderText("Lietotajvardaa un parolee jabut vismaz 4 simboliem, ka ari lietotajvards nedrikst saturet atstarpes");
  313. alert.show();
  314. password.setText("");
  315. password2.setText("");
  316. }
  317. }
  318. else {
  319. Alert alert = new Alert(Alert.AlertType.ERROR);
  320. System.out.println("Registracija neveiksmiga(paroles nesakrit)");
  321. alert.setTitle("Kluda");
  322. alert.setHeaderText("Paroles nesakrit");
  323. alert.show();
  324. password.setText("");
  325. password2.setText("");
  326. }
  327. }
  328. else {
  329. Alert alert = new Alert(Alert.AlertType.ERROR);
  330. System.out.println("Registracija neveiksmiga(nepiekrit noteikumiem)");
  331. alert.setTitle("Kluda");
  332. alert.setHeaderText("Jums japiekrit noteikumiem, lai registretos");
  333. alert.show();
  334. password.setText("");
  335. password2.setText("");
  336. }
  337. }
  338. });
  339. ruleslink.setOnAction(new EventHandler<ActionEvent>() {
  340.  
  341. @Override
  342. public void handle(ActionEvent event) {
  343. getHostServices().showDocument("http://178.62.8.75/noteikumi.html");
  344. }
  345. });
  346. box.getChildren().addAll(imageView, reginf, usernametxt, username, usernametooshort, userexists, namecontainsspaces, passtxt, password, passtooshort, passtxt2, password2, passnotmatch, rulescheckBox, ruleslink, register);
  347. Scene sc = new Scene(box, 350, 407);
  348. sc.getStylesheets().add("registerstylesheet.css");
  349. stage.setScene(sc);
  350. stage.getIcons().add(new Image("logo.png"));
  351. stage.setTitle("Registracija");
  352. stage.resizableProperty().setValue(Boolean.FALSE);
  353. stage.show();
  354. }
  355.  
  356. public void mainview() {
  357. functions.funcgetloggeduser(loggeduser);
  358. functions.funcgetuserlevel(userlevel);
  359. Stage stage = new Stage();
  360. stage.setMaximized(true);
  361. BorderPane border = new BorderPane();
  362. Scene sc = new Scene(border, 600, 600);
  363. //Window top toolbar setup
  364. Image logoimg = new Image("logo.png");
  365. ImageView logoimgview = new ImageView(logoimg);
  366. logoimgview.setFitHeight(30);
  367. logoimgview.setFitWidth(30);
  368. Button toolbarjauns = new Button("Jauns");
  369. toolbarjauns.setGraphic(new ImageView("plus.png"));
  370. Button toolbarprofils = new Button("Profils");
  371. toolbarprofils.setGraphic(new ImageView("profileicon.png"));
  372. Button toolbarmod = new Button("Mod");
  373. toolbarmod.setGraphic(new ImageView("modicon.png"));
  374. Button toolbarlogout = new Button("Iziet");
  375. toolbarlogout.setGraphic(new ImageView("logouticon.png"));
  376. Button toolbaradmin = new Button("Admin");
  377. toolbaradmin.setGraphic(new ImageView("adminicon.png"));
  378. ToolBar toolBar = null;
  379.  
  380. //Toolbar config
  381. if(userlevel.equals("mod")){
  382. toolBar = new ToolBar(
  383. logoimgview,
  384. toolbarjauns,
  385. toolbarprofils,
  386. toolbarmod,
  387. toolbarlogout
  388. );
  389. }
  390. else if(userlevel.equals("admin")){
  391. toolBar = new ToolBar(
  392. logoimgview,
  393. toolbarjauns,
  394. toolbarprofils,
  395. toolbarmod,
  396. toolbaradmin,
  397. toolbarlogout
  398. );
  399. }
  400. else{
  401. toolBar = new ToolBar(
  402. logoimgview,
  403. toolbarjauns,
  404. toolbarprofils,
  405. toolbarlogout
  406. );
  407. }
  408. border.setTop(toolBar);
  409.  
  410. //Window midbox setup
  411. VBox centerbox = new VBox();
  412. functions.mainwindow(centerbox);
  413. Image img = new Image("forummainpage.png");
  414. ImageView imageView = new ImageView(img);
  415. centerbox.getChildren().addAll(imageView);
  416. border.setCenter(centerbox);
  417.  
  418. //Left panel setup
  419. VBox leftbox = new VBox();
  420. HBox searchbox = functions.searchbox();
  421. Label categoryinfo = new Label(" Kategorijas ");
  422. leftbox.setStyle("-fx-background-color: #cc0000;");
  423. categoryinfo.setStyle("-fx-text-fill: rgb(255, 255, 255); -fx-font-weight: bold; -fx-font-size: 30;");
  424. Line seperator = new Line();
  425. seperator.setEndX(200);
  426. seperator.setStyle("-fx-stroke: white;");
  427. VBox allcategory = functions.loadallcategory();
  428. functions.categorywindow(allcategory);
  429. leftbox.getChildren().addAll(searchbox, categoryinfo, seperator, allcategory);
  430. border.setLeft(leftbox);
  431.  
  432. //Bottom panel setup
  433. HBox hbox = new HBox();
  434. Label infotext = new Label("©Forums 2018-2019");
  435. hbox.getChildren().addAll(infotext);
  436. border.setBottom(hbox);
  437. Stage panelstage = new Stage();
  438. stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  439. @Override
  440. public void handle(WindowEvent event) {
  441. panelstage.close();
  442. }
  443. });
  444. //Toolbar functions
  445. toolbarlogout.setOnAction(new EventHandler<ActionEvent>() {
  446.  
  447. @Override
  448. public void handle(ActionEvent event) {
  449. try {
  450. stage.close();
  451. panelstage.close();
  452. System.out.println("Lietotajs beidzis sesiju");
  453. start(actualstage);
  454. } catch (Exception e) {
  455. e.printStackTrace();
  456. }
  457.  
  458. }
  459. });
  460.  
  461. toolbaradmin.setOnAction(new EventHandler<ActionEvent>() {
  462.  
  463. @Override
  464. public void handle(ActionEvent event) {
  465. adminpanel(panelstage);
  466. }
  467. });
  468. toolbarmod.setOnAction(new EventHandler<ActionEvent>() {
  469. @Override
  470. public void handle(ActionEvent event) {
  471. modpanel(panelstage);
  472. }
  473. });
  474. toolbarjauns.setOnAction(new EventHandler<ActionEvent>() {
  475. @Override
  476. public void handle(ActionEvent event) {
  477. addpanel(userlevel, panelstage);
  478. }
  479. });
  480. toolbarprofils.setOnAction(new EventHandler<ActionEvent>() {
  481. @Override
  482. public void handle(ActionEvent event) {
  483. try {
  484. profilepanel(centerbox, panelstage);
  485. } catch (SQLException e) {
  486. e.printStackTrace();
  487. }
  488. }
  489. });
  490. //Window final setup
  491. sc.getStylesheets().add("mainpagestylesheet.css");
  492. stage.setScene(sc);
  493. stage.getIcons().add(new Image("logo.png"));
  494. stage.setTitle("Forums");
  495. stage.show();
  496. }
  497. public void adminpanel(Stage stage){
  498. VBox vbox = new VBox();
  499. Label usermanage = new Label("Lietotaju parvaldisana");
  500. usermanage.setStyle("-fx-font-size: 18; -fx-font-weight: bold;");
  501. usermanage.setTranslateX(60);
  502. VBox levelview = functions.userlevelchangeview();
  503. VBox deluser = functions.deleteuserview();
  504. Label catmanage = new Label("Kategoriju parvaldisana");
  505. catmanage.setStyle("-fx-font-size: 18; -fx-font-weight: bold;");
  506. catmanage.setTranslateX(60);
  507. VBox deletecategory = functions.deletecategoryview();
  508. VBox updatecategory = functions.updatecategoryview();
  509. vbox.getChildren().addAll(usermanage, levelview, deluser, catmanage, deletecategory, updatecategory);
  510. Scene sc = new Scene(vbox, 330, 570);
  511. sc.getStylesheets().add("adminpanelstylesheet.css");
  512. stage.setScene(sc);
  513. stage.getIcons().add(new Image("logo.png"));
  514. stage.setTitle("Admin panel");
  515. stage.resizableProperty().setValue(Boolean.FALSE);
  516. stage.show();
  517. }
  518.  
  519. public void modpanel(Stage stage) {
  520. VBox vbox = new VBox();
  521. Label usermanage = new Label("Lietotaju parvaldisana");
  522. usermanage.setStyle("-fx-font-size: 18; -fx-font-weight: bold;");
  523. usermanage.setTranslateX(60);
  524. VBox banview = functions.userbanview();
  525. VBox changename = functions.changenameview();
  526. vbox.getChildren().addAll(usermanage, banview, changename);
  527. Scene sc = new Scene(vbox, 330, 400);
  528. sc.getStylesheets().add("modpanelstylesheet.css");
  529. stage.setScene(sc);
  530. stage.getIcons().add(new Image("logo.png"));
  531. stage.setTitle("Moderation panel");
  532. stage.resizableProperty().setValue(Boolean.FALSE);
  533. stage.show();
  534. }
  535. public void addpanel(String userlevel, Stage stage) {
  536. VBox vbox = new VBox();
  537. VBox addtopic = functions.addtopicview(stage);
  538. vbox.getChildren().add(addtopic);
  539. if(userlevel.equals("admin")) {
  540. VBox addcategory = functions.addcategory();
  541. vbox.getChildren().add(addcategory);
  542. }
  543. Scene sc = new Scene(vbox, 700, 500);
  544. sc.getStylesheets().add("addpanelstylesheet.css");
  545. stage.setScene(sc);
  546. stage.getIcons().add(new Image("logo.png"));
  547. stage.setTitle("Pievienot");
  548. stage.resizableProperty().setValue(Boolean.FALSE);
  549. stage.show();
  550. }
  551. public void profilepanel(VBox midbox, Stage stage) throws SQLException {
  552. int loggeduserid = 0;
  553. loggeduserid = dbcon.getuserid(loggeduser);
  554. VBox profile = new VBox();
  555. VBox userinfobox = new VBox();
  556. userinfobox.setStyle("-fx-background-color: #ffc4c4;");
  557. Image img = new Image("avatar.png");
  558. ImageView imageView = new ImageView(img);
  559. Label loggedusertext = new Label(loggeduser);
  560. Label loggeduserlevel = new Label(userlevel);
  561. loggedusertext.setStyle("-fx-font-size: 30; -fx-font-weight: bold;");
  562. userinfobox.getChildren().addAll(imageView, loggedusertext, loggeduserlevel);
  563. userinfobox.setAlignment(Pos.CENTER);
  564. Label mytopicsinfo = new Label("Manas izveidotas diskusijas:");
  565. mytopicsinfo.setStyle("-fx-font-size: 22; -fx-font-weight: bold;");
  566. VBox mytopiclist = functions.loadtopiclist(0, "profiletopics", loggeduserid, null);
  567. Label myreplyinfo = new Label("Manas komentetas diskusijas:");
  568. VBox myreplylist = functions.loadtopiclist(0, "profilereply", loggeduserid, null);
  569. myreplyinfo.setStyle("-fx-font-size: 22; -fx-font-weight: bold;");
  570. HBox buttons = new HBox();
  571. buttons.setStyle("-fx-background-color: #ffc4c4;");
  572. Button changename = new Button("Mainit lietotajvardu");
  573. Button changepassword = new Button("Mainit paroli");
  574. changename.setOnAction(new EventHandler<ActionEvent>() {
  575. @Override
  576. public void handle(ActionEvent event) {
  577. profilepanelchangename(stage, midbox);
  578.  
  579. }
  580. });
  581. changepassword.setOnAction(new EventHandler<ActionEvent>() {
  582. @Override
  583. public void handle(ActionEvent event) {
  584. profilepanelchangepassword(stage, midbox);
  585.  
  586. }
  587. });
  588. buttons.getChildren().addAll(changename, changepassword);
  589. profile.getChildren().addAll(userinfobox, mytopicsinfo, mytopiclist, myreplyinfo, myreplylist, buttons);
  590. midbox.getChildren().clear();
  591. midbox.getChildren().add(profile);
  592. }
  593. public void profilepanelchangename(Stage stage, VBox midbox) {
  594. VBox vbox = new VBox();
  595. Label changenameinfo = new Label("Lietotajvarda maina");
  596. changenameinfo.setStyle("-fx-font-size: 30; -fx-font-weight: bold;");
  597. changenameinfo.setMaxWidth(Double.MAX_VALUE);
  598. AnchorPane.setLeftAnchor(changenameinfo, 0.0);
  599. AnchorPane.setRightAnchor(changenameinfo, 0.0);
  600. changenameinfo.setAlignment(Pos.CENTER);
  601. Label usernametxt = new Label("Izvelies lietotajvardu");
  602. usernametxt.setStyle("-fx-font-weight: bold;");
  603. TextField username = new TextField();
  604. Label userexists = new Label("Lietotajvards jau ir aiznemts");
  605. userexists.setStyle("-fx-text-fill: red;");
  606. userexists.setManaged(false);
  607. userexists.setVisible(false);
  608. Label usernametooshort = new Label("Lietotajvarda jabut vismaz 4 simboliem");
  609. usernametooshort.setStyle("-fx-text-fill: red;");
  610. Label namecontainsspaces = new Label("Lietotajvards nedrikst saturet atstarpes");
  611. namecontainsspaces.setStyle("-fx-text-fill: red;");
  612. namecontainsspaces.setManaged(false);
  613. namecontainsspaces.setVisible(false);
  614. Label passtxt = new Label("Ievadi paroli");
  615. passtxt.setStyle("-fx-font-weight: bold;");
  616. PasswordField password = new PasswordField();
  617. Button confirm = new Button("Mainit");
  618. username.setOnKeyReleased(new EventHandler<KeyEvent>() {
  619.  
  620. @Override
  621. public void handle(KeyEvent arg0) {
  622. boolean status = true;
  623. String getTextfromusername = username.getText();
  624. try {
  625. status = dbcon.checkIfUserExists(getTextfromusername);
  626. } catch (SQLException e) {
  627. e.printStackTrace();
  628. }
  629. if(status && getTextfromusername.length() > 1) {
  630. userexists.setManaged(true);
  631. userexists.setVisible(true);
  632. }
  633. else {
  634. userexists.setManaged(false);
  635. userexists.setVisible(false);
  636. }
  637. if(getTextfromusername.length() < 4) {
  638. usernametooshort.setManaged(true);
  639. usernametooshort.setVisible(true);
  640. }
  641. else {
  642. usernametooshort.setManaged(false);
  643. usernametooshort.setVisible(false);
  644. }
  645. int spacescount = 0;
  646. spacescount = functions.countspacesinusername(getTextfromusername);
  647. if (spacescount > 0) {
  648. namecontainsspaces.setManaged(true);
  649. namecontainsspaces.setVisible(true);
  650. }
  651. else {
  652. namecontainsspaces.setManaged(false);
  653. namecontainsspaces.setVisible(false);
  654. }
  655. }
  656. });
  657. confirm.setOnAction(new EventHandler<ActionEvent>() {
  658.  
  659. @Override
  660. public void handle(ActionEvent event) {
  661. boolean status = false;
  662. String getTextfromusername = username.getText();
  663. String getTextfrompass = password.getText();
  664. String passwordcode = "";
  665. int spacescount = 0;
  666. Alert alert2 = new Alert(AlertType.CONFIRMATION);
  667. alert2.setTitle("Mainit lietotajvardu?");
  668. alert2.setHeaderText("Vai tiesam mainit lietotajvardu?");
  669.  
  670. Optional<ButtonType> result = alert2.showAndWait();
  671. if (result.get() == ButtonType.OK){
  672. try {
  673. passwordcode = functions.encrypt(getTextfrompass);
  674. status = dbcon.login(loggeduser, passwordcode);
  675. } catch (Exception e1) {
  676. e1.printStackTrace();
  677. }
  678. if(status) {
  679. spacescount = functions.countspacesinusername(getTextfromusername);
  680. if(getTextfromusername.length() > 3 && spacescount == 0) {
  681.  
  682. try {
  683. status = dbcon.changename(loggeduser, getTextfromusername);
  684. } catch (SQLException e) {
  685. e.printStackTrace();
  686. }
  687. if(status) {
  688. setloggeduser(getTextfromusername);
  689. functions.funcgetloggeduser(loggeduser);
  690. Alert regsuc = new Alert(Alert.AlertType.INFORMATION);
  691. System.out.println("Nickname mainits(" + getTextfromusername + ")");
  692. regsuc.setTitle("Operacija veiksmiga");
  693. regsuc.setHeaderText("Lietotajvards mainits");
  694. regsuc.show();
  695. midbox.getChildren().clear();
  696. try {
  697. profilepanel(midbox, stage);
  698. } catch (SQLException e) {
  699. e.printStackTrace();
  700. }
  701. stage.close();
  702. }
  703. else {
  704. Alert regfail = new Alert(Alert.AlertType.ERROR);
  705. System.out.println("Operacija neveiksmiga(server error vai username aiznemts)");
  706. regfail.setTitle("Operacija neizdevusies");
  707. regfail.setHeaderText("Operacija neveiksmiga, lietotajvards nav pieejams");
  708. regfail.show();
  709. password.setText("");
  710. }
  711. }
  712. else {
  713. Alert alert = new Alert(Alert.AlertType.ERROR);
  714. System.out.println("Operacija neveiksmiga(parak iss username)");
  715. alert.setTitle("Operacija neveiksmiga");
  716. alert.setHeaderText("Lietotajvardaa jabut vismaz 4 simboliem, ka ari lietotajvards nedrikst saturet atstarpes");
  717. alert.show();
  718. password.setText("");
  719. }
  720. }
  721. else {
  722. Alert alert = new Alert(Alert.AlertType.ERROR);
  723. System.out.println("Operacija neveiksmiga(nepareiza parole)");
  724. alert.setTitle("Operacija neveiksmiga");
  725. alert.setHeaderText("Nepareiza parole");
  726. alert.show();
  727. password.setText("");
  728. }
  729. }
  730. }
  731. });
  732. vbox.getChildren().addAll(changenameinfo, usernametxt, username, userexists, usernametooshort, namecontainsspaces, passtxt, password, confirm);
  733. Scene sc = new Scene(vbox, 300, 180);
  734. sc.getStylesheets().add("changenamepanel.css");
  735. stage.setScene(sc);
  736. stage.getIcons().add(new Image("logo.png"));
  737. stage.setTitle("Mainit lietotajvardu");
  738. stage.resizableProperty().setValue(Boolean.FALSE);
  739. stage.show();
  740. }
  741. public void profilepanelchangepassword(Stage stage, VBox midbox) {
  742. VBox vbox = new VBox();
  743. Label changenameinfo = new Label("Paroles maina");
  744. changenameinfo.setStyle("-fx-font-size: 30; -fx-font-weight: bold;");
  745. changenameinfo.setMaxWidth(Double.MAX_VALUE);
  746. AnchorPane.setLeftAnchor(changenameinfo, 0.0);
  747. AnchorPane.setRightAnchor(changenameinfo, 0.0);
  748. changenameinfo.setAlignment(Pos.CENTER);
  749. Label passtxt = new Label("Ievadi veco paroli");
  750. passtxt.setStyle("-fx-font-weight: bold;");
  751. PasswordField password = new PasswordField();
  752. Label passtxt2 = new Label("Ievadi jauno paroli");
  753. passtxt2.setStyle("-fx-font-weight: bold;");
  754. PasswordField password2 = new PasswordField();
  755. Label passtxt3 = new Label("Ievadi velreiz jauno paroli");
  756. passtxt3.setStyle("-fx-font-weight: bold;");
  757. PasswordField password3 = new PasswordField();
  758. Button change = new Button("Mainit");
  759. Label passtooshort = new Label("Parole jabut vismaz 4 simboliem");
  760. passtooshort.setStyle("-fx-text-fill: red;");
  761. Label passnotmatch = new Label("Paroles nesakrit!");
  762. passnotmatch.setStyle("-fx-text-fill: red;");
  763. password2.setOnKeyReleased(new EventHandler<KeyEvent>() {
  764.  
  765. @Override
  766. public void handle(KeyEvent arg0) {
  767. String getTextfrompassword = password2.getText();
  768. String getTextfrompassword2 = password3.getText();
  769. if(!getTextfrompassword.equals(getTextfrompassword2) && getTextfrompassword.length() > 0) {
  770. passnotmatch.setVisible(true);
  771. passnotmatch.setManaged(true);
  772. }
  773. else {
  774. passnotmatch.setManaged(false);
  775. passnotmatch.setVisible(false);
  776. }
  777. if(getTextfrompassword.length() < 4) {
  778. passtooshort.setVisible(true);
  779. passtooshort.setManaged(true);
  780. }
  781. else {
  782. passtooshort.setManaged(false);
  783. passtooshort.setVisible(false);
  784. }
  785. }
  786. });
  787. password3.setOnKeyReleased(new EventHandler<KeyEvent>() {
  788.  
  789. @Override
  790. public void handle(KeyEvent arg0) {
  791. String getTextfrompassword = password2.getText();
  792. String getTextfrompassword2 = password3.getText();
  793. if(!getTextfrompassword.equals(getTextfrompassword2) && getTextfrompassword.length() > 0) {
  794. passnotmatch.setVisible(true);
  795. passnotmatch.setManaged(true);
  796. }
  797. else {
  798. passnotmatch.setManaged(false);
  799. passnotmatch.setVisible(false);
  800. }
  801. if(getTextfrompassword.length() < 4) {
  802. passtooshort.setVisible(true);
  803. passtooshort.setManaged(true);
  804. }
  805. else {
  806. passtooshort.setManaged(false);
  807. passtooshort.setVisible(false);
  808. }
  809. }
  810. });
  811. change.setOnAction(new EventHandler<ActionEvent>() {
  812. @Override
  813. public void handle(ActionEvent event) {
  814. String passwordcode = "";
  815. boolean status = false;
  816. boolean changestatus = false;
  817. String newcode = "";
  818. int ID_user = 0;
  819. Alert alert2 = new Alert(AlertType.CONFIRMATION);
  820. alert2.setTitle("Mainit paroli?");
  821. alert2.setHeaderText("Vai tiesam mainit paroli?");
  822.  
  823. Optional<ButtonType> result = alert2.showAndWait();
  824. if (result.get() == ButtonType.OK){
  825. if(password2.getText().equals(password3.getText()) && password2.getText().length() > 3) {
  826. try {
  827. passwordcode = functions.encrypt(password.getText());
  828. status = dbcon.login(loggeduser, passwordcode);
  829. } catch (Exception e1) {
  830. e1.printStackTrace();
  831. }
  832. if(status) {
  833. try {
  834. ID_user = dbcon.getuserid(loggeduser);
  835. } catch (SQLException e) {
  836. e.printStackTrace();
  837. }
  838. try {
  839. newcode = functions.encrypt(password2.getText());
  840. } catch (Exception e1) {
  841. e1.printStackTrace();
  842. }
  843. try {
  844. changestatus = dbcon.changepassword(ID_user, newcode);
  845. } catch (SQLException e) {
  846. e.printStackTrace();
  847. }
  848. if(changestatus) {
  849. stage.close();
  850. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  851. System.out.println("Parole nomainita");
  852. alert.setTitle("Operacija veiksmiga");
  853. alert.setHeaderText("Parole mainita");
  854. alert.show();
  855. }
  856. else {
  857. Alert alert = new Alert(Alert.AlertType.ERROR);
  858. System.out.println("Operacija neveiksmiga");
  859. alert.setTitle("Operacija neveiksmiga");
  860. alert.setHeaderText("Parole netika mainita");
  861. alert.show();
  862. }
  863. }
  864. else {
  865. password.setText("");
  866. password2.setText("");
  867. password3.setText("");
  868. Alert alert = new Alert(Alert.AlertType.ERROR);
  869. System.out.println("Operacija neveiksmiga(nepareiza parole)");
  870. alert.setTitle("Operacija neveiksmiga");
  871. alert.setHeaderText("Nepareiza parole");
  872. alert.show();
  873. }
  874. }
  875. else {
  876. password.setText("");
  877. password2.setText("");
  878. password3.setText("");
  879. Alert alert = new Alert(Alert.AlertType.ERROR);
  880. System.out.println("Operacija neveiksmiga(neizpilditas prasibas)");
  881. alert.setTitle("Operacija neveiksmiga");
  882. alert.setHeaderText("Nav izpilditas paroles mainas prasibas");
  883. alert.show();
  884. }
  885. }
  886. }
  887. });
  888. vbox.getChildren().addAll(changenameinfo, passtxt, password, passtxt2, password2, passtxt3, password3, passtooshort, passnotmatch, change);
  889. Scene sc = new Scene(vbox, 300, 230);
  890. sc.getStylesheets().add("changepasswordpanel.css");
  891. stage.setScene(sc);
  892. stage.getIcons().add(new Image("logo.png"));
  893. stage.setTitle("Mainit paroli");
  894. stage.resizableProperty().setValue(Boolean.FALSE);
  895. stage.show();
  896. }
  897. //Misc functions
  898. public void setloggeduser(String loggeduser) {
  899. this.loggeduser = loggeduser;
  900. }
  901. public String getloggeduser() {
  902. return loggeduser;
  903. }
  904. public void setuserlevel(String userlevel) {
  905. this.loggeduser = userlevel;
  906. }
  907. public String getuserlevel() {
  908. return userlevel;
  909. }
  910. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement