Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.06 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import javax.swing.filechooser.*;
  4. import java.awt.*;
  5. import java.awt.List;
  6. import java.awt.event.*;
  7. import java.io.*;
  8. import java.util.*;
  9. import java.io.*;
  10.  
  11. class MapArea extends JPanel {
  12.  
  13. private ImageIcon mapPicture;
  14.  
  15. public MapArea(String fileName) {
  16. mapPicture = new ImageIcon(fileName);
  17. setLayout(null);
  18. int mapPictureWidth = mapPicture.getIconWidth();
  19. int mapPictureHeight = mapPicture.getIconHeight();
  20. setPreferredSize(new Dimension(mapPictureWidth, mapPictureHeight));
  21. setMaximumSize(new Dimension(mapPictureWidth, mapPictureHeight));
  22. setMinimumSize(new Dimension(mapPictureWidth, mapPictureHeight));
  23. }
  24.  
  25. public void updateMap(String fileName) {
  26. mapPicture = new ImageIcon(fileName);
  27. int mapPictureWidth = mapPicture.getIconWidth();
  28. int mapPictureHeight = mapPicture.getIconHeight();
  29. setPreferredSize(new Dimension(mapPictureWidth, mapPictureHeight));
  30. setMaximumSize(new Dimension(mapPictureWidth, mapPictureHeight));
  31. setMinimumSize(new Dimension(mapPictureWidth, mapPictureHeight));
  32. }
  33.  
  34.  
  35.  
  36. protected void paintComponent(Graphics g) {
  37. super.paintComponent(g);
  38.  
  39. g.drawImage(mapPicture.getImage(), 0, 0, this);
  40.  
  41. }
  42.  
  43. }
  44.  
  45. public class MapProgram extends JFrame {
  46.  
  47. private HashMap<Position, Place> listOfPlaces = new HashMap<Position, Place>();
  48. //byt till hashset
  49. private Set<Place> listOfSelectedPlaces = new HashSet<Place>();
  50. //private HashMap<Position, Place> listOfSelectedPlaces = new HashMap<Position, Place>();
  51.  
  52. private HashMap<String, ArrayList<Place>> listOfNameSortedPlaces = new HashMap<String, ArrayList<Place>>();
  53. private HashMap<String, ArrayList<Place>> listOfCathegorySortedPlaces = new HashMap<String, ArrayList<Place>>();
  54.  
  55. private final String[] categories = { "Bus", "Underground", "Train" };
  56. private JMenuBar menuBar;
  57. private JRadioButton namedPlaceButton;
  58. private JRadioButton describedPlaceButton;
  59. private JTextField searchBox;
  60. private JButton searchButton;
  61. private JButton hideButton;
  62. private JButton removeButton;
  63. private JButton coordinatesButton;
  64. private JButton newPlaceButton;
  65. private JList<String> categoryList;
  66. private JButton hideCategoryButton;
  67. private String lastLoadedFile = null;
  68. private MapArea picArea;
  69. private boolean checkSave = false;
  70. public File fileChoser(Boolean save) {
  71. JFileChooser fileChooser = new JFileChooser();
  72. int answer;
  73. if (save == true) {
  74. answer = fileChooser.showSaveDialog(MapProgram.this);
  75. } else {
  76. answer = fileChooser.showOpenDialog(MapProgram.this);
  77. }
  78. if (answer == fileChooser.APPROVE_OPTION) {
  79. File f = fileChooser.getSelectedFile();
  80.  
  81. return f;
  82. }
  83. return null;
  84. }
  85.  
  86. public MapProgram() {
  87.  
  88. JPanel eastPanel = new JPanel();
  89. JPanel northPanel = new JPanel();
  90.  
  91. setLayout(new BorderLayout());
  92. menuBar = new JMenuBar();
  93. northPanel.add(menuBar);
  94.  
  95. JMenu archiveMenu = new JMenu("Archive");
  96. menuBar.add(archiveMenu);
  97.  
  98. JMenuItem newMapItem = new JMenuItem("New map");
  99. archiveMenu.add(newMapItem);
  100. newMapItem.addActionListener(new ArchiveChoice());
  101.  
  102. JMenuItem loadPlaceItem = new JMenuItem("Load places");
  103. archiveMenu.add(loadPlaceItem);
  104. loadPlaceItem.addActionListener(new ArchiveChoice());
  105.  
  106. JMenuItem saveItem = new JMenuItem("Save");
  107. archiveMenu.add(saveItem);
  108. saveItem.addActionListener(new ArchiveChoice());
  109.  
  110. JMenuItem exitItem = new JMenuItem("Exit");
  111. archiveMenu.add(exitItem);
  112. exitItem.addActionListener(new ArchiveChoice());
  113.  
  114. northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  115. add(northPanel, BorderLayout.NORTH);
  116.  
  117. newPlaceButton = new JButton("new");
  118. northPanel.add(newPlaceButton);
  119. newPlaceButton.addActionListener(new NewPlace());
  120.  
  121. namedPlaceButton = new JRadioButton("Name", true);
  122. northPanel.add(namedPlaceButton);
  123.  
  124. describedPlaceButton = new JRadioButton("Described");
  125. northPanel.add(describedPlaceButton);
  126.  
  127. ButtonGroup buttonGroup = new ButtonGroup();
  128.  
  129. buttonGroup.add(namedPlaceButton);
  130. buttonGroup.add(describedPlaceButton);
  131.  
  132. searchBox = new JTextField("Search");
  133. northPanel.add(searchBox);
  134.  
  135. searchButton = new JButton("search");
  136. searchButton.addActionListener(new SearchListener());
  137. northPanel.add(searchButton);
  138.  
  139. hideButton = new JButton("hide");
  140. hideButton.addActionListener(new HideListener());
  141. northPanel.add(hideButton);
  142.  
  143. removeButton = new JButton("remove");
  144. removeButton.addActionListener(new RemoveListener());
  145. northPanel.add(removeButton);
  146.  
  147. coordinatesButton = new JButton("coordinates");
  148. coordinatesButton.addActionListener(new CoordListener());
  149. northPanel.add(coordinatesButton);
  150.  
  151. eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
  152. add(eastPanel, BorderLayout.EAST);
  153.  
  154. categoryList = new JList<String>(categories);
  155. categoryList.addListSelectionListener(new ListListener());
  156. //notis
  157. categoryList.addMouseListener(new MouseAdapter() {
  158. int lastSelectedIndex;
  159. public void mouseClicked(MouseEvent e) {
  160.  
  161. int index = categoryList.locationToIndex(e.getPoint());
  162.  
  163. if (index != -1 && index == lastSelectedIndex) {
  164. categoryList.clearSelection();
  165. }
  166. lastSelectedIndex = categoryList.getSelectedIndex();
  167. }
  168. });
  169. eastPanel.add(new JLabel("Cathegories"));
  170. eastPanel.add(categoryList);
  171.  
  172. hideCategoryButton = new JButton("Hide Category");
  173. hideCategoryButton.addActionListener(new HideCathegoryListener());
  174. eastPanel.add(hideCategoryButton);
  175. setJMenuBar(menuBar);
  176. setSize(550, 400);
  177. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  178. addWindowListener(new WindowAdapter() {
  179. public void windowClosing(WindowEvent e) {
  180. confirmClose();
  181. }
  182. });
  183.  
  184. setLocationRelativeTo(null);
  185. setVisible(true);
  186.  
  187. }
  188.  
  189. class ArchiveChoice implements ActionListener {
  190. public void actionPerformed(ActionEvent ave) {
  191.  
  192. System.out.println(ave.getActionCommand());
  193. String choice = ave.getActionCommand();
  194.  
  195. switch (choice) {
  196.  
  197. case "New map":
  198. newMap();
  199.  
  200. break;
  201.  
  202. case "Load places":
  203. loadPlacesToMap();
  204. break;
  205.  
  206. case "Save":
  207.  
  208. savePlaces();
  209.  
  210. break;
  211.  
  212. case "Exit":
  213.  
  214. confirmClose();
  215. break;
  216.  
  217. }
  218.  
  219. }
  220. }
  221.  
  222. // nytt
  223. private boolean saveWindow(boolean exit) {
  224. JPanel formPanel = new JPanel();
  225.  
  226. int formAnswer = JOptionPane.showConfirmDialog(MapProgram.this, formPanel, "Do you want to save? ",
  227. JOptionPane.YES_NO_CANCEL_OPTION);
  228.  
  229. if (formAnswer == JOptionPane.YES_OPTION) {
  230. if (savePlaces()) {
  231. return true;
  232. }
  233. return false;
  234.  
  235. } else if (formAnswer == JOptionPane.NO_OPTION) {
  236. if(exit==true){
  237. System.exit(0);
  238. }
  239. } else if (formAnswer == JOptionPane.CANCEL_OPTION) {
  240. return false;
  241. }
  242. return true;
  243. }
  244.  
  245. private void confirmClose() {
  246. if (lastLoadedFile == null && listOfPlaces.isEmpty()) {
  247. System.exit(0);
  248. } else if (lastLoadedFile == null && !listOfPlaces.isEmpty()) {
  249. saveWindow(true);
  250. }
  251.  
  252. else if (checkSave == true) {
  253. saveWindow(true);
  254.  
  255. } else {
  256. System.exit(0);
  257. }
  258. }
  259.  
  260. private boolean checkSave() {
  261. HashMap<Position, Place> saveListOfPlaces = new HashMap<Position, Place>();
  262. FileReader infile = null;
  263.  
  264. try {
  265. infile = new FileReader(lastLoadedFile);
  266.  
  267. BufferedReader in = new BufferedReader(infile);
  268. String line;
  269.  
  270. while ((line = in.readLine()) != null) {
  271. String[] data = line.split(",");
  272.  
  273. String sort = data[0];
  274. String cath = data[1];
  275. int X = Integer.parseInt(data[2]);
  276. int Y = Integer.parseInt(data[3]);
  277. String name = data[4];
  278. String desc = null;
  279. if (data.length == 6) {
  280. desc = data[5];
  281. }
  282. if (sort.equals("Named")) {
  283. Position p = new Position(X, Y);
  284. NamedPlace n = new NamedPlace(name, cath, p);
  285. saveListOfPlaces.put(p, n);
  286.  
  287. }
  288.  
  289. else if (sort.equals("Described")) {
  290. Position p = new Position(X, Y);
  291. DescribedPlace d = new DescribedPlace(name, cath, p, desc);
  292. saveListOfPlaces.put(p, d);
  293.  
  294. }
  295. }
  296. infile.close();
  297. in.close();
  298.  
  299. } catch (FileNotFoundException e) {
  300.  
  301. e.printStackTrace();
  302. errorMessage("File not found");
  303. } catch (IOException e) {
  304. e.printStackTrace();
  305. errorMessage("Error ");
  306.  
  307. }
  308. boolean b = false;
  309. /*Place p;
  310. Byt till en boolsk variabel för om något ändrats
  311. for (Map.Entry<Position, Place> entry : listOfPlaces.entrySet()) {
  312. p = entry.getValue();
  313. Position po = new Position(p.returnX(), p.returnY());
  314. if (saveListOfPlaces.get(po) == null) {
  315. b = true;
  316. return b;
  317. }
  318. }
  319. for (Map.Entry<Position, Place> entry : saveListOfPlaces.entrySet()) {
  320. p = entry.getValue();
  321. Position po = new Position(p.returnX(), p.returnY());
  322. if (listOfPlaces.get(po) == null) {
  323. b = true;
  324. return b;
  325. }
  326. }*/
  327. return b;
  328. }
  329.  
  330. // nytt, fixa duplicerad/dålig kod
  331. private void newMap() {
  332. boolean b = true;
  333. if (lastLoadedFile == null && listOfPlaces.isEmpty()) {
  334.  
  335. } else if (lastLoadedFile == null && !listOfPlaces.isEmpty()) {
  336. b = saveWindow(false);
  337. } else if (checkSave) {
  338. b = saveWindow(false);
  339. }
  340. if (b == true) {
  341. File f = fileChoser(false);
  342. if (f != null) {
  343. emptyMaps();
  344. if (picArea == null) {
  345. picArea = new MapArea(f.getAbsolutePath());
  346. add(picArea, BorderLayout.CENTER);
  347. JScrollPane scroll = new JScrollPane(picArea);
  348. add(scroll, BorderLayout.CENTER);
  349.  
  350. //GlobalListener g = new GlobalListener();
  351. //picArea.addMouseListener(g);
  352. pack();
  353. validate();
  354. } else {
  355. picArea.updateMap(f.getAbsolutePath());
  356. lastLoadedFile = null;
  357. repaint();
  358. pack();
  359. validate();
  360. }
  361. }
  362. }
  363.  
  364. }
  365.  
  366. // ny funktion
  367. private void emptyMaps() {
  368. listOfPlaces.clear();
  369. if (picArea != null) {
  370. picArea.removeAll();
  371. }
  372. listOfSelectedPlaces.clear();
  373. listOfNameSortedPlaces.clear();
  374. listOfCathegorySortedPlaces.clear();
  375. }
  376.  
  377. //kika på namnet
  378. private void loadPlacesToMap() {
  379. // duplicerat till inläsning av ny map
  380. boolean checkSave = true;
  381. boolean b = true;
  382. if (lastLoadedFile == null && listOfPlaces.isEmpty()) {
  383.  
  384. } else if (lastLoadedFile == null && !listOfPlaces.isEmpty()) {
  385. b = saveWindow(false);
  386. } else if (checkSave) {
  387. b = saveWindow(false);
  388. }
  389.  
  390. if (b == true) {
  391. File l = fileChoser(false);
  392. if (l != null) {
  393. lastLoadedFile = l.getAbsolutePath();
  394. FileReader infile = null;
  395.  
  396. try {
  397.  
  398. emptyMaps();
  399. infile = new FileReader(l.getAbsolutePath());
  400.  
  401. BufferedReader in = new BufferedReader(infile);
  402. String line;
  403.  
  404. while ((line = in.readLine()) != null) {
  405. String[] data = line.split(",");
  406.  
  407. String sort = data[0];
  408. String category = data[1];
  409. int x = Integer.parseInt(data[2]);
  410. int y = Integer.parseInt(data[3]);
  411. String name = data[4];
  412. String description = null;
  413. if (data.length == 6) {
  414. description = data[5];
  415. }
  416. if (sort.equals("Named")) {
  417. Position p = new Position(x, y);
  418. NamedPlace n = new NamedPlace(name, category, p);
  419.  
  420. addToMap(n);
  421.  
  422. }
  423.  
  424. else if (sort.equals("Described")) {
  425. Position p = new Position(x, y);
  426. DescribedPlace d = new DescribedPlace(name, category, p, description);
  427.  
  428. addToMap(d);
  429. }
  430. }
  431. infile.close();
  432. in.close();
  433. }
  434.  
  435. catch (FileNotFoundException e) {
  436. errorMessage("File not found ");
  437. e.printStackTrace();
  438.  
  439. } catch (IOException e) {
  440. errorMessage("Error ");
  441. e.printStackTrace();
  442.  
  443. }
  444. }
  445. }
  446. }
  447.  
  448. private boolean savePlaces() {
  449. File l = fileChoser(true);
  450. if (l != null) {
  451. lastLoadedFile = l.getAbsolutePath();
  452.  
  453. try {
  454.  
  455. FileWriter utfil = new FileWriter(l.getAbsolutePath());
  456. PrintWriter out = new PrintWriter(utfil);
  457. //lägg till lista för att spara
  458. Collection<Place> key = listOfPlaces.values();
  459. Iterator<Place> valueit = key.iterator();
  460.  
  461. if (valueit.hasNext()) {
  462. do {
  463. String add = "";
  464. Place tmp = valueit.next();
  465. DescribedPlace d = null;
  466. if (tmp instanceof DescribedPlace) {
  467. d = (DescribedPlace) tmp;
  468. add += "Described";
  469. add += ",";
  470. add += d.returnCath();
  471. add += ",";
  472. add += d.returnX();
  473. add += ",";
  474. add += d.returnY();
  475. add += ",";
  476. add += d.returnName();
  477. add += ",";
  478. add += d.returnDesc();
  479. out.println(add);
  480. } else if (tmp instanceof NamedPlace) {
  481. add += "Named";
  482. add += ",";
  483. add += tmp.returnCath();
  484. add += ",";
  485. add += tmp.returnX();
  486. add += ",";
  487. add += tmp.returnY();
  488. add += ",";
  489. add += tmp.returnName();
  490. out.println(add);
  491. }
  492. } while (valueit.hasNext());
  493.  
  494. }
  495. out.close();
  496. return true;
  497.  
  498. } catch (FileNotFoundException e) {
  499.  
  500. e.printStackTrace();
  501. errorMessage("File not found ");
  502. } catch (IOException e) {
  503.  
  504. e.printStackTrace();
  505. errorMessage("Error ");
  506. }
  507. }
  508. return false;
  509. }
  510.  
  511. // annat namn?
  512. class NewPlace implements ActionListener {
  513. public void actionPerformed(ActionEvent ave) {
  514. if (picArea != null) {
  515. MouseListener mouseListener = new MouseListener();
  516.  
  517. picArea.addMouseListener(mouseListener);
  518. picArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  519. }
  520. }
  521.  
  522. }
  523.  
  524. class MouseListener extends MouseAdapter {
  525. @Override
  526. public void mouseClicked(MouseEvent mev) {
  527. picArea.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  528. openPlaceForm(categoryList.getSelectedValue(), mev.getX(), mev.getY());
  529.  
  530. picArea.removeMouseListener(this);
  531.  
  532. }
  533. }
  534.  
  535. class GlobalListener extends MouseAdapter {
  536. @Override
  537. public void mouseClicked(MouseEvent mev) {
  538.  
  539. if(mev.getSource() instanceof Place){
  540. Place p = (Place)mev.getSource();
  541. if(mev.getButton() == 1){
  542. if (p.returnSelected()) {
  543. p.setSelected(false);
  544. } else if (p.returnSelected() == false) {
  545. p.setSelected(true);
  546. }
  547. }
  548. if(mev.getButton() == 3 && p.returnVisible()){
  549. if (p instanceof NamedPlace) {
  550. String displayMessage = "X: " + p.returnX() + "Y: " + p.returnY();
  551. JOptionPane.showMessageDialog(MapProgram.this, displayMessage);
  552. } else {
  553. String displayMessage = "X: " + p.returnX() + "Y: " + p.returnY() + "\n"
  554. + ((DescribedPlace) p).returnDesc();
  555. JOptionPane.showMessageDialog(MapProgram.this, displayMessage);
  556.  
  557. }
  558. }
  559. }
  560.  
  561. repaint();
  562. }
  563. }
  564. class ClickListner implements ActionListener{
  565. public void actionPerformed(ActionEvent ave){
  566.  
  567. }
  568. }
  569. //byts till hashset
  570. class HideListener implements ActionListener {
  571. public void actionPerformed(ActionEvent ave) {
  572. for(Place p: listOfSelectedPlaces){
  573. if (p.returnSelected()) {
  574. p.setVisible(false);
  575. p.setSelected(false);
  576. listOfPlaces.put(p.getPosition(), p);
  577.  
  578.  
  579. listOfSelectedPlaces.add(p);
  580. //listOfSelectedPlaces.put(p.getPosition(), p);
  581. //picArea.remove(p);
  582. //picArea.add(p);
  583. }
  584.  
  585.  
  586. }
  587.  
  588.  
  589.  
  590.  
  591. /*for (Map.Entry<Position, Place> entry : listOfSelectedPlaces.entrySet()) {
  592. Place p = entry.getValue();
  593. if (p.returnSelected()) {
  594. p.setVisible(false);
  595. p.setSelected(false);
  596. listOfPlaces.put(p.getPosition(), p);
  597. listOfSelectedPlaces.put(p.getPosition(), p);
  598. //picArea.remove(p);
  599. //picArea.add(p);
  600. }
  601. }*/
  602. repaint();
  603. }
  604.  
  605. }
  606.  
  607. class HideCathegoryListener implements ActionListener {
  608. public void actionPerformed(ActionEvent ave) {
  609. String choice = "";
  610.  
  611. if (listOfCathegorySortedPlaces.get(categoryList.getSelectedValue()) != null) {
  612. choice = categoryList.getSelectedValue();
  613. setVisible(choice);
  614. repaint();
  615. }
  616. }
  617.  
  618. }
  619. //notis
  620. public void setVisible(String nyckelNamn){
  621. listOfCathegorySortedPlaces.get(nyckelNamn).forEach(Place -> Place.setVisible(false));
  622. }
  623.  
  624. //för namne-sorted och cathegory-sorted: för opjekt P - Map.get(P.getName).remove(P);
  625. class RemoveListener implements ActionListener {
  626. public void actionPerformed(ActionEvent ave) {
  627. checkSave = true;
  628. /*for (Iterator<Map.Entry<Position, Place>> it = listOfPlaces.entrySet().iterator(); it.hasNext();) {
  629. Place p = it.next().getValue();
  630. if (p.returnSelected()) {
  631. it.remove();
  632. picArea.remove(p);
  633.  
  634. }
  635. }*/
  636.  
  637.  
  638. /*for (Iterator<Map.Entry<Position, Place>> it = listOfSelectedPlaces.entrySet().iterator(); it.hasNext();) {
  639. Place p = it.next().getValue();
  640. if (p.returnSelected()) {
  641. it.remove();
  642.  
  643. }
  644.  
  645. }*/
  646.  
  647. /*for (Iterator<Map.Entry<String, ArrayList<Place>>> it2 = listOfNameSortedPlaces.entrySet().iterator(); it2
  648. .hasNext();) {
  649. ArrayList<Place> h = it2.next().getValue();
  650.  
  651. for (Iterator<Place> it3 = h.iterator(); it3.hasNext();) {
  652. if (it3.hasNext()) {
  653.  
  654. Place nameSortedPlace = it3.next();
  655. if (nameSortedPlace.returnSelected()) {
  656. it3.remove();
  657.  
  658. }
  659. }
  660. }
  661. }*/
  662.  
  663. /*for (Iterator<Map.Entry<String, ArrayList<Place>>> it2 = listOfCathegorySortedPlaces.entrySet()
  664. .iterator(); it2.hasNext();) {
  665. ArrayList<Place> h = it2.next().getValue();
  666. for (Iterator<Place> it3 = h.iterator(); it3.hasNext();) {
  667. if (it3.hasNext()) {
  668.  
  669. Place cathegorySortedPlace = it3.next();
  670. if (cathegorySortedPlace.returnSelected()) {
  671. it3.remove();
  672.  
  673. }
  674. }
  675. }
  676. }*/
  677.  
  678. //ny hashset
  679. ArrayList<Place> tempList = new ArrayList<Place>();
  680. for(Place p: listOfSelectedPlaces){
  681. if(p.returnSelected()==true){
  682. listOfNameSortedPlaces.get(p.returnName()).remove(p);
  683. listOfCathegorySortedPlaces.get(p.returnCath()).remove(p);
  684. picArea.remove(p);
  685. tempList.add(p);
  686. }
  687. }
  688. listOfSelectedPlaces.removeAll(tempList);
  689. tempList.clear();
  690.  
  691.  
  692.  
  693.  
  694.  
  695. repaint();
  696. }
  697. }
  698.  
  699. class CoordListener implements ActionListener {
  700. public void actionPerformed(ActionEvent ave) {
  701. openCoordForm();
  702. }
  703. }
  704.  
  705. class SearchListener implements ActionListener {
  706. public void actionPerformed(ActionEvent ave) {
  707.  
  708. if (listOfNameSortedPlaces.get(searchBox.getText()) != null) {
  709. for (Place p : listOfNameSortedPlaces.get(searchBox.getText())) {
  710. p.setVisible(true);
  711. p.setSelected(true);
  712. listOfPlaces.put(p.getPosition(), p);
  713.  
  714.  
  715. listOfSelectedPlaces.add(p);
  716. //listOfSelectedPlaces.put(p.getPosition(), p);
  717. }
  718. }
  719. repaint();
  720. }
  721. }
  722.  
  723. class ListListener implements ListSelectionListener {
  724. public void valueChanged(ListSelectionEvent event) {
  725. if (!event.getValueIsAdjusting()) {
  726. if (picArea != null) {
  727.  
  728. String selected = "";
  729.  
  730. if (listOfCathegorySortedPlaces.get(categoryList.getSelectedValue()) != null) {
  731. selected = categoryList.getSelectedValue().toString();
  732.  
  733. for (Place p : listOfCathegorySortedPlaces.get(selected)) {
  734. if (p.returnCath().equals(selected)) {//kolla
  735. p.setVisible(true);
  736. listOfPlaces.put(p.getPosition(), p);
  737.  
  738. listOfSelectedPlaces.add(p);
  739. //listOfSelectedPlaces.put(p.getPosition(), p);
  740. }
  741.  
  742. }
  743. repaint();
  744. }
  745. }
  746. }
  747. }
  748. }
  749.  
  750. private void openCoordForm() {
  751. try {
  752. CoordForm form = new CoordForm();
  753. int formsAnswer = JOptionPane.showConfirmDialog(MapProgram.this, form, "Input",
  754. JOptionPane.OK_CANCEL_OPTION);
  755.  
  756. boolean notFound = true;
  757.  
  758. if (formsAnswer != JOptionPane.OK_OPTION) {
  759. return;
  760. } else {
  761. Position po = new Position(form.returnX(), form.returnY());
  762. if (listOfPlaces.containsKey(po)) {
  763. Place p = listOfPlaces.get(po);
  764. p.setSelected(true);
  765. p.setVisible(true);
  766. listOfPlaces.put(po, p);
  767.  
  768. listOfSelectedPlaces.add(p);
  769. //listOfSelectedPlaces.put(po, p);
  770. notFound = false;
  771. repaint();
  772. }
  773. }
  774. if (notFound) {
  775. JOptionPane.showMessageDialog(MapProgram.this, "There is nothing on the entered coordinates");
  776.  
  777. }
  778.  
  779. } catch (NumberFormatException e) {
  780. errorMessage("Error, wrong type of data entered ");
  781. }
  782. }
  783.  
  784. private void openPlaceForm(String category, int musX, int musY) {
  785. try {
  786. boolean descB = false;
  787. descB = describedPlaceButton.isSelected();
  788. PlaceForm form = new PlaceForm(descB);
  789. int formAnswer = JOptionPane.showConfirmDialog(MapProgram.this, form, "Input",
  790. JOptionPane.OK_CANCEL_OPTION);
  791. String placeName;
  792. String description;
  793.  
  794. if (formAnswer != JOptionPane.OK_OPTION) {
  795. return;
  796.  
  797. } else {
  798. placeName = form.getName();
  799.  
  800. }
  801. Position coord = new Position(musX, musY);
  802. if(category == null){
  803. category="";
  804. }
  805. if (describedPlaceButton.isSelected()) {
  806. description = form.getDesc();
  807. DescribedPlace d = new DescribedPlace(placeName, category, coord, description);
  808. addToMap(d);
  809. }
  810.  
  811. else {
  812. NamedPlace n = new NamedPlace(placeName, category, coord);
  813. addToMap(n);
  814. }
  815. repaint();
  816. } catch (NumberFormatException e) {
  817. errorMessage("Error, wrong type of data entered ");
  818. }
  819. }
  820.  
  821. private void addToMap(Place p) {
  822. GlobalListener g = new GlobalListener();
  823. picArea.add(p);
  824. p.addMouseListener(g);
  825. listOfPlaces.put(p.getPosition(), p);
  826.  
  827. listOfSelectedPlaces.add(p);
  828. //listOfSelectedPlaces.put(p.getPosition(), p);
  829.  
  830. if (listOfNameSortedPlaces.containsKey(p.returnName())) {
  831. listOfNameSortedPlaces.get(p.returnName()).add(p);
  832.  
  833. } else {
  834. ArrayList<Place> h = new ArrayList<Place>();
  835. h.add(p);
  836. listOfNameSortedPlaces.put(p.returnName(), h);
  837.  
  838. }
  839.  
  840. if (listOfCathegorySortedPlaces.containsKey(p.returnCath())) {
  841. listOfCathegorySortedPlaces.get(p.returnCath()).add(p);
  842.  
  843.  
  844. } else {
  845. ArrayList<Place> h = new ArrayList<Place>();
  846. h.add(p);
  847. listOfCathegorySortedPlaces.put(p.returnCath(), h);
  848.  
  849. }
  850. repaint();
  851. }
  852.  
  853. private void errorMessage(String msg) {
  854.  
  855. JOptionPane.showMessageDialog(MapProgram.this, msg);
  856.  
  857. }
  858.  
  859. public static void main(String[] args) {
  860.  
  861. MapProgram m = new MapProgram();
  862. m.pack();
  863.  
  864. }
  865. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement