Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.filechooser.*;
- import java.awt.*;
- import java.awt.List;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
- import java.io.*;
- class MapArea extends JPanel {
- private ImageIcon mapPicture;
- public MapArea(String fileName) {
- mapPicture = new ImageIcon(fileName);
- setLayout(null);
- int mapPictureWidth = mapPicture.getIconWidth();
- int mapPictureHeight = mapPicture.getIconHeight();
- setPreferredSize(new Dimension(mapPictureWidth, mapPictureHeight));
- setMaximumSize(new Dimension(mapPictureWidth, mapPictureHeight));
- setMinimumSize(new Dimension(mapPictureWidth, mapPictureHeight));
- }
- public void updateMap(String fileName) {
- mapPicture = new ImageIcon(fileName);
- int mapPictureWidth = mapPicture.getIconWidth();
- int mapPictureHeight = mapPicture.getIconHeight();
- setPreferredSize(new Dimension(mapPictureWidth, mapPictureHeight));
- setMaximumSize(new Dimension(mapPictureWidth, mapPictureHeight));
- setMinimumSize(new Dimension(mapPictureWidth, mapPictureHeight));
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(mapPicture.getImage(), 0, 0, this);
- }
- }
- public class MapProgram extends JFrame {
- private ArrayList<Place> listOfSavePlaces = new ArrayList<Place>();
- private HashMap<Position, Place> listOfPlaces = new HashMap<Position, Place>();
- private Set<Place> listOfSelectedPlaces = new HashSet<Place>();
- private HashMap<String, ArrayList<Place>> listOfNameSortedPlaces = new HashMap<String, ArrayList<Place>>();
- private HashMap<String, ArrayList<Place>> listOfCathegorySortedPlaces = new HashMap<String, ArrayList<Place>>();
- private final String[] categories = { "Bus", "Underground", "Train" };
- private JMenuBar menuBar;
- private JRadioButton namedPlaceButton;
- private JRadioButton describedPlaceButton;
- private JTextField searchBox;
- private JButton searchButton;
- private JButton hideButton;
- private JButton removeButton;
- private JButton coordinatesButton;
- private JButton newPlaceButton;
- private JList<String> categoryList;
- private JButton hideCategoryButton;
- private String lastLoadedFile = null;
- private MapArea picArea;
- private boolean checkSave = false;
- public File fileChoser(Boolean save) {
- JFileChooser fileChooser = new JFileChooser();
- int answer;
- if (save == true) {
- answer = fileChooser.showSaveDialog(MapProgram.this);
- } else {
- answer = fileChooser.showOpenDialog(MapProgram.this);
- }
- if (answer == fileChooser.APPROVE_OPTION) {
- File f = fileChooser.getSelectedFile();
- return f;
- }
- return null;
- }
- public MapProgram() {
- JPanel eastPanel = new JPanel();
- JPanel northPanel = new JPanel();
- setLayout(new BorderLayout());
- menuBar = new JMenuBar();
- northPanel.add(menuBar);
- JMenu archiveMenu = new JMenu("Archive");
- menuBar.add(archiveMenu);
- JMenuItem newMapItem = new JMenuItem("New map");
- archiveMenu.add(newMapItem);
- newMapItem.addActionListener(new ArchiveChoice());
- JMenuItem loadPlaceItem = new JMenuItem("Load places");
- archiveMenu.add(loadPlaceItem);
- loadPlaceItem.addActionListener(new ArchiveChoice());
- JMenuItem saveItem = new JMenuItem("Save");
- archiveMenu.add(saveItem);
- saveItem.addActionListener(new ArchiveChoice());
- JMenuItem exitItem = new JMenuItem("Exit");
- archiveMenu.add(exitItem);
- exitItem.addActionListener(new ArchiveChoice());
- northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
- add(northPanel, BorderLayout.NORTH);
- newPlaceButton = new JButton("new");
- northPanel.add(newPlaceButton);
- newPlaceButton.addActionListener(new NewPlace());
- namedPlaceButton = new JRadioButton("Name", true);
- northPanel.add(namedPlaceButton);
- describedPlaceButton = new JRadioButton("Described");
- northPanel.add(describedPlaceButton);
- ButtonGroup buttonGroup = new ButtonGroup();
- buttonGroup.add(namedPlaceButton);
- buttonGroup.add(describedPlaceButton);
- searchBox = new JTextField("Search");
- northPanel.add(searchBox);
- searchButton = new JButton("search");
- searchButton.addActionListener(new SearchListener());
- northPanel.add(searchButton);
- hideButton = new JButton("hide");
- hideButton.addActionListener(new HideListener());
- northPanel.add(hideButton);
- removeButton = new JButton("remove");
- removeButton.addActionListener(new RemoveListener());
- northPanel.add(removeButton);
- coordinatesButton = new JButton("coordinates");
- coordinatesButton.addActionListener(new CoordListener());
- northPanel.add(coordinatesButton);
- eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
- add(eastPanel, BorderLayout.EAST);
- categoryList = new JList<String>(categories);
- categoryList.addListSelectionListener(new ListListener());
- //notis
- categoryList.addMouseListener(new MouseAdapter() {
- int lastInList;
- public void mouseClicked(MouseEvent e) {
- int i = categoryList.locationToIndex(e.getPoint());
- if (i != -1 && i == lastInList) {
- categoryList.clearSelection();
- }
- lastInList = categoryList.getSelectedIndex();
- }
- });
- eastPanel.add(new JLabel("Cathegories"));
- eastPanel.add(categoryList);
- hideCategoryButton = new JButton("Hide Category");
- hideCategoryButton.addActionListener(new HideCathegoryListener());
- eastPanel.add(hideCategoryButton);
- setJMenuBar(menuBar);
- setSize(550, 400);
- setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- confirmClose();
- }
- });
- setLocationRelativeTo(null);
- setVisible(true);
- }
- class ArchiveChoice implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- System.out.println(ave.getActionCommand());
- String choice = ave.getActionCommand();
- switch (choice) {
- case "New map":
- newMap();
- break;
- case "Load places":
- loadPlacesToMap();
- break;
- case "Save":
- savePlaces();
- break;
- case "Exit":
- confirmClose();
- break;
- }
- }
- }
- // nytt
- private boolean saveWindow(boolean exit) {
- JPanel formPanel = new JPanel();
- int formAnswer = JOptionPane.showConfirmDialog(MapProgram.this, formPanel, "Do you want to save? ",
- JOptionPane.YES_NO_CANCEL_OPTION);
- if (formAnswer == JOptionPane.YES_OPTION) {
- if (savePlaces()) {
- return true;
- }
- return false;
- } else if (formAnswer == JOptionPane.NO_OPTION) {
- if(exit==true){
- System.exit(0);
- }
- } else if (formAnswer == JOptionPane.CANCEL_OPTION) {
- return false;
- }
- return true;
- }
- private void confirmClose() {
- if (lastLoadedFile == null && listOfPlaces.isEmpty()) {
- System.exit(0);
- } else if (lastLoadedFile == null && !listOfPlaces.isEmpty()) {
- if(saveWindow(true)) {
- System.exit(0);
- }
- }
- else if (checkSave == true) {
- if(saveWindow(true)) {
- System.exit(0);
- }
- } else {
- System.exit(0);
- }
- }
- private boolean checkSave() {
- HashMap<Position, Place> saveListOfPlaces = new HashMap<Position, Place>();
- FileReader infile = null;
- try {
- infile = new FileReader(lastLoadedFile);
- BufferedReader in = new BufferedReader(infile);
- String line;
- while ((line = in.readLine()) != null) {
- String[] data = line.split(",");
- String sort = data[0];
- String cath = data[1];
- int X = Integer.parseInt(data[2]);
- int Y = Integer.parseInt(data[3]);
- String name = data[4];
- String desc = null;
- if (data.length == 6) {
- desc = data[5];
- }
- if (sort.equals("Named")) {
- Position p = new Position(X, Y);
- NamedPlace n = new NamedPlace(name, cath, p);
- saveListOfPlaces.put(p, n);
- }
- else if (sort.equals("Described")) {
- Position p = new Position(X, Y);
- DescribedPlace d = new DescribedPlace(name, cath, p, desc);
- saveListOfPlaces.put(p, d);
- }
- }
- infile.close();
- in.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- errorMessage("File not found");
- } catch (IOException e) {
- e.printStackTrace();
- errorMessage("Error ");
- }
- boolean b = false;
- /*Place p;
- Byt till en boolsk variabel för om något ändrats
- for (Map.Entry<Position, Place> entry : listOfPlaces.entrySet()) {
- p = entry.getValue();
- Position po = new Position(p.returnX(), p.returnY());
- if (saveListOfPlaces.get(po) == null) {
- b = true;
- return b;
- }
- }
- for (Map.Entry<Position, Place> entry : saveListOfPlaces.entrySet()) {
- p = entry.getValue();
- Position po = new Position(p.returnX(), p.returnY());
- if (listOfPlaces.get(po) == null) {
- b = true;
- return b;
- }
- }*/
- return b;
- }
- // nytt, fixa duplicerad/dålig kod
- private void newMap() {
- boolean b = true;
- if (lastLoadedFile == null && listOfPlaces.isEmpty()) {
- } else if (lastLoadedFile == null && !listOfPlaces.isEmpty()) {
- b = saveWindow(false);
- } else if (checkSave) {
- b = saveWindow(false);
- }
- if (b == true) {
- File f = fileChoser(false);
- if (f != null) {
- emptyMaps();
- if (picArea == null) {
- picArea = new MapArea(f.getAbsolutePath());
- add(picArea, BorderLayout.CENTER);
- JScrollPane scroll = new JScrollPane(picArea);
- add(scroll, BorderLayout.CENTER);
- pack();
- validate();
- } else {
- picArea.updateMap(f.getAbsolutePath());
- lastLoadedFile = null;
- repaint();
- pack();
- validate();
- }
- }
- }
- }
- // ny funktion
- private void emptyMaps() {
- listOfSavePlaces.clear();
- listOfPlaces.clear();
- if (picArea != null) {
- picArea.removeAll();
- }
- listOfSelectedPlaces.clear();
- listOfNameSortedPlaces.clear();
- listOfCathegorySortedPlaces.clear();
- }
- private void loadPlacesToMap() {
- // duplicerat till inläsning av ny map
- boolean checkSave = true;
- boolean b = true;
- if (lastLoadedFile == null && listOfPlaces.isEmpty()) {
- } else if (lastLoadedFile == null && !listOfPlaces.isEmpty()) {
- b = saveWindow(false);
- } else if (checkSave) {
- b = saveWindow(false);
- }
- if (b == true) {
- File l = fileChoser(false);
- if (l != null) {
- lastLoadedFile = l.getAbsolutePath();
- FileReader infile = null;
- try {
- emptyMaps();
- infile = new FileReader(l.getAbsolutePath());
- BufferedReader in = new BufferedReader(infile);
- String line;
- while ((line = in.readLine()) != null) {
- String[] data = line.split(",");
- String sort = data[0];
- String category = data[1];
- int x = Integer.parseInt(data[2]);
- int y = Integer.parseInt(data[3]);
- String name = data[4];
- String description = null;
- if (data.length == 6) {
- description = data[5];
- }
- if (sort.equals("Named")) {
- Position p = new Position(x, y);
- NamedPlace n = new NamedPlace(name, category, p);
- addToLists(n);
- }
- else if (sort.equals("Described")) {
- Position p = new Position(x, y);
- DescribedPlace d = new DescribedPlace(name, category, p, description);
- addToLists(d);
- }
- }
- infile.close();
- in.close();
- }
- catch (FileNotFoundException e) {
- errorMessage("File not found ");
- e.printStackTrace();
- } catch (IOException e) {
- errorMessage("Error ");
- e.printStackTrace();
- }
- }
- }
- }
- private boolean savePlaces() {
- File l = fileChoser(true);
- if (l != null) {
- lastLoadedFile = l.getAbsolutePath();
- try {
- FileWriter utfil = new FileWriter(l.getAbsolutePath());
- PrintWriter out = new PrintWriter(utfil);
- for(int i = 0; i < listOfSavePlaces.size(); i++){
- String add = "";
- Place tmp = listOfSavePlaces.get(i);
- DescribedPlace d = null;
- if (tmp instanceof DescribedPlace) {
- d = (DescribedPlace) tmp;
- add += "Described";
- add += ",";
- add += d.returnCath();
- add += ",";
- add += d.returnX();
- add += ",";
- add += d.returnY();
- add += ",";
- add += d.returnName();
- add += ",";
- add += d.returnDesc();
- out.println(add);
- } else if (tmp instanceof NamedPlace) {
- add += "Named";
- add += ",";
- add += tmp.returnCath();
- add += ",";
- add += tmp.returnX();
- add += ",";
- add += tmp.returnY();
- add += ",";
- add += tmp.returnName();
- out.println(add);
- }
- }
- out.close();
- checkSave = false;
- return true;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- errorMessage("File not found ");
- } catch (IOException e) {
- e.printStackTrace();
- errorMessage("Error ");
- }
- }
- return false;
- }
- // annat namn?
- class NewPlace implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- if (picArea != null) {
- MouseListener mouseListener = new MouseListener();
- picArea.addMouseListener(mouseListener);
- picArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- }
- }
- }
- class MouseListener extends MouseAdapter {
- @Override
- public void mouseClicked(MouseEvent mev) {
- picArea.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
- openPlaceForm(categoryList.getSelectedValue(), mev.getX(), mev.getY());
- picArea.removeMouseListener(this);
- }
- }
- class GlobalListener extends MouseAdapter {
- @Override
- public void mouseClicked(MouseEvent mev) {
- if(mev.getSource() instanceof Place){
- Place p = (Place)mev.getSource();
- if(mev.getButton() == 1){
- if (p.returnSelected()) {
- p.setSelected(false);
- } else if (p.returnSelected() == false) {
- p.setSelected(true);
- }
- }
- if(mev.getButton() == 3 && p.returnVisible()){
- if (p instanceof NamedPlace) {
- String displayMessage = "X: " + p.returnX() + "Y: " + p.returnY();
- JOptionPane.showMessageDialog(MapProgram.this, displayMessage);
- } else {
- String displayMessage = "X: " + p.returnX() + "Y: " + p.returnY() + "\n"
- + ((DescribedPlace) p).returnDesc();
- JOptionPane.showMessageDialog(MapProgram.this, displayMessage);
- }
- }
- }
- repaint();
- }
- }
- class ClickListner implements ActionListener{
- public void actionPerformed(ActionEvent ave){
- }
- }
- class HideListener implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- for(Place p: listOfSelectedPlaces){
- if (p.returnSelected()) {
- p.setVisible(false);
- p.setSelected(false);
- listOfPlaces.put(p.getPosition(), p);
- listOfSelectedPlaces.add(p);
- listOfSavePlaces.add(p);
- }
- }
- repaint();
- }
- }
- class HideCathegoryListener implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- String choice = "";
- if (listOfCathegorySortedPlaces.get(categoryList.getSelectedValue()) != null) {
- choice = categoryList.getSelectedValue();
- setVisible(choice);
- repaint();
- }
- }
- }
- public void setVisible(String nyckelNamn){
- listOfCathegorySortedPlaces.get(nyckelNamn).forEach(Place -> Place.setVisible(false));
- }
- class RemoveListener implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- checkSave = true;
- ArrayList<Place> tempList = new ArrayList<Place>();
- for(Place p: listOfSelectedPlaces){
- if(p.returnSelected()==true){
- listOfNameSortedPlaces.get(p.returnName()).remove(p);
- listOfCathegorySortedPlaces.get(p.returnCath()).remove(p);
- picArea.remove(p);
- tempList.add(p);
- }
- }
- listOfSelectedPlaces.removeAll(tempList);
- tempList.clear();
- repaint();
- }
- }
- class CoordListener implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- openCoordForm();
- }
- }
- class SearchListener implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- if (listOfNameSortedPlaces.get(searchBox.getText()) != null) {
- for (Place p : listOfNameSortedPlaces.get(searchBox.getText())) {
- p.setVisible(true);
- p.setSelected(true);
- listOfPlaces.put(p.getPosition(), p);
- listOfSavePlaces.add(p);
- listOfSelectedPlaces.add(p);
- }
- }
- repaint();
- }
- }
- class ListListener implements ListSelectionListener {
- public void valueChanged(ListSelectionEvent event) {
- if (!event.getValueIsAdjusting()) {
- if (picArea != null) {
- String selected = "";
- if (listOfCathegorySortedPlaces.get(categoryList.getSelectedValue()) != null) {
- selected = categoryList.getSelectedValue().toString();
- for (Place p : listOfCathegorySortedPlaces.get(selected)) {
- //kolla
- p.setVisible(true);
- listOfPlaces.put(p.getPosition(), p);
- listOfSavePlaces.add(p);
- listOfSelectedPlaces.add(p);
- }
- repaint();
- }
- }
- }
- }
- }
- private void openCoordForm() {
- try {
- CoordForm form = new CoordForm();
- int formsAnswer = JOptionPane.showConfirmDialog(MapProgram.this, form, "Input",
- JOptionPane.OK_CANCEL_OPTION);
- boolean notFound = true;
- if (formsAnswer != JOptionPane.OK_OPTION) {
- return;
- } else {
- Position po = new Position(form.returnX(), form.returnY());
- if (listOfPlaces.containsKey(po)) {
- Place p = listOfPlaces.get(po);
- p.setSelected(true);
- p.setVisible(true);
- listOfPlaces.put(po, p);
- listOfSavePlaces.add(p);
- listOfSelectedPlaces.add(p);
- notFound = false;
- repaint();
- }
- }
- if (notFound) {
- JOptionPane.showMessageDialog(MapProgram.this, "There is nothing on the entered coordinates");
- }
- } catch (NumberFormatException e) {
- errorMessage("Error, wrong type of data entered ");
- }
- }
- private void openPlaceForm(String category, int musX, int musY) {
- try {
- boolean descB = false;
- descB = describedPlaceButton.isSelected();
- PlaceForm form = new PlaceForm(descB);
- int formAnswer = JOptionPane.showConfirmDialog(MapProgram.this, form, "Input",
- JOptionPane.OK_CANCEL_OPTION);
- String placeName;
- String description;
- if (formAnswer != JOptionPane.OK_OPTION) {
- return;
- } else {
- placeName = form.getName();
- }
- Position coord = new Position(musX, musY);
- if(category == null){
- category="None";
- }
- if (describedPlaceButton.isSelected()) {
- description = form.getDesc();
- DescribedPlace d = new DescribedPlace(placeName, category, coord, description);
- addToLists(d);
- }
- else {
- NamedPlace n = new NamedPlace(placeName, category, coord);
- addToLists(n);
- }
- repaint();
- } catch (NumberFormatException e) {
- errorMessage("Error, wrong type of data entered ");
- }
- }
- private void addToLists(Place p) {
- checkSave = true;
- GlobalListener g = new GlobalListener();
- picArea.add(p);
- p.addMouseListener(g);
- listOfPlaces.put(p.getPosition(), p);
- listOfSavePlaces.add(p);
- listOfSelectedPlaces.add(p);
- if (listOfNameSortedPlaces.containsKey(p.returnName())) {
- listOfNameSortedPlaces.get(p.returnName()).add(p);
- } else {
- ArrayList<Place> h = new ArrayList<Place>();
- h.add(p);
- listOfNameSortedPlaces.put(p.returnName(), h);
- }
- if (listOfCathegorySortedPlaces.containsKey(p.returnCath())) {
- listOfCathegorySortedPlaces.get(p.returnCath()).add(p);
- } else {
- ArrayList<Place> h = new ArrayList<Place>();
- h.add(p);
- listOfCathegorySortedPlaces.put(p.returnCath(), h);
- }
- repaint();
- }
- private void errorMessage(String msg) {
- JOptionPane.showMessageDialog(MapProgram.this, msg);
- }
- public static void main(String[] args) {
- MapProgram m = new MapProgram();
- m.pack();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement