Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- @author Mustafa Bay 970225-8857, Jakob Ödman Lindecrantz 970724-7475
- */
- //Fel i search, använder for loop genom hela triangles
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.*;
- import java.util.function.UnaryOperator;
- import javax.imageio.ImageIO;
- import javafx.application.Application;
- import javafx.beans.value.ChangeListener;
- import javafx.beans.value.ObservableValue;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.embed.swing.SwingFXUtils;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Cursor;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.Button;
- import javafx.scene.control.ButtonType;
- import javafx.scene.control.Label;
- import javafx.scene.control.ListView;
- import javafx.scene.control.Menu;
- import javafx.scene.control.MenuBar;
- import javafx.scene.control.MenuItem;
- import javafx.scene.control.RadioButton;
- import javafx.scene.control.TextField;
- import javafx.scene.control.TextFormatter;
- import javafx.scene.control.ToggleGroup;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.MouseButton;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.scene.paint.Paint;
- import javafx.scene.shape.Polygon;
- import javafx.stage.FileChooser;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- public class Main extends Application {
- private ImageView display;
- private TextField search;
- private RadioButton namnBtn;
- private RadioButton describedBtn;
- private Image image;
- private Pane center;
- private ListView<String> listView;
- private boolean changed = false;
- private Button newBtn;
- private NewLocation newLocation = new NewLocation();
- private Stage primaryStage;
- private Paint color;
- private Place triangle;
- private Position position;
- private Paint colorTemp;
- private boolean mapExist = false;
- private ObservableList<String> names = FXCollections.observableArrayList("Bus", "Underground", "Train");
- private Set<Place> triangles = new HashSet<>();
- private Map<Position, Place> positions = new HashMap<>();
- private Map<String, Set<Place>> alla = new HashMap<>();
- private Map<String, Set<Place>> categoryMap = new HashMap<>();
- private Map<Boolean, Set<Place>> selectedMap = new TreeMap<>();
- class FileLoader implements EventHandler<ActionEvent> {
- public void handle(ActionEvent t) {
- if (changed) {
- Alert alert = new Alert(AlertType.CONFIRMATION);
- alert.setHeaderText("Unsaved actions, LOAD MAP anyway?");
- Optional<ButtonType> res = alert.showAndWait();
- if (res.isPresent() && res.get() == ButtonType.CANCEL) {
- return;
- }
- }
- FileChooser fileChooser = new FileChooser();
- FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
- fileChooser.getExtensionFilters().add(extFilterPNG);
- File file = fileChooser.showOpenDialog(primaryStage);
- if (file == null)
- return;
- try {
- BufferedImage bufferedImage = ImageIO.read(file);
- image = SwingFXUtils.toFXImage(bufferedImage, null);
- display.setImage(image);
- primaryStage.sizeToScene();
- } catch (IOException ex) {
- System.err.println("IOException error");
- }
- center.getChildren().clear();
- center.getChildren().add(display);
- changed = false;
- mapExist = true;
- positions.clear();
- alla.clear();
- selectedMap.clear();
- categoryMap.clear();
- triangles.clear();
- }
- }
- public void start(Stage primaryStage) {
- this.primaryStage = primaryStage;
- BorderPane root = new BorderPane();
- root.setStyle("-fx-font-size: 18");
- primaryStage.setTitle("Map");
- center = new Pane();
- root.setCenter(center);
- display = new ImageView();
- center.getChildren().add(display);
- VBox vbox = new VBox();
- MenuBar menuBar = new MenuBar();
- vbox.getChildren().add(menuBar);
- Menu file = new Menu("File");
- menuBar.getMenus().add(file);
- MenuItem m1 = new MenuItem("Load Map");
- MenuItem m2 = new MenuItem("Load Places");
- MenuItem m3 = new MenuItem("Save");
- MenuItem m4 = new MenuItem("Exit");
- file.getItems().add(m1);
- file.getItems().add(m2);
- file.getItems().add(m3);
- file.getItems().add(m4);
- m1.setOnAction(new FileLoader());
- m2.setOnAction(new OpenPlaces());
- m3.setOnAction(new SaveHandler());
- m4.setOnAction(e -> primaryStage.fireEvent(new WindowEvent(primaryStage, WindowEvent.WINDOW_CLOSE_REQUEST)));
- primaryStage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, new ExitHandler());
- HBox hboxTop = new HBox(5);
- vbox.getChildren().add(hboxTop);
- hboxTop.setPadding(new Insets(5));
- hboxTop.setAlignment(Pos.CENTER);
- newBtn = new Button("New");
- hboxTop.getChildren().add(newBtn);
- newBtn.setOnMousePressed(new NewLocationEnable());
- VBox radioBtns = new VBox();
- namnBtn = new RadioButton("Named");
- describedBtn = new RadioButton("Described");
- ToggleGroup group = new ToggleGroup();
- namnBtn.setToggleGroup(group);
- describedBtn.setToggleGroup(group);
- radioBtns.getChildren().addAll(namnBtn, describedBtn);
- hboxTop.getChildren().add(radioBtns);
- search = new TextField();
- hboxTop.getChildren().add(search);
- Button searchBtn = new Button("Search");
- searchBtn.setOnAction(new Search());
- hboxTop.getChildren().add(searchBtn);
- Button hideBtn = new Button("Hide");
- hideBtn.setOnAction(new Hide());
- hboxTop.getChildren().add(hideBtn);
- Button removeBtn = new Button("Remove");
- removeBtn.setOnAction(new Remove());
- hboxTop.getChildren().add(removeBtn);
- Button coordinatesBtn = new Button("Coordinates");
- coordinatesBtn.setOnAction(new Coordinates());
- hboxTop.getChildren().add(coordinatesBtn);
- root.setTop(vbox);
- VBox right = new VBox(5);
- right.setPadding(new Insets(5));
- right.getChildren().add(new Label("Categories"));
- listView = new ListView<>(names);
- right.getChildren().add(listView);
- root.setRight(right);
- right.setAlignment(Pos.CENTER);
- listView.setPrefSize(300, 130);
- Button hideCatagoryBtn = new Button("Hide Category");
- hideCatagoryBtn.setOnAction(new HideCatagory());
- listView.setOnMouseClicked(new ViewCat());
- right.getChildren().add(hideCatagoryBtn);
- Scene scene = new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- class SaveHandler implements EventHandler<ActionEvent> {
- @Override
- public void handle(ActionEvent event) {
- try {
- // Otydligt i instruktionerna ifall vi skulle ange filtyp vid sparning. Ange
- // ".txt" vid sparning för att spara som textfil.
- FileChooser fileChooser = new FileChooser();
- File file = fileChooser.showSaveDialog(primaryStage);
- if (file == null)
- return;
- String filnamn = file.getAbsolutePath();
- FileWriter outFile = new FileWriter(filnamn);
- PrintWriter out = new PrintWriter(outFile);
- for (Place p : triangles) {
- out.println(p.toString());
- out.close();
- }
- changed = false;
- } catch (IOException ioe) {
- System.err.println("Fel!");
- }
- }
- }
- class OpenPlaces implements EventHandler<ActionEvent> {
- @Override
- public void handle(ActionEvent event) {
- if (!mapExist) {
- Alert alert = new Alert(AlertType.ERROR);
- alert.setHeaderText("Load a map first!");
- alert.showAndWait();
- } else {
- triangles.clear();
- positions.clear();
- if (changed) {
- Alert alert = new Alert(AlertType.CONFIRMATION);
- alert.setHeaderText("Unsaved actions, LOAD PLACES anyway?");
- Optional<ButtonType> res = alert.showAndWait();
- if (res.get() == ButtonType.CANCEL) {
- return;
- }
- }
- try {
- FileChooser fileChooser = new FileChooser();
- File file = fileChooser.showOpenDialog(primaryStage);
- if (file == null)
- return;
- String filnamn = file.getAbsolutePath();
- FileReader in = new FileReader(filnamn);
- BufferedReader br = new BufferedReader(in);
- String line;
- while ((line = br.readLine()) != null) {
- String[] tokens = line.split(",");
- String type = tokens[0];
- String cat = tokens[1];
- double x = Double.parseDouble(tokens[2]);
- double y = Double.parseDouble(tokens[3]);
- String name = tokens[4];
- if (cat.equalsIgnoreCase("none")) {
- colorTemp = Color.BLACK;
- } else if (cat.equalsIgnoreCase("bus")) {
- colorTemp = Color.RED;
- } else if (cat.equalsIgnoreCase("underground")) {
- colorTemp = Color.BLUE;
- } else if (cat.equalsIgnoreCase("train")) {
- colorTemp = Color.GREEN;
- }
- if (type.equalsIgnoreCase("described")) {
- if (tokens.length > 4) {
- String desc = tokens[5];
- triangle = new DescribedPlace(x, y, colorTemp, false, name, cat, type, desc);
- position = new Position(triangle.getX(), triangle.getY());
- }
- } else {
- triangle = new NamedPlace(x, y, colorTemp, false, name, cat, type);
- position = new Position(triangle.getX(), triangle.getY());
- }
- Boolean key = false;
- Set<Place> setFalse = selectedMap.get(key);
- if (setFalse == null) {
- setFalse = new HashSet<Place>();
- selectedMap.put(key, setFalse);
- }
- setFalse.add(triangle);
- String namn = triangle.getName();
- Set<Place> nPlaces = alla.get(namn);
- if (nPlaces == null) {
- nPlaces = new HashSet<>();
- alla.put(namn, nPlaces);
- }
- nPlaces.add(triangle);
- if (triangle.getCat().equalsIgnoreCase("bus")) {
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else if (triangle.getCat().equalsIgnoreCase("underground")) {
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else if (triangle.getCat().equalsIgnoreCase("train")) {
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else {
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- }
- triangles.add(triangle);
- positions.put(position, triangle);
- center.getChildren().clear();
- center.getChildren().add(display);
- center.getChildren().addAll(triangles);
- changed = true;
- }
- br.close();
- } catch (FileNotFoundException fnfe) {
- System.err.println("No such file!");
- } catch (IOException ioe) {
- System.err.println("IO-error has occured!");
- System.err.println(ioe.getMessage());
- } catch (ArrayIndexOutOfBoundsException knas) {
- Alert alert = new Alert(AlertType.ERROR);
- alert.setHeaderText("Wrong file format");
- alert.showAndWait();
- }
- }
- }
- }
- class ExitHandler implements EventHandler<WindowEvent> {
- public void handle(WindowEvent event) {
- if (changed) {
- Alert alert = new Alert(AlertType.CONFIRMATION);
- alert.setHeaderText("Unsaved actions, EXIT anyway?");
- Optional<ButtonType> res = alert.showAndWait();
- if (res.isPresent() && res.get() == ButtonType.CANCEL)
- event.consume();
- }
- }
- }
- abstract class Place extends Polygon {
- private double x;
- private double y;
- private String name;
- public boolean selected;
- public Paint paint;
- private String cat;
- private String type;
- private Position coordinates;
- public Place(double x, double y, Paint paint, boolean selected, String name, String cat, String type) {
- super(x, y, x - 15, y - 30, x + 15, y - 30);
- this.x = x;
- this.y = y;
- this.paint = paint;
- this.name = name;
- this.selected = selected;
- this.cat = cat;
- this.type = type;
- this.coordinates = new Position(this.x, this.y);
- setFill(paint);
- setStroke(paint);
- relocate(x, y);
- setOnMouseClicked(new ClickHandle());
- }
- class ClickHandle implements EventHandler<MouseEvent> {
- public void handle(MouseEvent event) {
- MouseButton button = event.getButton();
- if (button == MouseButton.PRIMARY) {
- if (!selected) {
- Boolean key = true;
- Set<Place> set = selectedMap.get(key);
- if (set == null) {
- set = new HashSet<Place>();
- selectedMap.put(key, set);
- }
- set.add(Place.this);
- Set<Place> setFalse = selectedMap.get(false);
- if (setFalse == null) {
- setFalse = new HashSet<Place>();
- selectedMap.put(false, setFalse);
- }
- setFalse.remove(Place.this);
- setStroke(null);
- setStroke(Color.BLACK);
- selected = true;
- for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- } else {
- Boolean key = false;
- Set<Place> set = selectedMap.get(key);
- if (set == null) {
- set = new HashSet<Place>();
- selectedMap.put(key, set);
- }
- set.add(Place.this);
- Set<Place> setTrue = selectedMap.get(true);
- if (setTrue == null) {
- setTrue = new HashSet<Place>();
- selectedMap.put(false, setTrue);
- }
- setTrue.remove(Place.this);
- setStroke(null);
- setStroke(paint);
- selected = false;
- for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- }
- }
- if (button == MouseButton.SECONDARY) {
- double x = getX();
- double y = getY();
- String name = getName();
- DescAlert dialog = new DescAlert(name, x, y);
- dialog.showAndWait();
- }
- }
- }
- public boolean getSelected() {
- return selected;
- }
- public void setSelected(boolean selected) {
- this.selected = selected;
- }
- public String getName() {
- return name;
- }
- public String toString() {
- return type + "," + cat + "," + x + "," + y + "," + name;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public String getCat() {
- return cat;
- }
- public String getType() {
- return type;
- }
- public Position getCoordinates() {
- return coordinates;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o)
- return true;
- if (o == null || getClass() != o.getClass())
- return false;
- Place place = (Place) o;
- return Double.compare(place.x, x) == 0 && Double.compare(place.y, y) == 0;
- }
- @Override
- public int hashCode() {
- return Objects.hash(x, y, name);
- }
- class DescAlert extends Alert {
- public DescAlert(String name, double x, double y) {
- super(AlertType.INFORMATION);
- GridPane grid = new GridPane();
- getDialogPane().setHeaderText(name + "[" + x + "," + y + "]");
- getDialogPane().setContent(grid);
- }
- }
- }
- class DescribedPlace extends Place {
- private String description;
- public DescribedPlace(double x, double y, Paint paint, boolean selected, String name, String cat, String type,
- String description) {
- super(x, y, paint, selected, name, cat, type);
- this.description = description;
- this.selected = selected;
- setOnMouseClicked(new ClickHandleDesc());
- }
- class ClickHandleDesc implements EventHandler<MouseEvent> {
- public void handle(MouseEvent event) {
- MouseButton button = event.getButton();
- if (button == MouseButton.PRIMARY) {
- if (!selected) {
- Boolean key = true;
- Set<Place> set = selectedMap.get(key);
- if (set == null) {
- set = new HashSet<Place>();
- selectedMap.put(key, set);
- }
- set.add(DescribedPlace.this);
- Set<Place> setFalse = selectedMap.get(false);
- if (setFalse == null) {
- setFalse = new HashSet<Place>();
- selectedMap.put(false, setFalse);
- }
- setFalse.remove(DescribedPlace.this);
- setStroke(Color.BLACK);
- selected = true;
- } else {
- Boolean key = false;
- Set<Place> set = selectedMap.get(key);
- if (set == null) {
- set = new HashSet<Place>();
- selectedMap.put(key, set);
- }
- set.add(DescribedPlace.this);
- Set<Place> setTrue = selectedMap.get(true);
- if (setTrue == null) {
- setTrue = new HashSet<Place>();
- selectedMap.put(false, setTrue);
- }
- setTrue.remove(DescribedPlace.this);
- setStroke(paint);
- selected = false;
- }
- }
- if (button == MouseButton.SECONDARY) {
- double x = getX();
- double y = getY();
- String name = getName();
- DescAlertDesc dialog = new DescAlertDesc(name, x, y);
- dialog.showAndWait();
- }
- }
- }
- public String getDescription() {
- return description;
- }
- public String toString() {
- String str = super.toString() + "," + getDescription();
- return str;
- }
- class DescAlertDesc extends Alert {
- public DescAlertDesc(String name, double x, double y) {
- super(AlertType.INFORMATION);
- GridPane grid = new GridPane();
- getDialogPane().setHeaderText(name + "[" + x + "," + y + "]");
- grid.addRow(1, new Label(getDescription()));
- getDialogPane().setContent(grid);
- }
- }
- }
- class DescribedPlaceAlert extends Alert {
- private TextField nameField = new TextField();
- private TextField description = new TextField();
- public DescribedPlaceAlert() {
- super(AlertType.CONFIRMATION);
- addTextLimiter(description, 50);
- addTextLimiter(nameField, 20);
- GridPane grid = new GridPane();
- grid.addRow(0, new Label("Name:"), nameField);
- grid.addRow(0, new Label("Description:"), description);
- getDialogPane().setHeaderText("Input name and description");
- getDialogPane().setContent(grid);
- }
- public String getName() {
- return nameField.getText();
- }
- public String getDescription() {
- return description.getText();
- }
- public void setDesc(String string) {
- description.setText(string);
- }
- public void setName(String string) {
- nameField.setText(string);
- }
- public void addTextLimiter(final TextField tf, final int maxLength) {
- tf.textProperty().addListener(new ChangeListener<String>() {
- @Override
- public void changed(final ObservableValue<? extends String> ov, final String oldValue,
- final String newValue) {
- if (tf.getText().length() > maxLength) {
- String s = tf.getText().substring(0, maxLength);
- tf.setText(s);
- }
- }
- });
- }
- }
- class NamedPlace extends Place {
- public NamedPlace(double x, double y, Paint paint, boolean selected, String name, String cat, String type) {
- super(x, y, paint, selected, name, cat, type);
- }
- public String toString() {
- String str = super.toString();
- return str;
- }
- }
- class NamedPlaceAlert extends Alert {
- private TextField nameField = new TextField();
- public NamedPlaceAlert() {
- super(AlertType.CONFIRMATION);
- addTextLimiter(nameField, 20);
- GridPane grid = new GridPane();
- grid.addRow(0, new Label("Namn:"), nameField);
- getDialogPane().setHeaderText("Input name");
- getDialogPane().setContent(grid);
- }
- public String getName() {
- return nameField.getText();
- }
- public void setName(String string) {
- nameField.setText(string);
- }
- public void addTextLimiter(final TextField tf, final int maxLength) {
- tf.textProperty().addListener(new ChangeListener<String>() {
- @Override
- public void changed(final ObservableValue<? extends String> ov, final String oldValue,
- final String newValue) {
- if (tf.getText().length() > maxLength) {
- String s = tf.getText().substring(0, maxLength);
- tf.setText(s);
- }
- }
- });
- }
- }
- class ViewCat implements EventHandler<MouseEvent> {
- public void handle(MouseEvent mouseEvent) {
- String item = listView.getSelectionModel().getSelectedItem();
- if (item.equalsIgnoreCase("bus")) {
- Set<Place> set = categoryMap.get("Bus");
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- p.setVisible(true);
- p.setStroke(null);
- }
- }
- } else if (item.equalsIgnoreCase("underground")) {
- Set<Place> set = categoryMap.get("Underground");
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- p.setVisible(true);
- p.setStroke(null);
- }
- }
- } else if (item.equalsIgnoreCase("train")) {
- Set<Place> set = categoryMap.get("Train");
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- p.setVisible(true);
- p.setStroke(null);
- }
- }
- }
- }
- }
- class HideCatagory implements EventHandler<ActionEvent> {
- public void handle(ActionEvent event) {
- String item = listView.getSelectionModel().getSelectedItem();
- if (item.equalsIgnoreCase("bus")) {
- Set<Place> set = categoryMap.get("Bus");
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- p.setVisible(false);
- }
- }
- } else if (item.equalsIgnoreCase("underground")) {
- Set<Place> set = categoryMap.get("Underground");
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- p.setVisible(false);
- }
- }
- } else if (item.equalsIgnoreCase("Train")) {
- Set<Place> set = categoryMap.get("Train");
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- p.setVisible(false);
- }
- }
- }
- }
- }
- class Coordinates implements EventHandler<ActionEvent> {
- public void handle(ActionEvent event) {
- CoordinatesAlert alert = new CoordinatesAlert();
- Optional<ButtonType> result = alert.showAndWait();
- if (result.isPresent() && result.get() == ButtonType.OK) {
- try {
- position = new Position(alert.getXField(), alert.getYField());
- positions.get(position).setId("npe check");
- Set<Place> set = selectedMap.get(true);
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- Set<Place> setFalse = selectedMap.computeIfAbsent(false, k -> new HashSet<Place>());
- setFalse.add(p);
- }
- Set<Place> setTrue = selectedMap.get(true);
- Iterator<Place> trueIt = setTrue.iterator();
- while (trueIt.hasNext()) {
- Place p = trueIt.next();
- if (setTrue == null) {
- setTrue = new HashSet<Place>();
- selectedMap.put(true, setTrue);
- }
- trueIt.remove();
- p.setSelected(false);
- p.setStroke(null);
- }
- }
- positions.get(position).setSelected(true);
- positions.get(position).setVisible(true);
- positions.get(position).setStroke(Color.BLACK);
- Place p1 = positions.get(position);
- Boolean key = true;
- Set<Place> addPosToTrueSet = selectedMap.get(key);
- if (addPosToTrueSet == null) {
- addPosToTrueSet = new HashSet<Place>();
- selectedMap.put(key, addPosToTrueSet);
- }
- addPosToTrueSet.add(p1);
- } catch (NumberFormatException ioe) {
- System.out.println("error");
- } catch (NullPointerException npe) {
- Alert alert1 = new Alert(AlertType.ERROR, "No such place");
- alert1.showAndWait();
- }
- }
- }
- }
- class Remove implements EventHandler<ActionEvent> {
- public void handle(ActionEvent event) {
- Set<Place> setSelected = selectedMap.get(true);
- if (setSelected != null) {
- Iterator<Place> iterSelected = setSelected.iterator();
- while (iterSelected.hasNext()) {
- Place p = iterSelected.next();
- String name = p.getName();
- Set<Place> nameSet = alla.get(name);
- Iterator<Place> iterName = nameSet.iterator();
- while (iterName.hasNext()) {
- Place item = iterName.next();
- if (p.equals(item))
- ;
- {
- iterName.remove();
- break;
- }
- }
- String cat = p.getCat();
- Set<Place> catSet = categoryMap.get(cat);
- Iterator<Place> iterCat = catSet.iterator();
- while (iterCat.hasNext()) {
- Place item = iterCat.next();
- if (item.equals(p)) {
- iterCat.remove();
- }
- }
- Position pos = p.getCoordinates();
- positions.remove(pos);
- triangles.remove(p);
- iterSelected.remove();
- }
- }
- center.getChildren().clear();
- center.getChildren().add(display);
- center.getChildren().addAll(triangles);
- for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- for (Map.Entry<String, Set<Place>> entry : categoryMap.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- for (Map.Entry<String, Set<Place>> entry : alla.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- for (Map.Entry<Position, Place> entry : positions.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- for (Place p : triangles) {
- System.out.println(p.toString());
- }
- }
- }
- class Hide implements EventHandler<ActionEvent> {
- public void handle(ActionEvent event) {
- Set<Place> set = selectedMap.get(true);
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- Boolean key = false;
- Set<Place> setFalse = selectedMap.get(key);
- if (setFalse == null) {
- setFalse = new HashSet<Place>();
- selectedMap.put(key, setFalse);
- }
- setFalse.add(p);
- iter.remove();
- p.setVisible(false);
- p.setSelected(false);
- p.setStroke(color);
- for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- }
- }
- }
- }
- class Search implements EventHandler<ActionEvent> {
- @Override
- public void handle(ActionEvent event) {
- try {
- Set<Place> set = selectedMap.get(true);
- if (set != null) {
- Iterator<Place> iter = set.iterator();
- while (iter.hasNext()) {
- Place p = iter.next();
- Set<Place> setFalse = selectedMap.computeIfAbsent(false, k -> new HashSet<Place>());
- setFalse.add(p);
- }
- Set<Place> setTrue = selectedMap.get(true);
- Iterator<Place> trueIt = setTrue.iterator();
- while (trueIt.hasNext()) {
- Place p = trueIt.next();
- if (setTrue == null) {
- setTrue = new HashSet<Place>();
- selectedMap.put(true, setTrue);
- }
- trueIt.remove();
- p.setSelected(false);
- p.setStroke(null);
- }
- }
- String word = search.getText();
- Set<Place> nameSet = alla.get(word);
- if (nameSet != null) {
- for (Place p : nameSet) {
- Set<Place> setTrue = selectedMap.computeIfAbsent(true, k -> new HashSet<Place>());
- setTrue.add(p);
- if (nameSet == null) {
- nameSet = new HashSet<Place>();
- selectedMap.put(false, nameSet);
- }
- Set<Place> setFalse = selectedMap.get(false);
- setFalse.remove(p);
- p.setVisible(true);
- p.setSelected(true);
- for (Map.Entry<Boolean, Set<Place>> entry : selectedMap.entrySet()) {
- System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
- }
- }
- Set<Place> borderSet = selectedMap.get(true);
- Iterator<Place> borderIt = borderSet.iterator();
- while (borderIt.hasNext()) {
- Place p = borderIt.next();
- p.setStroke(Color.BLACK);
- }
- }
- } catch(NullPointerException npe){
- System.out.println("Can not find " + search.getText());
- }
- }
- }
- class NewLocation implements EventHandler<MouseEvent> {
- public void handle(MouseEvent event) {
- String temp = "";
- display.setCursor(Cursor.DEFAULT);
- if (listView.getSelectionModel().isEmpty()) {
- color = Color.BLACK;
- temp = "none";
- } else if (listView.getSelectionModel().getSelectedItem().equalsIgnoreCase("bus")) {
- color = Color.RED;
- temp = "Bus";
- } else if (listView.getSelectionModel().getSelectedItem().equalsIgnoreCase("underground")) {
- color = Color.BLUE;
- temp = "Underground";
- } else if (listView.getSelectionModel().getSelectedItem().equalsIgnoreCase("train")) {
- color = Color.GREEN;
- temp = "Train";
- }
- if (namnBtn.isSelected()) {
- String type = "Named";
- NamedPlaceAlert nameAlert = new NamedPlaceAlert();
- Optional<ButtonType> nameResult = nameAlert.showAndWait();
- if (nameAlert.getName().isEmpty()) {
- Alert alert = new Alert(AlertType.ERROR, "Please input a name");
- alert.showAndWait();
- display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
- newBtn.setDisable(false);
- return;
- }
- if (nameResult.get() == ButtonType.OK) {
- Position tempP = new Position(event.getX(), event.getY());
- if (positions.containsKey(tempP)) {
- Alert msg = new Alert(Alert.AlertType.ERROR, "Place already exists on given coordinates");
- msg.showAndWait();
- return;
- }
- triangle = new NamedPlace(event.getX() - 15, event.getY() - 30, color, false, nameAlert.getName(),
- temp, type);
- position = new Position(triangle.getX(), triangle.getY());
- center.getChildren().add(triangle);
- Boolean key = false;
- Set<Place> setFalse = selectedMap.get(key);
- if (setFalse == null) {
- setFalse = new HashSet<Place>();
- selectedMap.put(key, setFalse);
- }
- setFalse.add(triangle);
- String namn = triangle.getName();
- Set<Place> nPlaces = alla.get(namn);
- if (nPlaces == null) {
- nPlaces = new HashSet<>();
- alla.put(namn, nPlaces);
- }
- nPlaces.add(triangle);
- triangles.add(triangle);
- positions.put(position, triangle);
- changed = true;
- if (triangle.getCat().equalsIgnoreCase("bus")) {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else if (triangle.getCat().equalsIgnoreCase("underground")) {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else if (triangle.getCat().equalsIgnoreCase("train")) {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- }
- }
- display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
- newBtn.setDisable(false);
- } else if (describedBtn.isSelected()) {
- String type = "Described";
- DescribedPlaceAlert describedAlert = new DescribedPlaceAlert();
- Optional<ButtonType> describedResult = describedAlert.showAndWait();
- if (describedAlert.getName().isEmpty() || describedAlert.getDescription().isEmpty()) {
- Alert alert = new Alert(AlertType.ERROR, "Please input both fields");
- alert.showAndWait();
- display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
- newBtn.setDisable(false);
- return;
- }
- if (describedResult.get() == ButtonType.OK) {
- Position tempP = new Position(event.getX(), event.getY());
- if (positions.containsKey(tempP)) {
- Alert msg = new Alert(Alert.AlertType.ERROR, "Place already exists on given coordinates");
- msg.showAndWait();
- }
- triangle = new DescribedPlace(event.getX() - 15, event.getY() - 30, color, false,
- describedAlert.getName(), temp, type, describedAlert.getDescription());
- position = new Position(triangle.getX(), triangle.getY());
- center.getChildren().add(triangle);
- Boolean key = false;
- Set<Place> setFalse = selectedMap.get(key);
- if (setFalse == null) {
- setFalse = new HashSet<Place>();
- selectedMap.put(key, setFalse);
- }
- setFalse.add(triangle);
- String namn = triangle.getName();
- Set<Place> nPlaces = alla.get(namn);
- if (nPlaces == null) {
- nPlaces = new HashSet<>();
- alla.put(namn, nPlaces);
- }
- nPlaces.add(triangle);
- if (triangle.getCat().equalsIgnoreCase("Bus")) {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else if (triangle.getCat().equalsIgnoreCase("Underground")) {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else if (triangle.getCat().equalsIgnoreCase("Train")) {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- } else {
- String cat = triangle.getCat();
- Set<Place> placeSet = categoryMap.get(cat);
- if (placeSet == null) {
- placeSet = new HashSet<>();
- categoryMap.put(cat, placeSet);
- }
- placeSet.add(triangle);
- }
- triangles.add(triangle);
- positions.put(position, triangle);
- changed = true;
- }
- display.removeEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
- newBtn.setDisable(false);
- }
- }
- }
- class NewLocationEnable implements EventHandler<MouseEvent> {
- public void handle(MouseEvent event) {
- try {
- if (!image.equals(null)) {
- display.addEventHandler(MouseEvent.MOUSE_CLICKED, newLocation);
- display.setCursor(Cursor.CROSSHAIR);
- newBtn.setDisable(true);
- }
- } catch (NullPointerException npe) {
- System.out.println("Error! No map file added");
- }
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- class CoordinatesAlert extends Alert {
- private TextField xField = new TextField();
- private TextField yField = new TextField();
- public CoordinatesAlert() {
- super(AlertType.CONFIRMATION);
- GridPane grid = new GridPane();
- grid.addRow(0, new Label("x:"), xField);
- grid.addRow(1, new Label("y:"), yField);
- getDialogPane().setContent(grid);
- getDialogPane().setHeaderText("Input Coordinates");
- getDialogPane().setGraphic(null);
- UnaryOperator<TextFormatter.Change> filter = new UnaryOperator<TextFormatter.Change>() {
- @Override
- public TextFormatter.Change apply(TextFormatter.Change t) {
- if (t.isReplaced())
- if (t.getText().matches("[^0-9]"))
- t.setText(t.getControlText().substring(t.getRangeStart(), t.getRangeEnd()));
- if (t.isAdded()) {
- if (t.getControlText().contains(".")) {
- if (t.getText().matches("[^0-9]")) {
- t.setText("");
- }
- } else if (t.getText().matches("[^0-9.]")) {
- t.setText("");
- Alert msg = new Alert(Alert.AlertType.ERROR);
- msg.setContentText("Only numeric values accepted Henrik!");
- msg.showAndWait();
- }
- }
- return t;
- }
- };
- xField.setTextFormatter(new TextFormatter<Object>(filter));
- yField.setTextFormatter(new TextFormatter<Object>(filter));
- }
- public double getXField() {
- return Double.parseDouble(xField.getText());
- }
- public double getYField() {
- return Double.parseDouble(yField.getText());
- }
- }
- class CoordinatesError extends Alert {
- public CoordinatesError() {
- super(AlertType.ERROR);
- getDialogPane().setHeaderText("No location at given coordinates!");
- }
- }
- class Position {
- private double x;
- private double y;
- public Position(double x, double y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o)
- return true;
- if (o == null || getClass() != o.getClass())
- return false;
- Position position = (Position) o;
- return Double.compare(position.x, x) == 0 && Double.compare(position.y, y) == 0;
- }
- @Override
- public int hashCode() {
- return Objects.hash(x, y);
- }
- @Override
- public String toString() {
- return "Position{" + "x=" + x + ", y=" + y + '}';
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement