Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.47 KB | None | 0 0
  1. /**
  2. @author Mustafa Bay 970225-8857, Jakob Ödman Lindecrantz 970724-7475
  3. */
  4.  
  5. //Fel i search, använder for loop genom hela triangles
  6.  
  7.  
  8. import java.awt.image.BufferedImage;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.io.PrintWriter;
  16. import java.util.*;
  17. import java.util.function.UnaryOperator;
  18.  
  19. import javax.imageio.ImageIO;
  20.  
  21. import javafx.application.Application;
  22. import javafx.beans.value.ChangeListener;
  23. import javafx.beans.value.ObservableValue;
  24. import javafx.collections.FXCollections;
  25. import javafx.collections.ObservableList;
  26. import javafx.embed.swing.SwingFXUtils;
  27. import javafx.event.ActionEvent;
  28. import javafx.event.EventHandler;
  29. import javafx.geometry.Insets;
  30. import javafx.geometry.Pos;
  31. import javafx.scene.Cursor;
  32. import javafx.scene.Scene;
  33. import javafx.scene.control.Alert;
  34. import javafx.scene.control.Alert.AlertType;
  35. import javafx.scene.control.Button;
  36. import javafx.scene.control.ButtonType;
  37. import javafx.scene.control.Label;
  38. import javafx.scene.control.ListView;
  39. import javafx.scene.control.Menu;
  40. import javafx.scene.control.MenuBar;
  41. import javafx.scene.control.MenuItem;
  42. import javafx.scene.control.RadioButton;
  43. import javafx.scene.control.TextField;
  44. import javafx.scene.control.TextFormatter;
  45. import javafx.scene.control.ToggleGroup;
  46. import javafx.scene.image.Image;
  47. import javafx.scene.image.ImageView;
  48. import javafx.scene.input.MouseButton;
  49. import javafx.scene.input.MouseEvent;
  50. import javafx.scene.layout.BorderPane;
  51. import javafx.scene.layout.GridPane;
  52. import javafx.scene.layout.HBox;
  53. import javafx.scene.layout.Pane;
  54. import javafx.scene.layout.VBox;
  55. import javafx.scene.paint.Color;
  56. import javafx.scene.paint.Paint;
  57. import javafx.scene.shape.Polygon;
  58. import javafx.stage.FileChooser;
  59. import javafx.stage.Stage;
  60. import javafx.stage.WindowEvent;
  61.  
  62. public class Main extends Application {
  63.  
  64. private ImageView display;
  65. private TextField search;
  66. private RadioButton namnBtn;
  67. private RadioButton describedBtn;
  68. private Image image;
  69. private Pane center;
  70. private ListView<String> listView;
  71. private boolean changed = false;
  72. private Button newBtn;
  73. private NewLocation newLocation = new NewLocation();
  74. private Stage primaryStage;
  75. private Paint color;
  76. private Place triangle;
  77. private Position position;
  78. private Paint colorTemp;
  79. private boolean mapExist = false;
  80.  
  81. private ObservableList<String> names = FXCollections.observableArrayList("Bus", "Underground", "Train");
  82. private Set<Place> triangles = new HashSet<>();
  83. private Map<Position, Place> positions = new HashMap<>();
  84. private Map<String, Set<Place>> alla = new HashMap<>();
  85. private Map<String, Set<Place>> categoryMap = new HashMap<>();
  86. private Map<Boolean, Set<Place>> selectedMap = new TreeMap<>();
  87.  
  88. class FileLoader implements EventHandler<ActionEvent> {
  89. public void handle(ActionEvent t) {
  90. if (changed) {
  91. Alert alert = new Alert(AlertType.CONFIRMATION);
  92. alert.setHeaderText("Unsaved actions, LOAD MAP anyway?");
  93. Optional<ButtonType> res = alert.showAndWait();
  94. if (res.isPresent() && res.get() == ButtonType.CANCEL) {
  95. return;
  96. }
  97. }
  98. FileChooser fileChooser = new FileChooser();
  99. FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
  100. fileChooser.getExtensionFilters().add(extFilterPNG);
  101. File file = fileChooser.showOpenDialog(primaryStage);
  102. if (file == null)
  103. return;
  104.  
  105. try {
  106.  
  107. BufferedImage bufferedImage = ImageIO.read(file);
  108. image = SwingFXUtils.toFXImage(bufferedImage, null);
  109. display.setImage(image);
  110. primaryStage.sizeToScene();
  111. } catch (IOException ex) {
  112. System.err.println("IOException error");
  113. }
  114. center.getChildren().clear();
  115. center.getChildren().add(display);
  116. changed = false;
  117. mapExist = true;
  118. positions.clear();
  119. alla.clear();
  120. selectedMap.clear();
  121. categoryMap.clear();
  122. triangles.clear();
  123. }
  124. }
  125.  
  126. public void start(Stage primaryStage) {
  127. this.primaryStage = primaryStage;
  128. BorderPane root = new BorderPane();
  129. root.setStyle("-fx-font-size: 18");
  130. primaryStage.setTitle("Map");
  131.  
  132. center = new Pane();
  133. root.setCenter(center);
  134. display = new ImageView();
  135. center.getChildren().add(display);
  136.  
  137. VBox vbox = new VBox();
  138. MenuBar menuBar = new MenuBar();
  139. vbox.getChildren().add(menuBar);
  140. Menu file = new Menu("File");
  141. menuBar.getMenus().add(file);
  142. MenuItem m1 = new MenuItem("Load Map");
  143. MenuItem m2 = new MenuItem("Load Places");
  144. MenuItem m3 = new MenuItem("Save");
  145. MenuItem m4 = new MenuItem("Exit");
  146. file.getItems().add(m1);
  147. file.getItems().add(m2);
  148. file.getItems().add(m3);
  149. file.getItems().add(m4);
  150. m1.setOnAction(new FileLoader());
  151. m2.setOnAction(new OpenPlaces());
  152. m3.setOnAction(new SaveHandler());
  153. m4.setOnAction(e -> primaryStage.fireEvent(new WindowEvent(primaryStage, WindowEvent.WINDOW_CLOSE_REQUEST)));
  154. primaryStage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, new ExitHandler());
  155.  
  156. HBox hboxTop = new HBox(5);
  157. vbox.getChildren().add(hboxTop);
  158. hboxTop.setPadding(new Insets(5));
  159. hboxTop.setAlignment(Pos.CENTER);
  160.  
  161. newBtn = new Button("New");
  162. hboxTop.getChildren().add(newBtn);
  163. newBtn.setOnMousePressed(new NewLocationEnable());
  164.  
  165. VBox radioBtns = new VBox();
  166. namnBtn = new RadioButton("Named");
  167. describedBtn = new RadioButton("Described");
  168. ToggleGroup group = new ToggleGroup();
  169. namnBtn.setToggleGroup(group);
  170. describedBtn.setToggleGroup(group);
  171. radioBtns.getChildren().addAll(namnBtn, describedBtn);
  172. hboxTop.getChildren().add(radioBtns);
  173.  
  174. search = new TextField();
  175. hboxTop.getChildren().add(search);
  176.  
  177. Button searchBtn = new Button("Search");
  178. searchBtn.setOnAction(new Search());
  179. hboxTop.getChildren().add(searchBtn);
  180.  
  181. Button hideBtn = new Button("Hide");
  182. hideBtn.setOnAction(new Hide());
  183. hboxTop.getChildren().add(hideBtn);
  184.  
  185. Button removeBtn = new Button("Remove");
  186. removeBtn.setOnAction(new Remove());
  187. hboxTop.getChildren().add(removeBtn);
  188.  
  189. Button coordinatesBtn = new Button("Coordinates");
  190. coordinatesBtn.setOnAction(new Coordinates());
  191. hboxTop.getChildren().add(coordinatesBtn);
  192. root.setTop(vbox);
  193.  
  194. VBox right = new VBox(5);
  195. right.setPadding(new Insets(5));
  196. right.getChildren().add(new Label("Categories"));
  197. listView = new ListView<>(names);
  198. right.getChildren().add(listView);
  199. root.setRight(right);
  200. right.setAlignment(Pos.CENTER);
  201. listView.setPrefSize(300, 130);
  202.  
  203. Button hideCatagoryBtn = new Button("Hide Category");
  204. hideCatagoryBtn.setOnAction(new HideCatagory());
  205. listView.setOnMouseClicked(new ViewCat());
  206. right.getChildren().add(hideCatagoryBtn);
  207. Scene scene = new Scene(root);
  208. primaryStage.setScene(scene);
  209. primaryStage.show();
  210.  
  211. }
  212.  
  213. class SaveHandler implements EventHandler<ActionEvent> {
  214. @Override
  215. public void handle(ActionEvent event) {
  216. try {
  217. // Otydligt i instruktionerna ifall vi skulle ange filtyp vid sparning. Ange
  218. // ".txt" vid sparning för att spara som textfil.
  219. FileChooser fileChooser = new FileChooser();
  220. File file = fileChooser.showSaveDialog(primaryStage);
  221. if (file == null)
  222. return;
  223. String filnamn = file.getAbsolutePath();
  224. FileWriter outFile = new FileWriter(filnamn);
  225. PrintWriter out = new PrintWriter(outFile);
  226. for (Place p : triangles) {
  227. out.println(p.toString());
  228. out.close();
  229. }
  230.  
  231. changed = false;
  232.  
  233. } catch (IOException ioe) {
  234. System.err.println("Fel!");
  235. }
  236. }
  237. }
  238.  
  239. class OpenPlaces implements EventHandler<ActionEvent> {
  240. @Override
  241. public void handle(ActionEvent event) {
  242. if (!mapExist) {
  243. Alert alert = new Alert(AlertType.ERROR);
  244. alert.setHeaderText("Load a map first!");
  245. alert.showAndWait();
  246. } else {
  247. triangles.clear();
  248. positions.clear();
  249. if (changed) {
  250. Alert alert = new Alert(AlertType.CONFIRMATION);
  251. alert.setHeaderText("Unsaved actions, LOAD PLACES anyway?");
  252. Optional<ButtonType> res = alert.showAndWait();
  253. if (res.get() == ButtonType.CANCEL) {
  254. return;
  255. }
  256. }
  257. try {
  258. FileChooser fileChooser = new FileChooser();
  259. File file = fileChooser.showOpenDialog(primaryStage);
  260. if (file == null)
  261. return;
  262. String filnamn = file.getAbsolutePath();
  263. FileReader in = new FileReader(filnamn);
  264. BufferedReader br = new BufferedReader(in);
  265. String line;
  266. while ((line = br.readLine()) != null) {
  267. String[] tokens = line.split(",");
  268. String type = tokens[0];
  269. String cat = tokens[1];
  270. double x = Double.parseDouble(tokens[2]);
  271. double y = Double.parseDouble(tokens[3]);
  272. String name = tokens[4];
  273. if (cat.equalsIgnoreCase("none")) {
  274. colorTemp = Color.BLACK;
  275. } else if (cat.equalsIgnoreCase("bus")) {
  276. colorTemp = Color.RED;
  277. } else if (cat.equalsIgnoreCase("underground")) {
  278. colorTemp = Color.BLUE;
  279. } else if (cat.equalsIgnoreCase("train")) {
  280. colorTemp = Color.GREEN;
  281. }
  282. if (type.equalsIgnoreCase("described")) {
  283. if (tokens.length > 4) {
  284. String desc = tokens[5];
  285. triangle = new DescribedPlace(x, y, colorTemp, false, name, cat, type, desc);
  286. position = new Position(triangle.getX(), triangle.getY());
  287. }
  288. } else {
  289. triangle = new NamedPlace(x, y, colorTemp, false, name, cat, type);
  290. position = new Position(triangle.getX(), triangle.getY());
  291. }
  292.  
  293. Boolean key = false;
  294. Set<Place> setFalse = selectedMap.get(key);
  295. if (setFalse == null) {
  296. setFalse = new HashSet<Place>();
  297. selectedMap.put(key, setFalse);
  298. }
  299. setFalse.add(triangle);
  300.  
  301. String namn = triangle.getName();
  302. Set<Place> nPlaces = alla.get(namn);
  303. if (nPlaces == null) {
  304. nPlaces = new HashSet<>();
  305. alla.put(namn, nPlaces);
  306. }
  307. nPlaces.add(triangle);
  308.  
  309. if (triangle.getCat().equalsIgnoreCase("bus")) {
  310. Set<Place> placeSet = categoryMap.get(cat);
  311. if (placeSet == null) {
  312. placeSet = new HashSet<>();
  313. categoryMap.put(cat, placeSet);
  314. }
  315. placeSet.add(triangle);
  316. } else if (triangle.getCat().equalsIgnoreCase("underground")) {
  317. Set<Place> placeSet = categoryMap.get(cat);
  318. if (placeSet == null) {
  319. placeSet = new HashSet<>();
  320. categoryMap.put(cat, placeSet);
  321. }
  322. placeSet.add(triangle);
  323. } else if (triangle.getCat().equalsIgnoreCase("train")) {
  324. Set<Place> placeSet = categoryMap.get(cat);
  325. if (placeSet == null) {
  326. placeSet = new HashSet<>();
  327. categoryMap.put(cat, placeSet);
  328. }
  329. placeSet.add(triangle);
  330. } else {
  331. Set<Place> placeSet = categoryMap.get(cat);
  332. if (placeSet == null) {
  333. placeSet = new HashSet<>();
  334. categoryMap.put(cat, placeSet);
  335. }
  336. placeSet.add(triangle);
  337. }
  338. triangles.add(triangle);
  339. positions.put(position, triangle);
  340.  
  341. center.getChildren().clear();
  342. center.getChildren().add(display);
  343. center.getChildren().addAll(triangles);
  344. changed = true;
  345.  
  346.  
  347. }
  348. br.close();
  349. } catch (FileNotFoundException fnfe) {
  350. System.err.println("No such file!");
  351. } catch (IOException ioe) {
  352. System.err.println("IO-error has occured!");
  353. System.err.println(ioe.getMessage());
  354. } catch (ArrayIndexOutOfBoundsException knas) {
  355. Alert alert = new Alert(AlertType.ERROR);
  356. alert.setHeaderText("Wrong file format");
  357. alert.showAndWait();
  358. }
  359.  
  360. }
  361. }
  362.  
  363. }
  364.  
  365. class ExitHandler implements EventHandler<WindowEvent> {
  366. public void handle(WindowEvent event) {
  367. if (changed) {
  368. Alert alert = new Alert(AlertType.CONFIRMATION);
  369. alert.setHeaderText("Unsaved actions, EXIT anyway?");
  370. Optional<ButtonType> res = alert.showAndWait();
  371. if (res.isPresent() && res.get() == ButtonType.CANCEL)
  372. event.consume();
  373. }
  374.  
  375. }
  376. }
  377.  
  378. abstract class Place extends Polygon {
  379. private double x;
  380. private double y;
  381. private String name;
  382. public boolean selected;
  383. public Paint paint;
  384. private String cat;
  385. private String type;
  386. private Position coordinates;
  387.  
  388. public Place(double x, double y, Paint paint, boolean selected, String name, String cat, String type) {
  389. super(x, y, x - 15, y - 30, x + 15, y - 30);
  390. this.x = x;
  391. this.y = y;
  392. this.paint = paint;
  393. this.name = name;
  394. this.selected = selected;
  395. this.cat = cat;
  396. this.type = type;
  397. this.coordinates = new Position(this.x, this.y);
  398.  
  399. setFill(paint);
  400. setStroke(paint);
  401. relocate(x, y);
  402. setOnMouseClicked(new ClickHandle());
  403.  
  404. }
  405.  
  406. class ClickHandle implements EventHandler<MouseEvent> {
  407. public void handle(MouseEvent event) {
  408. MouseButton button = event.getButton();
  409. if (button == MouseButton.PRIMARY) {
  410. if (!selected) {
  411. Boolean key = true;
  412. Set<Place> set = selectedMap.get(key);
  413. if (set == null) {
  414. set = new HashSet<Place>();
  415. selectedMap.put(key, set);
  416. }
  417. set.add(Place.this);
  418. Set<Place> setFalse = selectedMap.get(false);
  419. if (setFalse == null) {
  420. setFalse = new HashSet<Place>();
  421. selectedMap.put(false, setFalse);
  422. }
  423. setFalse.remove(Place.this);
  424.  
  425. setStroke(null);
  426. setStroke(Color.BLACK);
  427. selected = true;
  428.  
  429. for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
  430. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  431. }
  432. } else {
  433. Boolean key = false;
  434. Set<Place> set = selectedMap.get(key);
  435. if (set == null) {
  436. set = new HashSet<Place>();
  437. selectedMap.put(key, set);
  438. }
  439. set.add(Place.this);
  440. Set<Place> setTrue = selectedMap.get(true);
  441. if (setTrue == null) {
  442. setTrue = new HashSet<Place>();
  443. selectedMap.put(false, setTrue);
  444. }
  445. setTrue.remove(Place.this);
  446.  
  447. setStroke(null);
  448. setStroke(paint);
  449. selected = false;
  450.  
  451. for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
  452. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  453. }
  454. }
  455. }
  456. if (button == MouseButton.SECONDARY) {
  457. double x = getX();
  458. double y = getY();
  459. String name = getName();
  460. DescAlert dialog = new DescAlert(name, x, y);
  461. dialog.showAndWait();
  462. }
  463. }
  464. }
  465.  
  466. public boolean getSelected() {
  467. return selected;
  468.  
  469. }
  470.  
  471. public void setSelected(boolean selected) {
  472. this.selected = selected;
  473. }
  474.  
  475. public String getName() {
  476. return name;
  477.  
  478. }
  479.  
  480. public String toString() {
  481. return type + "," + cat + "," + x + "," + y + "," + name;
  482. }
  483.  
  484. public double getX() {
  485. return x;
  486. }
  487.  
  488. public double getY() {
  489. return y;
  490. }
  491.  
  492. public String getCat() {
  493. return cat;
  494. }
  495.  
  496. public String getType() {
  497. return type;
  498. }
  499.  
  500. public Position getCoordinates() {
  501. return coordinates;
  502. }
  503.  
  504. @Override
  505. public boolean equals(Object o) {
  506. if (this == o)
  507. return true;
  508. if (o == null || getClass() != o.getClass())
  509. return false;
  510. Place place = (Place) o;
  511. return Double.compare(place.x, x) == 0 && Double.compare(place.y, y) == 0;
  512. }
  513.  
  514. @Override
  515. public int hashCode() {
  516. return Objects.hash(x, y, name);
  517. }
  518.  
  519. class DescAlert extends Alert {
  520.  
  521. public DescAlert(String name, double x, double y) {
  522. super(AlertType.INFORMATION);
  523.  
  524. GridPane grid = new GridPane();
  525. getDialogPane().setHeaderText(name + "[" + x + "," + y + "]");
  526. getDialogPane().setContent(grid);
  527. }
  528. }
  529.  
  530. }
  531.  
  532. class DescribedPlace extends Place {
  533. private String description;
  534.  
  535. public DescribedPlace(double x, double y, Paint paint, boolean selected, String name, String cat, String type,
  536. String description) {
  537. super(x, y, paint, selected, name, cat, type);
  538. this.description = description;
  539. this.selected = selected;
  540.  
  541. setOnMouseClicked(new ClickHandleDesc());
  542.  
  543. }
  544.  
  545. class ClickHandleDesc implements EventHandler<MouseEvent> {
  546. public void handle(MouseEvent event) {
  547. MouseButton button = event.getButton();
  548. if (button == MouseButton.PRIMARY) {
  549. if (!selected) {
  550. Boolean key = true;
  551. Set<Place> set = selectedMap.get(key);
  552. if (set == null) {
  553. set = new HashSet<Place>();
  554. selectedMap.put(key, set);
  555. }
  556. set.add(DescribedPlace.this);
  557. Set<Place> setFalse = selectedMap.get(false);
  558. if (setFalse == null) {
  559. setFalse = new HashSet<Place>();
  560. selectedMap.put(false, setFalse);
  561. }
  562. setFalse.remove(DescribedPlace.this);
  563.  
  564. setStroke(Color.BLACK);
  565. selected = true;
  566. } else {
  567. Boolean key = false;
  568. Set<Place> set = selectedMap.get(key);
  569. if (set == null) {
  570. set = new HashSet<Place>();
  571. selectedMap.put(key, set);
  572. }
  573. set.add(DescribedPlace.this);
  574. Set<Place> setTrue = selectedMap.get(true);
  575. if (setTrue == null) {
  576. setTrue = new HashSet<Place>();
  577. selectedMap.put(false, setTrue);
  578. }
  579. setTrue.remove(DescribedPlace.this);
  580.  
  581. setStroke(paint);
  582. selected = false;
  583. }
  584. }
  585. if (button == MouseButton.SECONDARY) {
  586. double x = getX();
  587. double y = getY();
  588. String name = getName();
  589. DescAlertDesc dialog = new DescAlertDesc(name, x, y);
  590. dialog.showAndWait();
  591. }
  592. }
  593. }
  594.  
  595. public String getDescription() {
  596. return description;
  597. }
  598.  
  599. public String toString() {
  600. String str = super.toString() + "," + getDescription();
  601. return str;
  602. }
  603.  
  604. class DescAlertDesc extends Alert {
  605.  
  606. public DescAlertDesc(String name, double x, double y) {
  607. super(AlertType.INFORMATION);
  608.  
  609. GridPane grid = new GridPane();
  610. getDialogPane().setHeaderText(name + "[" + x + "," + y + "]");
  611. grid.addRow(1, new Label(getDescription()));
  612. getDialogPane().setContent(grid);
  613. }
  614. }
  615.  
  616. }
  617.  
  618. class DescribedPlaceAlert extends Alert {
  619. private TextField nameField = new TextField();
  620. private TextField description = new TextField();
  621.  
  622. public DescribedPlaceAlert() {
  623. super(AlertType.CONFIRMATION);
  624. addTextLimiter(description, 50);
  625. addTextLimiter(nameField, 20);
  626. GridPane grid = new GridPane();
  627. grid.addRow(0, new Label("Name:"), nameField);
  628. grid.addRow(0, new Label("Description:"), description);
  629. getDialogPane().setHeaderText("Input name and description");
  630. getDialogPane().setContent(grid);
  631. }
  632.  
  633. public String getName() {
  634. return nameField.getText();
  635. }
  636.  
  637. public String getDescription() {
  638. return description.getText();
  639. }
  640.  
  641. public void setDesc(String string) {
  642. description.setText(string);
  643. }
  644.  
  645. public void setName(String string) {
  646. nameField.setText(string);
  647. }
  648.  
  649. public void addTextLimiter(final TextField tf, final int maxLength) {
  650. tf.textProperty().addListener(new ChangeListener<String>() {
  651. @Override
  652. public void changed(final ObservableValue<? extends String> ov, final String oldValue,
  653. final String newValue) {
  654. if (tf.getText().length() > maxLength) {
  655. String s = tf.getText().substring(0, maxLength);
  656. tf.setText(s);
  657. }
  658. }
  659. });
  660. }
  661.  
  662. }
  663.  
  664. class NamedPlace extends Place {
  665.  
  666. public NamedPlace(double x, double y, Paint paint, boolean selected, String name, String cat, String type) {
  667. super(x, y, paint, selected, name, cat, type);
  668. }
  669.  
  670. public String toString() {
  671. String str = super.toString();
  672. return str;
  673. }
  674.  
  675. }
  676.  
  677. class NamedPlaceAlert extends Alert {
  678. private TextField nameField = new TextField();
  679.  
  680. public NamedPlaceAlert() {
  681. super(AlertType.CONFIRMATION);
  682. addTextLimiter(nameField, 20);
  683. GridPane grid = new GridPane();
  684. grid.addRow(0, new Label("Namn:"), nameField);
  685. getDialogPane().setHeaderText("Input name");
  686. getDialogPane().setContent(grid);
  687. }
  688.  
  689. public String getName() {
  690. return nameField.getText();
  691. }
  692.  
  693. public void setName(String string) {
  694. nameField.setText(string);
  695. }
  696.  
  697. public void addTextLimiter(final TextField tf, final int maxLength) {
  698. tf.textProperty().addListener(new ChangeListener<String>() {
  699. @Override
  700. public void changed(final ObservableValue<? extends String> ov, final String oldValue,
  701. final String newValue) {
  702. if (tf.getText().length() > maxLength) {
  703. String s = tf.getText().substring(0, maxLength);
  704. tf.setText(s);
  705. }
  706. }
  707. });
  708. }
  709.  
  710. }
  711.  
  712. class ViewCat implements EventHandler<MouseEvent> {
  713. public void handle(MouseEvent mouseEvent) {
  714. String item = listView.getSelectionModel().getSelectedItem();
  715. if (item.equalsIgnoreCase("bus")) {
  716. Set<Place> set = categoryMap.get("Bus");
  717. if (set != null) {
  718. Iterator<Place> iter = set.iterator();
  719. while (iter.hasNext()) {
  720. Place p = iter.next();
  721. p.setVisible(true);
  722. p.setStroke(null);
  723. }
  724. }
  725. } else if (item.equalsIgnoreCase("underground")) {
  726. Set<Place> set = categoryMap.get("Underground");
  727. if (set != null) {
  728. Iterator<Place> iter = set.iterator();
  729. while (iter.hasNext()) {
  730. Place p = iter.next();
  731. p.setVisible(true);
  732. p.setStroke(null);
  733. }
  734. }
  735. } else if (item.equalsIgnoreCase("train")) {
  736. Set<Place> set = categoryMap.get("Train");
  737. if (set != null) {
  738. Iterator<Place> iter = set.iterator();
  739. while (iter.hasNext()) {
  740. Place p = iter.next();
  741. p.setVisible(true);
  742. p.setStroke(null);
  743. }
  744. }
  745. }
  746. }
  747. }
  748.  
  749. class HideCatagory implements EventHandler<ActionEvent> {
  750. public void handle(ActionEvent event) {
  751. String item = listView.getSelectionModel().getSelectedItem();
  752. if (item.equalsIgnoreCase("bus")) {
  753. Set<Place> set = categoryMap.get("Bus");
  754. if (set != null) {
  755. Iterator<Place> iter = set.iterator();
  756. while (iter.hasNext()) {
  757. Place p = iter.next();
  758. p.setVisible(false);
  759. }
  760. }
  761. } else if (item.equalsIgnoreCase("underground")) {
  762. Set<Place> set = categoryMap.get("Underground");
  763. if (set != null) {
  764. Iterator<Place> iter = set.iterator();
  765. while (iter.hasNext()) {
  766. Place p = iter.next();
  767. p.setVisible(false);
  768. }
  769. }
  770. } else if (item.equalsIgnoreCase("Train")) {
  771. Set<Place> set = categoryMap.get("Train");
  772. if (set != null) {
  773. Iterator<Place> iter = set.iterator();
  774. while (iter.hasNext()) {
  775. Place p = iter.next();
  776. p.setVisible(false);
  777. }
  778. }
  779. }
  780. }
  781. }
  782.  
  783. class Coordinates implements EventHandler<ActionEvent> {
  784. public void handle(ActionEvent event) {
  785. CoordinatesAlert alert = new CoordinatesAlert();
  786. Optional<ButtonType> result = alert.showAndWait();
  787. if (result.isPresent() && result.get() == ButtonType.OK) {
  788. try {
  789. position = new Position(alert.getXField(), alert.getYField());
  790. positions.get(position).setId("npe check");
  791.  
  792.  
  793. Set<Place> set = selectedMap.get(true);
  794. if (set != null) {
  795. Iterator<Place> iter = set.iterator();
  796. while (iter.hasNext()) {
  797. Place p = iter.next();
  798. Set<Place> setFalse = selectedMap.computeIfAbsent(false, k -> new HashSet<Place>());
  799. setFalse.add(p);
  800. }
  801. Set<Place> setTrue = selectedMap.get(true);
  802. Iterator<Place> trueIt = setTrue.iterator();
  803. while (trueIt.hasNext()) {
  804. Place p = trueIt.next();
  805. if (setTrue == null) {
  806. setTrue = new HashSet<Place>();
  807. selectedMap.put(true, setTrue);
  808. }
  809. trueIt.remove();
  810. p.setSelected(false);
  811. p.setStroke(null);
  812. }
  813. }
  814. positions.get(position).setSelected(true);
  815. positions.get(position).setVisible(true);
  816. positions.get(position).setStroke(Color.BLACK);
  817. Place p1 = positions.get(position);
  818. Boolean key = true;
  819. Set<Place> addPosToTrueSet = selectedMap.get(key);
  820. if (addPosToTrueSet == null) {
  821. addPosToTrueSet = new HashSet<Place>();
  822. selectedMap.put(key, addPosToTrueSet);
  823. }
  824. addPosToTrueSet.add(p1);
  825. } catch (NumberFormatException ioe) {
  826. System.out.println("error");
  827. } catch (NullPointerException npe) {
  828. Alert alert1 = new Alert(AlertType.ERROR, "No such place");
  829. alert1.showAndWait();
  830. }
  831. }
  832. }
  833. }
  834.  
  835. class Remove implements EventHandler<ActionEvent> {
  836. public void handle(ActionEvent event) {
  837. Set<Place> setSelected = selectedMap.get(true);
  838. if (setSelected != null) {
  839. Iterator<Place> iterSelected = setSelected.iterator();
  840. while (iterSelected.hasNext()) {
  841. Place p = iterSelected.next();
  842. String name = p.getName();
  843. Set<Place> nameSet = alla.get(name);
  844. Iterator<Place> iterName = nameSet.iterator();
  845. while (iterName.hasNext()) {
  846. Place item = iterName.next();
  847. if (p.equals(item))
  848. ;
  849. {
  850. iterName.remove();
  851. break;
  852. }
  853. }
  854. String cat = p.getCat();
  855. Set<Place> catSet = categoryMap.get(cat);
  856. Iterator<Place> iterCat = catSet.iterator();
  857. while (iterCat.hasNext()) {
  858. Place item = iterCat.next();
  859. if (item.equals(p)) {
  860. iterCat.remove();
  861. }
  862. }
  863. Position pos = p.getCoordinates();
  864. positions.remove(pos);
  865. triangles.remove(p);
  866. iterSelected.remove();
  867. }
  868. }
  869.  
  870. center.getChildren().clear();
  871. center.getChildren().add(display);
  872. center.getChildren().addAll(triangles);
  873.  
  874. for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
  875. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  876. }
  877. for (Map.Entry<String, Set<Place>> entry : categoryMap.entrySet()) {
  878. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  879. }
  880. for (Map.Entry<String, Set<Place>> entry : alla.entrySet()) {
  881. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  882. }
  883. for (Map.Entry<Position, Place> entry : positions.entrySet()) {
  884. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  885. }
  886. for (Place p : triangles) {
  887. System.out.println(p.toString());
  888. }
  889. }
  890. }
  891.  
  892. class Hide implements EventHandler<ActionEvent> {
  893. public void handle(ActionEvent event) {
  894. Set<Place> set = selectedMap.get(true);
  895. if (set != null) {
  896. Iterator<Place> iter = set.iterator();
  897. while (iter.hasNext()) {
  898. Place p = iter.next();
  899. Boolean key = false;
  900. Set<Place> setFalse = selectedMap.get(key);
  901. if (setFalse == null) {
  902. setFalse = new HashSet<Place>();
  903. selectedMap.put(key, setFalse);
  904. }
  905. setFalse.add(p);
  906. iter.remove();
  907. p.setVisible(false);
  908. p.setSelected(false);
  909. p.setStroke(color);
  910.  
  911. for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
  912. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  913. }
  914. }
  915. }
  916. }
  917. }
  918.  
  919. class Search implements EventHandler<ActionEvent> {
  920. @Override
  921. public void handle(ActionEvent event) {
  922. try {
  923. Set<Place> set = selectedMap.get(true);
  924. if (set != null) {
  925. Iterator<Place> iter = set.iterator();
  926. while (iter.hasNext()) {
  927. Place p = iter.next();
  928. Set<Place> setFalse = selectedMap.computeIfAbsent(false, k -> new HashSet<Place>());
  929. setFalse.add(p);
  930. }
  931. Set<Place> setTrue = selectedMap.get(true);
  932. Iterator<Place> trueIt = setTrue.iterator();
  933. while (trueIt.hasNext()) {
  934. Place p = trueIt.next();
  935. if (setTrue == null) {
  936. setTrue = new HashSet<Place>();
  937. selectedMap.put(true, setTrue);
  938. }
  939. trueIt.remove();
  940. p.setSelected(false);
  941. p.setStroke(null);
  942. }
  943. }
  944. String word = search.getText();
  945. Set<Place> nameSet = alla.get(word);
  946. if (nameSet != null) {
  947. for (Place p : nameSet) {
  948. Set<Place> setTrue = selectedMap.computeIfAbsent(true, k -> new HashSet<Place>());
  949. setTrue.add(p);
  950. if (nameSet == null) {
  951. nameSet = new HashSet<Place>();
  952. selectedMap.put(false, nameSet);
  953. }
  954. Set<Place> setFalse = selectedMap.get(false);
  955. setFalse.remove(p);
  956. p.setVisible(true);
  957. p.setSelected(true);
  958. for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
  959. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  960. }
  961. }
  962. Set<Place> borderSet = selectedMap.get(true);
  963. Iterator<Place> borderIt = borderSet.iterator();
  964. while (borderIt.hasNext()) {
  965. Place p = borderIt.next();
  966. p.setStroke(Color.BLACK);
  967. }
  968. }
  969. } catch(NullPointerException npe){
  970. System.out.println("Can not find " + search.getText());
  971. }
  972. }
  973. }
  974.  
  975. class NewLocation implements EventHandler<MouseEvent> {
  976. public void handle(MouseEvent event) {
  977. String temp = "";
  978. display.setCursor(Cursor.DEFAULT);
  979. if (listView.getSelectionModel().isEmpty()) {
  980. color = Color.BLACK;
  981. temp = "none";
  982. } else if (listView.getSelectionModel().getSelectedItem().equalsIgnoreCase("bus")) {
  983. color = Color.RED;
  984. temp = "Bus";
  985. } else if (listView.getSelectionModel().getSelectedItem().equalsIgnoreCase("underground")) {
  986. color = Color.BLUE;
  987. temp = "Underground";
  988. } else if (listView.getSelectionModel().getSelectedItem().equalsIgnoreCase("train")) {
  989. color = Color.GREEN;
  990. temp = "Train";
  991. }
  992. if (namnBtn.isSelected()) {
  993. String type = "Named";
  994. NamedPlaceAlert nameAlert = new NamedPlaceAlert();
  995. Optional<ButtonType> nameResult = nameAlert.showAndWait();
  996. if (nameAlert.getName().isEmpty()) {
  997. Alert alert = new Alert(AlertType.ERROR, "Please input a name");
  998. alert.showAndWait();
  999. display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
  1000. newBtn.setDisable(false);
  1001. return;
  1002. }
  1003. if (nameResult.get() == ButtonType.OK) {
  1004. Position tempP = new Position(event.getX(), event.getY());
  1005. if (positions.containsKey(tempP)) {
  1006. Alert msg = new Alert(Alert.AlertType.ERROR, "Place already exists on given coordinates");
  1007. msg.showAndWait();
  1008. return;
  1009. }
  1010. triangle = new NamedPlace(event.getX() - 15, event.getY() - 30, color, false, nameAlert.getName(),
  1011. temp, type);
  1012. position = new Position(triangle.getX(), triangle.getY());
  1013. center.getChildren().add(triangle);
  1014. Boolean key = false;
  1015. Set<Place> setFalse = selectedMap.get(key);
  1016. if (setFalse == null) {
  1017. setFalse = new HashSet<Place>();
  1018. selectedMap.put(key, setFalse);
  1019. }
  1020. setFalse.add(triangle);
  1021.  
  1022. String namn = triangle.getName();
  1023. Set<Place> nPlaces = alla.get(namn);
  1024. if (nPlaces == null) {
  1025. nPlaces = new HashSet<>();
  1026. alla.put(namn, nPlaces);
  1027. }
  1028. nPlaces.add(triangle);
  1029.  
  1030. triangles.add(triangle);
  1031. positions.put(position, triangle);
  1032.  
  1033. changed = true;
  1034.  
  1035. if (triangle.getCat().equalsIgnoreCase("bus")) {
  1036. String cat = triangle.getCat();
  1037. Set<Place> placeSet = categoryMap.get(cat);
  1038. if (placeSet == null) {
  1039. placeSet = new HashSet<>();
  1040. categoryMap.put(cat, placeSet);
  1041. }
  1042. placeSet.add(triangle);
  1043. } else if (triangle.getCat().equalsIgnoreCase("underground")) {
  1044. String cat = triangle.getCat();
  1045. Set<Place> placeSet = categoryMap.get(cat);
  1046. if (placeSet == null) {
  1047. placeSet = new HashSet<>();
  1048. categoryMap.put(cat, placeSet);
  1049. }
  1050. placeSet.add(triangle);
  1051. } else if (triangle.getCat().equalsIgnoreCase("train")) {
  1052. String cat = triangle.getCat();
  1053. Set<Place> placeSet = categoryMap.get(cat);
  1054. if (placeSet == null) {
  1055. placeSet = new HashSet<>();
  1056. categoryMap.put(cat, placeSet);
  1057. }
  1058. placeSet.add(triangle);
  1059. } else {
  1060. String cat = triangle.getCat();
  1061. Set<Place> placeSet = categoryMap.get(cat);
  1062. if (placeSet == null) {
  1063. placeSet = new HashSet<>();
  1064. categoryMap.put(cat, placeSet);
  1065. }
  1066. placeSet.add(triangle);
  1067. }
  1068.  
  1069. }
  1070. display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
  1071. newBtn.setDisable(false);
  1072.  
  1073. } else if (describedBtn.isSelected()) {
  1074. String type = "Described";
  1075. DescribedPlaceAlert describedAlert = new DescribedPlaceAlert();
  1076. Optional<ButtonType> describedResult = describedAlert.showAndWait();
  1077. if (describedAlert.getName().isEmpty() || describedAlert.getDescription().isEmpty()) {
  1078. Alert alert = new Alert(AlertType.ERROR, "Please input both fields");
  1079. alert.showAndWait();
  1080. display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
  1081. newBtn.setDisable(false);
  1082. return;
  1083. }
  1084. if (describedResult.get() == ButtonType.OK) {
  1085. Position tempP = new Position(event.getX(), event.getY());
  1086. if (positions.containsKey(tempP)) {
  1087. Alert msg = new Alert(Alert.AlertType.ERROR, "Place already exists on given coordinates");
  1088. msg.showAndWait();
  1089. }
  1090. triangle = new DescribedPlace(event.getX() - 15, event.getY() - 30, color, false,
  1091. describedAlert.getName(), temp, type, describedAlert.getDescription());
  1092. position = new Position(triangle.getX(), triangle.getY());
  1093. center.getChildren().add(triangle);
  1094.  
  1095. Boolean key = false;
  1096. Set<Place> setFalse = selectedMap.get(key);
  1097. if (setFalse == null) {
  1098. setFalse = new HashSet<Place>();
  1099. selectedMap.put(key, setFalse);
  1100. }
  1101. setFalse.add(triangle);
  1102.  
  1103. String namn = triangle.getName();
  1104. Set<Place> nPlaces = alla.get(namn);
  1105. if (nPlaces == null) {
  1106. nPlaces = new HashSet<>();
  1107. alla.put(namn, nPlaces);
  1108. }
  1109. nPlaces.add(triangle);
  1110.  
  1111. if (triangle.getCat().equalsIgnoreCase("Bus")) {
  1112. String cat = triangle.getCat();
  1113. Set<Place> placeSet = categoryMap.get(cat);
  1114. if (placeSet == null) {
  1115. placeSet = new HashSet<>();
  1116. categoryMap.put(cat, placeSet);
  1117. }
  1118. placeSet.add(triangle);
  1119. } else if (triangle.getCat().equalsIgnoreCase("Underground")) {
  1120. String cat = triangle.getCat();
  1121. Set<Place> placeSet = categoryMap.get(cat);
  1122. if (placeSet == null) {
  1123. placeSet = new HashSet<>();
  1124. categoryMap.put(cat, placeSet);
  1125. }
  1126. placeSet.add(triangle);
  1127. } else if (triangle.getCat().equalsIgnoreCase("Train")) {
  1128. String cat = triangle.getCat();
  1129. Set<Place> placeSet = categoryMap.get(cat);
  1130. if (placeSet == null) {
  1131. placeSet = new HashSet<>();
  1132. categoryMap.put(cat, placeSet);
  1133. }
  1134. placeSet.add(triangle);
  1135. } else {
  1136. String cat = triangle.getCat();
  1137. Set<Place> placeSet = categoryMap.get(cat);
  1138. if (placeSet == null) {
  1139. placeSet = new HashSet<>();
  1140. categoryMap.put(cat, placeSet);
  1141. }
  1142. placeSet.add(triangle);
  1143. }
  1144. triangles.add(triangle);
  1145. positions.put(position, triangle);
  1146.  
  1147. changed = true;
  1148. }
  1149. display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
  1150. newBtn.setDisable(false);
  1151.  
  1152. }
  1153. }
  1154. }
  1155.  
  1156. class NewLocationEnable implements EventHandler<MouseEvent> {
  1157. public void handle(MouseEvent event) {
  1158. try {
  1159. if (!image.equals(null)) {
  1160. display.addEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
  1161. display.setCursor(Cursor.CROSSHAIR);
  1162. newBtn.setDisable(true);
  1163. }
  1164.  
  1165. } catch (NullPointerException npe) {
  1166. System.out.println("Error! No map file added");
  1167. }
  1168.  
  1169. }
  1170.  
  1171. }
  1172.  
  1173. public static void main(String[] args) {
  1174. launch(args);
  1175. }
  1176. }
  1177.  
  1178. class CoordinatesAlert extends Alert {
  1179. private TextField xField = new TextField();
  1180. private TextField yField = new TextField();
  1181.  
  1182. public CoordinatesAlert() {
  1183. super(AlertType.CONFIRMATION);
  1184.  
  1185. GridPane grid = new GridPane();
  1186.  
  1187. grid.addRow(0, new Label("x:"), xField);
  1188. grid.addRow(1, new Label("y:"), yField);
  1189. getDialogPane().setContent(grid);
  1190. getDialogPane().setHeaderText("Input Coordinates");
  1191. getDialogPane().setGraphic(null);
  1192. UnaryOperator<TextFormatter.Change> filter = new UnaryOperator<TextFormatter.Change>() {
  1193.  
  1194. @Override
  1195. public TextFormatter.Change apply(TextFormatter.Change t) {
  1196.  
  1197. if (t.isReplaced())
  1198. if (t.getText().matches("[^0-9]"))
  1199. t.setText(t.getControlText().substring(t.getRangeStart(), t.getRangeEnd()));
  1200.  
  1201. if (t.isAdded()) {
  1202. if (t.getControlText().contains(".")) {
  1203. if (t.getText().matches("[^0-9]")) {
  1204. t.setText("");
  1205. }
  1206. } else if (t.getText().matches("[^0-9.]")) {
  1207. t.setText("");
  1208. Alert msg = new Alert(Alert.AlertType.ERROR);
  1209. msg.setContentText("Only numeric values accepted Henrik!");
  1210. msg.showAndWait();
  1211. }
  1212. }
  1213.  
  1214. return t;
  1215. }
  1216. };
  1217.  
  1218. xField.setTextFormatter(new TextFormatter<Object>(filter));
  1219. yField.setTextFormatter(new TextFormatter<Object>(filter));
  1220. }
  1221.  
  1222. public double getXField() {
  1223. return Double.parseDouble(xField.getText());
  1224. }
  1225.  
  1226. public double getYField() {
  1227. return Double.parseDouble(yField.getText());
  1228.  
  1229. }
  1230. }
  1231.  
  1232. class CoordinatesError extends Alert {
  1233. public CoordinatesError() {
  1234. super(AlertType.ERROR);
  1235.  
  1236. getDialogPane().setHeaderText("No location at given coordinates!");
  1237.  
  1238. }
  1239. }
  1240.  
  1241. class Position {
  1242. private double x;
  1243. private double y;
  1244.  
  1245. public Position(double x, double y) {
  1246. this.x = x;
  1247. this.y = y;
  1248. }
  1249.  
  1250. @Override
  1251. public boolean equals(Object o) {
  1252. if (this == o)
  1253. return true;
  1254. if (o == null || getClass() != o.getClass())
  1255. return false;
  1256. Position position = (Position) o;
  1257. return Double.compare(position.x, x) == 0 && Double.compare(position.y, y) == 0;
  1258. }
  1259.  
  1260. @Override
  1261. public int hashCode() {
  1262. return Objects.hash(x, y);
  1263. }
  1264.  
  1265. @Override
  1266. public String toString() {
  1267. return "Position{" + "x=" + x + ", y=" + y + '}';
  1268. }
  1269.  
  1270. public double getX() {
  1271. return x;
  1272. }
  1273.  
  1274. public double getY() {
  1275. return y;
  1276. }
  1277.  
  1278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement